- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Closed
Labels
A-NLLArea: Non-lexical lifetimes (NLL)Area: Non-lexical lifetimes (NLL)NLL-completeWorking towards the "valid code works" goalWorking towards the "valid code works" goalP-highHigh priorityHigh priority
Description
NLL causes compilation to fail here:
fn f(s: &str) {
    g(s, |x| h(x));
}
fn g<T, F>(_: T, _: F)
where F: Fn(&mut (&(T, T), T)) {}
fn h<T>(_: &mut (&(T, T), T)) {}error[E0621]: explicit lifetime required in the type of `s`
 --> src/lib.rs:2:5
  |
1 | fn f(s: &str) {
  |         ---- help: add explicit lifetime `'static` to the type of `s`: `&'static str`
2 |     g(s, |x| h(x));
  |     ^^^^^^^^^^^^^^ lifetime `'static` required
error: aborting due to previous error
For more information about this error, try `rustc --explain E0621`.
Oddly, it does compile if I replace g(s, |x| h(x)) by g(s, h). Alternatively, if I give &str an explicit lifetime 'a, the compiler says that s escapes the function body:
warning[E0521]: borrowed data escapes outside of function
 --> src/lib.rs:2:5
  |
1 | fn f<'a>(s: &'a str) {
  |          - `s` is a reference that is only valid in the function body
2 |     g(s, |x| h(x));
  |     ^^^^^^^^^^^^^^ `s` escapes the function body here
  |
  = warning: this error has been downgraded to a warning for backwards compatibility with previous releases
  = warning: this represents potential undefined behavior in your code and this warning will become a hard error in the future
Metadata
Metadata
Assignees
Labels
A-NLLArea: Non-lexical lifetimes (NLL)Area: Non-lexical lifetimes (NLL)NLL-completeWorking towards the "valid code works" goalWorking towards the "valid code works" goalP-highHigh priorityHigh priority