Skip to content

Lifetime inference failed on HRTB input #54124

Closed
@earthengine

Description

@earthengine

The following code

use std::marker::PhantomData;

trait Lt<'a> {
    type T;
}
impl<'a> Lt<'a> for () {
    type T = PhantomData<&'a ()>;
}

fn test<'a>() {
    let _:fn(<() as Lt<'_>>::T) = |_:PhantomData<&'a ()>| {};
}

fn main() {
    test();
}

gives

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> src/main.rs:11:59
   |
11 |     let _:fn(<() as Lt<'_>>::T) = |_:PhantomData<&'a ()>| {};
   |                                                           ^^
   |
note: first, the lifetime cannot outlive the lifetime 'a as defined on the function body at 10:1...
  --> src/main.rs:10:1
   |
10 | fn test<'a>() {
   | ^^^^^^^^^^^^^
   = note: ...so that the types are compatible:
           expected std::marker::PhantomData<&()>
              found std::marker::PhantomData<&'a ()>
note: but, the lifetime must be valid for the anonymous lifetime #2 defined on the body at 11:35...
  --> src/main.rs:11:35
   |
11 |     let _:fn(<() as Lt<'_>>::T) = |_:PhantomData<&'a ()>| {};
   |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: ...so that the types are compatible:
           expected Lt<'_>
              found Lt<'_>

(this is a slightly different version of this stackoverflow question)

Analysis

The error message suggest that the type inference engine consider the upper bound of '_ is the lifetime of the closure. This is incorrect, because this lifetime is in an input argument, so it should consider the lower bound to be 'a instead.

This issue may have relation with #53420, but I am not sure yet.

Metadata

Metadata

Assignees

Labels

A-NLLArea: Non-lexical lifetimes (NLL)A-lifetimesArea: Lifetimes / regionsI-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️NLL-soundWorking towards the "invalid code does not compile" goal

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions