Skip to content

Commit 1b2f969

Browse files
authored
Rollup merge of rust-lang#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 rust-lang#90627
2 parents 749b557 + 8568f44 commit 1b2f969

File tree

8 files changed

+38
-41
lines changed

8 files changed

+38
-41
lines changed

compiler/rustc_typeck/src/check/demand.rs

+11-26
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3131
error: TypeError<'tcx>,
3232
) {
3333
self.annotate_expected_due_to_let_ty(err, expr, error);
34-
self.suggest_box_deref(err, expr, expected, expr_ty);
35-
self.suggest_compatible_variants(err, expr, expected, expr_ty);
3634
self.suggest_deref_ref_or_into(err, expr, expected, expr_ty, expected_ty_expr);
35+
self.suggest_compatible_variants(err, expr, expected, expr_ty);
3736
if self.suggest_calling_boxed_future_when_appropriate(err, expr, expected, expr_ty) {
3837
return;
3938
}
@@ -259,23 +258,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
259258
}
260259
}
261260

262-
fn suggest_box_deref(
263-
&self,
264-
err: &mut DiagnosticBuilder<'_>,
265-
expr: &hir::Expr<'_>,
266-
expected: Ty<'tcx>,
267-
expr_ty: Ty<'tcx>,
268-
) {
269-
if expr_ty.is_box() && expr_ty.boxed_ty() == expected {
270-
err.span_suggestion_verbose(
271-
expr.span.shrink_to_lo(),
272-
"try dereferencing the `Box`",
273-
"*".to_string(),
274-
Applicability::MachineApplicable,
275-
);
276-
}
277-
}
278-
279261
/// If the expected type is an enum (Issue #55250) with any variants whose
280262
/// sole field is of the found type, suggest such variants. (Issue #42764)
281263
fn suggest_compatible_variants(
@@ -857,14 +839,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
857839
Applicability::MachineApplicable,
858840
false,
859841
));
860-
} else if self.infcx.type_is_copy_modulo_regions(
861-
self.param_env,
862-
expected,
863-
sp,
864-
) {
865-
// For this suggestion to make sense, the type would need to be `Copy`.
842+
}
843+
844+
// For this suggestion to make sense, the type would need to be `Copy`,
845+
// or we have to be moving out of a `Box<T>`
846+
if self.infcx.type_is_copy_modulo_regions(self.param_env, expected, sp)
847+
|| checked_ty.is_box()
848+
{
866849
if let Ok(code) = sm.span_to_snippet(expr.span) {
867-
let message = if checked_ty.is_region_ptr() {
850+
let message = if checked_ty.is_box() {
851+
"consider unboxing the value"
852+
} else if checked_ty.is_region_ptr() {
868853
"consider dereferencing the borrow"
869854
} else {
870855
"consider dereferencing the type"

src/test/ui/infinite/infinite-autoderef.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ error[E0308]: mismatched types
22
--> $DIR/infinite-autoderef.rs:20:13
33
|
44
LL | x = Box::new(x);
5-
| ^^^^^^^^^^^- help: try using a conversion method: `.to_string()`
6-
| |
7-
| cyclic type of infinite size
5+
| ^^^^^^^^^^^ cyclic type of infinite size
6+
|
7+
help: consider unboxing the value
8+
|
9+
LL | x = *Box::new(x);
10+
| +
811

912
error[E0055]: reached the recursion limit while auto-dereferencing `Foo`
1013
--> $DIR/infinite-autoderef.rs:25:5

src/test/ui/occurs-check-2.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ error[E0308]: mismatched types
22
--> $DIR/occurs-check-2.rs:7:9
33
|
44
LL | f = Box::new(g);
5-
| ^^^^^^^^^^^- help: try using a conversion method: `.to_string()`
6-
| |
7-
| cyclic type of infinite size
5+
| ^^^^^^^^^^^ cyclic type of infinite size
6+
|
7+
help: consider unboxing the value
8+
|
9+
LL | f = *Box::new(g);
10+
| +
811

912
error: aborting due to previous error
1013

src/test/ui/occurs-check.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ error[E0308]: mismatched types
22
--> $DIR/occurs-check.rs:5:9
33
|
44
LL | f = Box::new(f);
5-
| ^^^^^^^^^^^- help: try using a conversion method: `.to_string()`
6-
| |
7-
| cyclic type of infinite size
5+
| ^^^^^^^^^^^ cyclic type of infinite size
6+
|
7+
help: consider unboxing the value
8+
|
9+
LL | f = *Box::new(f);
10+
| +
811

912
error: aborting due to previous error
1013

src/test/ui/span/coerce-suggestions.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ error[E0308]: mismatched types
3838
--> $DIR/coerce-suggestions.rs:17:9
3939
|
4040
LL | f = Box::new(f);
41-
| ^^^^^^^^^^^- help: try using a conversion method: `.to_string()`
42-
| |
43-
| cyclic type of infinite size
41+
| ^^^^^^^^^^^ cyclic type of infinite size
42+
|
43+
help: consider unboxing the value
44+
|
45+
LL | f = *Box::new(f);
46+
| +
4447

4548
error[E0308]: mismatched types
4649
--> $DIR/coerce-suggestions.rs:21:9

src/test/ui/suggestions/boxed-variant-field.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn foo(x: Ty) -> Ty {
88
Ty::Unit => Ty::Unit,
99
Ty::List(elem) => foo(elem),
1010
//~^ ERROR mismatched types
11-
//~| HELP try dereferencing the `Box`
11+
//~| HELP consider unboxing the value
1212
//~| HELP try wrapping
1313
}
1414
}

src/test/ui/suggestions/boxed-variant-field.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | Ty::List(elem) => foo(elem),
66
|
77
= note: expected enum `Ty`
88
found struct `Box<Ty>`
9-
help: try dereferencing the `Box`
9+
help: consider unboxing the value
1010
|
1111
LL | Ty::List(elem) => foo(*elem),
1212
| +

src/test/ui/terr-sorts.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | want_foo(b);
66
|
77
= note: expected struct `Foo`
88
found struct `Box<Foo>`
9-
help: try dereferencing the `Box`
9+
help: consider unboxing the value
1010
|
1111
LL | want_foo(*b);
1212
| +

0 commit comments

Comments
 (0)