Skip to content

Do not suggest borrow that is already there in fully-qualified call #132469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,13 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
has_custom_message: bool,
) -> bool {
let span = obligation.cause.span;
let param_env = obligation.param_env;

let mk_result = |trait_pred_and_new_ty| {
let obligation =
self.mk_trait_obligation_with_new_self_ty(param_env, trait_pred_and_new_ty);
self.predicate_must_hold_modulo_regions(&obligation)
};

let code = match obligation.cause.code() {
ObligationCauseCode::FunctionArg { parent_code, .. } => parent_code,
Expand All @@ -1195,6 +1202,76 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
c @ ObligationCauseCode::WhereClauseInExpr(_, _, hir_id, _)
if self.tcx.hir_span(*hir_id).lo() == span.lo() =>
{
// `hir_id` corresponds to the HIR node that introduced a `where`-clause obligation.
// If that obligation comes from a type in an associated method call, we need
// special handling here.
if let hir::Node::Expr(expr) = self.tcx.parent_hir_node(*hir_id)
&& let hir::ExprKind::Call(base, _) = expr.kind
&& let hir::ExprKind::Path(hir::QPath::TypeRelative(ty, segment)) = base.kind
&& let hir::Node::Expr(outer) = self.tcx.parent_hir_node(expr.hir_id)
&& let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, mtbl, _) = outer.kind
&& ty.span == span
{
// We've encountered something like `&str::from("")`, where the intended code
// was likely `<&str>::from("")`. The former is interpreted as "call method
// `from` on `str` and borrow the result", while the latter means "call method
// `from` on `&str`".

let trait_pred_and_imm_ref = poly_trait_pred.map_bound(|p| {
(p, Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_static, p.self_ty()))
});
let trait_pred_and_mut_ref = poly_trait_pred.map_bound(|p| {
(p, Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_static, p.self_ty()))
});

let imm_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_imm_ref);
let mut_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_mut_ref);
let sugg_msg = |pre: &str| {
format!(
"you likely meant to call the associated function `{FN}` for type \
`&{pre}{TY}`, but the code as written calls associated function `{FN}` on \
type `{TY}`",
FN = segment.ident,
TY = poly_trait_pred.self_ty(),
)
};
match (imm_ref_self_ty_satisfies_pred, mut_ref_self_ty_satisfies_pred, mtbl) {
(true, _, hir::Mutability::Not) | (_, true, hir::Mutability::Mut) => {
err.multipart_suggestion_verbose(
sugg_msg(mtbl.prefix_str()),
vec![
(outer.span.shrink_to_lo(), "<".to_string()),
(span.shrink_to_hi(), ">".to_string()),
],
Applicability::MachineApplicable,
);
}
(true, _, hir::Mutability::Mut) => {
// There's an associated function found on the immutable borrow of the
err.multipart_suggestion_verbose(
sugg_msg("mut "),
vec![
(outer.span.shrink_to_lo().until(span), "<&".to_string()),
(span.shrink_to_hi(), ">".to_string()),
],
Applicability::MachineApplicable,
);
}
(_, true, hir::Mutability::Not) => {
err.multipart_suggestion_verbose(
sugg_msg(""),
vec![
(outer.span.shrink_to_lo().until(span), "<&mut ".to_string()),
(span.shrink_to_hi(), ">".to_string()),
],
Applicability::MachineApplicable,
);
}
_ => {}
}
// If we didn't return early here, we would instead suggest `&&str::from("")`.
return false;
}
c
}
c if matches!(
Expand All @@ -1220,8 +1297,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
never_suggest_borrow.push(def_id);
}

let param_env = obligation.param_env;

// Try to apply the original trait bound by borrowing.
let mut try_borrowing = |old_pred: ty::PolyTraitPredicate<'tcx>,
blacklist: &[DefId]|
Expand All @@ -1243,11 +1318,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
)
});

