about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2022-07-09 18:39:51 +0000
committerMichael Goulet <michael@errs.io>2022-07-11 06:57:48 +0000
commitf2d8af10c24324f85a6ad82d3f8595ce4bd7ce95 (patch)
tree0b3b42ddc2ac63139cfcf8c6452072ed4d613ca3
parenta51fb2ba827f24ec4d676a9575a0a5e46afd302a (diff)
downloadrust-f2d8af10c24324f85a6ad82d3f8595ce4bd7ce95.tar.gz
rust-f2d8af10c24324f85a6ad82d3f8595ce4bd7ce95.zip
Do not mention private Self types from other crates
-rw-r--r--compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs22
-rw-r--r--src/test/ui/suggestions/auxiliary/meow.rs11
-rw-r--r--src/test/ui/suggestions/issue-99080.rs16
-rw-r--r--src/test/ui/suggestions/issue-99080.stderr20
-rw-r--r--src/test/ui/traits/bound/assoc-fn-bound-root-obligation.stderr1
5 files changed, 67 insertions, 3 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
index aa1c9136289..7ac3815179a 100644
--- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
+++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
@@ -1805,8 +1805,26 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
                         || self.tcx.is_builtin_derive(def_id)
                 })
                 .filter_map(|def_id| self.tcx.impl_trait_ref(def_id))
-                // Avoid mentioning type parameters.
-                .filter(|trait_ref| !matches!(trait_ref.self_ty().kind(), ty::Param(_)))
+                .filter(|trait_ref| {
+                    let self_ty = trait_ref.self_ty();
+                    // Avoid mentioning type parameters.
+                    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.
+                        match self.tcx.visibility(def.did()) {
+                            ty::Visibility::Public => true,
+                            ty::Visibility::Restricted(def_id) => def_id.is_local(),
+                            ty::Visibility::Invisible => false,
+                        }
+                    } else {
+                        true
+                    }
+                })
                 .collect();
             return report(normalized_impl_candidates, err);
         }
diff --git a/src/test/ui/suggestions/auxiliary/meow.rs b/src/test/ui/suggestions/auxiliary/meow.rs
new file mode 100644
index 00000000000..115df70a690
--- /dev/null
+++ b/src/test/ui/suggestions/auxiliary/meow.rs
@@ -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 {}
diff --git a/src/test/ui/suggestions/issue-99080.rs b/src/test/ui/suggestions/issue-99080.rs
new file mode 100644
index 00000000000..91f574f35b8
--- /dev/null
+++ b/src/test/ui/suggestions/issue-99080.rs
@@ -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 {}
diff --git a/src/test/ui/suggestions/issue-99080.stderr b/src/test/ui/suggestions/issue-99080.stderr
new file mode 100644
index 00000000000..d1908dd9d0d
--- /dev/null
+++ b/src/test/ui/suggestions/issue-99080.stderr
@@ -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`.
diff --git a/src/test/ui/traits/bound/assoc-fn-bound-root-obligation.stderr b/src/test/ui/traits/bound/assoc-fn-bound-root-obligation.stderr
index 115539a6dc2..6ce57b6263e 100644
--- a/src/test/ui/traits/bound/assoc-fn-bound-root-obligation.stderr
+++ b/src/test/ui/traits/bound/assoc-fn-bound-root-obligation.stderr
@@ -15,7 +15,6 @@ LL |     s.strip_suffix(b'\n').unwrap_or(s)
              &'c &'b str
              [char; N]
              char
-             pattern::MultiCharEqPattern<C>
    = note: required because of the requirements on the impl of `Pattern<'_>` for `u8`
 
 error: aborting due to previous error