RFC #1733 (Trait Aliases): use of lifetime parameter in trait alias not detected
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
This is an issue with RFC rust-lang/rfcs#1733 (Trait Aliases, Tracking Issue #41517 ). I can't describe well what might be the issue, I just stumbled upon it with the following MVE:
#![feature(trait_alias)]
trait TestTrait<'a, A> = Fn(A) + 'a;
type TestTypeNormal<'a, A> = dyn Fn(A) + 'a;
type TestTypeNested<'a, A> = dyn TestTrait<'a, A>;
struct TestSpelledOut<'a, A> {
data: Box<dyn Fn(A) + 'a>
}
struct TestNormalTypeAlias<'a, A> {
data: Box<TestTypeNormal<'a, A>>
}
struct TestTraitAlias<'a, A> {
data: Box<dyn TestTrait<'a, A>>
}
struct TestTraitAliasInsideTypeAlias<'a, A> {
data: Box<TestTypeNested<'a, A>>
}
The error output is as follows:
error[E0392]: lifetime parameter `'a` is never used
--> src/lib.rs:13:23
|
13 | struct TestTraitAlias<'a, A> {
| ^^ unused lifetime parameter
|
= help: consider removing `'a`, referring to it in a field, or using a marker such as `PhantomData`
error[E0392]: lifetime parameter `'a` is never used
--> src/lib.rs:16:38
|
16 | struct TestTraitAliasInsideTypeAlias<'a, A> {
| ^^ unused lifetime parameter
|
= help: consider removing `'a`, referring to it in a field, or using a marker such as `PhantomData`
This is unexpected because clearly 'a is being used equally in all cases! But in the cases where a trait alias is involved in the definition of the trait object type, the compiler doesn't detect this. I believe this is a bug, or at the very least extremely counterintuitive.
I haven't experimented with this issue more to test the bounds of what exactly fails (such as impl instead of dyn, nested trait aliases,...) and might not have the time to either. For my use case the workaround of using an extension trait with blanket impl works, but trait aliases would clearly be the more semantically correct choice. I mostly just wanted you to be aware of this, so trait aliases don't stabilize with incorrect behavior. And of course, if this was already known and I just didn't read the conversation enough, just ignore me.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal reproducer in src/lib.rs and compare the cases using dyn Fn, ordinary type aliases, and trait aliases. Read RFC #1733 and tracking issue #41517 for the intended trait-alias behavior, then verify that equivalent lifetime uses are handled consistently and the incorrect E0392 diagnostics no longer occur.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100