Skip to content

Fix rule ordering of trait object lifetime elision#2281

Open
fmease wants to merge 1 commit into
rust-lang:masterfrom
fmease:objltdef-fix-ordering
Open

Fix rule ordering of trait object lifetime elision#2281
fmease wants to merge 1 commit into
rust-lang:masterfrom
fmease:objltdef-fix-ordering

Conversation

@fmease

@fmease fmease commented Jun 8, 2026

Copy link
Copy Markdown
Member

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_bound which returns the unique lifetime that (directly or transitively) bounds the principal trait if extant (that function internally consults object_region_bounds). If there's no such bound, it lowers the lifetime normally using lower_lifetime. That procedure queries named_bound_var which 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)

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!
@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Jun 8, 2026
Comment thread src/lifetime-elision.md

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:

@fmease fmease Jun 8, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

View changes since the review

Comment thread src/lifetime-elision.md
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.

@fmease fmease Jun 8, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

View changes since the review

Comment thread src/lifetime-elision.md
* If there is no such container, then the lifetime is inferred in expressions and is `'static` outside of expressions.

```rust
// For the following trait...

@fmease fmease Jun 8, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Add examples that demonstrate this ordering. None of the current examples exercise it FYI.

View changes since the review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: The marked PR is awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants