From f2d8af10c24324f85a6ad82d3f8595ce4bd7ce95 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 9 Jul 2022 18:39:51 +0000 Subject: Do not mention private Self types from other crates --- .../src/traits/error_reporting/mod.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_trait_selection/src') 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); } -- cgit 1.4.1-3-g733a5 From 913023b6b475ac5caed060771526e0777a13ae94 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 11 Jul 2022 07:06:18 +0000 Subject: Use Visibility::is_accessible_from to make suggestion more specific --- .../rustc_trait_selection/src/traits/error_reporting/mod.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'compiler/rustc_trait_selection/src') 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 7ac3815179a..34f4a9f7902 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -673,6 +673,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { if !self.report_similar_impl_candidates( impl_candidates, trait_ref, + obligation.cause.body_id, &mut err, ) { // This is *almost* equivalent to @@ -707,6 +708,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { self.report_similar_impl_candidates( impl_candidates, trait_ref, + obligation.cause.body_id, &mut err, ); } @@ -1353,6 +1355,7 @@ trait InferCtxtPrivExt<'hir, 'tcx> { &self, impl_candidates: Vec>, trait_ref: ty::PolyTraitRef<'tcx>, + body_id: hir::HirId, err: &mut Diagnostic, ) -> bool; @@ -1735,6 +1738,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> { &self, impl_candidates: Vec>, trait_ref: ty::PolyTraitRef<'tcx>, + body_id: hir::HirId, err: &mut Diagnostic, ) -> bool { let report = |mut candidates: Vec>, err: &mut Diagnostic| { @@ -1816,11 +1820,9 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> { // 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, - } + self.tcx + .visibility(def.did()) + .is_accessible_from(body_id.owner.to_def_id(), self.tcx) } else { true } -- cgit 1.4.1-3-g733a5