diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index 362052e9fdbdd..dda517074f412 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -22,6 +22,7 @@ use rustc_hir::{ expr_needs_parens, is_range_literal, }; use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferCtxt, InferOk}; +use rustc_middle::middle::privacy::Level; use rustc_middle::traits::IsConstable; use rustc_middle::ty::error::TypeError; use rustc_middle::ty::print::{ @@ -2775,11 +2776,20 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { ty::ClauseKind::Trait(trait_pred) => { let def_id = trait_pred.def_id(); let visible_item = if let Some(local) = def_id.as_local() { - // Check for local traits being reachable. - let vis = &tcx.resolutions(()).effective_visibilities; - // Account for non-`pub` traits in the root of the local crate. - let is_locally_reachable = tcx.parent(def_id).is_crate_root(); - vis.is_reachable(local) || is_locally_reachable + let ty = trait_pred.self_ty(); + // when `TraitA: TraitB` and `S` only impl TraitA, + // we check if `TraitB` can be reachable from `S` + // to determine whether to note `TraitA` is sealed trait. + if let ty::Adt(adt, _) = ty.kind() { + let visibilities = tcx.effective_visibilities(()); + visibilities.effective_vis(local).is_none_or(|v| { + v.at_level(Level::Reexported) + .is_accessible_from(adt.did(), tcx) + }) + } else { + // FIXME(xizheyin): if the type is not ADT, we should not suggest it + true + } } else { // Check for foreign traits being reachable. tcx.visible_parent_map(()).get(&def_id).is_some() diff --git a/tests/ui/privacy/sealed-traits/false-sealed-traits-note.rs b/tests/ui/privacy/sealed-traits/false-sealed-traits-note.rs new file mode 100644 index 0000000000000..13f3065e442e5 --- /dev/null +++ b/tests/ui/privacy/sealed-traits/false-sealed-traits-note.rs @@ -0,0 +1,13 @@ +// We should not emit sealed traits note, see issue #143392 + +mod inner { + pub trait TraitA {} + + pub trait TraitB: TraitA {} +} + +struct Struct; + +impl inner::TraitB for Struct {} //~ ERROR the trait bound `Struct: TraitA` is not satisfied [E0277] + +fn main(){} diff --git a/tests/ui/privacy/sealed-traits/false-sealed-traits-note.stderr b/tests/ui/privacy/sealed-traits/false-sealed-traits-note.stderr new file mode 100644 index 0000000000000..f80d985ad6e63 --- /dev/null +++ b/tests/ui/privacy/sealed-traits/false-sealed-traits-note.stderr @@ -0,0 +1,20 @@ +error[E0277]: the trait bound `Struct: TraitA` is not satisfied + --> $DIR/false-sealed-traits-note.rs:11:24 + | +LL | impl inner::TraitB for Struct {} + | ^^^^^^ the trait `TraitA` is not implemented for `Struct` + | +help: this trait has no implementations, consider adding one + --> $DIR/false-sealed-traits-note.rs:4:5 + | +LL | pub trait TraitA {} + | ^^^^^^^^^^^^^^^^ +note: required by a bound in `TraitB` + --> $DIR/false-sealed-traits-note.rs:6:23 + | +LL | pub trait TraitB: TraitA {} + | ^^^^^^ required by this bound in `TraitB` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0277`.