Skip to content

Commit 9b63263

Browse files
committed
Auto merge of #33229 - timothy-mcroy:master, r=guillaumegomez
Explain E0434
2 parents 8b1dcf4 + 0cfb5a0 commit 9b63263

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/librustc_resolve/diagnostics.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,51 @@ use something_which_doesnt_exist;
948948
Please verify you didn't misspell the import's name.
949949
"##,
950950

951+
E0434: r##"
952+
This error indicates that a variable usage inside an inner function is invalid
953+
because the variable comes from a dynamic environment. Inner functions do not
954+
have access to their containing environment.
955+
956+
Example of erroneous code:
957+
958+
```compile_fail
959+
fn foo() {
960+
let y = 5;
961+
fn bar() -> u32 {
962+
y // error: can't capture dynamic environment in a fn item; use the
963+
// || { ... } closure form instead.
964+
}
965+
}
966+
```
967+
968+
Functions do not capture local variables. To fix this error, you can replace the
969+
function with a closure:
970+
971+
```
972+
fn foo() {
973+
let y = 5;
974+
let bar = || {
975+
y
976+
};
977+
}
978+
```
979+
980+
or replace the captured variable with a constant or a static item:
981+
982+
```
983+
fn foo() {
984+
static mut X: u32 = 4;
985+
const Y: u32 = 5;
986+
fn bar() -> u32 {
987+
unsafe {
988+
X = 3;
989+
}
990+
Y
991+
}
992+
}
993+
```
994+
"##,
995+
951996
E0435: r##"
952997
A non-constant value was used to initialise a constant. Example of erroneous
953998
code:
@@ -1044,5 +1089,4 @@ register_diagnostics! {
10441089
E0421, // unresolved associated const
10451090
E0427, // cannot use `ref` binding mode with ...
10461091
E0429, // `self` imports are only allowed within a { } list
1047-
E0434, // can't capture dynamic environment in a fn item
10481092
}

0 commit comments

Comments
 (0)