Fix rule ordering of trait object lifetime elision#2281
Open
fmease wants to merge 1 commit into
Open
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!
fmease
commented
Jun 8, 2026
|
|
||
| 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: |
Member
Author
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...
fmease
commented
Jun 8, 2026
| 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. |
Member
Author
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...
fmease
commented
Jun 8, 2026
| * If there is no such container, then the lifetime is inferred in expressions and is `'static` outside of expressions. | ||
|
|
||
| ```rust | ||
| // For the following trait... |
Member
Author
There was a problem hiding this comment.
TODO: Add examples that demonstrate this ordering. None of the current examples exercise it FYI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 code1. To explain a bit what's going on: The result of
compute_object_lifetime_bound(COLB) is obviously preferred over the result ofnamed_bound_var(NBV) (also take a look at the definition oflower_lifetime); here, COLB consultsobject_region_boundsinternally which in turn looks at the outlives-bounds in the list of (elaborated) predicates of the (principal) trait ref. Only if there's no object region bound doesnamed_bound_varget called; the latter being a query that calls 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)
Footnotes
I actually find that snippet quite hard to read, so I'm simplifying it in PR https://github.com/rust-lang/rust/pull/157591. Here's how it'll look after the refactoring. ↩