Repository navigation
Fix dyn suggestion for bare trait objects starting with :: - #163679
Open
simonether wants to merge 1 commit into
Open
simonether wants to merge 1 commit into
simonether wants to merge 1 commit into
Conversation
In edition 2015, `dyn ::a::B` parses as the path `dyn::a::B`, so the suggestion to add `dyn` wraps a global trait path in parentheses. It wrapped all bounds, though, and `dyn (::std::any::Any + 'static)` is rejected in every edition. It also added the parentheses in later editions, where `unused_parens` warns about them. Add them only in edition 2015, and only around the first bound when it is a path starting with `::`.
Collaborator
|
HIR ty lowering was modified cc @fmease |
Collaborator
|
r? @mu001999 rustbot has assigned @mu001999. Use Why was this reviewer chosen?The reviewer was selected based on:
|
mu001999
reviewed
Oct 12, 2026
| //[e2015,e2018]~^^ ERROR trait objects without an explicit `dyn` are deprecated | ||
| //[e2015,e2018]~| WARN this is accepted in the current edition | ||
|
|
||
| pub type LifetimeFirst = Box<'static + ::std::any::Any>; |
Member
There was a problem hiding this comment.
Could you also add tests for Send + ::std::any::Any and ::std::marker::Send + ::std::any::Any?
And for the latter, I think the suggestion should be dyn (::std::marker::Send) + ::std::any::Any.
Collaborator
|
Reminder, once the PR becomes ready for a review, use |
This branch has not been deployed
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.
Fixes #63330
The
dynsuggestion for a bare trait object whose trait path starts with::wraps all bounds in parentheses. ForBox<::std::any::Any + 'static>it suggestsBox<dyn (::std::any::Any + 'static)>, which fails to compile in every edition with "incorrect parentheses around trait bounds". The suggestion is machine-applicable, socargo fix --editionhits it too (rust-lang/cargo#7349). In edition 2018 and later it also adds parentheses afterdyn, which is a keyword there, andunused_parensthen asks to remove them.The parentheses are only needed in edition 2015, where
dyn ::a::Bparses as the pathdyn::a::B. This change adds them only in 2015, and only around the first bound when the text right after the inserteddynstarts with::.unused_parensalready treats parentheses around a leading::path as necessary in 2015 (dyn2015_exceptionincompiler/rustc_lint/src/unused.rs), so the suggestion and the lint now agree. The E0782 error in edition 2021 uses the same suggestion and is fixed by the same change.Box<::std::any::Any + 'static>dyn (::std::any::Any) + 'staticdyn ::std::any::Any + 'staticBox<::std::any::Any>dyn (::std::any::Any)(unchanged)dyn ::std::any::AnyBox<'static + ::std::any::Any>dyn 'static + ::std::any::AnyWhen the trait is the only bound and the type also needs angle brackets (
::Dyn::func()becomes<dyn (::Dyn)>::func()),)and>stay in a single insertion so their order is fixed.tests/ui/lint/bare-trait-objects-path.stderris unchanged.The new test
tests/ui/dyn-keyword/bare-trait-object-global-path-sugg.rshas 2015, 2018 and 2021 revisions withrun-rustfixand#![deny(unused_parens)], so the fixed code has to compile and can't contain parentheses it doesn't need. On main, all three revisions fail, and the code suggested there doesn't compile in any of the three editions . The existing UI tests indyn-keyword,lint,parser,traits/object,castandtypeckpass with no snapshot changes, and tidy passes.