From e84df5bd708f2457276308b653fa62d7901cb174 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 11 Apr 2024 13:15:34 +0000 Subject: [PATCH] Error on using `yield` without also using `#[coroutine]` on the closure And suggest adding the `#[coroutine]` to the closure --- tests/fail/coroutine-pinned-moved.rs | 4 +-- tests/pass/coroutine.rs | 36 +++++++++---------- .../coroutine-self-referential.rs | 4 +-- tests/pass/track-caller-attribute.rs | 6 ++-- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/tests/fail/coroutine-pinned-moved.rs b/tests/fail/coroutine-pinned-moved.rs index 005ae7e913..8648be2a26 100644 --- a/tests/fail/coroutine-pinned-moved.rs +++ b/tests/fail/coroutine-pinned-moved.rs @@ -1,5 +1,5 @@ //@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -#![feature(coroutines, coroutine_trait)] +#![feature(coroutines, coroutine_trait, stmt_expr_attributes)] use std::{ ops::{Coroutine, CoroutineState}, @@ -7,7 +7,7 @@ use std::{ }; fn firstn() -> impl Coroutine { - static move || { + #[coroutine] static move || { let mut num = 0; let num = &mut num; *num += 0; diff --git a/tests/pass/coroutine.rs b/tests/pass/coroutine.rs index 7e1f64df04..e76abfc418 100644 --- a/tests/pass/coroutine.rs +++ b/tests/pass/coroutine.rs @@ -1,6 +1,6 @@ //@revisions: stack tree //@[tree]compile-flags: -Zmiri-tree-borrows -#![feature(coroutines, coroutine_trait, never_type)] +#![feature(coroutines, coroutine_trait, never_type, stmt_expr_attributes)] use std::fmt::Debug; use std::mem::ManuallyDrop; @@ -43,9 +43,9 @@ fn basic() { panic!() } - finish(1, false, || yield 1); + finish(1, false, #[coroutine] || yield 1); - finish(3, false, || { + finish(3, false, #[coroutine] || { let mut x = 0; yield 1; x += 1; @@ -55,27 +55,27 @@ fn basic() { assert_eq!(x, 2); }); - finish(7 * 8 / 2, false, || { + finish(7 * 8 / 2, false, #[coroutine] || { for i in 0..8 { yield i; } }); - finish(1, false, || { + finish(1, false, #[coroutine] || { if true { yield 1; } else { } }); - finish(1, false, || { + finish(1, false, #[coroutine] || { if false { } else { yield 1; } }); - finish(2, false, || { + finish(2, false, #[coroutine] || { if { yield 1; false @@ -88,7 +88,7 @@ fn basic() { // also test self-referential coroutines assert_eq!( - finish(5, true, static || { + finish(5, true, #[coroutine] static || { let mut x = 5; let y = &mut x; *y = 5; @@ -99,7 +99,7 @@ fn basic() { 10 ); assert_eq!( - finish(5, true, || { + finish(5, true, #[coroutine] || { let mut x = Box::new(5); let y = &mut *x; *y = 5; @@ -111,7 +111,7 @@ fn basic() { ); let b = true; - finish(1, false, || { + finish(1, false, #[coroutine] || { yield 1; if b { return; @@ -123,7 +123,7 @@ fn basic() { drop(x); }); - finish(3, false, || { + finish(3, false, #[coroutine] || { yield 1; #[allow(unreachable_code)] let _x: (String, !) = (String::new(), { @@ -172,7 +172,7 @@ fn smoke_resume_arg() { } drain( - &mut |mut b| { + &mut #[coroutine] |mut b| { while b != 0 { b = yield (b + 1); } @@ -181,21 +181,21 @@ fn smoke_resume_arg() { vec![(1, Yielded(2)), (-45, Yielded(-44)), (500, Yielded(501)), (0, Complete(-1))], ); - expect_drops(2, || drain(&mut |a| yield a, vec![(DropMe, Yielded(DropMe))])); + expect_drops(2, || drain(&mut #[coroutine] |a| yield a, vec![(DropMe, Yielded(DropMe))])); expect_drops(6, || { drain( - &mut |a| yield yield a, + &mut #[coroutine] |a| yield yield a, vec![(DropMe, Yielded(DropMe)), (DropMe, Yielded(DropMe)), (DropMe, Complete(DropMe))], ) }); #[allow(unreachable_code)] - expect_drops(2, || drain(&mut |a| yield return a, vec![(DropMe, Complete(DropMe))])); + expect_drops(2, || drain(&mut #[coroutine] |a| yield return a, vec![(DropMe, Complete(DropMe))])); expect_drops(2, || { drain( - &mut |a: DropMe| { + &mut #[coroutine] |a: DropMe| { if false { yield () } else { a } }, vec![(DropMe, Complete(DropMe))], @@ -205,7 +205,7 @@ fn smoke_resume_arg() { expect_drops(4, || { drain( #[allow(unused_assignments, unused_variables)] - &mut |mut a: DropMe| { + &mut #[coroutine] |mut a: DropMe| { a = yield; a = yield; a = yield; @@ -228,7 +228,7 @@ fn uninit_fields() { } fn run(x: bool, y: bool) { - let mut c = || { + let mut c = #[coroutine] || { if x { let _a: T; if y { diff --git a/tests/pass/stacked-borrows/coroutine-self-referential.rs b/tests/pass/stacked-borrows/coroutine-self-referential.rs index c4b15c8758..bb98e024a0 100644 --- a/tests/pass/stacked-borrows/coroutine-self-referential.rs +++ b/tests/pass/stacked-borrows/coroutine-self-referential.rs @@ -1,6 +1,6 @@ // See https://github.com/rust-lang/unsafe-code-guidelines/issues/148: // this fails when Stacked Borrows is strictly applied even to `!Unpin` types. -#![feature(coroutines, coroutine_trait)] +#![feature(coroutines, coroutine_trait, stmt_expr_attributes)] use std::{ ops::{Coroutine, CoroutineState}, @@ -8,7 +8,7 @@ use std::{ }; fn firstn() -> impl Coroutine { - static move || { + #[coroutine] static move || { let mut num = 0; let num = &mut num; diff --git a/tests/pass/track-caller-attribute.rs b/tests/pass/track-caller-attribute.rs index d88bcc9885..c3803af3cc 100644 --- a/tests/pass/track-caller-attribute.rs +++ b/tests/pass/track-caller-attribute.rs @@ -232,7 +232,7 @@ fn test_coroutine() { } #[rustfmt::skip] - let coroutine = #[track_caller] |arg: String| { + let coroutine = #[track_caller] #[coroutine] |arg: String| { yield ("first", arg.clone(), Location::caller()); yield ("second", arg.clone(), Location::caller()); }; @@ -255,7 +255,7 @@ fn test_coroutine() { assert_eq!(mono_loc.column(), 42); #[rustfmt::skip] - let non_tracked_coroutine = || { yield Location::caller(); }; + let non_tracked_coroutine = #[coroutine] || { yield Location::caller(); }; let non_tracked_line = line!() - 1; // This is the line of the coroutine, not its caller let non_tracked_loc = match Box::pin(non_tracked_coroutine).as_mut().resume(()) { CoroutineState::Yielded(val) => val, @@ -263,7 +263,7 @@ fn test_coroutine() { }; assert_eq!(non_tracked_loc.file(), file!()); assert_eq!(non_tracked_loc.line(), non_tracked_line); - assert_eq!(non_tracked_loc.column(), 44); + assert_eq!(non_tracked_loc.column(), 57); } fn main() {