let mk_result = |trait_pred_and_new_ty| {
let obligation =
self.mk_trait_obligation_with_new_self_ty(param_env, trait_pred_and_new_ty);
self.predicate_must_hold_modulo_regions(&obligation)
};
let imm_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_imm_ref);
let mut_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_mut_ref);

Expand Down
17 changes: 17 additions & 0 deletions tests/ui/suggestions/dont-suggest-borrowing-existing-borrow.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ run-rustfix

struct S;
trait Trait {
fn foo() {}
}
impl Trait for &S {}
impl Trait for &mut S {}
fn main() {
let _ = <&str>::from("value");
//~^ ERROR the trait bound `str: From<_>` is not satisfied
//~| ERROR the size for values of type `str` cannot be known at compilation time
let _ = <&mut S>::foo();
//~^ ERROR the trait bound `S: Trait` is not satisfied
let _ = <&S>::foo();
//~^ ERROR the trait bound `S: Trait` is not satisfied
}
17 changes: 17 additions & 0 deletions tests/ui/suggestions/dont-suggest-borrowing-existing-borrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ run-rustfix

struct S;
trait Trait {
fn foo() {}
}
impl Trait for &S {}
impl Trait for &mut S {}
fn main() {
let _ = &str::from("value");
//~^ ERROR the trait bound `str: From<_>` is not satisfied
//~| ERROR the size for values of type `str` cannot be known at compilation time
let _ = &mut S::foo();
//~^ ERROR the trait bound `S: Trait` is not satisfied
let _ = &S::foo();
//~^ ERROR the trait bound `S: Trait` is not satisfied
}
58 changes: 58 additions & 0 deletions tests/ui/suggestions/dont-suggest-borrowing-existing-borrow.stderr
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I noticed there's another case that this PR doesn't account for, which provides an incorrect suggestion:

error[E0277]: the trait bound `S: Trait` is not satisfied
  --> $DIR/dont-suggest-borrowing-existing-borrow.rs:17:13
   |
LL |     let _ = S::foo();
   |             ^ the trait `Trait` is not implemented for `S`
   |
help: consider borrowing here
   |
LL |     let _ = &S::foo();
   |             +
LL |     let _ = &mut S::foo();
   |             ++++

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
error[E0277]: the trait bound `str: From<_>` is not satisfied
--> $DIR/dont-suggest-borrowing-existing-borrow.rs:10:14
|
LL | let _ = &str::from("value");
| ^^^ the trait `From<_>` is not implemented for `str`
|
= help: the following other types implement trait `From<T>`:
`String` implements `From<&String>`
`String` implements `From<&mut str>`
`String` implements `From<&str>`
`String` implements `From<Box<str>>`
`String` implements `From<Cow<'_, str>>`
`String` implements `From<char>`
help: you likely meant to call the associated function `from` for type `&str`, but the code as written calls associated function `from` on type `str`
|
LL | let _ = <&str>::from("value");
| + +

error[E0277]: the trait bound `S: Trait` is not satisfied
--> $DIR/dont-suggest-borrowing-existing-borrow.rs:13:18
|
LL | let _ = &mut S::foo();
| ^ the trait `Trait` is not implemented for `S`
|
= help: the following other types implement trait `Trait`:
&S
&mut S
help: you likely meant to call the associated function `foo` for type `&mut S`, but the code as written calls associated function `foo` on type `S`
|
LL | let _ = <&mut S>::foo();
| + +

error[E0277]: the trait bound `S: Trait` is not satisfied
--> $DIR/dont-suggest-borrowing-existing-borrow.rs:15:14
|
LL | let _ = &S::foo();
| ^ the trait `Trait` is not implemented for `S`
|
= help: the following other types implement trait `Trait`:
&S
&mut S
help: you likely meant to call the associated function `foo` for type `&S`, but the code as written calls associated function `foo` on type `S`
|
LL | let _ = <&S>::foo();
| + +

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/dont-suggest-borrowing-existing-borrow.rs:10:14
|
LL | let _ = &str::from("value");
| ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= note: the return type of a function must have a statically known size

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0277`.
Loading