Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

long error explanation for E0313 #103433

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 1 addition & 2 deletions compiler/rustc_error_codes/src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ E0309: include_str!("./error_codes/E0309.md"),
E0310: include_str!("./error_codes/E0310.md"),
E0311: include_str!("./error_codes/E0311.md"),
E0312: include_str!("./error_codes/E0312.md"),
E0313: include_str!("./error_codes/E0313.md"),
E0316: include_str!("./error_codes/E0316.md"),
E0317: include_str!("./error_codes/E0317.md"),
E0321: include_str!("./error_codes/E0321.md"),
Expand Down Expand Up @@ -569,8 +570,6 @@ E0790: include_str!("./error_codes/E0790.md"),
// E0300, // unexpanded macro
// E0304, // expected signed integer constant
// E0305, // expected constant
E0313, // lifetime of borrowed pointer outlives lifetime of captured
// variable
// E0314, // closure outlives stack frame
// E0315, // cannot invoke closure outside of its lifetime
// E0319, // trait impls for defaulted traits allowed just for structs/enums
Expand Down
43 changes: 43 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0313.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Lifetime of borrowed pointer outlives lifetime of captured variable.

Erroneous code example:

```compile_fail,E0313
fn foo(bar: &'static mut ()) {
|| foo(bar);
}
```

This error is raised because the body of the closure, which implements `FnMut`,
has a lifetime shorter than `'static`. Let's name this lifetime `'fooc`. This is
problematic because `foo`'s argument requires that `'fooc` must outlive
`'static`, but references to captured variables in a `FnMut` closure can't
escape the closure.

To fix this error, define a temporary variable inside the closure that takes
ownership of `foo`'s argument. Then, pass this into `foo`:

```
fn foo(bar: &'static mut ()) {
|| {
let baz = bar;
foo(baz);
};
}
```

This works because `bar` must be moved into the closure for the body to be
sensical. The closure now implements `FnOnce` because the value is moved in
then moved out in order to call the function, meaning it can only be called
once.

Additionally, it seems like the following would also work:

```
fn foo(bar: &'static mut ()) {
move || foo(bar);
}
```

Although the closure is forced to capture its environment by value, which is
what `FnOnce` does, `move` closures can still implement `FnMut` (or `Fn`).
4 changes: 2 additions & 2 deletions src/tools/tidy/src/error_codes_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use regex::Regex;

// A few of those error codes can't be tested but all the others can and *should* be tested!
const EXEMPTED_FROM_TEST: &[&str] = &[
"E0313", "E0377", "E0461", "E0462", "E0465", "E0476", "E0490", "E0514", "E0519", "E0523",
"E0554", "E0640", "E0717", "E0729", "E0789",
"E0377", "E0461", "E0462", "E0465", "E0476", "E0490", "E0514", "E0519", "E0523", "E0554",
"E0640", "E0717", "E0729", "E0789",
];

// Some error codes don't have any tests apparently...
Expand Down