Skip to content

Lifetime inference failure #15140

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
Florob opened this issue Jun 24, 2014 · 1 comment
Closed

Lifetime inference failure #15140

Florob opened this issue Jun 24, 2014 · 1 comment
Labels
A-lifetimes Area: Lifetimes / regions

Comments

@Florob
Copy link
Contributor

Florob commented Jun 24, 2014

In the code below Rust fails to infer an appropriate lifetime, even though y and w
clearly both live long enough to compare them.
Note that the suggested fix of giving the &str contained in y the lifetime 'a is undesirable in case y lives shorter than x and the returned value.

fn test<'a>(x: &'a Option<String>, y: Option<&str>) -> Option<&'a Option<String>> {
    let w = x.as_ref().map(|z| z.as_slice());
    if y == w {
        return Some(x);
    }
    None
}

fn main() {
    let x = Some("asdf".to_string());
    println!("{}", test(&x, Some("foo")));
}
<anon>:1:1: 7:2 note: consider using an explicit lifetime parameter as shown: fn test<'a>(x: &'a Option<String>, y: Option<&'a str>) ->
 Option<&'a Option<String>>
<anon>:1 fn test<'a>(x: &'a Option<String>, y: Option<&str>) -> Option<&'a Option<String>> {
<anon>:2     let w = x.as_ref().map(|z| z.as_slice());
<anon>:3     if y == w {
<anon>:4         return Some(x);
<anon>:5     }
<anon>:6     None
         ...
<anon>:2:13: 2:23 error: cannot infer an appropriate lifetime for autoref due to conflicting requirements
<anon>:2     let w = x.as_ref().map(|z| z.as_slice());
                     ^~~~~~~~~~
error: aborting due to previous error
playpen: application terminated with error code 101
lifthrasiir added a commit to snrs/sonorous that referenced this issue Jun 26, 2014
also features an updated version of `lex!` which allows for
user-defined parsers (at expense of a slight syntax modification);
we need it as, say, `f32` and `f64` can't be simply unified to
the same code without introducing a new trait.
a new trait `FromStrPrefix` subsumes the existing `scan_*` extension
methods, and thus `util::std::str` has been greatly reduced.

also due to the recent lifetime inference problem (rust-lang/rust#15140)
we now use custom methods `.as_ref_slice()` and `.as_ref_slice_or()`
for the major source of this problem.
@steveklabnik steveklabnik added the A-lifetimes Area: Lifetimes / regions label Jan 23, 2015
@Ryman
Copy link
Contributor

Ryman commented Feb 27, 2015

This is now fixed: http://is.gd/OKyPZK

@Florob Florob closed this as completed Mar 3, 2015
bors added a commit to rust-lang-ci/rust that referenced this issue Jul 17, 2023
…r=Veykril

Add the comment why `HasSource::source()` returns `Option`

Fix rust-lang/rust-analyzer#15088
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions
Projects
None yet
Development

No branches or pull requests

3 participants