-
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 possibly non-Rust closure syntax during parse #47763
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
41cba92
to
a3b3a75
Compare
src/libsyntax/parse/parser.rs
Outdated
node: StmtKind::Expr(expr), .. | ||
}) = stmts.last() { | ||
warn.span_note(block_sp, "...while this enclosing block..."); | ||
warn.span_note(expr.span, "...implicitely returns"); |
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.
Typo: "implicitly".
Other languages have a similar, but not quite the same to Rust, syntax for closures: ``` {|<args>| <stmts> } ``` Even worse, the above code *is* valid Rust if and only if there's only one statement, as the block would resolve to the single statement closure within it. This means that seemingly correct code will fail when adding a new statement, making modifying something that looks unrelated cause "value not found in this scope" and "trait bound not satisfied" errors. Because of this, make the parser detect the specific case where the code is incorrect *and* failing, and emit a warning explaining the correct syntax.
a3b3a75
to
e093191
Compare
This is a semantic error, so I'd rather not report it in the parser. Given that, I think there are two possibilities:
|
This is also somewhat a policy question, like @Xirdus mentioned in #27300 (comment). |
@petrochenkov I'm unclear on why you marked this as waiting on author — what kind of response are you expecting from @estebank? If this needs a "policy decision", then it seems like it needs to go to the appropriate team, instead. |
Making this an early warn-by-default lint instead of a hardcoded warning in parser.
I'm not entirely sure this needs a larger policy decision than rubber-stamping from me, but let's tag it as compiler-team-nominated anyway. |
I'm going to clear the nominated flag. My feeling is that targeted errors of this kind are worth it, if they are not too intrusive -- it's hard to make a hard-and-fast rule, but I love the reputation of rustc as a "helpful compiler", and if we see a place that people stub their toes a lot, we should try to patch that. (In particular, I see no reason to pretend other languages don't exist.) All that said, I don't have a strong opinion about whether this particular message should be given in the parser or a lint or what. I'd say that if the code does successfully parse, then some sort of lint seems like a better place for it. |
@estebank the reviewer asked to turn this into an early warn-by-default lint in #47763 (comment), could you do that? |
@pietroalbini I'll get back to this sometime this week to turn it into a lint. |
Would you also submit the opposite patch to Ruby 😇? Surprisingly, that's the biggest stumbling block I have switching between the two... |
@estebank this PR has been stale for more than two weeks, so I'm closing it. If you have time to work on this again in the future feel free to reopen it! |
Other languages have a similar, but not quite the same to Rust, syntax
for closures:
Even worse, the above code is valid Rust if and only if there's only
one statement, as the block would resolve to the single statement
closure within it. This means that seemingly correct code will fail when
adding a new statement, making modifying something that looks unrelated
cause "value not found in this scope" and "trait bound not satisfied"
errors. Because of this, make the parser detect the specific case where
the code is incorrect and failing, and emit a warning explaining the
correct syntax.
Fix #27300.