Skip to content

Commit 69a65cd

Browse files
committed
Add some tests to make sure iter macros do not accidentally become async closures
1 parent 2e6e242 commit 69a65cd

4 files changed

+124
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This test ensures iterators created with the `iter!` macro are not accidentally async closures.
2+
//@ edition: 2024
3+
4+
#![feature(yield_expr, iter_macro)]
5+
6+
use std::iter::iter;
7+
8+
fn call_async_once(_: impl AsyncFnOnce()) {
9+
}
10+
11+
fn main() {
12+
let f = iter! { move || {
13+
for i in 0..10 {
14+
yield i;
15+
}
16+
}};
17+
18+
call_async_once(f);
19+
//~^ ERROR AsyncFnOnce()` is not satisfied
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0277]: the trait bound `{gen closure@$DIR/iter-macro-not-async-closure-simplified.rs:12:21: 12:28}: AsyncFnOnce()` is not satisfied
2+
--> $DIR/iter-macro-not-async-closure-simplified.rs:18:21
3+
|
4+
LL | call_async_once(f);
5+
| --------------- ^ unsatisfied trait bound
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= help: the trait `AsyncFnOnce()` is not implemented for `{gen closure@$DIR/iter-macro-not-async-closure-simplified.rs:12:21: 12:28}`
10+
note: required by a bound in `call_async_once`
11+
--> $DIR/iter-macro-not-async-closure-simplified.rs:8:28
12+
|
13+
LL | fn call_async_once(_: impl AsyncFnOnce()) {
14+
| ^^^^^^^^^^^^^ required by this bound in `call_async_once`
15+
16+
error: aborting due to 1 previous error
17+
18+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This test ensures iterators created with the `iter!` macro are not accidentally async closures.
2+
//@ edition: 2024
3+
4+
#![feature(yield_expr, iter_macro)]
5+
6+
use std::task::{Waker, Context};
7+
use std::iter::iter;
8+
use std::pin::pin;
9+
use std::future::Future;
10+
11+
async fn call_async_once(f: impl AsyncFnOnce()) {
12+
f().await
13+
}
14+
15+
fn main() {
16+
let f = iter! { move || {
17+
for i in 0..10 {
18+
yield i;
19+
}
20+
}};
21+
22+
let x = pin!(call_async_once(f));
23+
//~^ ERROR AsyncFnOnce()` is not satisfied
24+
//~^^ ERROR AsyncFnOnce()` is not satisfied
25+
//~^^^ ERROR AsyncFnOnce()` is not satisfied
26+
x.poll(&mut Context::from_waker(Waker::noop()));
27+
//~^ ERROR AsyncFnOnce()` is not satisfied
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
error[E0277]: the trait bound `{gen closure@$DIR/iter-macro-not-async-closure.rs:16:21: 16:28}: AsyncFnOnce()` is not satisfied
2+
--> $DIR/iter-macro-not-async-closure.rs:22:34
3+
|
4+
LL | let x = pin!(call_async_once(f));
5+
| --------------- ^ unsatisfied trait bound
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= help: the trait `AsyncFnOnce()` is not implemented for `{gen closure@$DIR/iter-macro-not-async-closure.rs:16:21: 16:28}`
10+
note: required by a bound in `call_async_once`
11+
--> $DIR/iter-macro-not-async-closure.rs:11:34
12+
|
13+
LL | async fn call_async_once(f: impl AsyncFnOnce()) {
14+
| ^^^^^^^^^^^^^ required by this bound in `call_async_once`
15+
16+
error[E0277]: the trait bound `{gen closure@$DIR/iter-macro-not-async-closure.rs:16:21: 16:28}: AsyncFnOnce()` is not satisfied
17+
--> $DIR/iter-macro-not-async-closure.rs:22:18
18+
|
19+
LL | let x = pin!(call_async_once(f));
20+
| ^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
21+
|
22+
= help: the trait `AsyncFnOnce()` is not implemented for `{gen closure@$DIR/iter-macro-not-async-closure.rs:16:21: 16:28}`
23+
note: required by a bound in `call_async_once`
24+
--> $DIR/iter-macro-not-async-closure.rs:11:34
25+
|
26+
LL | async fn call_async_once(f: impl AsyncFnOnce()) {
27+
| ^^^^^^^^^^^^^ required by this bound in `call_async_once`
28+
29+
error[E0277]: the trait bound `{gen closure@$DIR/iter-macro-not-async-closure.rs:16:21: 16:28}: AsyncFnOnce()` is not satisfied
30+
--> $DIR/iter-macro-not-async-closure.rs:22:13
31+
|
32+
LL | let x = pin!(call_async_once(f));
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
34+
|
35+
= help: the trait `AsyncFnOnce()` is not implemented for `{gen closure@$DIR/iter-macro-not-async-closure.rs:16:21: 16:28}`
36+
note: required by a bound in `call_async_once`
37+
--> $DIR/iter-macro-not-async-closure.rs:11:34
38+
|
39+
LL | async fn call_async_once(f: impl AsyncFnOnce()) {
40+
| ^^^^^^^^^^^^^ required by this bound in `call_async_once`
41+
= note: this error originates in the macro `pin` (in Nightly builds, run with -Z macro-backtrace for more info)
42+
43+
error[E0277]: the trait bound `{gen closure@$DIR/iter-macro-not-async-closure.rs:16:21: 16:28}: AsyncFnOnce()` is not satisfied
44+
--> $DIR/iter-macro-not-async-closure.rs:26:5
45+
|
46+
LL | x.poll(&mut Context::from_waker(Waker::noop()));
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
48+
|
49+
= help: the trait `AsyncFnOnce()` is not implemented for `{gen closure@$DIR/iter-macro-not-async-closure.rs:16:21: 16:28}`
50+
note: required by a bound in `call_async_once`
51+
--> $DIR/iter-macro-not-async-closure.rs:11:34
52+
|
53+
LL | async fn call_async_once(f: impl AsyncFnOnce()) {
54+
| ^^^^^^^^^^^^^ required by this bound in `call_async_once`
55+
56+
error: aborting due to 4 previous errors
57+
58+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)