Skip to content

Incorrect lifetime inference? #18116

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

Closed
canndrew opened this issue Oct 17, 2014 · 2 comments
Closed

Incorrect lifetime inference? #18116

canndrew opened this issue Oct 17, 2014 · 2 comments

Comments

@canndrew
Copy link
Contributor

The following code fails to compile:

#![feature(unboxed_closures)]
#![feature(overloaded_calls)]

use std::io::stdio::stdin;

fn foo<'a, T>(f: &mut T)
    where T: FnMut(&'a mut (Reader + 'static))
{
  let mut r = stdin();
  (*f)(&mut r as &mut Reader);
}

fn main() {
  let mut c = |&mut: bar: &mut Reader| ();
  foo(&mut c);
}

With the error:

<anon>:10:13: 10:14 error: `r` does not live long enough
<anon>:10   (*f)(&mut r as &mut Reader);
                      ^
<anon>:8:1: 11:2 note: reference must be valid for the lifetime 'a as defined on the block at 8:0...
<anon>:8 {
<anon>:9   let mut r = stdin();
<anon>:10   (*f)(&mut r as &mut Reader);
<anon>:11 }
<anon>:8:1: 11:2 note: ...but borrowed value is only valid for the block at 8:0
<anon>:8 {
<anon>:9   let mut r = stdin();
<anon>:10   (*f)(&mut r as &mut Reader);
<anon>:11 }
error: aborting due to previous error

If I understand correctly, when the lifetime of a function argument isn't specified then it's assumed to be the lifetime of the function call. Therefore 'a should be instantiated as the lifetime of the call of (*f) on line 10, in which case r should outlive it.

So is this a bug in rust? Or is there another way to write this?

@bkoropoff
Copy link
Contributor

What you really want is something like the following:

fn foo<T>(f: &mut T) where T: <for 'a> FnMut(&'a mut (Reader + 'static)) { ... }

That is, the closure type rather than foo quantifies over 'a. This requires higher-rank trait lifetimes, which aren't implemented yet (#17661).

@canndrew
Copy link
Contributor Author

I see. Closing.

lnicola pushed a commit to lnicola/rust that referenced this issue Sep 25, 2024
fix: Fix printing of constants greater than `i128::MAX`

Fixes rust-lang#18116.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants