Skip to content

fix: don't fire explicit_outlives_requirements on ?Sized type params - #158615

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
Albab-Hasan:fix-explicit-outlives-unsized
Jul 29, 2026
Merged

fix: don't fire explicit_outlives_requirements on ?Sized type params#158615
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
Albab-Hasan:fix-explicit-outlives-unsized

Conversation

@Albab-Hasan

Copy link
Copy Markdown
Contributor

explicit_outlives_requirements uses tcx.inferred_outlives_of() to check whether an explicit T: 'a bound is structurally implied by struct fields. for a field r: &'a T, it is — so the lint fired and suggested removing it.

but RFC 599 object lifetime defaults are computed from explicit HIR bounds not inferred ones. removing an explicit T: 'a from a ?Sized type param silently changes every Struct<dyn Trait> use from dyn Trait + 'a to dyn Trait + 'static, breaking callers without any error.

added a guard: before emitting the lint for a type param outlives bound, check whether the param carries any ?Sized bound (BoundPolarity::Maybe in HIR). if so, skip — the bound may be the sole anchor for the object lifetime default.

also suppresses the pre-existing false positive in edition-lint-infer-outlives.rs (noted in issue #105150) which was an instance of the same bug.

closes #134902

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 30, 2026
@rustbot

rustbot commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

r? @jieyouxu

rustbot has assigned @jieyouxu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 17 candidates

@rustbot

This comment has been minimized.

@Albab-Hasan
Albab-Hasan force-pushed the fix-explicit-outlives-unsized branch from 2b2893b to 0013e7b Compare June 30, 2026 14:01
@Albab-Hasan

Copy link
Copy Markdown
Contributor Author

r? @folkertdev

@rustbot rustbot assigned folkertdev and unassigned jieyouxu Jul 1, 2026
@fmease fmease changed the title fix: don't fire \explicit_outlives_requirements\ on \?Sized\ type params fix: don't fire explicit_outlives_requirements on ?Sized type params Jul 1, 2026
@folkertdev

Copy link
Copy Markdown
Contributor

r? types

@rustbot rustbot added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Jul 1, 2026
@rustbot rustbot assigned oli-obk and unassigned folkertdev Jul 1, 2026

@fmease fmease left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Funnily, I just added a disclaimer in PR #157998.

Updating the lint itself I didn't dare to do since I was wondering whether this would require T-lang involvement (via T-lang-nomination). While it's a bug fix I feel like that this drastically cuts down the number of times this lints fires in practice. OTOH Sized type parameters bounded by 'a have a lot of use cases (see the stdlib), so I dunno.

View changes since this review

Comment thread compiler/rustc_lint/src/builtin.rs Outdated
Comment thread compiler/rustc_lint/src/builtin.rs Outdated
Comment thread compiler/rustc_lint/src/builtin.rs Outdated
Comment thread compiler/rustc_lint/src/builtin.rs Outdated
Comment thread compiler/rustc_lint/src/builtin.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 1, 2026
@rustbot

rustbot commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@Albab-Hasan

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 1, 2026
@Albab-Hasan
Albab-Hasan requested a review from fmease July 1, 2026 10:43
@oli-obk oli-obk assigned fmease and unassigned oli-obk Jul 1, 2026
@Albab-Hasan

Copy link
Copy Markdown
Contributor Author

@fmease hey, sorry for bothering you. just wanted to follow up on the pr

@Albab-Hasan

Copy link
Copy Markdown
Contributor Author

@fmease ...

@fmease fmease left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Sorry for the delay, I didn't have much time for the project the last few weeks

One suggestion + squash.

View changes since this review

// `T: 'a` is implied by the field, but removing it would change the object lifetime
// default for `dyn Trait` uses from `'a` to `'static` — the lint must not fire here.
struct InferredWhereBoundWithInlineBound<'a, T: ?Sized>
//~^ ERROR outlives requirements can be inferred

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you drop the : ?Sized instead since your change no longer exercises #105150.

@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 26, 2026
When a type param `T` is not `Sized` (i.e. `is_sized` returns false),
removing an explicit `T: 'r` bound can silently change trait object
lifetime defaults per RFC 599 — `Struct<dyn Trait>` would shift from
`dyn Trait + 'r` to `dyn Trait + 'static`. Suppress the lint in that
case using `Ty::new_param(...).is_sized(tcx, typing_env)`.

Fixes rust-lang#134902
@Albab-Hasan

Copy link
Copy Markdown
Contributor Author

thanks for the review!

@Albab-Hasan
Albab-Hasan force-pushed the fix-explicit-outlives-unsized branch from 44622b7 to 1d08359 Compare July 27, 2026 00:53
@rustbot

rustbot commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Albab-Hasan
Albab-Hasan requested a review from fmease July 27, 2026 01:03
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 27, 2026
@fmease

fmease commented Jul 28, 2026

Copy link
Copy Markdown
Member

I've pushed a commit that adds back a lengthy note about trait object lifetime defaulting to the description of the lint to address this part of #158615 (comment):

Ideally, you'd preserve parts of the disclaimer and turn it into a normal paragraph that justifies the fact that this lint doesn't fire if the type parameter isn't bounded by Sized since it's not unlikely that users will stumble upon this in the future and wonder if this is a false negative.

Apart from this reason I just didn't want my efforts from #157998 to go to waste ;)

@bors r+ rollup

@rust-bors

rust-bors Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

📌 Commit bf49633 has been approved by fmease

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 28, 2026
rust-bors Bot pushed a commit that referenced this pull request Jul 29, 2026
Rollup of 11 pull requests

Successful merges:

 - #158168 (Added implementation on `set_permissions_nofollow` for all primary platforms)
 - #160055 (Simplify `MaybeRequiresStorage`)
 - #157226 (Partially stabilize `box_vec_non_null`)
 - #158879 (simplify `slice::Iter[Mut]::next_chunk` implementation)
 - #159413 (Enable `#[diagnostic::on_unknown]` during late res)
 - #160091 (Fix rustdoc toolbar height when title is taller than one line)
 - #158615 (fix: don't fire `explicit_outlives_requirements` on `?Sized` type params)
 - #159666 (fix(ld64.lld): route version mismatch warnings to linker_info on macOS)
 - #160032 (rustdoc-json: Make `Stability` compatible with non-self-describing serde formats)
 - #160039 (Add regression test for enum unconstrained parameter )
 - #160049 (Use assert_eq! in splat codegen tests)
rust-bors Bot pushed a commit that referenced this pull request Jul 29, 2026
Rollup of 11 pull requests

Successful merges:

 - #158168 (Added implementation on `set_permissions_nofollow` for all primary platforms)
 - #160055 (Simplify `MaybeRequiresStorage`)
 - #157226 (Partially stabilize `box_vec_non_null`)
 - #158879 (simplify `slice::Iter[Mut]::next_chunk` implementation)
 - #159413 (Enable `#[diagnostic::on_unknown]` during late res)
 - #160091 (Fix rustdoc toolbar height when title is taller than one line)
 - #158615 (fix: don't fire `explicit_outlives_requirements` on `?Sized` type params)
 - #159666 (fix(ld64.lld): route version mismatch warnings to linker_info on macOS)
 - #160032 (rustdoc-json: Make `Stability` compatible with non-self-describing serde formats)
 - #160039 (Add regression test for enum unconstrained parameter )
 - #160049 (Use assert_eq! in splat codegen tests)
@rust-bors
rust-bors Bot merged commit cc89039 into rust-lang:main Jul 29, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 29, 2026
rust-timer added a commit that referenced this pull request Jul 29, 2026
Rollup merge of #158615 - Albab-Hasan:fix-explicit-outlives-unsized, r=fmease

fix: don't fire `explicit_outlives_requirements` on `?Sized` type params

`explicit_outlives_requirements` uses `tcx.inferred_outlives_of()` to check whether an explicit `T: 'a` bound is structurally implied by struct fields. for a field `r: &'a T`, it is — so the lint fired and suggested removing it.

but RFC 599 object lifetime defaults are computed from *explicit* HIR bounds not inferred ones. removing an explicit `T: 'a` from a `?Sized` type param silently changes every `Struct<dyn Trait>` use from `dyn Trait + 'a` to `dyn Trait + 'static`, breaking callers without any error.

added a guard: before emitting the lint for a type param outlives bound, check whether the param carries any `?Sized` bound (`BoundPolarity::Maybe` in HIR). if so, skip — the bound may be the sole anchor for the object lifetime default.

also suppresses the pre-existing false positive in `edition-lint-infer-outlives.rs` (noted in issue #105150) which was an instance of the same bug.

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

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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

Successfully merging this pull request may close these issues.

explicit-outlives-requirements suggestion breaks code

6 participants