Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ast_lowering_bad_return_type_notation_position = return type notation not allowe
ast_lowering_clobber_abi_not_supported =
`clobber_abi` is not supported on this target

ast_lowering_closure_cannot_be_static = closures cannot be static
ast_lowering_closure_cannot_be_static = {$modifier}closures cannot be static

ast_lowering_coroutine_too_many_parameters =
too many parameters for a coroutine (expected 0 or 1 parameters)
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_ast_lowering/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ pub(crate) struct CoroutineTooManyParameters {

#[derive(Diagnostic)]
#[diag(ast_lowering_closure_cannot_be_static, code = E0697)]
pub(crate) struct ClosureCannotBeStatic {
pub(crate) struct ClosureCannotBeStatic<'a> {
#[primary_span]
pub fn_decl_span: Span,
pub modifier: &'a str,
}

#[derive(Diagnostic)]
Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
e.id,
expr_hir_id,
*coroutine_kind,
*movability,
fn_decl,
body,
*fn_decl_span,
Expand Down Expand Up @@ -1125,7 +1126,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
None => {
if movability == Movability::Static {
self.dcx().emit_err(ClosureCannotBeStatic { fn_decl_span });
self.dcx().emit_err(ClosureCannotBeStatic { modifier: "", fn_decl_span });
}
hir::ClosureKind::Closure
}
Expand Down Expand Up @@ -1154,6 +1155,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
closure_id: NodeId,
closure_hir_id: HirId,
coroutine_kind: CoroutineKind,
movability: Movability,
decl: &FnDecl,
body: &Expr,
fn_decl_span: Span,
Expand All @@ -1170,6 +1172,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
};

// Movability doesn't make sense on an async/gen closure, so reject outright.
match movability {
Movability::Static => {
self.dcx().emit_err(ClosureCannotBeStatic {
modifier: &format!("{coroutine_desugaring:#}"),
fn_decl_span,
});
}
Movability::Movable => {}
}

let body = self.with_new_scopes(fn_decl_span, |this| {
let inner_decl =
FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/unpretty/exhaustive.expanded.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ mod expressions {
async move || value;
static || value;
static move || value;
(static async || value);
(static async move || value);
(static async ||
value);
(static async move ||
value);
|| -> u8 { value };
1 + || {};
}
Expand Down
14 changes: 13 additions & 1 deletion tests/ui/unpretty/exhaustive.hir.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ error[E0697]: closures cannot be static
LL | static move || value;
| ^^^^^^^^^^^^^^

error[E0697]: `async` closures cannot be static
--> $DIR/exhaustive.rs:211:10
|
LL | (static async || value);
| ^^^^^^^^^^^^^^^

error[E0697]: `async` closures cannot be static
--> $DIR/exhaustive.rs:212:10
|
LL | (static async move || value);
| ^^^^^^^^^^^^^^^^^^^^

error[E0728]: `await` is only allowed inside `async` functions and blocks
--> $DIR/exhaustive.rs:239:13
|
Expand Down Expand Up @@ -166,7 +178,7 @@ LL | let _: impl for<'a> Send;
= help: add `#![feature(impl_trait_in_bindings)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 20 previous errors
error: aborting due to 22 previous errors

Some errors have detailed explanations: E0214, E0562, E0697, E0703, E0728.
For more information about an error, try `rustc --explain E0214`.
12 changes: 10 additions & 2 deletions tests/ui/unpretty/exhaustive.hir.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,16 @@ mod expressions {
move || |mut _task_context: ResumeTy| { { let _t = value; _t } };
|| value;
move || value;
|| |mut _task_context: ResumeTy| { { let _t = value; _t } };
move || |mut _task_context: ResumeTy| { { let _t = value; _t } };
||
|mut _task_context: ResumeTy|
{
{ let _t = value; _t }
};
move ||
|mut _task_context: ResumeTy|
{
{ let _t = value; _t }
};
|| -> u8 { value };
1 + (|| { });
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/unpretty/exhaustive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ mod expressions {
async move || value;
static || value; //[hir]~ ERROR closures cannot be static
static move || value; //[hir]~ ERROR closures cannot be static
(static async || value);
(static async move || value);
(static async || value); //[hir]~ ERROR `async` closures cannot be static
(static async move || value); //[hir]~ ERROR `async` closures cannot be static
|| -> u8 { value };
1 + || {};
}
Expand Down
Loading