-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
pprust: fix parenthesization of exprs #43742
Conversation
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
Looks good to me. There's also HIR pretty-printing in |
Hi @epdtry! I think @petrochenkov has some concerns about your PR. Could you try to address them? Just a friendly ping to make sure this isn't getting lost |
Hi, sorry about the delay. I've written some new commits to address @petrochenkov's comments, but I did the work for this PR "on the clock", so there's a review process the changes need to go through before I can publish them anywhere. This will hopefully be done soon, but it could be up to another week or two. I'll update this PR with the new commits as soon as I get the approval. |
@petrochenkov I've added similar parenthesization fixes to the HIR printer. This part doesn't have a test case, because I'm not sure how to write one. (The tricks I used for testing the AST printer don't work easily because there's no HIR parser and no HIR |
Rebased (adding support for |
Github says there was an error, but I don't see any errors on the travis build page, only some xcode builds that were cancelled midway through cloning the repo. Can I ignore this, or is something actually broken? |
The Travis error looks spurious. @bors r+ |
📌 Commit 347db06 has been approved by |
⌛ Testing commit 347db06 with merge 6de8f2b23b0f876de69fbc48dac8ff776ab5e4fe... |
💔 Test failed - status-appveyor |
Error message
Diff (ignoring whitespace)--- 1.rs 2017-09-07 15:14:53.000000000 +0800
+++ 2.rs 2017-09-07 15:15:06.000000000 +0800
@@ -44,7 +44,7 @@
}
as
&[&str]),
- (&(match (()
+ (&((match (()
as
())
{
@@ -55,7 +55,7 @@
[std::fmt::ArgumentV1<'_>; 0]),
}
as
- [std::fmt::ArgumentV1<'_>; 0])
+ [std::fmt::ArgumentV1<'_>; 0]))
as
&[std::fmt::ArgumentV1<'_>; 0]))
as |
Should be fixed in the latest commit. I turned up the precedence of the block-like exprs ( |
@bors r+ |
📌 Commit 3454d99 has been approved by |
⌛ Testing commit 3454d99 with merge fd20f603855e10430b2cf9b06793f5c854c603c3... |
💔 Test failed - status-travis |
@bors retry Travis's Macs are not feeling well this week. https://www.traviscistatus.com/incidents/4qylrqvy50gy |
⌛ Testing commit 3454d99 with merge 7eefbffdd79d1f1b18395bb578a607a2647c6b82... |
💔 Test failed - status-travis |
I see failures in run-make/sanitizer-address and run-make/sanitizer-dylib-link. I can't imagine how those tests could be affected by this PR, so is this another travis bug of some kind? If not, how should I start debugging? I've run those tests locally and they both pass. |
@bors retry |
⌛ Testing commit 3454d99 with merge cd398828b8d7e44c534eabcfd198dbe82007debc... |
@bors: retry |
⌛ Testing commit 3454d99 with merge 57fe99b7a42ae25b3ea22f1bf37786c531cbbb56... |
💔 Test failed - status-appveyor |
@bors retry |
pprust: fix parenthesization of exprs The pretty printer in `syntax::print::pprust` currently relies on the presence of `ExprKind::Paren` hints in order to correctly parenthesize expressions in its output. If `Paren` nodes are missing, it sometimes produces wrong output, such as printing `1 - (2 - 3)` as `1 - 2 - 3`. This PR fixes `pprust` to correctly print expressions regardless of the presence or absence of `Paren` nodes. This should make `pprust` easier to use with programmatically constructed ASTs. A few notes: * I added a function for assigning precedence values to exprs in `syntax::util::parser`, since there is already code there for assigning precedence values to binops. Let me know if I should move this somewhere more `pprust`-specific. * I also moved the `contains_exterior_struct_lit` function from `rustc_lint::unused::UnusedParens` into `syntax::util::parser`, since it's needed for determining the correct parenthesization of `if`/`while` conditional expressions. * I couldn't find a good way to compare two exprs for equivalence while ignoring semantically-irrelevant details like spans. So the test for the new behavior relies on a slight hack: it adds `Paren` nodes everywhere, so that the pretty-printed version exactly reflects the structure of the AST, and then compares the printed strings. This works, but let me know if there's a better way.
☀️ Test successful - status-appveyor, status-travis |
Detect invalid exprs in parser used by pretty-printer tests This PR fixes a bug in rust-lang#133730 inherited from rust-lang#43742. Before this fix, the test might silently only operate on a prefix of some of the test cases in this table: https://github.com/rust-lang/rust/blob/13170cd787cb733ed24842ee825bcbd98dc01476/tests/ui-fulldeps/pprust-parenthesis-insertion.rs#L57 For example, adding the test case `1 .. 2 .. 3` (a syntactically invalid expression) into the table would unexpectedly succeed the test instead of crashing at this unwrap: https://github.com/rust-lang/rust/blob/13170cd787cb733ed24842ee825bcbd98dc01476/tests/ui-fulldeps/pprust-parenthesis-insertion.rs#L199-L200 because `parse_expr` would successfully parse just `1 .. 2` and disregard the last `.. 3`. This PR adds a check that `parse_expr` reaches `Eof`, ensuring all the test cases actually test the whole expression they look like they are supposed to.
Rollup merge of rust-lang#134599 - dtolnay:fulldepsparser, r=fmease Detect invalid exprs in parser used by pretty-printer tests This PR fixes a bug in rust-lang#133730 inherited from rust-lang#43742. Before this fix, the test might silently only operate on a prefix of some of the test cases in this table: https://github.com/rust-lang/rust/blob/13170cd787cb733ed24842ee825bcbd98dc01476/tests/ui-fulldeps/pprust-parenthesis-insertion.rs#L57 For example, adding the test case `1 .. 2 .. 3` (a syntactically invalid expression) into the table would unexpectedly succeed the test instead of crashing at this unwrap: https://github.com/rust-lang/rust/blob/13170cd787cb733ed24842ee825bcbd98dc01476/tests/ui-fulldeps/pprust-parenthesis-insertion.rs#L199-L200 because `parse_expr` would successfully parse just `1 .. 2` and disregard the last `.. 3`. This PR adds a check that `parse_expr` reaches `Eof`, ensuring all the test cases actually test the whole expression they look like they are supposed to.
The pretty printer in
syntax::print::pprust
currently relies on the presence ofExprKind::Paren
hints in order to correctly parenthesize expressions in its output. IfParen
nodes are missing, it sometimes produces wrong output, such as printing1 - (2 - 3)
as1 - 2 - 3
. This PR fixespprust
to correctly print expressions regardless of the presence or absence ofParen
nodes. This should makepprust
easier to use with programmatically constructed ASTs.A few notes:
I added a function for assigning precedence values to exprs in
syntax::util::parser
, since there is already code there for assigning precedence values to binops. Let me know if I should move this somewhere morepprust
-specific.I also moved the
contains_exterior_struct_lit
function fromrustc_lint::unused::UnusedParens
intosyntax::util::parser
, since it's needed for determining the correct parenthesization ofif
/while
conditional expressions.I couldn't find a good way to compare two exprs for equivalence while ignoring semantically-irrelevant details like spans. So the test for the new behavior relies on a slight hack: it adds
Paren
nodes everywhere, so that the pretty-printed version exactly reflects the structure of the AST, and then compares the printed strings. This works, but let me know if there's a better way.