-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Sidestep bad precedence of closures in let
bindings
#64277
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
Conversation
Tested locally on project with the issue:
|
src/libsyntax/print/pprust.rs
Outdated
|| parser::needs_par_as_let_scrutinee(scrutinee.precedence().order()) | ||
|| parser::needs_par_as_let_scrutinee(scrutinee.precedence().order()) | ||
// #51635: handle closures correctly, even though they have a really low | ||
// precedence, they *don't* need parens for assignment 🤷. |
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.
Heh; would be good to replace 🤷 with the actual reason if you have a sec to figure it out.
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 waiting on insight from @petrochenkov, I believe the precedence order between let
and closures is incorrectly, but I didn't want to affect all other relative precedences accidentally so I did this, but the underlying reason would require going over every Precedence
and double checking them. Clearly the Parser
disagrees with what we have encoded :-|
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.
The alternatives would of course be to either change the parser to conform to the encoded precedence (potentially breaking working code) or changing the precedence order (potentially breaking working code) :-|
I don't know right away whether this change is correct or not and don't have time to investigate. |
So the precedences here are a bit tricky. This may take some time to think through but I'll try to find some of that soon. |
Update for wg-triage: I will set aside time to review this tomorrow or declare reviewer bankruptcy if it takes a lot more. :) |
@Centril did you get a chance to take a look at this? |
Here's my investigation thus far: There seems to be more wrong with the current code than this PR intends to fix but it doesn't seem like the diff in the PR is the right fix either. It could be that the parser is incorrect, but if not, then pprust should do something more like: /// Print a `let pat = scrutinee` expression.
crate fn print_let(&mut self, pat: &ast::Pat, scrutinee: &ast::Expr) {
self.s.word("let ");
self.print_pat(pat);
self.s.space();
self.word_space("=");
let order = scrutinee.precedence().order();
self.print_expr_cond_paren(
scrutinee,
parser::contains_exterior_struct_lit(scrutinee)
|| parser::needs_par_as_let_scrutinee(order) && order > PREC_JUMP
)
} I think we'll also need to add appropriate tests to the PR. The playground above might be reusable for that. |
Avoid adding unnecessary parentheses around closures in `if let _ = || ()`.
@Centril the pretty printer actually has some of these weird parses correct:
The above gets interpreted as the addition of
|
I'm not sure. Technically it's a breaking change because of conditional compilation and macros to move the error to the parser. Also, I don't have an intuition for why this should and should not parse without a formally encoded grammar and wg-grammar is nowhere near the ability to make such assertions. cc @rust-lang/wg-grammar tho. It's maybe best to only adjust pprust conservatively (in the way I showed above) for now and leave FIXMEs where appropriate? |
ba8252f
to
9f4076d
Compare
9f4076d
to
22aab52
Compare
@estebank Ideally we would add some tests also with FIXMEs where appropriate. =P |
Avoid adding unnecessary parentheses around closures in
if let _ = || ()
.CC #51635.
r? @petrochenkov