fix: don't fire explicit_outlives_requirements on ?Sized type params - #158615
Conversation
|
r? @jieyouxu rustbot has assigned @jieyouxu. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
2b2893b to
0013e7b
Compare
|
r? @folkertdev |
explicit_outlives_requirements\ on \?Sized\ type paramsexplicit_outlives_requirements on ?Sized type params
|
r? types |
There was a problem hiding this comment.
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.
|
Reminder, once the PR becomes ready for a review, use |
|
@rustbot ready |
|
@fmease hey, sorry for bothering you. just wanted to follow up on the pr |
|
@fmease ... |
| // `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 |
There was a problem hiding this comment.
Could you drop the : ?Sized instead since your change no longer exercises #105150.
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
|
thanks for the review! |
44622b7 to
1d08359
Compare
|
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. |
…licit_outlives_requirements`
|
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):
Apart from this reason I just didn't want my efforts from #157998 to go to waste ;) @bors r+ rollup |
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)
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)
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
explicit_outlives_requirementsusestcx.inferred_outlives_of()to check whether an explicitT: 'abound is structurally implied by struct fields. for a fieldr: &'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: 'afrom a?Sizedtype param silently changes everyStruct<dyn Trait>use fromdyn Trait + 'atodyn 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
?Sizedbound (BoundPolarity::Maybein 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