Skip to content
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

Merged
merged 1 commit into from
May 25, 2016

Conversation

GuillaumeGomez
Copy link
Member

Fixes #33613.

cc @LeoTestard

@rust-highfive
Copy link
Collaborator

r? @Aatch

(rust_highfive has picked a reviewer for you, use r? to override)

} else {
"".to_string()
}
}
Copy link
Contributor

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...

Copy link
Member Author

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.

@GuillaumeGomez
Copy link
Member Author

Updated.

@LeoTestard: Does the error message seem better to you or still not good?

@GuillaumeGomez GuillaumeGomez force-pushed the field_static_method branch 2 times, most recently from 9209cb4 to ec6877a Compare May 15, 2016 10:16
@bors
Copy link
Contributor

bors commented May 16, 2016

☔ The latest upstream changes (presumably #33505) made this pull request unmergeable. Please resolve the merge conflicts.

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 \
Copy link
Contributor

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".

Copy link
Member Author

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.

Copy link
Member

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".

@GuillaumeGomez GuillaumeGomez force-pushed the field_static_method branch 6 times, most recently from 3424c47 to 5f7c7dd Compare May 18, 2016 18:20
@@ -1665,7 +1663,7 @@ impl<'a> Resolver<'a> {
let type_parameters =
HasTypeParameters(&sig.generics,
FnSpace,
MethodRibKind);
MethodRibKind(false));
Copy link
Contributor

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.

@GuillaumeGomez
Copy link
Member Author

Updated.

@GuillaumeGomez
Copy link
Member Author

r? @pnkfelix

@rust-highfive rust-highfive assigned pnkfelix and unassigned Aatch May 24, 2016
@@ -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),
Copy link
Member

@pnkfelix pnkfelix May 24, 2016

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 of UnresolvedName, 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.)

@pnkfelix
Copy link
Member

@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 \
Copy link
Member

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".

Copy link
Member

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?

Copy link
Member Author

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.

@GuillaumeGomez GuillaumeGomez force-pushed the field_static_method branch 3 times, most recently from fa338ee to 145747e Compare May 24, 2016 17:46
@GuillaumeGomez
Copy link
Member Author

GuillaumeGomez commented May 24, 2016

@bors: r=pnkfelix

@bors
Copy link
Contributor

bors commented May 24, 2016

📌 Commit 145747e has been approved by @pnkfelix

@bors
Copy link
Contributor

bors commented May 24, 2016

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Contributor

bors commented May 24, 2016

📌 Commit 145747e has been approved by pnkfelix

@bors
Copy link
Contributor

bors commented May 25, 2016

⌛ Testing commit 145747e with merge 487de42...

bors added a commit that referenced this pull request May 25, 2016
Don't suggest using fields in a static method

Fixes #33613.

cc @LeoTestard
@bors bors merged commit 145747e into rust-lang:master May 25, 2016
@GuillaumeGomez GuillaumeGomez deleted the field_static_method branch May 25, 2016 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants