Rollup merge of #99091 - compiler-errors:private-types-should-stay-private, r=lcnr
Do not mention private types from other crates as impl candidates Fixes #99080
This commit is contained in:
commit
93f71d4e01
5 changed files with 69 additions and 3 deletions
|
@ -673,6 +673,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
if !self.report_similar_impl_candidates(
|
if !self.report_similar_impl_candidates(
|
||||||
impl_candidates,
|
impl_candidates,
|
||||||
trait_ref,
|
trait_ref,
|
||||||
|
obligation.cause.body_id,
|
||||||
&mut err,
|
&mut err,
|
||||||
) {
|
) {
|
||||||
// This is *almost* equivalent to
|
// This is *almost* equivalent to
|
||||||
|
@ -707,6 +708,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
self.report_similar_impl_candidates(
|
self.report_similar_impl_candidates(
|
||||||
impl_candidates,
|
impl_candidates,
|
||||||
trait_ref,
|
trait_ref,
|
||||||
|
obligation.cause.body_id,
|
||||||
&mut err,
|
&mut err,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1353,6 +1355,7 @@ trait InferCtxtPrivExt<'hir, 'tcx> {
|
||||||
&self,
|
&self,
|
||||||
impl_candidates: Vec<ImplCandidate<'tcx>>,
|
impl_candidates: Vec<ImplCandidate<'tcx>>,
|
||||||
trait_ref: ty::PolyTraitRef<'tcx>,
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
||||||
|
body_id: hir::HirId,
|
||||||
err: &mut Diagnostic,
|
err: &mut Diagnostic,
|
||||||
) -> bool;
|
) -> bool;
|
||||||
|
|
||||||
|
@ -1735,6 +1738,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
&self,
|
&self,
|
||||||
impl_candidates: Vec<ImplCandidate<'tcx>>,
|
impl_candidates: Vec<ImplCandidate<'tcx>>,
|
||||||
trait_ref: ty::PolyTraitRef<'tcx>,
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
||||||
|
body_id: hir::HirId,
|
||||||
err: &mut Diagnostic,
|
err: &mut Diagnostic,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let report = |mut candidates: Vec<TraitRef<'tcx>>, err: &mut Diagnostic| {
|
let report = |mut candidates: Vec<TraitRef<'tcx>>, err: &mut Diagnostic| {
|
||||||
|
@ -1805,8 +1809,24 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
|| self.tcx.is_builtin_derive(def_id)
|
|| self.tcx.is_builtin_derive(def_id)
|
||||||
})
|
})
|
||||||
.filter_map(|def_id| self.tcx.impl_trait_ref(def_id))
|
.filter_map(|def_id| self.tcx.impl_trait_ref(def_id))
|
||||||
|
.filter(|trait_ref| {
|
||||||
|
let self_ty = trait_ref.self_ty();
|
||||||
// Avoid mentioning type parameters.
|
// Avoid mentioning type parameters.
|
||||||
.filter(|trait_ref| !matches!(trait_ref.self_ty().kind(), ty::Param(_)))
|
if let ty::Param(_) = self_ty.kind() {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
// Avoid mentioning types that are private to another crate
|
||||||
|
else if let ty::Adt(def, _) = self_ty.peel_refs().kind() {
|
||||||
|
// FIXME(compiler-errors): This could be generalized, both to
|
||||||
|
// be more granular, and probably look past other `#[fundamental]`
|
||||||
|
// types, too.
|
||||||
|
self.tcx
|
||||||
|
.visibility(def.did())
|
||||||
|
.is_accessible_from(body_id.owner.to_def_id(), self.tcx)
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
return report(normalized_impl_candidates, err);
|
return report(normalized_impl_candidates, err);
|
||||||
}
|
}
|
||||||
|
|
11
src/test/ui/suggestions/auxiliary/meow.rs
Normal file
11
src/test/ui/suggestions/auxiliary/meow.rs
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
pub trait Meow {
|
||||||
|
fn meow(&self) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct GlobalMeow;
|
||||||
|
|
||||||
|
impl Meow for GlobalMeow {}
|
||||||
|
|
||||||
|
pub(crate) struct PrivateMeow;
|
||||||
|
|
||||||
|
impl Meow for PrivateMeow {}
|
16
src/test/ui/suggestions/issue-99080.rs
Normal file
16
src/test/ui/suggestions/issue-99080.rs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
// aux-build:meow.rs
|
||||||
|
|
||||||
|
extern crate meow;
|
||||||
|
|
||||||
|
use meow::Meow;
|
||||||
|
|
||||||
|
fn needs_meow<T: Meow>(t: T) {}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
needs_meow(1usize);
|
||||||
|
//~^ ERROR the trait bound `usize: Meow` is not satisfied
|
||||||
|
}
|
||||||
|
|
||||||
|
struct LocalMeow;
|
||||||
|
|
||||||
|
impl Meow for LocalMeow {}
|
20
src/test/ui/suggestions/issue-99080.stderr
Normal file
20
src/test/ui/suggestions/issue-99080.stderr
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
error[E0277]: the trait bound `usize: Meow` is not satisfied
|
||||||
|
--> $DIR/issue-99080.rs:10:16
|
||||||
|
|
|
||||||
|
LL | needs_meow(1usize);
|
||||||
|
| ---------- ^^^^^^ the trait `Meow` is not implemented for `usize`
|
||||||
|
| |
|
||||||
|
| required by a bound introduced by this call
|
||||||
|
|
|
||||||
|
= help: the following other types implement trait `Meow`:
|
||||||
|
GlobalMeow
|
||||||
|
LocalMeow
|
||||||
|
note: required by a bound in `needs_meow`
|
||||||
|
--> $DIR/issue-99080.rs:7:18
|
||||||
|
|
|
||||||
|
LL | fn needs_meow<T: Meow>(t: T) {}
|
||||||
|
| ^^^^ required by this bound in `needs_meow`
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0277`.
|
|
@ -15,7 +15,6 @@ LL | s.strip_suffix(b'\n').unwrap_or(s)
|
||||||
&'c &'b str
|
&'c &'b str
|
||||||
[char; N]
|
[char; N]
|
||||||
char
|
char
|
||||||
pattern::MultiCharEqPattern<C>
|
|
||||||
= note: required because of the requirements on the impl of `Pattern<'_>` for `u8`
|
= note: required because of the requirements on the impl of `Pattern<'_>` for `u8`
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue