1
Fork 0

Rollup merge of #54777 - zackmdavis:async_pretty_ice, r=cramertj

abolish ICE when pretty-printing async block

@jnetterf reported an ICE when the unused-parentheses lint triggered around an async block (#54752). In order to compose an autofixable suggestion, the lint invokes the pretty-printer on the unnecessarily-parenthesized expression. (One wonders why the lint doesn't just use `SourceMap::span_to_snippet` instead, to preserve the formatting of the original source?—but to answer that, you'd have to ask the author of 5c9f806d.)

But then the pretty-printer panics when trying to call `<pprust::State as PrintState>::end` when `State.boxes` is empty. Empirically, the problem would seem to be solved if we start some "boxes" beforehand in the `ast::ExprKind::Async` arm of the big match in `print_expr_outer_attr_style`, exactly like we do in the immediately-preceding match arm for `ast::ExprKind::Block`—it would seem pretty ("pretty") reasonable for the pretty-printing of async blocks to work a lot like the pretty-printing of ordinary non-async blocks, right??

Of course, it would be shamefully cargo-culty to commit code on the basis of this kind of mere reasoning-by-analogy (in contrast to understanding the design of the pretty-printer in such detail that the correctness of the patch is comprehended with all the lucid certainty of mathematical proof, rather than being merely surmised by intuition). But maybe we care more about fixing the bug with high probability today, than with certainty in some indefinite hypothetical future?  Maybe the effort is worth [a fifth of a shirt](https://hacktoberfest.digitalocean.com/stats/zackmdavis)??

Humbly resolves #54752.

r? @cramertj
This commit is contained in:
Pietro Albini 2018-10-04 12:20:12 +02:00 committed by GitHub
commit 7523cdf101
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View file

@ -2228,6 +2228,9 @@ impl<'a> State<'a> {
self.word_nbsp("async")?;
self.print_capture_clause(capture_clause)?;
self.s.space()?;
// cbox/ibox in analogy to the `ExprKind::Block` arm above
self.cbox(INDENT_UNIT)?;
self.ibox(0)?;
self.print_block_with_attrs(blk, attrs)?;
}
ast::ExprKind::Assign(ref lhs, ref rhs) => {

View file

@ -0,0 +1,7 @@
#![feature(async_await)]
#![allow(unused_parens)]
// edition:2018
// pp-exact
fn main() { let _a = (async { }); }