-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Implement asymmetrical precedence for closures and jumps #134847
base: master
Are you sure you want to change the base?
Conversation
I'm going to look at this in a few hours. |
One note on testing: the best approach I've found in Syn for thoroughly testing the pretty-printing algorithm and parenthesis insertion is the following pair of tests:
For the second test, a major difference between Syn's and rustc's is this: rust/tests/ui-fulldeps/pprust-expr-roundtrip.rs Lines 231 to 232 in ceb0441
Rustc's test just throws up its hands and says "sometimes the printer will just generate invalid syntax". (It is not just the one case mentioned in the comment.) Once the pretty-printer is in better shape, the goal will be to turn this into an unwrap, effectively asserting that the pretty-printer always produces syntactically valid Rust output for any arbitrary syntax tree input that a macro could have generated. That will provide exhaustive coverage of the edge cases. Syn's printer is tested this way. In rustc, for now there are still too many false positives and false negatives to easily make an exhaustive test of the set of expressions that currently happen to work. Fixing false positives (like this PR) is an important prerequisite to being able to fix certain false negatives without indiscriminately inserting way too many parentheses. Meanwhile, PRs like #134661 and #134600 have been fixing false negatives. |
☔ The latest upstream changes (presumably #127541) made this pull request unmergeable. Please resolve the merge conflicts. |
2bd5187
to
7764444
Compare
Rebased to resolve conflict with #127541 in tests/ui/invalid/invalid-rustc_legacy_const_generics-issue-123077.stderr. |
I have been through a series of asymmetrical precedence designs in Syn, and finally have one that I like and is worth backporting into rustc. It is based on just 2 bits of state:
next_operator_can_begin_expr
andnext_operator_can_continue_expr
.Asymmetrical precedence is the thing that enables
(return 1) + 1
to require parentheses while1 + return 1
does not, despite+
always having stronger precedence thanreturn
according to the Rust Reference. This is facilitated bynext_operator_can_continue_expr
.Relatedly, it is the thing that enables
(return) - 1
to require parentheses whilereturn + 1
does not, despite+
and-
having exactly the same precedence. This is facilitated bynext_operator_can_begin_expr
.Example:
-Zunpretty=expanded
Before:After: