Skip to content

Commit 9d862de

Browse files
committed
Note expr being cast when encounter NonScalar cast error
Signed-off-by: xizheyin <[email protected]>
1 parent a18d29f commit 9d862de

36 files changed

+85
-0
lines changed

compiler/rustc_hir_typeck/src/cast.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,15 @@ impl<'a, 'tcx> CastCheck<'tcx> {
408408
self.expr_ty,
409409
fcx.ty_to_string(self.cast_ty)
410410
);
411+
412+
if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span) {
413+
err.note(format!(
414+
"casting expr `{}` as `{}`",
415+
snippet,
416+
fcx.ty_to_string(self.cast_ty)
417+
));
418+
}
419+
411420
let mut sugg = None;
412421
let mut sugg_mutref = false;
413422
if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {

tests/ui/cast/cast-from-nil.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0605]: non-primitive cast: `()` as `u32`
33
|
44
LL | fn main() { let u = (assert!(true) as u32); }
55
| ^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
6+
|
7+
= note: casting expr `assert!(true)` as `u32`
68

79
error: aborting due to 1 previous error
810

tests/ui/cast/cast-to-bare-fn.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ error[E0605]: non-primitive cast: `fn(isize) {foo}` as `extern "C" fn() -> isize
33
|
44
LL | let x = foo as extern "C" fn() -> isize;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
6+
|
7+
= note: casting expr `foo` as `extern "C" fn() -> isize`
68

79
error[E0605]: non-primitive cast: `u64` as `fn(isize) -> (isize, isize)`
810
--> $DIR/cast-to-bare-fn.rs:7:13
911
|
1012
LL | let y = v as extern "Rust" fn(isize) -> (isize, isize);
1113
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
14+
|
15+
= note: casting expr `v` as `fn(isize) -> (isize, isize)`
1216

1317
error: aborting due to 2 previous errors
1418

tests/ui/cast/cast-to-nil.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0605]: non-primitive cast: `u32` as `()`
33
|
44
LL | fn main() { let u = 0u32 as (); }
55
| ^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
6+
|
7+
= note: casting expr `0u32` as `()`
68

79
error: aborting due to 1 previous error
810

tests/ui/cast/enum-to-numeric-cast.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error[E0605]: non-primitive cast: `NotUnitOnlyOrFieldless` as `isize`
44
LL | let _ = not_unit_only_or_fieldless as isize;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can be used to convert enum types to numeric types only if the enum type is unit-only or field-less
66
|
7+
= note: casting expr `not_unit_only_or_fieldless` as `isize`
78
= note: see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information
89

910
error[E0605]: non-primitive cast: `NotUnitOnlyOrFieldless` as `i32`
@@ -12,6 +13,7 @@ error[E0605]: non-primitive cast: `NotUnitOnlyOrFieldless` as `i32`
1213
LL | let _ = not_unit_only_or_fieldless as i32;
1314
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can be used to convert enum types to numeric types only if the enum type is unit-only or field-less
1415
|
16+
= note: casting expr `not_unit_only_or_fieldless` as `i32`
1517
= note: see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information
1618

1719
error[E0605]: non-primitive cast: `NotUnitOnlyOrFieldless` as `usize`
@@ -20,6 +22,7 @@ error[E0605]: non-primitive cast: `NotUnitOnlyOrFieldless` as `usize`
2022
LL | let _ = not_unit_only_or_fieldless as usize;
2123
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can be used to convert enum types to numeric types only if the enum type is unit-only or field-less
2224
|
25+
= note: casting expr `not_unit_only_or_fieldless` as `usize`
2326
= note: see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information
2427

2528
error[E0605]: non-primitive cast: `NotUnitOnlyOrFieldless` as `u32`
@@ -28,6 +31,7 @@ error[E0605]: non-primitive cast: `NotUnitOnlyOrFieldless` as `u32`
2831
LL | let _ = not_unit_only_or_fieldless as u32;
2932
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can be used to convert enum types to numeric types only if the enum type is unit-only or field-less
3033
|
34+
= note: casting expr `not_unit_only_or_fieldless` as `u32`
3135
= note: see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information
3236

3337
error: aborting due to 4 previous errors

tests/ui/cast/fat-ptr-cast.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ error[E0605]: non-primitive cast: `Box<[i32]>` as `usize`
3535
|
3636
LL | b as usize;
3737
| ^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
38+
|
39+
= note: casting expr `b` as `usize`
3840

3941
error[E0606]: casting `*const [i32]` as `usize` is invalid
4042
--> $DIR/fat-ptr-cast.rs:15:5

tests/ui/cast/func-pointer-issue-140491.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0605]: non-primitive cast: `&for<'a, 'b> fn(&'a Event<'b>) {my_fn}` as `&
33
|
44
LL | ..._>) = &my_fn as _;
55
| ^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
6+
|
7+
= note: casting expr `&my_fn` as `&for<'a, 'b> fn(&'a Event<'b>)`
68

79
error: aborting due to 1 previous error
810

tests/ui/cast/issue-106883-is-empty.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ error[E0605]: non-primitive cast: `String` as `bool`
1616
LL | let _ = String::from("foo") as bool;
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
1818
|
19+
= note: casting expr `String::from("foo")` as `bool`
1920
note: this expression `Deref`s to `str` which implements `is_empty`
2021
--> $DIR/issue-106883-is-empty.rs:17:13
2122
|
@@ -33,6 +34,7 @@ error[E0605]: non-primitive cast: `Foo` as `bool`
3334
LL | let _ = Foo as bool;
3435
| ^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
3536
|
37+
= note: casting expr `Foo` as `bool`
3638
note: this expression `Deref`s to `[u8]` which implements `is_empty`
3739
--> $DIR/issue-106883-is-empty.rs:20:13
3840
|

tests/ui/cast/issue-10991.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0605]: non-primitive cast: `()` as `usize`
33
|
44
LL | let _t = nil as usize;
55
| ^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
6+
|
7+
= note: casting expr `nil` as `usize`
68

79
error: aborting due to 1 previous error
810

tests/ui/cast/issue-84213.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error[E0605]: non-primitive cast: `Something` as `*const Something`
44
LL | let _pointer_to_something = something as *const Something;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
66
|
7+
= note: casting expr `something` as `*const Something`
78
help: consider borrowing the value
89
|
910
LL | let _pointer_to_something = &something as *const Something;
@@ -15,6 +16,7 @@ error[E0605]: non-primitive cast: `Something` as `*mut Something`
1516
LL | let _mut_pointer_to_something = something as *mut Something;
1617
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
1718
|
19+
= note: casting expr `something` as `*mut Something`
1820
help: consider borrowing the value
1921
|
2022
LL | let _mut_pointer_to_something = &mut something as *mut Something;

0 commit comments

Comments
 (0)