Prevent generators from being movable

This commit is contained in:
Oli Scherer 2023-10-24 10:03:40 +00:00
parent 4ac25faf9f
commit 6223744078
3 changed files with 29 additions and 1 deletions

View File

@ -715,7 +715,7 @@ pub(super) fn make_gen_expr(
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,
}))
}

View File

@ -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();
}

View File

@ -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`.