Skip to content

Commit 822caa8

Browse files
Avoid side-effects from try_coerce when suggesting borrowing LHS of cast
1 parent 6ef7d16 commit 822caa8

File tree

5 files changed

+12
-72
lines changed

5 files changed

+12
-72
lines changed

compiler/rustc_hir_typeck/src/cast.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -389,65 +389,49 @@ impl<'a, 'tcx> CastCheck<'tcx> {
389389
if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
390390
if let ty::RawPtr(TypeAndMut { ty: expr_ty, .. }) = *self.expr_ty.kind()
391391
&& fcx
392-
.try_coerce(
393-
self.expr,
392+
.can_coerce(
394393
Ty::new_ref(fcx.tcx,
395394
fcx.tcx.lifetimes.re_erased,
396395
TypeAndMut { ty: expr_ty, mutbl },
397396
),
398397
self.cast_ty,
399-
AllowTwoPhase::No,
400-
None,
401398
)
402-
.is_ok()
403399
{
404400
sugg = Some((format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
405401
} else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind()
406402
&& expr_mutbl == Mutability::Not
407403
&& mutbl == Mutability::Mut
408404
&& fcx
409-
.try_coerce(
410-
self.expr,
405+
.can_coerce(
411406
Ty::new_ref(fcx.tcx,
412407
expr_reg,
413408
TypeAndMut { ty: expr_ty, mutbl: Mutability::Mut },
414409
),
415410
self.cast_ty,
416-
AllowTwoPhase::No,
417-
None,
418411
)
419-
.is_ok()
420412
{
421413
sugg_mutref = true;
422414
}
423415

424416
if !sugg_mutref
425417
&& sugg == None
426418
&& fcx
427-
.try_coerce(
428-
self.expr,
419+
.can_coerce(
429420
Ty::new_ref(fcx.tcx,reg, TypeAndMut { ty: self.expr_ty, mutbl }),
430421
self.cast_ty,
431-
AllowTwoPhase::No,
432-
None,
433422
)
434-
.is_ok()
435423
{
436424
sugg = Some((format!("&{}", mutbl.prefix_str()), false));
437425
}
438426
} else if let ty::RawPtr(TypeAndMut { mutbl, .. }) = *self.cast_ty.kind()
439427
&& fcx
440-
.try_coerce(
441-
self.expr,
428+
.can_coerce(
442429
Ty::new_ref(fcx.tcx,
443430
fcx.tcx.lifetimes.re_erased,
444431
TypeAndMut { ty: self.expr_ty, mutbl },
445432
),
446433
self.cast_ty,
447-
AllowTwoPhase::No,
448-
None,
449434
)
450-
.is_ok()
451435
{
452436
sugg = Some((format!("&{}", mutbl.prefix_str()), false));
453437
}

tests/ui/traits/trait-upcasting/type-checking-test-1.current.stderr

+3-17
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,8 @@ error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<_>`
22
--> $DIR/type-checking-test-1.rs:19:13
33
|
44
LL | let _ = x as &dyn Bar<_>; // Ambiguous
5-
| ^^^^^^^^^^^^^^^^ invalid cast
6-
|
7-
help: consider borrowing the value
8-
|
9-
LL | let _ = &x as &dyn Bar<_>; // Ambiguous
10-
| +
11-
12-
error[E0277]: the trait bound `&dyn Foo: Bar<_>` is not satisfied
13-
--> $DIR/type-checking-test-1.rs:19:13
14-
|
15-
LL | let _ = x as &dyn Bar<_>; // Ambiguous
16-
| ^ the trait `Bar<_>` is not implemented for `&dyn Foo`
17-
|
18-
= note: required for the cast from `&&dyn Foo` to `&dyn Bar<_>`
5+
| ^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
196

20-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
218

22-
Some errors have detailed explanations: E0277, E0605.
23-
For more information about an error, try `rustc --explain E0277`.
9+
For more information about this error, try `rustc --explain E0605`.

tests/ui/traits/trait-upcasting/type-checking-test-1.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ fn test_specific(x: &dyn Foo) {
1818
fn test_unknown_version(x: &dyn Foo) {
1919
let _ = x as &dyn Bar<_>; // Ambiguous
2020
//~^ ERROR non-primitive cast
21-
//[current]~^^ ERROR the trait bound `&dyn Foo: Bar<_>` is not satisfied
2221
}
2322

2423
fn test_infer_version(x: &dyn Foo) {

tests/ui/traits/trait-upcasting/type-checking-test-2.rs

-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ fn test_specific2(x: &dyn Foo<u32>) {
1818
fn test_specific3(x: &dyn Foo<i32>) {
1919
let _ = x as &dyn Bar<u32>; // Error
2020
//~^ ERROR non-primitive cast
21-
//~^^ ERROR the trait bound `&dyn Foo<i32>: Bar<u32>` is not satisfied
2221
}
2322

2423
fn test_infer_arg(x: &dyn Foo<u32>) {
2524
let a = x as &dyn Bar<_>; // Ambiguous
2625
//~^ ERROR non-primitive cast
27-
//~^^ ERROR the trait bound `&dyn Foo<u32>: Bar<_>` is not satisfied
2826
let _ = a.bar();
2927
}
3028

tests/ui/traits/trait-upcasting/type-checking-test-2.stderr

+5-32
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,14 @@ error[E0605]: non-primitive cast: `&dyn Foo<i32>` as `&dyn Bar<u32>`
22
--> $DIR/type-checking-test-2.rs:19:13
33
|
44
LL | let _ = x as &dyn Bar<u32>; // Error
5-
| ^^^^^^^^^^^^^^^^^^ invalid cast
6-
|
7-
help: consider borrowing the value
8-
|
9-
LL | let _ = &x as &dyn Bar<u32>; // Error
10-
| +
11-
12-
error[E0277]: the trait bound `&dyn Foo<i32>: Bar<u32>` is not satisfied
13-
--> $DIR/type-checking-test-2.rs:19:13
14-
|
15-
LL | let _ = x as &dyn Bar<u32>; // Error
16-
| ^ the trait `Bar<u32>` is not implemented for `&dyn Foo<i32>`
17-
|
18-
= note: required for the cast from `&&dyn Foo<i32>` to `&dyn Bar<u32>`
5+
| ^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
196

207
error[E0605]: non-primitive cast: `&dyn Foo<u32>` as `&dyn Bar<_>`
21-
--> $DIR/type-checking-test-2.rs:25:13
8+
--> $DIR/type-checking-test-2.rs:24:13
229
|
2310
LL | let a = x as &dyn Bar<_>; // Ambiguous
24-
| ^^^^^^^^^^^^^^^^ invalid cast
25-
|
26-
help: consider borrowing the value
27-
|
28-
LL | let a = &x as &dyn Bar<_>; // Ambiguous
29-
| +
30-
31-
error[E0277]: the trait bound `&dyn Foo<u32>: Bar<_>` is not satisfied
32-
--> $DIR/type-checking-test-2.rs:25:13
33-
|
34-
LL | let a = x as &dyn Bar<_>; // Ambiguous
35-
| ^ the trait `Bar<_>` is not implemented for `&dyn Foo<u32>`
36-
|
37-
= note: required for the cast from `&&dyn Foo<u32>` to `&dyn Bar<_>`
11+
| ^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
3812

39-
error: aborting due to 4 previous errors
13+
error: aborting due to 2 previous errors
4014

41-
Some errors have detailed explanations: E0277, E0605.
42-
For more information about an error, try `rustc --explain E0277`.
15+
For more information about this error, try `rustc --explain E0605`.

0 commit comments

Comments
 (0)