suggest adding a reference to a trait assoc item
This commit is contained in:
parent
75b7e52e92
commit
64b3e4af20
4 changed files with 161 additions and 90 deletions
|
@ -882,6 +882,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
obligation.cause.code()
|
obligation.cause.code()
|
||||||
{
|
{
|
||||||
&parent_code
|
&parent_code
|
||||||
|
} else if let ObligationCauseCode::ItemObligation(_) = obligation.cause.code() {
|
||||||
|
obligation.cause.code()
|
||||||
} else if let ExpnKind::Desugaring(DesugaringKind::ForLoop) =
|
} else if let ExpnKind::Desugaring(DesugaringKind::ForLoop) =
|
||||||
span.ctxt().outer_expn_data().kind
|
span.ctxt().outer_expn_data().kind
|
||||||
{
|
{
|
||||||
|
@ -906,8 +908,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
let param_env = obligation.param_env;
|
let param_env = obligation.param_env;
|
||||||
|
|
||||||
// Try to apply the original trait binding obligation by borrowing.
|
// Try to apply the original trait binding obligation by borrowing.
|
||||||
let mut try_borrowing =
|
let mut try_borrowing = |old_pred: ty::PolyTraitPredicate<'tcx>,
|
||||||
|old_pred: ty::PolyTraitPredicate<'tcx>, blacklist: &[DefId]| -> bool {
|
blacklist: &[DefId]|
|
||||||
|
-> bool {
|
||||||
if blacklist.contains(&old_pred.def_id()) {
|
if blacklist.contains(&old_pred.def_id()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -933,7 +936,16 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
let imm_result = mk_result(trait_pred_and_imm_ref);
|
let imm_result = mk_result(trait_pred_and_imm_ref);
|
||||||
let mut_result = mk_result(trait_pred_and_mut_ref);
|
let mut_result = mk_result(trait_pred_and_mut_ref);
|
||||||
|
|
||||||
if imm_result || mut_result {
|
let ref_inner_ty_result =
|
||||||
|
if let ObligationCauseCode::ItemObligation(_) = obligation.cause.code()
|
||||||
|
&& let ty::Ref(_, ty, mutability) = old_pred.self_ty().skip_binder().kind()
|
||||||
|
{
|
||||||
|
Some((mk_result(old_pred.map_bound(|trait_pred| (trait_pred, *ty))), mutability))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
if imm_result || mut_result || ref_inner_ty_result.map_or(false, |(result, _)| result) {
|
||||||
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
|
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
|
||||||
// We have a very specific type of error, where just borrowing this argument
|
// We have a very specific type of error, where just borrowing this argument
|
||||||
// might solve the problem. In cases like this, the important part is the
|
// might solve the problem. In cases like this, the important part is the
|
||||||
|
@ -981,13 +993,17 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
let is_mut = mut_result
|
||||||
|
|| ref_inner_ty_result.map_or(false, |(_, mutabl)| {
|
||||||
|
matches!(mutabl, hir::Mutability::Mut)
|
||||||
|
});
|
||||||
err.span_suggestion_verbose(
|
err.span_suggestion_verbose(
|
||||||
span.shrink_to_lo(),
|
span.shrink_to_lo(),
|
||||||
&format!(
|
&format!(
|
||||||
"consider{} borrowing here",
|
"consider{} borrowing here",
|
||||||
if mut_result { " mutably" } else { "" }
|
if is_mut { " mutably" } else { "" }
|
||||||
),
|
),
|
||||||
format!("&{}", if mut_result { "mut " } else { "" }),
|
format!("&{}", if is_mut { "mut " } else { "" }),
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1001,7 +1017,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
if let ObligationCauseCode::ImplDerivedObligation(cause) = &*code {
|
if let ObligationCauseCode::ImplDerivedObligation(cause) = &*code {
|
||||||
try_borrowing(cause.derived.parent_trait_pred, &[])
|
try_borrowing(cause.derived.parent_trait_pred, &[])
|
||||||
} else if let ObligationCauseCode::BindingObligation(_, _)
|
} else if let ObligationCauseCode::BindingObligation(_, _)
|
||||||
| ObligationCauseCode::ItemObligation(_) = code
|
| ObligationCauseCode::ItemObligation(..) = code
|
||||||
{
|
{
|
||||||
try_borrowing(poly_trait_pred, &never_suggest_borrow)
|
try_borrowing(poly_trait_pred, &never_suggest_borrow)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
// run-rustfix
|
||||||
|
#![allow(unused_variables)]
|
||||||
|
|
||||||
|
fn foo(foo: &mut usize) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar(bar: &usize) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
foo(&mut Default::default()); //~ the trait bound `&mut usize: Default` is not satisfied
|
||||||
|
bar(&Default::default()); //~ the trait bound `&usize: Default` is not satisfied
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
// run-rustfix
|
||||||
|
#![allow(unused_variables)]
|
||||||
|
|
||||||
|
fn foo(foo: &mut usize) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar(bar: &usize) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
foo(Default::default()); //~ the trait bound `&mut usize: Default` is not satisfied
|
||||||
|
bar(Default::default()); //~ the trait bound `&usize: Default` is not satisfied
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
error[E0277]: the trait bound `&mut usize: Default` is not satisfied
|
||||||
|
--> $DIR/suggest-adding-reference-to-trait-assoc-item.rs:13:9
|
||||||
|
|
|
||||||
|
LL | foo(Default::default());
|
||||||
|
| ^^^^^^^^^^^^^^^^ expected an implementor of trait `Default`
|
||||||
|
|
|
||||||
|
help: consider mutably borrowing here
|
||||||
|
|
|
||||||
|
LL | foo(&mut Default::default());
|
||||||
|
| ++++
|
||||||
|
|
||||||
|
error[E0277]: the trait bound `&usize: Default` is not satisfied
|
||||||
|
--> $DIR/suggest-adding-reference-to-trait-assoc-item.rs:14:9
|
||||||
|
|
|
||||||
|
LL | bar(Default::default());
|
||||||
|
| ^^^^^^^^^^^^^^^^ expected an implementor of trait `Default`
|
||||||
|
|
|
||||||
|
help: consider borrowing here
|
||||||
|
|
|
||||||
|
LL | bar(&Default::default());
|
||||||
|
| +
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0277`.
|
Loading…
Add table
Reference in a new issue