Skip to content

Error when inferring a lifetime in return type using GATs #81487

Closed
@0x182d4454fb211940

Description

I tried this code:

#![feature(generic_associated_types)]

struct IntRef<'a>(&'a mut i32);

trait Trait {
    type Ref<'a>;
}

impl Trait for () {
    type Ref<'a> = IntRef<'a>;
}

struct RefWrapper<'a, T: Trait>(&'a mut <T as Trait>::Ref<'a>);

fn wrap<'a, T: Trait>(the_ref: &'a mut T::Ref<'a>) -> RefWrapper<'a, T> {
    RefWrapper(the_ref)
}

fn main() {
    let mut x = 3;
    let mut int_ref = IntRef(&mut x);
    let wrapper = wrap::<()>(&mut int_ref);
    *wrapper.0.0 = 2;
}

I expected to see this happen: the code compiles

Instead, this happened: the code does not compile unless you replace:

    RefWrapper(the_ref)

with

    RefWrapper::<'a, T>(the_ref)

Meta

rustc --version --verbose:

rustc 1.51.0-nightly (a2f8f6281 2021-01-27)
binary: rustc
commit-hash: a2f8f6281817d430e20726128b739d3c6708561c
commit-date: 2021-01-27
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly
LLVM version: 11.0.1
Backtrace

warning: the feature `generic_associated_types` is incomplete and may not be safe to use and/or cause compiler crashes
 --> x.rs:1:12
  |
1 | #![feature(generic_associated_types)]
  |            ^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(incomplete_features)]` on by default
  = note: see issue #44265 </~https://github.com/rust-lang/rust/issues/44265> for more information

error[E0309]: the associated type `<T as Trait>::Ref<'_>` may not live long enough
  --> x.rs:16:5
   |
16 |     RefWrapper(the_ref)
   |     ^^^^^^^^^^
   |
   = help: consider adding an explicit lifetime bound `<T as Trait>::Ref<'_>: 'a`...
   = note: ...so that the type `<T as Trait>::Ref<'_>` will meet its required lifetime bounds

error[E0309]: the associated type `<T as Trait>::Ref<'_>` may not live long enough
  --> x.rs:16:5
   |
16 |     RefWrapper(the_ref)
   |     ^^^^^^^^^^^^^^^^^^^
   |
   = help: consider adding an explicit lifetime bound `<T as Trait>::Ref<'_>: 'a`...
   = note: ...so that the type `<T as Trait>::Ref<'_>` will meet its required lifetime bounds

error: aborting due to 2 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0309`.

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)C-bugCategory: This is a bug.F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions