Avoid side-effects from try_coerce when suggesting borrowing LHS of cast

This commit is contained in:
Michael Goulet 2023-08-15 02:23:49 +00:00
parent 6ef7d16be0
commit 822caa8b80
5 changed files with 12 additions and 72 deletions

View File

@ -389,34 +389,26 @@ impl<'a, 'tcx> CastCheck<'tcx> {
if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() { if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
if let ty::RawPtr(TypeAndMut { ty: expr_ty, .. }) = *self.expr_ty.kind() if let ty::RawPtr(TypeAndMut { ty: expr_ty, .. }) = *self.expr_ty.kind()
&& fcx && fcx
.try_coerce( .can_coerce(
self.expr,
Ty::new_ref(fcx.tcx, Ty::new_ref(fcx.tcx,
fcx.tcx.lifetimes.re_erased, fcx.tcx.lifetimes.re_erased,
TypeAndMut { ty: expr_ty, mutbl }, TypeAndMut { ty: expr_ty, mutbl },
), ),
self.cast_ty, self.cast_ty,
AllowTwoPhase::No,
None,
) )
.is_ok()
{ {
sugg = Some((format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty)); sugg = Some((format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
} else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind() } else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind()
&& expr_mutbl == Mutability::Not && expr_mutbl == Mutability::Not
&& mutbl == Mutability::Mut && mutbl == Mutability::Mut
&& fcx && fcx
.try_coerce( .can_coerce(
self.expr,
Ty::new_ref(fcx.tcx, Ty::new_ref(fcx.tcx,
expr_reg, expr_reg,
TypeAndMut { ty: expr_ty, mutbl: Mutability::Mut }, TypeAndMut { ty: expr_ty, mutbl: Mutability::Mut },
), ),
self.cast_ty, self.cast_ty,
AllowTwoPhase::No,
None,
) )
.is_ok()
{ {
sugg_mutref = true; sugg_mutref = true;
} }
@ -424,30 +416,22 @@ impl<'a, 'tcx> CastCheck<'tcx> {
if !sugg_mutref if !sugg_mutref
&& sugg == None && sugg == None
&& fcx && fcx
.try_coerce( .can_coerce(
self.expr,
Ty::new_ref(fcx.tcx,reg, TypeAndMut { ty: self.expr_ty, mutbl }), Ty::new_ref(fcx.tcx,reg, TypeAndMut { ty: self.expr_ty, mutbl }),
self.cast_ty, self.cast_ty,
AllowTwoPhase::No,
None,
) )
.is_ok()
{ {
sugg = Some((format!("&{}", mutbl.prefix_str()), false)); sugg = Some((format!("&{}", mutbl.prefix_str()), false));
} }
} else if let ty::RawPtr(TypeAndMut { mutbl, .. }) = *self.cast_ty.kind() } else if let ty::RawPtr(TypeAndMut { mutbl, .. }) = *self.cast_ty.kind()
&& fcx && fcx
.try_coerce( .can_coerce(
self.expr,
Ty::new_ref(fcx.tcx, Ty::new_ref(fcx.tcx,
fcx.tcx.lifetimes.re_erased, fcx.tcx.lifetimes.re_erased,
TypeAndMut { ty: self.expr_ty, mutbl }, TypeAndMut { ty: self.expr_ty, mutbl },
), ),
self.cast_ty, self.cast_ty,
AllowTwoPhase::No,
None,
) )
.is_ok()
{ {
sugg = Some((format!("&{}", mutbl.prefix_str()), false)); sugg = Some((format!("&{}", mutbl.prefix_str()), false));
} }

View File

@ -2,22 +2,8 @@ error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<_>`
--> $DIR/type-checking-test-1.rs:19:13 --> $DIR/type-checking-test-1.rs:19:13
| |
LL | let _ = x as &dyn Bar<_>; // Ambiguous LL | let _ = x as &dyn Bar<_>; // Ambiguous
| ^^^^^^^^^^^^^^^^ invalid cast | ^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
|
help: consider borrowing the value
|
LL | let _ = &x as &dyn Bar<_>; // Ambiguous
| +
error[E0277]: the trait bound `&dyn Foo: Bar<_>` is not satisfied error: aborting due to previous error
--> $DIR/type-checking-test-1.rs:19:13
|
LL | let _ = x as &dyn Bar<_>; // Ambiguous
| ^ the trait `Bar<_>` is not implemented for `&dyn Foo`
|
= note: required for the cast from `&&dyn Foo` to `&dyn Bar<_>`
error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0605`.
Some errors have detailed explanations: E0277, E0605.
For more information about an error, try `rustc --explain E0277`.

View File

@ -18,7 +18,6 @@ fn test_specific(x: &dyn Foo) {
fn test_unknown_version(x: &dyn Foo) { fn test_unknown_version(x: &dyn Foo) {
let _ = x as &dyn Bar<_>; // Ambiguous let _ = x as &dyn Bar<_>; // Ambiguous
//~^ ERROR non-primitive cast //~^ ERROR non-primitive cast
//[current]~^^ ERROR the trait bound `&dyn Foo: Bar<_>` is not satisfied
} }
fn test_infer_version(x: &dyn Foo) { fn test_infer_version(x: &dyn Foo) {

View File

@ -18,13 +18,11 @@ fn test_specific2(x: &dyn Foo<u32>) {
fn test_specific3(x: &dyn Foo<i32>) { fn test_specific3(x: &dyn Foo<i32>) {
let _ = x as &dyn Bar<u32>; // Error let _ = x as &dyn Bar<u32>; // Error
//~^ ERROR non-primitive cast //~^ ERROR non-primitive cast
//~^^ ERROR the trait bound `&dyn Foo<i32>: Bar<u32>` is not satisfied
} }
fn test_infer_arg(x: &dyn Foo<u32>) { fn test_infer_arg(x: &dyn Foo<u32>) {
let a = x as &dyn Bar<_>; // Ambiguous let a = x as &dyn Bar<_>; // Ambiguous
//~^ ERROR non-primitive cast //~^ ERROR non-primitive cast
//~^^ ERROR the trait bound `&dyn Foo<u32>: Bar<_>` is not satisfied
let _ = a.bar(); let _ = a.bar();
} }

View File

@ -2,41 +2,14 @@ error[E0605]: non-primitive cast: `&dyn Foo<i32>` as `&dyn Bar<u32>`
--> $DIR/type-checking-test-2.rs:19:13 --> $DIR/type-checking-test-2.rs:19:13
| |
LL | let _ = x as &dyn Bar<u32>; // Error LL | let _ = x as &dyn Bar<u32>; // Error
| ^^^^^^^^^^^^^^^^^^ invalid cast | ^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
|
help: consider borrowing the value
|
LL | let _ = &x as &dyn Bar<u32>; // Error
| +
error[E0277]: the trait bound `&dyn Foo<i32>: Bar<u32>` is not satisfied
--> $DIR/type-checking-test-2.rs:19:13
|
LL | let _ = x as &dyn Bar<u32>; // Error
| ^ the trait `Bar<u32>` is not implemented for `&dyn Foo<i32>`
|
= note: required for the cast from `&&dyn Foo<i32>` to `&dyn Bar<u32>`
error[E0605]: non-primitive cast: `&dyn Foo<u32>` as `&dyn Bar<_>` error[E0605]: non-primitive cast: `&dyn Foo<u32>` as `&dyn Bar<_>`
--> $DIR/type-checking-test-2.rs:25:13 --> $DIR/type-checking-test-2.rs:24:13
| |
LL | let a = x as &dyn Bar<_>; // Ambiguous LL | let a = x as &dyn Bar<_>; // Ambiguous
| ^^^^^^^^^^^^^^^^ invalid cast | ^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
|
help: consider borrowing the value
|
LL | let a = &x as &dyn Bar<_>; // Ambiguous
| +
error[E0277]: the trait bound `&dyn Foo<u32>: Bar<_>` is not satisfied error: aborting due to 2 previous errors
--> $DIR/type-checking-test-2.rs:25:13
|
LL | let a = x as &dyn Bar<_>; // Ambiguous
| ^ the trait `Bar<_>` is not implemented for `&dyn Foo<u32>`
|
= note: required for the cast from `&&dyn Foo<u32>` to `&dyn Bar<_>`
error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0605`.
Some errors have detailed explanations: E0277, E0605.
For more information about an error, try `rustc --explain E0277`.