Skip to content

Commit d1b810a

Browse files
committed
Create defs for generators
1 parent 60bc83d commit d1b810a

File tree

9 files changed

+61
-43
lines changed

9 files changed

+61
-43
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,27 @@ impl<'hir> LoweringContext<'_, 'hir> {
375375
c.value.span,
376376
);
377377
}
378-
ExprKind::Closure(_) => {
378+
ExprKind::Closure(box Closure { coroutine_kind, .. }) => {
379+
let closure_def = self.create_def(
380+
self.current_def_id_parent,
381+
e.id,
382+
kw::Empty,
383+
DefKind::Closure,
384+
e.span,
385+
);
386+
if let Some(coroutine_kind) = coroutine_kind {
387+
self.with_def_id_parent(closure_def, |this| {
388+
this.create_def(
389+
this.current_def_id_parent,
390+
coroutine_kind.closure_id(),
391+
kw::Empty,
392+
DefKind::Closure,
393+
e.span,
394+
);
395+
})
396+
}
397+
}
398+
ExprKind::Gen(..) => {
379399
self.create_def(
380400
self.current_def_id_parent,
381401
e.id,
@@ -649,13 +669,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
649669
coroutine_source: hir::CoroutineSource,
650670
body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
651671
) -> hir::ExprKind<'hir> {
652-
let closure_def_id = self.create_def(
653-
self.current_def_id_parent,
654-
closure_node_id,
655-
kw::Empty,
656-
DefKind::Closure,
657-
span,
658-
);
672+
let closure_def_id = self.local_def_id(closure_node_id);
659673
let coroutine_kind = hir::CoroutineKind::Desugared(desugaring_kind, coroutine_source);
660674

661675
// The `async` desugaring takes a resume argument and maintains a `task_context`,

tests/ui/async-await/async-closures/def-path.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/def-path.rs:9:9
33
|
44
LL | let x = async || {};
5-
| -- the expected `async` closure body
5+
| ----------- the expected `async` closure body
66
LL |
77
LL | let () = x();
88
| ^^ --- this expression has type `{static main::{closure#0}::{closure#0}<?17t> upvar_tys=?16t witness=?6t}`

tests/ui/async-await/async-closures/higher-ranked-return.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: lifetime may not live long enough
44
LL | let x = async move |x: &str| -> &str {
55
| ________________________________-________----_^
66
| | | |
7-
| | | return type of async closure `{async closure body@$DIR/higher-ranked-return.rs:13:46: 15:10}` contains a lifetime `'2`
7+
| | | return type of async closure `{async closure body@$DIR/higher-ranked-return.rs:13:17: 15:10}` contains a lifetime `'2`
88
| | let's call the lifetime of this reference `'1`
99
LL | | x
1010
LL | | };

tests/ui/async-await/async-closures/is-not-fn.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0271]: expected `{async closure@is-not-fn.rs:7:14}` to be a closure that returns `()`, but it returns `{async closure body@$DIR/is-not-fn.rs:7:23: 7:25}`
1+
error[E0271]: expected `{async closure@is-not-fn.rs:7:14}` to be a closure that returns `()`, but it returns `{async closure body@$DIR/is-not-fn.rs:7:14: 7:25}`
22
--> $DIR/is-not-fn.rs:7:14
33
|
44
LL | needs_fn(async || {});
@@ -7,7 +7,7 @@ LL | needs_fn(async || {});
77
| required by a bound introduced by this call
88
|
99
= note: expected unit type `()`
10-
found `async` closure body `{async closure body@$DIR/is-not-fn.rs:7:23: 7:25}`
10+
found `async` closure body `{async closure body@$DIR/is-not-fn.rs:7:14: 7:25}`
1111
note: required by a bound in `needs_fn`
1212
--> $DIR/is-not-fn.rs:6:25
1313
|

tests/ui/async-await/async-closures/move-consuming-capture.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0382]: use of moved value: `x`
22
--> $DIR/move-consuming-capture.rs:17:9
33
|
44
LL | let x = async move || {
5-
| - move occurs because `x` has type `{async closure@$DIR/move-consuming-capture.rs:13:17: 13:30}`, which does not implement the `Copy` trait
5+
| - move occurs because `x` has type `{async closure@$DIR/move-consuming-capture.rs:13:17: 15:10}`, which does not implement the `Copy` trait
66
...
77
LL | x().await;
88
| --- `x` moved due to this method call

tests/ui/async-await/async-closures/not-lending.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: lifetime may not live long enough
44
LL | let x = async move || -> &String { &s };
55
| ------------------------ ^^^^^^ returning this value requires that `'1` must outlive `'2`
66
| | |
7-
| | return type of async closure `{async closure body@$DIR/not-lending.rs:14:42: 14:48}` contains a lifetime `'2`
7+
| | return type of async closure `{async closure body@$DIR/not-lending.rs:14:17: 14:48}` contains a lifetime `'2`
88
| lifetime `'1` represents this closure's body
99
|
1010
= note: closure implements `Fn`, so references to captured variables can't escape the closure
@@ -15,7 +15,7 @@ error: lifetime may not live long enough
1515
LL | let x = async move || { &s };
1616
| ------------- ^^^^^^ returning this value requires that `'1` must outlive `'2`
1717
| | |
18-
| | return type of async closure `{async closure body@$DIR/not-lending.rs:18:31: 18:37}` contains a lifetime `'2`
18+
| | return type of async closure `{async closure body@$DIR/not-lending.rs:18:17: 18:37}` contains a lifetime `'2`
1919
| lifetime `'1` represents this closure's body
2020
|
2121
= note: closure implements `Fn`, so references to captured variables can't escape the closure

tests/ui/async-await/async-closures/wrong-fn-kind.stderr

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ error[E0525]: expected a closure that implements the `async Fn` trait, but this
22
--> $DIR/wrong-fn-kind.rs:17:20
33
|
44
LL | needs_async_fn(move || async move {
5-
| -------------- -^^^^^^
6-
| | |
7-
| _____|______________this closure implements `async FnOnce`, not `async Fn`
5+
| _____--------------_^
86
| | |
97
| | required by a bound introduced by this call
108
LL | |
119
LL | | println!("{x}");
1210
| | - closure is `async FnOnce` because it moves the variable `x` out of its environment
1311
LL | | });
14-
| |_____- the requirement to implement `async Fn` derives from here
12+
| | ^
13+
| | |
14+
| |_____this closure implements `async FnOnce`, not `async Fn`
15+
| the requirement to implement `async Fn` derives from here
1516
|
1617
note: required by a bound in `needs_async_fn`
1718
--> $DIR/wrong-fn-kind.rs:5:27
@@ -22,18 +23,20 @@ LL | fn needs_async_fn(_: impl async Fn()) {}
2223
error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
2324
--> $DIR/wrong-fn-kind.rs:9:20
2425
|
25-
LL | fn needs_async_fn(_: impl async Fn()) {}
26-
| --------------- change this to accept `FnMut` instead of `Fn`
26+
LL | fn needs_async_fn(_: impl async Fn()) {}
27+
| --------------- change this to accept `FnMut` instead of `Fn`
2728
...
28-
LL | needs_async_fn(async || {
29-
| -------------- ^^^^^^^^
30-
| | |
31-
| | cannot borrow as mutable
32-
| | in this closure
33-
| expects `Fn` instead of `FnMut`
34-
LL |
35-
LL | x += 1;
36-
| - mutable borrow occurs due to use of `x` in closure
29+
LL | needs_async_fn(async || {
30+
| -------------- -^^^^^^^
31+
| | |
32+
| _____|______________cannot borrow as mutable
33+
| | |
34+
| | expects `Fn` instead of `FnMut`
35+
LL | |
36+
LL | | x += 1;
37+
| | - mutable borrow occurs due to use of `x` in closure
38+
LL | | });
39+
| |_____- in this closure
3740

3841
error: aborting due to 2 previous errors
3942

tests/ui/async-await/async-is-unwindsafe.stderr

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
error[E0277]: the type `&mut Context<'_>` may not be safely transferred across an unwind boundary
22
--> $DIR/async-is-unwindsafe.rs:12:5
33
|
4-
LL | is_unwindsafe(async {
5-
| ^ ----- within this `{async block@$DIR/async-is-unwindsafe.rs:12:19: 12:24}`
6-
| _____|
7-
| |
8-
LL | |
9-
LL | | use std::ptr::null;
10-
LL | | use std::task::{Context, RawWaker, RawWakerVTable, Waker};
11-
... |
12-
LL | | drop(cx_ref);
13-
LL | | });
14-
| |______^ `&mut Context<'_>` may not be safely transferred across an unwind boundary
4+
LL | is_unwindsafe(async {
5+
| ______^ -
6+
| | ___________________|
7+
LL | ||
8+
LL | || use std::ptr::null;
9+
LL | || use std::task::{Context, RawWaker, RawWakerVTable, Waker};
10+
... ||
11+
LL | || drop(cx_ref);
12+
LL | || });
13+
| ||_____-^ `&mut Context<'_>` may not be safely transferred across an unwind boundary
14+
| |_____|
15+
| within this `{async block@$DIR/async-is-unwindsafe.rs:12:19: 29:6}`
1516
|
16-
= help: within `{async block@$DIR/async-is-unwindsafe.rs:12:19: 12:24}`, the trait `UnwindSafe` is not implemented for `&mut Context<'_>`, which is required by `{async block@$DIR/async-is-unwindsafe.rs:12:19: 12:24}: UnwindSafe`
17+
= help: within `{async block@$DIR/async-is-unwindsafe.rs:12:19: 29:6}`, the trait `UnwindSafe` is not implemented for `&mut Context<'_>`, which is required by `{async block@$DIR/async-is-unwindsafe.rs:12:19: 29:6}: UnwindSafe`
1718
= note: `UnwindSafe` is implemented for `&Context<'_>`, but not for `&mut Context<'_>`
1819
note: future does not implement `UnwindSafe` as this value is used across an await
1920
--> $DIR/async-is-unwindsafe.rs:25:18

tests/ui/async-await/issue-86507.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ note: captured value is not `Send` because `&` references cannot be sent unless
1313
|
1414
LL | let x = x;
1515
| ^ has type `&T` which is not `Send`, because `T` is not `Sync`
16-
= note: required for the cast from `Pin<Box<{async block@$DIR/issue-86507.rs:18:17: 18:27}>>` to `Pin<Box<(dyn Future<Output = ()> + Send + 'async_trait)>>`
16+
= note: required for the cast from `Pin<Box<{async block@$DIR/issue-86507.rs:18:17: 20:18}>>` to `Pin<Box<(dyn Future<Output = ()> + Send + 'async_trait)>>`
1717
help: consider further restricting this bound
1818
|
1919
LL | fn bar<'me, 'async_trait, T: Send + std::marker::Sync>(x: &'me T)

0 commit comments

Comments
 (0)