Rollup merge of #92810 - compiler-errors:deduplicate-box-deref-suggestion, r=camelid
Deduplicate box deref and regular deref suggestions Remove the suggestion code special-cased for Box deref. r? ```@camelid``` since you introduced the code in #90627
This commit is contained in:
commit
9d5cb20978
8 changed files with 38 additions and 41 deletions
|
@ -31,9 +31,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
error: TypeError<'tcx>,
|
error: TypeError<'tcx>,
|
||||||
) {
|
) {
|
||||||
self.annotate_expected_due_to_let_ty(err, expr, error);
|
self.annotate_expected_due_to_let_ty(err, expr, error);
|
||||||
self.suggest_box_deref(err, expr, expected, expr_ty);
|
|
||||||
self.suggest_compatible_variants(err, expr, expected, expr_ty);
|
|
||||||
self.suggest_deref_ref_or_into(err, expr, expected, expr_ty, expected_ty_expr);
|
self.suggest_deref_ref_or_into(err, expr, expected, expr_ty, expected_ty_expr);
|
||||||
|
self.suggest_compatible_variants(err, expr, expected, expr_ty);
|
||||||
if self.suggest_calling_boxed_future_when_appropriate(err, expr, expected, expr_ty) {
|
if self.suggest_calling_boxed_future_when_appropriate(err, expr, expected, expr_ty) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -259,23 +258,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn suggest_box_deref(
|
|
||||||
&self,
|
|
||||||
err: &mut DiagnosticBuilder<'_>,
|
|
||||||
expr: &hir::Expr<'_>,
|
|
||||||
expected: Ty<'tcx>,
|
|
||||||
expr_ty: Ty<'tcx>,
|
|
||||||
) {
|
|
||||||
if expr_ty.is_box() && expr_ty.boxed_ty() == expected {
|
|
||||||
err.span_suggestion_verbose(
|
|
||||||
expr.span.shrink_to_lo(),
|
|
||||||
"try dereferencing the `Box`",
|
|
||||||
"*".to_string(),
|
|
||||||
Applicability::MachineApplicable,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// If the expected type is an enum (Issue #55250) with any variants whose
|
/// If the expected type is an enum (Issue #55250) with any variants whose
|
||||||
/// sole field is of the found type, suggest such variants. (Issue #42764)
|
/// sole field is of the found type, suggest such variants. (Issue #42764)
|
||||||
fn suggest_compatible_variants(
|
fn suggest_compatible_variants(
|
||||||
|
@ -857,14 +839,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
Applicability::MachineApplicable,
|
Applicability::MachineApplicable,
|
||||||
false,
|
false,
|
||||||
));
|
));
|
||||||
} else if self.infcx.type_is_copy_modulo_regions(
|
}
|
||||||
self.param_env,
|
|
||||||
expected,
|
// For this suggestion to make sense, the type would need to be `Copy`,
|
||||||
sp,
|
// or we have to be moving out of a `Box<T>`
|
||||||
) {
|
if self.infcx.type_is_copy_modulo_regions(self.param_env, expected, sp)
|
||||||
// For this suggestion to make sense, the type would need to be `Copy`.
|
|| checked_ty.is_box()
|
||||||
|
{
|
||||||
if let Ok(code) = sm.span_to_snippet(expr.span) {
|
if let Ok(code) = sm.span_to_snippet(expr.span) {
|
||||||
let message = if checked_ty.is_region_ptr() {
|
let message = if checked_ty.is_box() {
|
||||||
|
"consider unboxing the value"
|
||||||
|
} else if checked_ty.is_region_ptr() {
|
||||||
"consider dereferencing the borrow"
|
"consider dereferencing the borrow"
|
||||||
} else {
|
} else {
|
||||||
"consider dereferencing the type"
|
"consider dereferencing the type"
|
||||||
|
|
|
@ -2,9 +2,12 @@ error[E0308]: mismatched types
|
||||||
--> $DIR/infinite-autoderef.rs:20:13
|
--> $DIR/infinite-autoderef.rs:20:13
|
||||||
|
|
|
|
||||||
LL | x = Box::new(x);
|
LL | x = Box::new(x);
|
||||||
| ^^^^^^^^^^^- help: try using a conversion method: `.to_string()`
|
| ^^^^^^^^^^^ cyclic type of infinite size
|
||||||
| |
|
|
|
||||||
| cyclic type of infinite size
|
help: consider unboxing the value
|
||||||
|
|
|
||||||
|
LL | x = *Box::new(x);
|
||||||
|
| +
|
||||||
|
|
||||||
error[E0055]: reached the recursion limit while auto-dereferencing `Foo`
|
error[E0055]: reached the recursion limit while auto-dereferencing `Foo`
|
||||||
--> $DIR/infinite-autoderef.rs:25:5
|
--> $DIR/infinite-autoderef.rs:25:5
|
||||||
|
|
|
@ -2,9 +2,12 @@ error[E0308]: mismatched types
|
||||||
--> $DIR/occurs-check-2.rs:7:9
|
--> $DIR/occurs-check-2.rs:7:9
|
||||||
|
|
|
|
||||||
LL | f = Box::new(g);
|
LL | f = Box::new(g);
|
||||||
| ^^^^^^^^^^^- help: try using a conversion method: `.to_string()`
|
| ^^^^^^^^^^^ cyclic type of infinite size
|
||||||
| |
|
|
|
||||||
| cyclic type of infinite size
|
help: consider unboxing the value
|
||||||
|
|
|
||||||
|
LL | f = *Box::new(g);
|
||||||
|
| +
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,12 @@ error[E0308]: mismatched types
|
||||||
--> $DIR/occurs-check.rs:5:9
|
--> $DIR/occurs-check.rs:5:9
|
||||||
|
|
|
|
||||||
LL | f = Box::new(f);
|
LL | f = Box::new(f);
|
||||||
| ^^^^^^^^^^^- help: try using a conversion method: `.to_string()`
|
| ^^^^^^^^^^^ cyclic type of infinite size
|
||||||
| |
|
|
|
||||||
| cyclic type of infinite size
|
help: consider unboxing the value
|
||||||
|
|
|
||||||
|
LL | f = *Box::new(f);
|
||||||
|
| +
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
|
|
@ -38,9 +38,12 @@ error[E0308]: mismatched types
|
||||||
--> $DIR/coerce-suggestions.rs:17:9
|
--> $DIR/coerce-suggestions.rs:17:9
|
||||||
|
|
|
|
||||||
LL | f = Box::new(f);
|
LL | f = Box::new(f);
|
||||||
| ^^^^^^^^^^^- help: try using a conversion method: `.to_string()`
|
| ^^^^^^^^^^^ cyclic type of infinite size
|
||||||
| |
|
|
|
||||||
| cyclic type of infinite size
|
help: consider unboxing the value
|
||||||
|
|
|
||||||
|
LL | f = *Box::new(f);
|
||||||
|
| +
|
||||||
|
|
||||||
error[E0308]: mismatched types
|
error[E0308]: mismatched types
|
||||||
--> $DIR/coerce-suggestions.rs:21:9
|
--> $DIR/coerce-suggestions.rs:21:9
|
||||||
|
|
|
@ -8,7 +8,7 @@ fn foo(x: Ty) -> Ty {
|
||||||
Ty::Unit => Ty::Unit,
|
Ty::Unit => Ty::Unit,
|
||||||
Ty::List(elem) => foo(elem),
|
Ty::List(elem) => foo(elem),
|
||||||
//~^ ERROR mismatched types
|
//~^ ERROR mismatched types
|
||||||
//~| HELP try dereferencing the `Box`
|
//~| HELP consider unboxing the value
|
||||||
//~| HELP try wrapping
|
//~| HELP try wrapping
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ LL | Ty::List(elem) => foo(elem),
|
||||||
|
|
|
|
||||||
= note: expected enum `Ty`
|
= note: expected enum `Ty`
|
||||||
found struct `Box<Ty>`
|
found struct `Box<Ty>`
|
||||||
help: try dereferencing the `Box`
|
help: consider unboxing the value
|
||||||
|
|
|
|
||||||
LL | Ty::List(elem) => foo(*elem),
|
LL | Ty::List(elem) => foo(*elem),
|
||||||
| +
|
| +
|
||||||
|
|
|
@ -6,7 +6,7 @@ LL | want_foo(b);
|
||||||
|
|
|
|
||||||
= note: expected struct `Foo`
|
= note: expected struct `Foo`
|
||||||
found struct `Box<Foo>`
|
found struct `Box<Foo>`
|
||||||
help: try dereferencing the `Box`
|
help: consider unboxing the value
|
||||||
|
|
|
|
||||||
LL | want_foo(*b);
|
LL | want_foo(*b);
|
||||||
| +
|
| +
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue