Skip to content

Commit 72990c5

Browse files
Reject async closures in AST lowering
1 parent 0330525 commit 72990c5

File tree

7 files changed

+46
-10
lines changed

7 files changed

+46
-10
lines changed

compiler/rustc_ast_lowering/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ast_lowering_bad_return_type_notation_position = return type notation not allowe
4848
ast_lowering_clobber_abi_not_supported =
4949
`clobber_abi` is not supported on this target
5050
51-
ast_lowering_closure_cannot_be_static = closures cannot be static
51+
ast_lowering_closure_cannot_be_static = {$modifier}closures cannot be static
5252
5353
ast_lowering_coroutine_too_many_parameters =
5454
too many parameters for a coroutine (expected 0 or 1 parameters)

compiler/rustc_ast_lowering/src/errors.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ pub(crate) struct CoroutineTooManyParameters {
126126

127127
#[derive(Diagnostic)]
128128
#[diag(ast_lowering_closure_cannot_be_static, code = E0697)]
129-
pub(crate) struct ClosureCannotBeStatic {
129+
pub(crate) struct ClosureCannotBeStatic<'a> {
130130
#[primary_span]
131131
pub fn_decl_span: Span,
132+
pub modifier: &'a str,
132133
}
133134

134135
#[derive(Diagnostic)]

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
226226
e.id,
227227
expr_hir_id,
228228
*coroutine_kind,
229+
*movability,
229230
fn_decl,
230231
body,
231232
*fn_decl_span,
@@ -1157,7 +1158,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11571158
}
11581159
None => {
11591160
if movability == Movability::Static {
1160-
self.dcx().emit_err(ClosureCannotBeStatic { fn_decl_span });
1161+
self.dcx().emit_err(ClosureCannotBeStatic { modifier: "", fn_decl_span });
11611162
}
11621163
hir::ClosureKind::Closure
11631164
}
@@ -1186,6 +1187,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11861187
closure_id: NodeId,
11871188
closure_hir_id: HirId,
11881189
coroutine_kind: CoroutineKind,
1190+
movability: Movability,
11891191
decl: &FnDecl,
11901192
body: &Expr,
11911193
fn_decl_span: Span,
@@ -1202,6 +1204,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
12021204
}
12031205
};
12041206

1207+
// Movability doesn't make sense on an async/gen closure, so reject outright.
1208+
match movability {
1209+
Movability::Static => {
1210+
self.dcx().emit_err(ClosureCannotBeStatic {
1211+
modifier: &format!("{coroutine_desugaring:#}"),
1212+
fn_decl_span,
1213+
});
1214+
}
1215+
Movability::Movable => {}
1216+
}
1217+
12051218
let body = self.with_new_scopes(fn_decl_span, |this| {
12061219
let inner_decl =
12071220
FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };

tests/ui/unpretty/exhaustive.expanded.stdout

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,10 @@ mod expressions {
185185
async move || value;
186186
static || value;
187187
static move || value;
188-
(static async || value);
189-
(static async move || value);
188+
(static async ||
189+
value);
190+
(static async move ||
191+
value);
190192
|| -> u8 { value };
191193
1 + || {};
192194
}

tests/ui/unpretty/exhaustive.hir.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ error[E0697]: closures cannot be static
1010
LL | static move || value;
1111
| ^^^^^^^^^^^^^^
1212

13+
error[E0697]: `async` closures cannot be static
14+
--> $DIR/exhaustive.rs:212:10
15+
|
16+
LL | (static async || value);
17+
| ^^^^^^^^^^^^^^^
18+
19+
error[E0697]: `async` closures cannot be static
20+
--> $DIR/exhaustive.rs:213:10
21+
|
22+
LL | (static async move || value);
23+
| ^^^^^^^^^^^^^^^^^^^^
24+
1325
error[E0728]: `await` is only allowed inside `async` functions and blocks
1426
--> $DIR/exhaustive.rs:239:13
1527
|
@@ -166,7 +178,7 @@ LL | let _: impl for<'a> Send;
166178
= help: add `#![feature(impl_trait_in_bindings)]` to the crate attributes to enable
167179
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
168180

169-
error: aborting due to 20 previous errors
181+
error: aborting due to 22 previous errors
170182

171183
Some errors have detailed explanations: E0214, E0562, E0697, E0703, E0728.
172184
For more information about an error, try `rustc --explain E0214`.

tests/ui/unpretty/exhaustive.hir.stdout

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,16 @@ mod expressions {
215215
move || |mut _task_context: ResumeTy| { { let _t = value; _t } };
216216
|| value;
217217
move || value;
218-
|| |mut _task_context: ResumeTy| { { let _t = value; _t } };
219-
move || |mut _task_context: ResumeTy| { { let _t = value; _t } };
218+
||
219+
|mut _task_context: ResumeTy|
220+
{
221+
{ let _t = value; _t }
222+
};
223+
move ||
224+
|mut _task_context: ResumeTy|
225+
{
226+
{ let _t = value; _t }
227+
};
220228
|| -> u8 { value };
221229
1 + (|| { });
222230
}

tests/ui/unpretty/exhaustive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ mod expressions {
208208
async move || value;
209209
static || value; //[hir]~ ERROR closures cannot be static
210210
static move || value; //[hir]~ ERROR closures cannot be static
211-
(static async || value);
212-
(static async move || value);
211+
(static async || value); //[hir]~ ERROR `async` closures cannot be static
212+
(static async move || value); //[hir]~ ERROR `async` closures cannot be static
213213
|| -> u8 { value };
214214
1 + || {};
215215
}

0 commit comments

Comments
 (0)