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

Conversation

xizheyin
Copy link
Contributor

@xizheyin xizheyin commented Jul 4, 2025

Fixes #143392

I used relative visibility instead of just determining if it's public or not.

r? compiler

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 4, 2025
@xizheyin xizheyin changed the title 143392 Use relative visibility when noting sealed trait to reduce false positive Jul 4, 2025
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code need a bunch of cleanup.

@@ -22,6 +22,7 @@ use rustc_hir::{
expr_needs_parens, is_range_literal,
};
use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferCtxt, InferOk};
use rustc_middle::query::Key;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use this trait.

let ty = trait_pred.self_ty();
if let Some(id) = ty.def_id_for_ty_in_cycle() {
let is_locally_reachable = tcx.parent(def_id).is_crate_root();
let vis: &rustc_middle::middle::privacy::EffectiveVisibilities =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type ascription isn't needed.

let is_locally_reachable = tcx.parent(def_id).is_crate_root();
vis.is_reachable(local) || is_locally_reachable
let ty = trait_pred.self_ty();
if let Some(id) = ty.def_id_for_ty_in_cycle() {
Copy link
Member

@compiler-errors compiler-errors Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be matching on the tykind here, rather than using def_id_for_ty_in_cycle. Doesn't it seem weird that you're using a function with that name? 😅

if let Some(id) = ty.def_id_for_ty_in_cycle() {
let is_locally_reachable = tcx.parent(def_id).is_crate_root();
let vis: &rustc_middle::middle::privacy::EffectiveVisibilities =
&tcx.resolutions(()).effective_visibilities;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

effective_visibilities is exposed through its own query, so using resolutions(()).effective_visibilities is less efficient.

let vis: &rustc_middle::middle::privacy::EffectiveVisibilities =
&tcx.resolutions(()).effective_visibilities;
let is_reachable = vis.effective_vis(local).is_some_and(|v| {
v.at_level(rustc_middle::middle::privacy::Level::Reachable)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not import Level?

v.at_level(rustc_middle::middle::privacy::Level::Reachable)
.is_accessible_from(id, tcx)
});
is_reachable || is_locally_reachable
Copy link
Member

@compiler-errors compiler-errors Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic seems subtle, and so at least needs a comment. Also, I may be wrong, but isn't locally_reachable implied by reachable?

let is_locally_reachable = tcx.parent(def_id).is_crate_root();
vis.is_reachable(local) || is_locally_reachable
let ty = trait_pred.self_ty();
if let Some(id) = ty.def_id_for_ty_in_cycle() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please give id a better name; we already have def_id in scope.

@compiler-errors compiler-errors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Diagnostic incorrectly identifying sealed traits
4 participants