Open ilonachan opened 3 months ago
Seems like a clear bug to me? @rustbot label C-bug -C-discussion
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.
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:
The error output is as follows:
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 ofdyn
, 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.