Skip to content

"Cannot infer appropriate lifetime" points at associated const of type Option<!> #54378

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
RalfJung opened this issue Sep 20, 2018 · 2 comments · Fixed by #97953
Closed

"Cannot infer appropriate lifetime" points at associated const of type Option<!> #54378

RalfJung opened this issue Sep 20, 2018 · 2 comments · Fixed by #97953
Labels
A-lifetimes Area: Lifetimes / regions E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@RalfJung
Copy link
Member

I have no idea what is going on in this piece of code:

#![feature(never_type)]

use std::marker::PhantomData;

pub trait Machine<'a, 'mir, 'tcx>: Sized {
    type MemoryKinds: ::std::fmt::Debug + Copy + Eq;
    const MUT_STATIC_KIND: Option<Self::MemoryKinds>;
}

pub struct CompileTimeEvaluator<'a, 'mir, 'tcx: 'a+'mir> {
    pub _data: PhantomData<(&'a (), &'mir (), &'tcx ())>,
}

impl<'a, 'mir, 'tcx: 'a + 'mir> Machine<'a, 'mir, 'tcx>
    for CompileTimeEvaluator<'a, 'mir, 'tcx>
{
    type MemoryKinds = !;

    const MUT_STATIC_KIND: Option<!> = None;
}

when it fails to compile saying

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'tcx` due to conflicting requirements
  --> src/lib.rs:19:5
   |
19 |     const MUT_STATIC_KIND: Option<!> = None;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime 'tcx as defined on the impl at 14:16...
  --> src/lib.rs:14:16
   |
14 | impl<'a, 'mir, 'tcx: 'a + 'mir> Machine<'a, 'mir, 'tcx>
   |                ^^^^
note: ...but the lifetime must also be valid for the lifetime 'a as defined on the impl at 14:6...
  --> src/lib.rs:14:6
   |
14 | impl<'a, 'mir, 'tcx: 'a + 'mir> Machine<'a, 'mir, 'tcx>
   |      ^^
   = note: ...so that the types are compatible:
           expected Machine<'a, 'mir, 'tcx>
              found Machine<'_, '_, '_>

error: aborting due to previous error

I would expect the code to be accepted. How can something of type Option<!> have problems inferring a lifetime?!? The error is entirely not helpful.

Curiously, if I remove the 'a + 'mir bound on 'tcx in the impl, this compiles. Removing a bound that's already implied by the type itself should not make a difference, right?

@RalfJung RalfJung changed the title "Cannot infer appropriate lifetime" points at value of type Option<!> "Cannot infer appropriate lifetime" points at associated const of type Option<!> Sep 20, 2018
@estebank estebank added the A-lifetimes Area: Lifetimes / regions label Oct 3, 2018
@estebank
Copy link
Contributor

estebank commented Oct 3, 2018

Also happens under nll.

@Spoonbender
Copy link

Triage: resolved

The code compiles on nightly 1.16.0-nightly

I think we can close this one

@RalfJung RalfJung added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Mar 22, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jun 10, 2022
Signed-off-by: Yuki Okushi <jtitor@2k36.org>
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 10, 2022
…askrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#93331 (refactor write_output_file to merge two invocation paths into one.)
 - rust-lang#97928 (Removes debug settings from wasm32_unknown_emscripten default link args)
 - rust-lang#97940 (Use relative links instead of linking to doc.rust-lang.org when possible)
 - rust-lang#97941 (nit: Fixed several error_codes/Exxxx.md messages which used UpperCamelCase…)
 - rust-lang#97953 (Add regression test for rust-lang#54378)
 - rust-lang#97957 (Make `std::` prefix suggestion test `run-rustfix`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in dd409da Jun 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants