-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Detect async visibility wrong order, async pub
#76447
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
niko is probably the wrong reviewer for this maybe r? @estebank |
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 would prefer to make this change to parse_fn_front_matter
:
rust/compiler/rustc_parse/src/parser/item.rs
Lines 1566 to 1594 in 0e2c128
/// Parses all the "front matter" (or "qualifiers") for a `fn` declaration, | |
/// up to and including the `fn` keyword. The formal grammar is: | |
/// | |
/// ``` | |
/// Extern = "extern" StringLit ; | |
/// FnQual = "const"? "async"? "unsafe"? Extern? ; | |
/// FnFrontMatter = FnQual? "fn" ; | |
/// ``` | |
pub(super) fn parse_fn_front_matter(&mut self) -> PResult<'a, FnHeader> { | |
let constness = self.parse_constness(); | |
let asyncness = self.parse_asyncness(); | |
let unsafety = self.parse_unsafety(); | |
let ext = self.parse_extern()?; | |
if let Async::Yes { span, .. } = asyncness { | |
self.ban_async_in_2015(span); | |
} | |
if !self.eat_keyword(kw::Fn) { | |
// It is possible for `expect_one_of` to recover given the contents of | |
// `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't | |
// account for this. | |
if !self.expect_one_of(&[], &[])? { | |
unreachable!() | |
} | |
} | |
Ok(FnHeader { constness, unsafety, asyncness, ext }) | |
} |
That way the parser can recover more gracefully and you also have the spans for each element to work with more easily. You could also check for other permutations, like async const pub fn
and unsafe async fn
.
| ^^^ | ||
| | | ||
| expected one of 9 possible tokens | ||
| help: visibility `pub` must come before `pub pub`: `pub pub pub` |
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 is weird, but at least users can know that visibility is duplicated. We could add a check but I probably don't want to do it in this patch.
What's left for another patch
Duplicate visibility (this one is obvious unlike the rest)
|
2e9de38
to
2c6ae2d
Compare
tidy error: /checkout/src/test/ui/parser/default.rs:24: line longer than 100 chars
tidy error: /checkout/src/test/ui/parser/duplicate-visibility.rs:5: line longer than 100 chars I don't know how to fix this without using |
@estebank Do you know how should I fix this? |
I would add the |
@estebank Can you please help to review again? |
src/test/ui/parser/default.stderr
Outdated
LL | default pub fn foo<T: Default>() -> T { T::default() } | ||
| ^^^ non-item starts here | ||
... | ||
| ^^^ | ||
| | | ||
| expected one of 7 possible tokens | ||
| help: visibility `pub` must come before `default pub`: `pub default pub` |
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.
Only one nitpick: this suggestion is incorrect. The span should point at default pub
and the suggestion content should be pub default
.
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.
pub default
? Can you show me an example output you mentioned? I don't think default pub
should be there, it should be pub
only right?
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.
Actually, it should be only default
, as pub
is implied, but yes the correct syntactic order is pub default fn foo()
.
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.
How should I check that? It doesn't seemed to be part of parse_fn_front_matter
but rather in parse_fn_kind
, I think it is related to
} else if self.check_fn_front_matter() {
// FUNCTION ITEM
let (ident, sig, generics, body) = self.parse_fn(attrs, req_name, lo)?;
(ident, ItemKind::Fn(def(), sig, generics, body))
It feels weird that defaultness
is not parsed as part of parse_fn_front_matter
.
I feel lazy to do this, requires cleaning on other parts, @estebank do you want to do it?
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 should be fixed. I didn't know this is a regression created by the patch I made.
ping from triage: |
I haven't get myself to work on the last thing the reviewer mentioned. I think it may be a bit different from where I worked on this, it would be good if @estebank could point out where needs to be changed, I feel less motivated to work on the remaining parts either since this may need changes in other parts. If @estebank want to work on this then he can take this up, otherwise I can take a look at it this weekend. |
@pickfire can you resolve the conflict ? will be faster as we can get a review and then merge it quickly |
This comment has been minimized.
This comment has been minimized.
@Dylan-DPC done |
☔ The latest upstream changes (presumably #82517) made this pull request unmergeable. Please resolve the merge conflicts. |
triage: @pickfire unfortunately there's another merge conflict. |
@rustbot label: -S-waiting-on-review +S-waiting-on-author |
@JohnCSimon Do I have to keep on updating it? I am thinking of getting a review first before updating it, I have updated a few times in the past for merge conflict but still no review. |
@pickfire better to, makes it easier to merge if the review is green after that |
Redirects `const? async? unsafe? pub` to `pub const? async? unsafe?`. Fix rust-lang#76437
async-pub check created a regression for default
@pickfire my apologies. I believed I could get around this to point you towards an alternative way of doing this, but there's no reason not to merge this as is and potentially implement my idea later. So it isn't lost to the corners of my mind, my thinking here was to make the parser advance through tokens that can be part of the fn front matter ( @bors r+ |
📌 Commit 21c1574 has been approved by |
☀️ Test successful - checks-actions |
Ah, but it seems like something that we aren't going to need. Why would someone duplicate the header? I don't see any reason someone would do so. Order I can understand if they are wrong, but maybe duplicating them is too much I believe. Maybe I am wrong, if people copy pasted it could happen, but we may need extra care not to slow down the usual path. |
Partially address #76437.