-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: xizheyin <[email protected]>
…tive Signed-off-by: xizheyin <[email protected]>
There was a problem hiding this 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; |
There was a problem hiding this comment.
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 = |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
Fixes #143392
I used relative visibility instead of just determining if it's public or not.
r? compiler