Skip to content

Use relative visibility when noting sealed trait to reduce false positive #143431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/privacy/sealed-traits/false-sealed-traits-note.rs
Original file line number Diff line number Diff line change
@@ -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(){}
20 changes: 20 additions & 0 deletions tests/ui/privacy/sealed-traits/false-sealed-traits-note.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading