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

The type-checker assumes that Trait<'a>::AssociatedType may be outlived by 'a, even with "where AssociatedType: 'a" #63253

Open
hiddenhare opened this issue Aug 4, 2019 · 1 comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@hiddenhare
Copy link

trait Trait<'a> {
    type Ty;
    fn method(ty_ref: &'a Self::Ty) where Self::Ty: 'a { }
}

fn caller<'a, T: Trait<'a>>(arg: &'a T::Ty) where T::Ty: 'a {
    T::method(arg)
}

Compilation fails on the playground (for stable 1.36.0 and nightly 1.38.0) with this error:

Compiling playground v0.0.1 (/playground)
error[E0309]: the associated type `<T as Trait<'_>>::Ty` may not live long enough
 --> src/lib.rs:7:9
  |
7 |         T::method(arg)
  |         ^^^^^^^^^
  |
  = help: consider adding an explicit lifetime bound `<T as Trait<'_>>::Ty: 'a`...
note: ...so that the type `<T as Trait<'_>>::Ty` will meet its required lifetime bounds
 --> src/lib.rs:7:9
  |
7 |         T::method(arg)
  |         ^^^^^^^^^

error: aborting due to previous error

Compilation succeeds if I make any of the following changes:

  • Add a lifetime constraint to Ty at its definition site, type Ty: 'a;

  • Move the lifetime parameter from the trait to the method, fn method<'a>(...)

Compilation fails with the same error message if I change method into a free function:

trait Trait<'a> {
    type Ty;
}

fn method<'a, T: Trait<'a>>(_arg: &'a T::Ty) where T::Ty: 'a { }

fn caller<'a, T: Trait<'a>>(arg: &'a T::Ty) where T::Ty: 'a {
    method::<T>(arg)
}

Even if this is intended behaviour, the error message could be improved.

@estebank estebank added A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Aug 5, 2019
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@ocstl
Copy link

ocstl commented Dec 13, 2021

This seems to have been fixed by #87281.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants