From 7c108ababc7a54bd090f85aa7daab606af8ac6e8 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 2 Dec 2021 22:33:40 -0800 Subject: [PATCH] Fix ICE when yielding in fn returning impl Trait --- .../src/type_check/input_output.rs | 26 ++++++++++++++++--- src/test/ui/generator/issue-91477.rs | 7 +++++ src/test/ui/generator/issue-91477.stderr | 9 +++++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/generator/issue-91477.rs create mode 100644 src/test/ui/generator/issue-91477.stderr diff --git a/compiler/rustc_borrowck/src/type_check/input_output.rs b/compiler/rustc_borrowck/src/type_check/input_output.rs index 92d2d04f23f23..bc740de515065 100644 --- a/compiler/rustc_borrowck/src/type_check/input_output.rs +++ b/compiler/rustc_borrowck/src/type_check/input_output.rs @@ -117,9 +117,29 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { } } - assert!(body.yield_ty().is_some() == universal_regions.yield_ty.is_some()); - if let Some(mir_yield_ty) = body.yield_ty() { - let ur_yield_ty = universal_regions.yield_ty.unwrap(); + debug!( + "equate_inputs_and_outputs: body.yield_ty {:?}, universal_regions.yield_ty {:?}", + body.yield_ty(), + universal_regions.yield_ty + ); + + // We will not have a universal_regions.yield_ty if we yield (by accident) + // outside of a generator and return an `impl Trait`, so emit a delay_span_bug + // because we don't want to panic in an assert here if we've already got errors. + if body.yield_ty().is_some() != universal_regions.yield_ty.is_some() { + self.tcx().sess.delay_span_bug( + body.span, + &format!( + "Expected body to have yield_ty ({:?}) iff we have a UR yield_ty ({:?})", + body.yield_ty(), + universal_regions.yield_ty, + ), + ); + } + + if let (Some(mir_yield_ty), Some(ur_yield_ty)) = + (body.yield_ty(), universal_regions.yield_ty) + { let yield_span = body.local_decls[RETURN_PLACE].source_info.span; self.equate_normalized_input_or_output(ur_yield_ty, mir_yield_ty, yield_span); } diff --git a/src/test/ui/generator/issue-91477.rs b/src/test/ui/generator/issue-91477.rs new file mode 100644 index 0000000000000..6c027feb422d1 --- /dev/null +++ b/src/test/ui/generator/issue-91477.rs @@ -0,0 +1,7 @@ +#![feature(generators)] + +fn foo() -> impl Sized { + yield 1; //~ ERROR E0627 +} + +fn main() {} diff --git a/src/test/ui/generator/issue-91477.stderr b/src/test/ui/generator/issue-91477.stderr new file mode 100644 index 0000000000000..4597dc1bcdfa5 --- /dev/null +++ b/src/test/ui/generator/issue-91477.stderr @@ -0,0 +1,9 @@ +error[E0627]: yield expression outside of generator literal + --> $DIR/issue-91477.rs:4:5 + | +LL | yield 1; + | ^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0627`.