-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Change privacy checks, particularly for tuple structs #58173
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
129226c
to
c565418
Compare
@@ -47,7 +47,7 @@ error: trait `priv_trait::PrivTr` is private | |||
--> $DIR/associated-item-privacy-trait.rs:25:16 | |||
| | |||
LL | let _: <Pub as PrivTr>::AssocTy; | |||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | |||
| ^^^^^^^^^^^^^^^^^^^^^^^^ private |
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.
It'd be nice to only emit one of this and the prior error.
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.
The hightlight is also not optimal. It should probably be just on the PrivTr
or at least on the <Pub as PrivTr>
This comment has been minimized.
This comment has been minimized.
Move tuple struct privacy checks away from `resolve` in order to evaluate more explicitely point at private tuple struct fields when they are the cause of the tuple struct being inaccessible. For structs that are inaccessible, point at the definition span. Reword privacy messages to be more specific about the ADT kind name. Group private field errors per struct.
c565418
to
7ce29fa
Compare
} | ||
self.emit_field_checks(adt, expr.span, field_errors, "built"); | ||
} | ||
hir::ExprKind::Call(ref path, ref fields) => { |
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.
Add a comment that this is about tuple struct constructors
} | ||
|
||
impl<'a, 'tcx> TypePrivacyVisitor<'a, 'tcx> { | ||
fn item_is_accessible(&self, did: DefId) -> bool { | ||
def_id_visibility(self.tcx, did).0.is_accessible_from(self.current_item, self.tcx) | ||
let (a, ..) = def_id_visibility(self.tcx, did); |
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.
use a better variable name than a
@@ -799,22 +801,71 @@ struct NamePrivacyVisitor<'a, 'tcx: 'a> { | |||
tables: &'a ty::TypeckTables<'tcx>, | |||
current_item: ast::NodeId, | |||
empty_tables: &'a ty::TypeckTables<'tcx>, | |||
reported_tuple_structs: FxHashSet<Span>, |
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.
explain this field here instead of just at the use sites
@@ -927,11 +1042,13 @@ struct TypePrivacyVisitor<'a, 'tcx: 'a> { | |||
in_body: bool, | |||
span: Span, | |||
empty_tables: &'a ty::TypeckTables<'tcx>, | |||
reported_tuple_structs: FxHashSet<Span>, |
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.
explain this field here instead of at the use sites
self.tcx.sess.span_err(self.span, &format!("{} `{}` is private", kind, descr)); | ||
let is_ok = self.item_is_accessible(def_id); | ||
if !is_ok { | ||
match self.tcx.hir().as_local_node_id(def_id) { |
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.
use if let
instead of matches here
@@ -1,8 +1,8 @@ | |||
error: type `for<'r> fn(&'r foo::S) {foo::S::f}` is private | |||
error: method `foo::S::f` is private |
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.
❤️
let kind_name = path.def.kind_name(); | ||
let sp = path.span; | ||
let msg = format!("{} `{}` is private", kind_name, path); | ||
let label = format!("{} not accesssible from here", kind_name); |
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.
let label = format!("{} not accesssible from here", kind_name); | |
let label = format!("private {} is not accesssible from here", kind_name); |
@@ -47,7 +47,7 @@ error: trait `priv_trait::PrivTr` is private | |||
--> $DIR/associated-item-privacy-trait.rs:25:16 | |||
| | |||
LL | let _: <Pub as PrivTr>::AssocTy; | |||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | |||
| ^^^^^^^^^^^^^^^^^^^^^^^^ private |
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.
The hightlight is also not optimal. It should probably be just on the PrivTr
or at least on the <Pub as PrivTr>
... | ||
LL | priv_parent_substs::mac!(); | ||
| --------------------------- in this macro invocation | ||
|
||
error: type `priv_parent_substs::Priv` is private | ||
error: struct `priv_parent_substs::Priv` is private | ||
--> $DIR/associated-item-privacy-trait.rs:131:35 | ||
| | ||
LL | pub type InSignatureTy2 = <Priv as PubTr<Pub>>::AssocTy; |
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.
Similarly here, the highlight should only be on the Priv
LL | let x = a::A(3, 4); | ||
| ---- ^ ^ private field | ||
| | | | ||
| | private field |
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.
I think there's a way to only have a single message for multiple spans in one line
From this description and a quick glance at the code this PR does a thing that's fundamentally wrong. I'll review this in detail a bit later, but here's the PR that introduced the current visibility rules for constructors and moved the privacy check to resolve - #38932. |
I'm against going into this direction of moving privacy checks from rustc_resolve to rustc_privacy. It would be ok to attach some extra data to |
A bit of context. When we resolving an entity (name, path, field, method) to a Privacy checks mostly follow this rule with exception of The situation is worse with stability checking, where path stability is still not moved to resolve-time, like it's done for method or associated item stability. What remains in |
☔ The latest upstream changes (presumably #58010) made this pull request unmergeable. Please resolve the merge conflicts. |
Move tuple struct privacy checks away from
resolve
in order to evaluatemore explicitly point at private tuple struct fields when they are the
cause of the tuple struct being inaccessible.
For structs that are inaccessible, point at the definition span.
Reword privacy messages to be more specific about the ADT kind name.
Group private field errors per struct.
Fix #58017.