Skip to content
Merged
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
7 changes: 5 additions & 2 deletions compiler/rustc_resolve/src/diagnostics/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,8 +1180,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {

err
}
ResolutionError::CannotCaptureDynamicEnvironmentInFnItem => {
self.dcx().create_err(diagnostics::CannotCaptureDynamicEnvironmentInFnItem { span })
ResolutionError::CannotCaptureDynamicEnvironmentInFnItem { suggest_closure } => {
self.dcx().create_err(diagnostics::CannotCaptureDynamicEnvironmentInFnItem {
span,
suggest_closure,
})
}
ResolutionError::AttemptToUseNonConstantValueInConstant {
ident,
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_resolve/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,11 @@ pub(crate) struct UnreachableLabelWithSimilarNameExists {

#[derive(Diagnostic)]
#[diag("can't capture dynamic environment in a fn item", code = E0434)]
#[help("use the `|| {\"{\"} ... {\"}\"}` closure form instead")]
pub(crate) struct CannotCaptureDynamicEnvironmentInFnItem {
#[primary_span]
pub(crate) span: Span,
#[help("use the `|| {\"{\"} ... {\"}\"}` closure form instead")]
pub(crate) suggest_closure: bool,
}

#[derive(Diagnostic)]
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
use ResolutionError::*;
let mut res_err = None;

let suggest_closure =
!ribs.iter().any(|rib| matches!(rib.kind, RibKind::AssocItem));

for rib in ribs {
match rib.kind {
RibKind::Normal
Expand All @@ -1525,7 +1528,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// we want certain other resolution errors (namely those
// emitted for `ConstantItemRibKind` below) to take
// precedence.
res_err = Some((span, CannotCaptureDynamicEnvironmentInFnItem));
res_err = Some((
span,
CannotCaptureDynamicEnvironmentInFnItem { suggest_closure },
));
}
}
RibKind::ConstantItem(_, item, requires_type) => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ enum ResolutionError<'ra> {
message: String,
},
/// Error E0434: can't capture dynamic environment in a fn item.
CannotCaptureDynamicEnvironmentInFnItem,
CannotCaptureDynamicEnvironmentInFnItem { suggest_closure: bool },
/// Error E0435: attempt to use a non-constant value in a constant.
AttemptToUseNonConstantValueInConstant {
ident: Ident,
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/resolve/closure-suggestion-in-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
trait T {
fn t() -> impl FnOnce() -> ();
}

fn f(x: String) {
struct S;
impl T for S {
fn t() -> impl FnOnce() -> () {
move || {
let _ = x; //~ ERROR can't capture dynamic environment in a fn item
}
}
}

impl S {
fn foo() {
let bar = 42;

fn inner() {
let _ = bar; //~ ERROR can't capture dynamic environment in a fn item

let _ = x; //~ ERROR can't capture dynamic environment in a fn item
}
}
}
}

fn main() {}
23 changes: 23 additions & 0 deletions tests/ui/resolve/closure-suggestion-in-impl.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0434]: can't capture dynamic environment in a fn item
--> $DIR/closure-suggestion-in-impl.rs:10:25
|
LL | let _ = x;
| ^

error[E0434]: can't capture dynamic environment in a fn item
--> $DIR/closure-suggestion-in-impl.rs:20:25
|
LL | let _ = bar;
| ^^^
|
= help: use the `|| { ... }` closure form instead

error[E0434]: can't capture dynamic environment in a fn item
--> $DIR/closure-suggestion-in-impl.rs:22:25
|
LL | let _ = x;
| ^

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0434`.
2 changes: 0 additions & 2 deletions tests/ui/resolve/impl-method-cant-capture-env.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ error[E0434]: can't capture dynamic environment in a fn item
|
LL | self.v0 = k0 ^ 0x736f6d6570736575;
| ^^
|
= help: use the `|| { ... }` closure form instead

error: aborting due to 1 previous error

Expand Down
2 changes: 0 additions & 2 deletions tests/ui/resolve/impl-trait-for-type-cant-capture-env.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ error[E0434]: can't capture dynamic environment in a fn item
|
LL | self.v0 = k0 ^ 0x736f6d6570736575;
| ^^
|
= help: use the `|| { ... }` closure form instead

error: aborting due to 1 previous error

Expand Down
4 changes: 0 additions & 4 deletions tests/ui/resolve/trait-impl-cant-capture-env.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ error[E0434]: can't capture dynamic environment in a fn item
|
LL | self.v0 = k0 ^ 0x736f6d6570736575;
| ^^
|
= help: use the `|| { ... }` closure form instead

error[E0434]: can't capture dynamic environment in a fn item
--> $DIR/trait-impl-cant-capture-env.rs:25:23
|
LL | self.v1 = k1 ^ 0x646f72616e646f6d;
| ^^
|
= help: use the `|| { ... }` closure form instead

error: aborting due to 2 previous errors

Expand Down
Loading