Skip to content
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

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

Closed
Tracked by #44265
0x182d4454fb211940 opened this issue Jan 28, 2021 · 2 comments · Fixed by #87281
Closed
Tracked by #44265

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

0x182d4454fb211940 opened this issue Jan 28, 2021 · 2 comments · Fixed by #87281
Labels
A-GATs Area: Generic associated types (GATs) C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@0x182d4454fb211940
Copy link

0x182d4454fb211940 commented Jan 28, 2021

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`.

@0x182d4454fb211940 0x182d4454fb211940 added the C-bug Category: This is a bug. label Jan 28, 2021
@jonas-schievink jonas-schievink added F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs requires-nightly This issue requires a nightly compiler in some way. labels Jan 28, 2021
@Johannesd3
Copy link

@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 29, 2021
@jedel1043
Copy link
Contributor

Minimized:

#![feature(generic_associated_types)]

trait Trait {
    type Ref<'a>;
}

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

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

fn wrap<'a, T: Trait>(reff: &'a <T as Trait>::Ref<'a>) -> RefRef<'a, T> {
    RefRef(reff)
}

fn main() {
    let num = 3;
    let wrapper = wrap::<()>(&&num);
}

@bors bors closed this as completed in e16d023 Jul 22, 2021
@fmease fmease added the A-GATs Area: Generic associated types (GATs) label Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-GATs Area: Generic associated types (GATs) C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants