Skip to content

Commit f999a90

Browse files
Reject async closures in AST lowering
1 parent 16ad385 commit f999a90

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
@@ -225,6 +225,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
225225
e.id,
226226
expr_hir_id,
227227
*coroutine_kind,
228+
*movability,
228229
fn_decl,
229230
body,
230231
*fn_decl_span,
@@ -1125,7 +1126,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11251126
}
11261127
None => {
11271128
if movability == Movability::Static {
1128-
self.dcx().emit_err(ClosureCannotBeStatic { fn_decl_span });
1129+
self.dcx().emit_err(ClosureCannotBeStatic { modifier: "", fn_decl_span });
11291130
}
11301131
hir::ClosureKind::Closure
11311132
}
@@ -1154,6 +1155,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11541155
closure_id: NodeId,
11551156
closure_hir_id: HirId,
11561157
coroutine_kind: CoroutineKind,
1158+
movability: Movability,
11571159
decl: &FnDecl,
11581160
body: &Expr,
11591161
fn_decl_span: Span,
@@ -1170,6 +1172,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
11701172
}
11711173
};
11721174

1175+
// Movability doesn't make sense on an async/gen closure, so reject outright.
1176+
match movability {
1177+
Movability::Static => {
1178+
self.dcx().emit_err(ClosureCannotBeStatic {
1179+
modifier: &format!("{coroutine_desugaring:#}"),
1180+
fn_decl_span,
1181+
});
1182+
}
1183+
Movability::Movable => {}
1184+
}
1185+
11731186
let body = self.with_new_scopes(fn_decl_span, |this| {
11741187
let inner_decl =
11751188
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:211:10
15+
|
16+
LL | (static async || value);
17+
| ^^^^^^^^^^^^^^^
18+
19+
error[E0697]: `async` closures cannot be static
20+
--> $DIR/exhaustive.rs:212: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)