Avoid side-effects from try_coerce when suggesting borrowing LHS of cast
This commit is contained in:
parent
6ef7d16be0
commit
822caa8b80
@ -389,34 +389,26 @@ impl<'a, 'tcx> CastCheck<'tcx> {
|
||||
if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
|
||||
if let ty::RawPtr(TypeAndMut { ty: expr_ty, .. }) = *self.expr_ty.kind()
|
||||
&& fcx
|
||||
.try_coerce(
|
||||
self.expr,
|
||||
.can_coerce(
|
||||
Ty::new_ref(fcx.tcx,
|
||||
fcx.tcx.lifetimes.re_erased,
|
||||
TypeAndMut { ty: expr_ty, mutbl },
|
||||
),
|
||||
self.cast_ty,
|
||||
AllowTwoPhase::No,
|
||||
None,
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
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()
|
||||
&& expr_mutbl == Mutability::Not
|
||||
&& mutbl == Mutability::Mut
|
||||
&& fcx
|
||||
.try_coerce(
|
||||
self.expr,
|
||||
.can_coerce(
|
||||
Ty::new_ref(fcx.tcx,
|
||||
expr_reg,
|
||||
TypeAndMut { ty: expr_ty, mutbl: Mutability::Mut },
|
||||
),
|
||||
self.cast_ty,
|
||||
AllowTwoPhase::No,
|
||||
None,
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
sugg_mutref = true;
|
||||
}
|
||||
@ -424,30 +416,22 @@ impl<'a, 'tcx> CastCheck<'tcx> {
|
||||
if !sugg_mutref
|
||||
&& sugg == None
|
||||
&& fcx
|
||||
.try_coerce(
|
||||
self.expr,
|
||||
.can_coerce(
|
||||
Ty::new_ref(fcx.tcx,reg, TypeAndMut { ty: self.expr_ty, mutbl }),
|
||||
self.cast_ty,
|
||||
AllowTwoPhase::No,
|
||||
None,
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
sugg = Some((format!("&{}", mutbl.prefix_str()), false));
|
||||
}
|
||||
} else if let ty::RawPtr(TypeAndMut { mutbl, .. }) = *self.cast_ty.kind()
|
||||
&& fcx
|
||||
.try_coerce(
|
||||
self.expr,
|
||||
.can_coerce(
|
||||
Ty::new_ref(fcx.tcx,
|
||||
fcx.tcx.lifetimes.re_erased,
|
||||
TypeAndMut { ty: self.expr_ty, mutbl },
|
||||
),
|
||||
self.cast_ty,
|
||||
AllowTwoPhase::No,
|
||||
None,
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
sugg = Some((format!("&{}", mutbl.prefix_str()), false));
|
||||
}
|
||||
|
@ -2,22 +2,8 @@ error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<_>`
|
||||
--> $DIR/type-checking-test-1.rs:19:13
|
||||
|
|
||||
LL | let _ = x as &dyn Bar<_>; // Ambiguous
|
||||
| ^^^^^^^^^^^^^^^^ invalid cast
|
||||
|
|
||||
help: consider borrowing the value
|
||||
|
|
||||
LL | let _ = &x as &dyn Bar<_>; // Ambiguous
|
||||
| +
|
||||
| ^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
|
||||
|
||||
error[E0277]: the trait bound `&dyn Foo: Bar<_>` is not satisfied
|
||||
--> $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 previous error
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0277, E0605.
|
||||
For more information about an error, try `rustc --explain E0277`.
|
||||
For more information about this error, try `rustc --explain E0605`.
|
||||
|
@ -18,7 +18,6 @@ fn test_specific(x: &dyn Foo) {
|
||||
fn test_unknown_version(x: &dyn Foo) {
|
||||
let _ = x as &dyn Bar<_>; // Ambiguous
|
||||
//~^ ERROR non-primitive cast
|
||||
//[current]~^^ ERROR the trait bound `&dyn Foo: Bar<_>` is not satisfied
|
||||
}
|
||||
|
||||
fn test_infer_version(x: &dyn Foo) {
|
||||
|
@ -18,13 +18,11 @@ fn test_specific2(x: &dyn Foo<u32>) {
|
||||
fn test_specific3(x: &dyn Foo<i32>) {
|
||||
let _ = x as &dyn Bar<u32>; // Error
|
||||
//~^ ERROR non-primitive cast
|
||||
//~^^ ERROR the trait bound `&dyn Foo<i32>: Bar<u32>` is not satisfied
|
||||
}
|
||||
|
||||
fn test_infer_arg(x: &dyn Foo<u32>) {
|
||||
let a = x as &dyn Bar<_>; // Ambiguous
|
||||
//~^ ERROR non-primitive cast
|
||||
//~^^ ERROR the trait bound `&dyn Foo<u32>: Bar<_>` is not satisfied
|
||||
let _ = a.bar();
|
||||
}
|
||||
|
||||
|
@ -2,41 +2,14 @@ error[E0605]: non-primitive cast: `&dyn Foo<i32>` as `&dyn Bar<u32>`
|
||||
--> $DIR/type-checking-test-2.rs:19:13
|
||||
|
|
||||
LL | let _ = x as &dyn Bar<u32>; // Error
|
||||
| ^^^^^^^^^^^^^^^^^^ invalid cast
|
||||
|
|
||||
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>`
|
||||
| ^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
|
||||
|
||||
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
|
||||
| ^^^^^^^^^^^^^^^^ invalid cast
|
||||
|
|
||||
help: consider borrowing the value
|
||||
|
|
||||
LL | let a = &x as &dyn Bar<_>; // Ambiguous
|
||||
| +
|
||||
| ^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
|
||||
|
||||
error[E0277]: the trait bound `&dyn Foo<u32>: Bar<_>` is not satisfied
|
||||
--> $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 2 previous errors
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0277, E0605.
|
||||
For more information about an error, try `rustc --explain E0277`.
|
||||
For more information about this error, try `rustc --explain E0605`.
|
||||
|
Loading…
x
Reference in New Issue
Block a user