Open
Description
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.
Metadata
Metadata
Assignees
Labels
Area: Associated items (types, constants & functions)Area: Messages for errors, warnings, and lintsArea: Lifetimes / regionsCategory: An issue proposing an enhancement or a PR with one.Relevant to the compiler team, which will review and decide on the PR/issue.Relevant to the language team, which will review and decide on the PR/issue.