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

RFC #1733 (Trait Aliases): use of lifetime parameter in trait alias not detected #127725

Open
ilonachan opened this issue Jul 14, 2024 · 2 comments
Labels
A-lifetimes Area: Lifetimes / regions A-trait-objects Area: trait objects, vtable layout C-bug Category: This is a bug. F-trait_alias `#![feature(trait_alias)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@ilonachan
Copy link

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.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 14, 2024
@fmease fmease transferred this issue from rust-lang/rfcs Jul 14, 2024
@fmease fmease added A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. F-trait_alias `#![feature(trait_alias)]` T-types Relevant to the types team, which will review and decide on the PR/issue. C-discussion Category: Discussion or questions that doesn't represent real issues. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 14, 2024
@Jules-Bertholet
Copy link
Contributor

Seems like a clear bug to me? @rustbot label C-bug -C-discussion

@rustbot rustbot added C-bug Category: This is a bug. and removed C-discussion Category: Discussion or questions that doesn't represent real issues. labels Jul 14, 2024
@fmease fmease added the A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) label Jul 14, 2024
@compiler-errors
Copy link
Member

compiler-errors commented Jul 14, 2024

This is not really a bug, but a poor interaction with features as they are implemented currently. Specifically, this has to do with the fact that trait aliases interact poorly with trait objects, and there's this ad-hoc expansion algorithm that takes place that doesn't really care about outlives lifetime bounds. So when you write:

trait TestTrait<'a, A> = Fn(A) + 'a;
type TestTypeNested<'a, A> = dyn TestTrait<'a, A>;

you're actually writing:

type TestTypeNested<'a, A> = dyn Fn(A);

That is, we only expand the principal of the trait alias and all of its auto traits. That outlives supertrait bound is not the same as the + 'lifetime that is obligatory (but sometimes elided) on a dyn trait object, and we don't consider it as such.

The state of how trait aliases interact with trait objects is kind of a mess, and I'd like to work on totally re-specifying how they should work since it's pretty unintuitive and also I'm causes other weird errors.

This isn't as simple as just making dyn TestTrait<'a, A> expand into dyn Fn(A) + 'a, since it doesn't really answer the more interesting questions about how do we handle supertrait outlives bounds, multiple outlives bounds, etc.

@compiler-errors compiler-errors added A-trait-objects Area: trait objects, vtable layout and removed A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) labels Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-trait-objects Area: trait objects, vtable layout C-bug Category: This is a bug. F-trait_alias `#![feature(trait_alias)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants