Uh oh!
There was an error while loading. Please reload this page.
Fix rule ordering of trait object lifetime elision - #2281
Conversation
rustc looks at the outlives-bounds of the principal trait (and its supertraits) *first* and only if there isn't an unambiguous choice it consults the containing type!
| r[lifetime-elision.trait-object.trait-bounds] | ||
| If neither of those rules apply, then the bounds on the trait are used: | ||
| The bounds on the trait are used: |
There was a problem hiding this comment.
I don't like the phrasing of this rule and its "lack of informative value". It's a consequence of me having almost mechanically swapped these two "groups" of rules...
| The bounds on the trait are used: | ||
| r[lifetime-elision.trait-object.trait-unique] | ||
| * If the trait is defined with a single lifetime _bound_ then that bound is used. |
There was a problem hiding this comment.
Note that we look at the outlives-bounds of the trait and any of its supertraits. I was wondering whether you consider this obvious / implied from the current wording or if it's actually unclear.
Personally I'm slightly leaning toward the latter, in my eyes it could be misinterpreted to mean just the "syntactic" bounds on the traits...
| * If there is no such container, then the lifetime is inferred in expressions and is `'static` outside of expressions. | ||
| ```rust | ||
| // For the following trait... |
There was a problem hiding this comment.
TODO: Add examples that demonstrate this ordering. None of the current examples exercise it FYI.
Part of a series which aims to address #1407 step by step.
This PR fixes the worst and most dominant issue of section Default trait object lifetimes.
Ever since that section was added by PR #187, the Reference wrongly claimed that outlives-bounds on the type containing the trait object type took precedence over the outlives-bounds on the (principal) trait. That's exactly opposite to what rustc is doing!
For context, here the is relevant part of rustc's source code. To explain a bit what's going on: To determine the lifetime bound of a trait object type we first consult
compute_object_lifetime_boundwhich returns the unique lifetime that (directly or transitively) bounds the principal trait if extant (that function internally consultsobject_region_bounds). If there's no such bound, it lowers the lifetime normally usinglower_lifetime. That procedure queriesnamed_bound_varwhich gets computed in module RBV which among other things computes the entire "scope tree" of trait object lifetime defaults for the local crate as deduced by "type containers" only.I don't intend for the scope of this PR to grow past what's declared now. I'd like to split this whole endeavor into quite small pieces to prevent it from becoming unmanageable. There are a lot of issues with the current section as #1407 highlights.
I'm of course very open to non-normative / editorial changes!
r? @traviscross (since you've shown some interest in this area, namely in rust-lang/rust#129543)