diff options
| author | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2023-10-24 10:03:40 +0000 |
|---|---|---|
| committer | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2023-10-27 13:05:49 +0000 |
| commit | 62237440782574b47f2a325c804e4b8cf77569e7 (patch) | |
| tree | 1964cfee6104ee35b1ad77165d7386494e196b8a | |
| parent | 4ac25faf9f1d7b2b2aaa76cb4b1da6688bc72dc5 (diff) | |
| download | rust-62237440782574b47f2a325c804e4b8cf77569e7.tar.gz rust-62237440782574b47f2a325c804e4b8cf77569e7.zip | |
Prevent generators from being movable
| -rw-r--r-- | compiler/rustc_ast_lowering/src/expr.rs | 2 | ||||
| -rw-r--r-- | tests/ui/coroutine/self_referential_gen_block.rs | 17 | ||||
| -rw-r--r-- | tests/ui/coroutine/self_referential_gen_block.stderr | 11 |
3 files changed, 29 insertions, 1 deletions
diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 5abb0ba722e..d6fa74dc3e6 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -715,7 +715,7 @@ impl<'hir> LoweringContext<'_, 'hir> { body, fn_decl_span: self.lower_span(span), fn_arg_span: None, - movability: Some(hir::Movability::Static), + movability: Some(Movability::Movable), constness: hir::Constness::NotConst, })) } diff --git a/tests/ui/coroutine/self_referential_gen_block.rs b/tests/ui/coroutine/self_referential_gen_block.rs new file mode 100644 index 00000000000..538d436ed14 --- /dev/null +++ b/tests/ui/coroutine/self_referential_gen_block.rs @@ -0,0 +1,17 @@ +// compile-flags: --edition 2024 -Zunstable-options +#![feature(gen_blocks)] +//! This test checks that we don't allow self-referential generators + +fn main() { + let mut x = { + let mut x = gen { + let y = 42; + let z = &y; //~ ERROR: borrow may still be in use when coroutine yields + yield 43; + panic!("{z}"); + }; + x.next(); + Box::new(x) + }; + x.next(); +} diff --git a/tests/ui/coroutine/self_referential_gen_block.stderr b/tests/ui/coroutine/self_referential_gen_block.stderr new file mode 100644 index 00000000000..f9cadf8eb04 --- /dev/null +++ b/tests/ui/coroutine/self_referential_gen_block.stderr @@ -0,0 +1,11 @@ +error[E0626]: borrow may still be in use when coroutine yields + --> $DIR/self_referential_gen_block.rs:9:21 + | +LL | let z = &y; + | ^^ +LL | yield 43; + | -------- possible yield occurs here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0626`. |
