Skip to content

Commit 93f71d4

Browse files
authored
Rollup merge of rust-lang#99091 - compiler-errors:private-types-should-stay-private, r=lcnr
Do not mention private types from other crates as impl candidates Fixes rust-lang#99080
2 parents 92b8adf + 913023b commit 93f71d4

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
673673
if !self.report_similar_impl_candidates(
674674
impl_candidates,
675675
trait_ref,
676+
obligation.cause.body_id,
676677
&mut err,
677678
) {
678679
// This is *almost* equivalent to
@@ -707,6 +708,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
707708
self.report_similar_impl_candidates(
708709
impl_candidates,
709710
trait_ref,
711+
obligation.cause.body_id,
710712
&mut err,
711713
);
712714
}
@@ -1353,6 +1355,7 @@ trait InferCtxtPrivExt<'hir, 'tcx> {
13531355
&self,
13541356
impl_candidates: Vec<ImplCandidate<'tcx>>,
13551357
trait_ref: ty::PolyTraitRef<'tcx>,
1358+
body_id: hir::HirId,
13561359
err: &mut Diagnostic,
13571360
) -> bool;
13581361

@@ -1735,6 +1738,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
17351738
&self,
17361739
impl_candidates: Vec<ImplCandidate<'tcx>>,
17371740
trait_ref: ty::PolyTraitRef<'tcx>,
1741+
body_id: hir::HirId,
17381742
err: &mut Diagnostic,
17391743
) -> bool {
17401744
let report = |mut candidates: Vec<TraitRef<'tcx>>, err: &mut Diagnostic| {
@@ -1805,8 +1809,24 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
18051809
|| self.tcx.is_builtin_derive(def_id)
18061810
})
18071811
.filter_map(|def_id| self.tcx.impl_trait_ref(def_id))
1808-
// Avoid mentioning type parameters.
1809-
.filter(|trait_ref| !matches!(trait_ref.self_ty().kind(), ty::Param(_)))
1812+
.filter(|trait_ref| {
1813+
let self_ty = trait_ref.self_ty();
1814+
// Avoid mentioning type parameters.
1815+
if let ty::Param(_) = self_ty.kind() {
1816+
false
1817+
}
1818+
// Avoid mentioning types that are private to another crate
1819+
else if let ty::Adt(def, _) = self_ty.peel_refs().kind() {
1820+
// FIXME(compiler-errors): This could be generalized, both to
1821+
// be more granular, and probably look past other `#[fundamental]`
1822+
// types, too.
1823+
self.tcx
1824+
.visibility(def.did())
1825+
.is_accessible_from(body_id.owner.to_def_id(), self.tcx)
1826+
} else {
1827+
true
1828+
}
1829+
})
18101830
.collect();
18111831
return report(normalized_impl_candidates, err);
18121832
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pub trait Meow {
2+
fn meow(&self) {}
3+
}
4+
5+
pub struct GlobalMeow;
6+
7+
impl Meow for GlobalMeow {}
8+
9+
pub(crate) struct PrivateMeow;
10+
11+
impl Meow for PrivateMeow {}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// aux-build:meow.rs
2+
3+
extern crate meow;
4+
5+
use meow::Meow;
6+
7+
fn needs_meow<T: Meow>(t: T) {}
8+
9+
fn main() {
10+
needs_meow(1usize);
11+
//~^ ERROR the trait bound `usize: Meow` is not satisfied
12+
}
13+
14+
struct LocalMeow;
15+
16+
impl Meow for LocalMeow {}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0277]: the trait bound `usize: Meow` is not satisfied
2+
--> $DIR/issue-99080.rs:10:16
3+
|
4+
LL | needs_meow(1usize);
5+
| ---------- ^^^^^^ the trait `Meow` is not implemented for `usize`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= help: the following other types implement trait `Meow`:
10+
GlobalMeow
11+
LocalMeow
12+
note: required by a bound in `needs_meow`
13+
--> $DIR/issue-99080.rs:7:18
14+
|
15+
LL | fn needs_meow<T: Meow>(t: T) {}
16+
| ^^^^ required by this bound in `needs_meow`
17+
18+
error: aborting due to previous error
19+
20+
For more information about this error, try `rustc --explain E0277`.

src/test/ui/traits/bound/assoc-fn-bound-root-obligation.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ LL | s.strip_suffix(b'\n').unwrap_or(s)
1515
&'c &'b str
1616
[char; N]
1717
char
18-
pattern::MultiCharEqPattern<C>
1918
= note: required because of the requirements on the impl of `Pattern<'_>` for `u8`
2019

2120
error: aborting due to previous error

0 commit comments

Comments
 (0)