Fix borrowed_box suggestion for trait objects to mention .as_ref() at call sites - #17794
AbhilashG12 wants to merge 1 commit into
Conversation
|
Thanks for the pull request, and welcome! You should hear from one of our reviewers after this PR gets at least 2 reviews from the community. Please see the contribution instructions for more information. |
This comment has been minimized.
This comment has been minimized.
799a550 to
ac1ee8a
Compare
|
This PR was rebased onto a different master 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. |
| LL | pub fn test14(_display: &Box<dyn Display>) {} | ||
| | ^^^^^^^^^^^^^^^^^ help: consider using just `&T`: `&dyn Display` | ||
| | | ||
| = help: call `.as_ref()` at the call site if it's a `&Box<dyn Trait>` |
There was a problem hiding this comment.
🤔
why limit this help to traits?
I think if we write the diagnostic like this, it should be generally applicable, or?
I may be missing something xD
| = help: call `.as_ref()` at the call site if it's a `&Box<dyn Trait>` | |
| = help: call `.as_ref()` at the call site to convert a `&Box<dyn Display>` to a `&dyn Display` |
There was a problem hiding this comment.
Good point on the wording. Kept it trait-object-only because sized types like &Box already deref-coerce to &bool automatically, so the note would be misleading there. But you were right that &Box was vague — I've now interpolated the actual type so it reads e.g. call .as_ref()at the call site to convert a&Boxto a&dyn Display``.
| diag.span_suggestion( | ||
| hir_ty.span, | ||
| "consider using just `&T`", | ||
| suggestion, |
There was a problem hiding this comment.
lets keep the reaon for non-applicability please 😉
Otherwise it is kind of hard on the next person why this is unspecified..
There was a problem hiding this comment.
Restored the original comment. Thanks for catching that.
|
|
||
| // Test for issue #11940: borrowed_box on boxed trait object should suggest .as_ref() | ||
| pub trait DayTrait { | ||
| fn get_display(&self) -> String; | ||
| } | ||
|
|
||
| pub fn test_borrowed_box_trait_object(day: &Box<dyn DayTrait>) { | ||
| //~^ borrowed_box | ||
| let _ = day.get_display(); | ||
| } |
There was a problem hiding this comment.
I think we are already covering the diagnostic in above dyn Display, or what would you like to test here?
| // Test for issue #11940: borrowed_box on boxed trait object should suggest .as_ref() | |
| pub trait DayTrait { | |
| fn get_display(&self) -> String; | |
| } | |
| pub fn test_borrowed_box_trait_object(day: &Box<dyn DayTrait>) { | |
| //~^ borrowed_box | |
| let _ = day.get_display(); | |
| } |
There was a problem hiding this comment.
You're right, test14 already covers this path. Removed the extra test.
There was a problem hiding this comment.
please remove the changes to CHANGELOG.md, as we do this via the changelog: in the PR description and then centrally once per release ^^
There was a problem hiding this comment.
Reverted. Good to know it's handled centrally from the PR description.
c2a8424 to
58ab68c
Compare
This comment has been minimized.
This comment has been minimized.
… call sites The lint suggested changing \&Box<dyn Trait>\ to \&dyn Trait\, but this breaks compilation at call sites that pass \&boxed_value\ because the coercion from \&Box<dyn Trait>\ to \&dyn Trait\ does not happen implicitly. Extend the help for trait objects to tell users to call \.as_ref()\ at call sites. Signed-off-by: AbhilashG12 <abhilashggg15@gmail.com>
58ab68c to
90a441e
Compare
|
Squashed into one commit.. Thanks for the review. |
The lint suggested changing
&Boxto&dyn Trait, butthis breaks compilation at call sites that pass
&boxed_valuebecausethe coercion
&Box -> &dyn Traitdoes not happen implicitly.Extend the message for trait objects to tell users to call
.as_ref()at call sites.
changelog: [
borrowed_box]: suggest calling.as_ref()at the call site when the boxed type is a trait objectFixes #11940