Skip to content

Commit 4b1297b

Browse files
committed
Suggest try_into when possible
1 parent a55c2eb commit 4b1297b

36 files changed

+3413
-725
lines changed

src/librustc_typeck/check/demand.rs

Lines changed: 121 additions & 208 deletions
Large diffs are not rendered by default.

src/test/ui/associated-types/associated-types-path-2.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error[E0308]: mismatched types
33
|
44
LL | f1(2i32, 4i32);
55
| ^^^^ expected u32, found i32
6+
help: change the type of the numeric literal from `i32` to `u32`
7+
|
8+
LL | f1(2i32, 4u32);
9+
| ^^^^
610

711
error[E0277]: the trait bound `u32: Foo` is not satisfied
812
--> $DIR/associated-types-path-2.rs:29:5
@@ -45,6 +49,10 @@ error[E0308]: mismatched types
4549
|
4650
LL | let _: i32 = f2(2i32);
4751
| ^^^^^^^^ expected i32, found u32
52+
help: you can convert an `u32` to `i32` or panic if it the converted value wouldn't fit
53+
|
54+
LL | let _: i32 = f2(2i32).try_into().unwrap();
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4856

4957
error: aborting due to 6 previous errors
5058

src/test/ui/discrim/discrim-ill-typed.stderr

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,80 @@ error[E0308]: mismatched types
33
|
44
LL | OhNo = 0_u8,
55
| ^^^^ expected i8, found u8
6+
help: change the type of the numeric literal from `u8` to `i8`
7+
|
8+
LL | OhNo = 0_i8,
9+
| ^^^^
610

711
error[E0308]: mismatched types
812
--> $DIR/discrim-ill-typed.rs:30:16
913
|
1014
LL | OhNo = 0_i8,
1115
| ^^^^ expected u8, found i8
16+
help: change the type of the numeric literal from `i8` to `u8`
17+
|
18+
LL | OhNo = 0_u8,
19+
| ^^^^
1220

1321
error[E0308]: mismatched types
1422
--> $DIR/discrim-ill-typed.rs:43:16
1523
|
1624
LL | OhNo = 0_u16,
1725
| ^^^^^ expected i16, found u16
26+
help: change the type of the numeric literal from `u16` to `i16`
27+
|
28+
LL | OhNo = 0_i16,
29+
| ^^^^^
1830

1931
error[E0308]: mismatched types
2032
--> $DIR/discrim-ill-typed.rs:56:16
2133
|
2234
LL | OhNo = 0_i16,
2335
| ^^^^^ expected u16, found i16
36+
help: change the type of the numeric literal from `i16` to `u16`
37+
|
38+
LL | OhNo = 0_u16,
39+
| ^^^^^
2440

2541
error[E0308]: mismatched types
2642
--> $DIR/discrim-ill-typed.rs:69:16
2743
|
2844
LL | OhNo = 0_u32,
2945
| ^^^^^ expected i32, found u32
46+
help: change the type of the numeric literal from `u32` to `i32`
47+
|
48+
LL | OhNo = 0_i32,
49+
| ^^^^^
3050

3151
error[E0308]: mismatched types
3252
--> $DIR/discrim-ill-typed.rs:82:16
3353
|
3454
LL | OhNo = 0_i32,
3555
| ^^^^^ expected u32, found i32
56+
help: change the type of the numeric literal from `i32` to `u32`
57+
|
58+
LL | OhNo = 0_u32,
59+
| ^^^^^
3660

3761
error[E0308]: mismatched types
3862
--> $DIR/discrim-ill-typed.rs:95:16
3963
|
4064
LL | OhNo = 0_u64,
4165
| ^^^^^ expected i64, found u64
66+
help: change the type of the numeric literal from `u64` to `i64`
67+
|
68+
LL | OhNo = 0_i64,
69+
| ^^^^^
4270

4371
error[E0308]: mismatched types
4472
--> $DIR/discrim-ill-typed.rs:108:16
4573
|
4674
LL | OhNo = 0_i64,
4775
| ^^^^^ expected u64, found i64
76+
help: change the type of the numeric literal from `i64` to `u64`
77+
|
78+
LL | OhNo = 0_u64,
79+
| ^^^^^
4880

4981
error: aborting due to 8 previous errors
5082

src/test/ui/float-literal-inference-restrictions.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ error[E0308]: mismatched types
1515
|
1616
LL | let y: f32 = 1f64;
1717
| ^^^^ expected f32, found f64
18+
help: change the type of the numeric literal from `f64` to `f32`
19+
|
20+
LL | let y: f32 = 1f32;
21+
| ^^^^
1822

1923
error: aborting due to 2 previous errors
2024

src/test/ui/indexing-requires-a-uint.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ error[E0308]: mismatched types
1212
|
1313
LL | bar::<isize>(i); // i should not be re-coerced back to an isize
1414
| ^ expected isize, found usize
15+
help: you can convert an `usize` to `isize` or panic if it the converted value wouldn't fit
16+
|
17+
LL | bar::<isize>(i.try_into().unwrap()); // i should not be re-coerced back to an isize
18+
| ^^^^^^^^^^^^^^^^^^^^^
1519

1620
error: aborting due to 2 previous errors
1721

0 commit comments

Comments
 (0)