-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Don't suggest using fields in a static method #33615
Conversation
r? @Aatch (rust_highfive has picked a reviewer for you, use r? to override) |
} else { | ||
"".to_string() | ||
} | ||
} |
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 find this whole thing is a bit unclear. Basically we are trying to detect things based on what fields there are in the struct rather than in what kind of expression the error occurs. What you're doing, if I understand correctly, is that you emit the help message when trying to refer to a variable named path_name
only if we are in a static method of a type that has a field of name path_name
, wether we are in the context of a call expression or not (which is fine, the type could have a closure field... ).
But you don't catch the cases where the user would try to invoke a method from the type... Maybe this is the right thing to do since good reporting would require remembering that we are in a call-expression context, which may be a bit overkill. But I don't know... the thing is that if the type has both an field and a method named foo
, then foo(...)
will spawn the help message saying that you don't have access to the fields, but if you have only a method named foo
and no such field, you will not have the message. All of this seems a bit inconsistent. But maybe it's not that much of a problem...
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.
Just updating the help message to include non-static methods as well would solve it I guess.
Updated. @LeoTestard: Does the error message seem better to you or still not good? |
9209cb4
to
ec6877a
Compare
☔ The latest upstream changes (presumably #33505) made this pull request unmergeable. Please resolve the merge conflicts. |
ec6877a
to
929d6ac
Compare
UnresolvedNameContext::Other => { } // no help available | ||
UnresolvedNameContext::Other => { | ||
if msg.is_empty() && is_static_method && is_field { | ||
err.help("This is a static method, you don't have access to \ |
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.
messages usually start lowercase, no?
Also I remember @steveklabnik saying that "static method" is not supposed to be used, but "associated function".
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.
messages usually start lowercase, no?
Indeed.
Also I remember @steveklabnik saying that "static method" is not supposed to be used, but "associated function".
We'll have to wait for confirmation.
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.
Yes, any fn
declared in an impl
block is an associated function, and we call an associated function that takes a self
a "method".
3424c47
to
5f7c7dd
Compare
@@ -1665,7 +1663,7 @@ impl<'a> Resolver<'a> { | |||
let type_parameters = | |||
HasTypeParameters(&sig.generics, | |||
FnSpace, | |||
MethodRibKind); | |||
MethodRibKind(false)); |
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'm not sure if it's useful but in doubt, it's certainly more correct to tell wether the method is static here, rather than just using false
. You have a MethodSig
so it should be trivial.
5f7c7dd
to
fa338ee
Compare
Updated. |
r? @pnkfelix |
@@ -148,7 +148,7 @@ enum ResolutionError<'a> { | |||
/// error E0424: `self` is not available in a static method | |||
SelfNotAvailableInStaticMethod, | |||
/// error E0425: unresolved name | |||
UnresolvedName(&'a str, &'a str, UnresolvedNameContext<'a>), | |||
UnresolvedName(&'a str, &'a str, UnresolvedNameContext<'a>, bool, bool), |
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 either:
- document what each of these
bool
's represents in a comment above this variant. (I know I can infer it by grepping for the other uses ofUnresolvedName
, but I don't like having to do that to make sense of this definition), OR - alternative 1: use a struct-style variant (i.e. named fields) instead of a tuple-style variant here, so that you can name each piece of state, or
- alternative 2: encode the significance of each
bool
here in the type.- (there are many ways to do this, such as introducing two new two variant enums, or introducing two structs that each just wrap around
bool
, so that the name of the struct dictates its significance.)
- (there are many ways to do this, such as introducing two new two variant enums, or introducing two structs that each just wrap around
@GuillaumeGomez this code looks fine; r=me after you address my nit regarding the way you are representing the boolean state being added to each variant. |
UnresolvedNameContext::Other => { } // no help available | ||
UnresolvedNameContext::Other => { | ||
if msg.is_empty() && is_static_method && is_field { | ||
err.help("this is an associated function, you don't have access to \ |
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.
If I understand correctly, this is only shown if there is a field by that name?
In that case, we should instead say something like "This is a static (non-self
) method, and does not have access to fields. If you wished to access self.{fieldname}
you should add a self
parameter to the method".
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, in this case, we should not suggest self.fieldname
directly -- I don't think this change makes that happen?
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.
If I understand correctly, this is only shown if there is a field by that name?
Or a method. So what follows your comment cannot really apply.
Also, in this case, we should not suggest
self.fieldname
directly -- I don't think this change makes that happen?
It does remove it.
fa338ee
to
145747e
Compare
@bors: r=pnkfelix |
📌 Commit 145747e has been approved by |
💡 This pull request was already approved, no need to approve it again.
|
📌 Commit 145747e has been approved by |
Don't suggest using fields in a static method Fixes #33613. cc @LeoTestard
Fixes #33613.
cc @LeoTestard