Skip to content

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

Closed
@RalfJung

Description

@RalfJung

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions