Skip to content

Fix borrowed_box suggestion for trait objects to mention .as_ref() at call sites - #17794

Open
AbhilashG12 wants to merge 1 commit into
rust-lang:masterfrom
AbhilashG12:fix-borrowed-box-trait-object-suggestion
Open

AbhilashG12 wants to merge 1 commit into
rust-lang:masterfrom
AbhilashG12:fix-borrowed-box-trait-object-suggestion

Conversation

@AbhilashG12

@AbhilashG12 AbhilashG12 commented Sep 26, 2026 •

Copy link
Copy Markdown

The lint suggested changing &Box to &dyn Trait, but
this breaks compilation at call sites that pass &boxed_value because
the coercion &Box -> &dyn Trait does 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 object

Fixes #11940

@rustbot rustbot added S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Sep 26, 2026
@rustbot

rustbot commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

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.

@rustbot

This comment has been minimized.

@AbhilashG12
AbhilashG12 force-pushed the fix-borrowed-box-trait-object-suggestion branch from 799a550 to ac1ee8a Compare September 26, 2026 17:05
@rustbot

rustbot commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

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.

@CommanderStorm CommanderStorm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Community review: there are a few things I found, none that are hard to rectify though, mostly minor stuff

View changes since this review

Comment thread tests/ui/borrowed_box.stderr Outdated
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>`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤔
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

Suggested change
= 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`

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lets keep the reaon for non-applicability please 😉
Otherwise it is kind of hard on the next person why this is unspecified..

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Restored the original comment. Thanks for catching that.

Comment thread tests/ui/borrowed_box.rs Outdated
Comment on lines +125 to +134

// 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();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we are already covering the diagnostic in above dyn Display, or what would you like to test here?

Suggested change
// 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();
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You're right, test14 already covers this path. Removed the extra test.

Comment thread CHANGELOG.md

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please remove the changes to CHANGELOG.md, as we do this via the changelog: in the PR description and then centrally once per release ^^

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Reverted. Good to know it's handled centrally from the PR description.

@CommanderStorm CommanderStorm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Community review: LGTM, please make sure to squash your changes

View changes since this review

@CommanderStorm CommanderStorm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

^- was meant as an +1

View changes since this review

@AbhilashG12
AbhilashG12 force-pushed the fix-borrowed-box-trait-object-suggestion branch from c2a8424 to 58ab68c Compare September 28, 2026 04:54
@rustbot

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>
@AbhilashG12
AbhilashG12 force-pushed the fix-borrowed-box-trait-object-suggestion branch from 58ab68c to 90a441e Compare September 28, 2026 05:00
@AbhilashG12

Copy link
Copy Markdown
Author

Squashed into one commit.. Thanks for the review.

@AbhilashG12 AbhilashG12 left a comment •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This branch has not been deployed

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

Labels

S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clippy suggestion about borrowed_box when trait object is boxed is incomplete

3 participants