From e7f8895359365403a955468156a35bf26a45e0bf Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 24 Mar 2020 15:20:19 -0400 Subject: [PATCH 1/3] save/restore `pessimistic_yield` when entering bodies This flag is used to make the execution order around `+=` operators pessimistic. Failure to save/restore the flag was causing independent async blocks to effect one another, leading to strange ICEs and failed assumptions. --- src/librustc_passes/region.rs | 2 ++ src/test/ui/async-await/issues/issue-69307.rs | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/test/ui/async-await/issues/issue-69307.rs diff --git a/src/librustc_passes/region.rs b/src/librustc_passes/region.rs index e771696a5b6bf..1807756fe524b 100644 --- a/src/librustc_passes/region.rs +++ b/src/librustc_passes/region.rs @@ -720,6 +720,7 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> { let outer_ec = mem::replace(&mut self.expr_and_pat_count, 0); let outer_cx = self.cx; let outer_ts = mem::take(&mut self.terminating_scopes); + let outer_pessimistic_yield = mem::replace(&mut self.pessimistic_yield, false); self.terminating_scopes.insert(body.value.hir_id.local_id); if let Some(root_id) = self.cx.root_id { @@ -771,6 +772,7 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> { self.expr_and_pat_count = outer_ec; self.cx = outer_cx; self.terminating_scopes = outer_ts; + self.pessimistic_yield = outer_pessimistic_yield; } fn visit_arm(&mut self, a: &'tcx Arm<'tcx>) { diff --git a/src/test/ui/async-await/issues/issue-69307.rs b/src/test/ui/async-await/issues/issue-69307.rs new file mode 100644 index 0000000000000..4dae96ec8a6a7 --- /dev/null +++ b/src/test/ui/async-await/issues/issue-69307.rs @@ -0,0 +1,23 @@ +// Regression test for #69307 +// +// Having a `async { .. foo.await .. }` block appear inside of a `+=` +// expression was causing an ICE due to a failure to save/restore +// state in the AST numbering pass when entering a nested body. +// +// check-pass +// edition:2018 + +fn block_on(_: F) -> usize { + 0 +} + +fn main() {} + +async fn bar() { + let mut sum = 0; + sum += block_on(async { + baz().await; + }); +} + +async fn baz() {} From cd9f709a33b952d52c34e037dc13adfe0a3efb8d Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Mon, 6 Apr 2020 14:51:00 +0000 Subject: [PATCH 2/3] add nested regression test --- .../async-await/issues/issue-69307-nested.rs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/test/ui/async-await/issues/issue-69307-nested.rs diff --git a/src/test/ui/async-await/issues/issue-69307-nested.rs b/src/test/ui/async-await/issues/issue-69307-nested.rs new file mode 100644 index 0000000000000..b7cdf3987f1cb --- /dev/null +++ b/src/test/ui/async-await/issues/issue-69307-nested.rs @@ -0,0 +1,30 @@ +// Regression test for #69307 +// +// Having a `async { .. foo.await .. }` block appear inside of a `+=` +// expression was causing an ICE due to a failure to save/restore +// state in the AST numbering pass when entering a nested body. +// +// check-pass +// edition:2018 + +fn block_on(_: F) -> usize { + 0 +} + +fn main() {} + +async fn bar() { + let mut sum = 0; + sum += { + block_on(async { + baz().await; + let mut inner = 1; + inner += block_on(async { + baz().await; + 0 + }) + }) + }; +} + +async fn baz() {} From 563152d8830737b65d39d280214b1a64aa006f98 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 8 Apr 2020 14:49:57 +0000 Subject: [PATCH 3/3] comment pessimistic yield and saving/restoring state --- src/librustc_passes/region.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/librustc_passes/region.rs b/src/librustc_passes/region.rs index 1807756fe524b..32e7e8843a07c 100644 --- a/src/librustc_passes/region.rs +++ b/src/librustc_passes/region.rs @@ -717,9 +717,16 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> { self.cx.parent ); + // Save all state that is specific to the outer function + // body. These will be restored once down below, once we've + // visited the body. let outer_ec = mem::replace(&mut self.expr_and_pat_count, 0); let outer_cx = self.cx; let outer_ts = mem::take(&mut self.terminating_scopes); + // The 'pessimistic yield' flag is set to true when we are + // processing a `+=` statement and have to make pessimistic + // control flow assumptions. This doesn't apply to nested + // bodies within the `+=` statements. See #69307. let outer_pessimistic_yield = mem::replace(&mut self.pessimistic_yield, false); self.terminating_scopes.insert(body.value.hir_id.local_id);