forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#67723 - ldm0:E0477, r=Dylan-DPC
Add error code explanation for E0477 Part of rust-lang#61137
- Loading branch information
Showing
8 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
The type does not fulfill the required lifetime. | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0477 | ||
use std::sync::Mutex; | ||
struct MyString<'a> { | ||
data: &'a str, | ||
} | ||
fn i_want_static_closure<F>(a: F) | ||
where F: Fn() + 'static {} | ||
fn print_string<'a>(s: Mutex<MyString<'a>>) { | ||
i_want_static_closure(move || { // error: this closure has lifetime 'a | ||
// rather than 'static | ||
println!("{}", s.lock().unwrap().data); | ||
}); | ||
} | ||
``` | ||
|
||
In this example, the closure does not satisfy the `'static` lifetime constraint. | ||
To fix this error, you need to double check the lifetime of the type. Here, we | ||
can fix this problem by giving `s` a static lifetime: | ||
|
||
``` | ||
use std::sync::Mutex; | ||
struct MyString<'a> { | ||
data: &'a str, | ||
} | ||
fn i_want_static_closure<F>(a: F) | ||
where F: Fn() + 'static {} | ||
fn print_string(s: Mutex<MyString<'static>>) { | ||
i_want_static_closure(move || { // error: this closure has lifetime 'a | ||
// rather than 'static | ||
println!("{}", s.lock().unwrap().data); | ||
}); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters