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

Modify message for keyword as identifier name #46497

Merged
merged 6 commits into from
Dec 7, 2017

Conversation

AgustinCB
Copy link
Contributor

This is a temporary solution to #46311.

The message is generic enough to cover both cases and is probably a fine enough solution to the specific problem described in the task. However, the underlying reason for this to be wrong is that next_token_inner returns Lifetime even if the token is a label. That's not simple, as the syntax for both can be quite similar and it may need to take a look to the next token to make a decision. I'm not sure I have enough knowledge about the project to be able to solve that (yet!), so I thought I'll fix the immediate problem first.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @eddyb (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@petrochenkov
Copy link
Contributor

This error doesn't necessarily have to be reported in lexer.
You can move it into AST validation, there you can discern between lifetimes and labels. (Some invalid label names are already reported there.)

@petrochenkov petrochenkov assigned petrochenkov and unassigned eddyb Dec 4, 2017
@AgustinCB
Copy link
Contributor Author

That's a good idea. Will do that tomorrow. Thanks for the suggestion.

@kennytm kennytm added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Dec 5, 2017
@AgustinCB
Copy link
Contributor Author

Done! I'm not sure if it was the best task to make my first contribution, as I wasn't completely sure I understood all the details and some of the decisions that I made. Let me know if I'm missing something. Thanks.

self.invalid_visibility(&item.vis, item.span, None);
for impl_item in impl_items {
self.invalid_visibility(&impl_item.vis, impl_item.span, None);
if let ImplItemKind::Method(ref sig, _) = impl_item.node {
self.check_trait_fn_not_const(sig.constness);
}
}
generics.lifetimes.iter().for_each(|l| self.check_lifetime(&l.lifetime))
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not necessary to check lifetimes in all possible contexts manually, you can add a visit_lifetime method to impl<'a> Visitor<'a> for AstValidator<'a> and call check_lifetime from it.

Visitor is kind of an object-oriented infrastructure for walking through AST. The default visitor in libsyntax/visit.rs is a base visitor that does nothing and only walks the tree and other concrete visitors (like AstValidator) can override its methods and add something useful to them.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is actually a good first task because the visitor is a quite important piece of infrastructure and it makes you familiar with it.

@@ -35,8 +34,16 @@ impl<'a> AstValidator<'a> {
&self.session.parse_sess.span_diagnostic
}

fn check_lifetime(&self, lifetime: &Lifetime) {
if !lifetime.ident.without_first_quote().is_valid() &&
Copy link
Contributor

@petrochenkov petrochenkov Dec 6, 2017

Choose a reason for hiding this comment

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

lifetime.ident.without_first_quote().is_valid() -> token::Ident(lifetime.ident).is_reserved_ident() and all the new functions (is_valid, is_used_keyword, is_static_keyword, etc) can be removed.
Also without_first_quote is used only once so it's probably better inlined right here.

Copy link
Contributor

@petrochenkov petrochenkov Dec 6, 2017

Choose a reason for hiding this comment

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

Invalid names for lifetimes are all is_reserved_ident names except for keywords::Static and keywords::Invalid.
Invalid names for labels are simply all is_reserved_ident plus '_.

@petrochenkov
Copy link
Contributor

@bors r+
Thanks!

@bors
Copy link
Contributor

bors commented Dec 7, 2017

📌 Commit 29e2680 has been approved by petrochenkov

@bors
Copy link
Contributor

bors commented Dec 7, 2017

⌛ Testing commit 29e2680 with merge c8ddf28...

bors added a commit that referenced this pull request Dec 7, 2017
Modify message for keyword as identifier name

This is a temporary solution to #46311.

The message is generic enough to cover both cases and is probably a fine enough solution to the specific problem described in the task. However, the underlying reason for this to be wrong is that `next_token_inner` returns `Lifetime` even if the token is a label. That's not simple, as the syntax for both can be quite similar and it may need to take a look to the next token to make a decision. I'm not sure I have enough knowledge about the project to be able to solve that (yet!), so I thought I'll fix the immediate problem first.
@bors
Copy link
Contributor

bors commented Dec 7, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: petrochenkov
Pushing c8ddf28 to master...

@bors bors merged commit 29e2680 into rust-lang:master Dec 7, 2017
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants