Skip to content

Commit f551d8a

Browse files
authored
Rollup merge of #71051 - ryr3:fix_try_into, r=estebank
Suggest .into() over try_into() when it would work It would be better to suggest x.into() instead, which is shorter, cannot fail, and doesn't require importing a trait. Tests have been added and made up to date. Fixes #70851
2 parents 3f61c73 + 5ffc1ab commit f551d8a

File tree

5 files changed

+289
-69
lines changed

5 files changed

+289
-69
lines changed

src/librustc_typeck/check/demand.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -753,17 +753,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
753753

754754
match (&expected_ty.kind, &checked_ty.kind) {
755755
(&ty::Int(ref exp), &ty::Int(ref found)) => {
756-
let is_fallible = match (found.bit_width(), exp.bit_width()) {
757-
(Some(found), Some(exp)) if found > exp => true,
756+
let is_fallible = match (exp.bit_width(), found.bit_width()) {
757+
(Some(exp), Some(found)) if exp < found => true,
758+
(None, Some(8 | 16)) => false,
758759
(None, _) | (_, None) => true,
759760
_ => false,
760761
};
761762
suggest_to_change_suffix_or_into(err, is_fallible);
762763
true
763764
}
764765
(&ty::Uint(ref exp), &ty::Uint(ref found)) => {
765-
let is_fallible = match (found.bit_width(), exp.bit_width()) {
766-
(Some(found), Some(exp)) if found > exp => true,
766+
let is_fallible = match (exp.bit_width(), found.bit_width()) {
767+
(Some(exp), Some(found)) if exp < found => true,
768+
(None, Some(8 | 16)) => false,
767769
(None, _) | (_, None) => true,
768770
_ => false,
769771
};

src/test/ui/integer-literal-suffix-inference.rs

+58
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fn main() {
1616
fn id_i16(n: i16) -> i16 { n }
1717
fn id_i32(n: i32) -> i32 { n }
1818
fn id_i64(n: i64) -> i64 { n }
19+
fn id_isize(n: isize) -> isize { n }
1920

2021
// the smallest values that need these types
2122
let b8: u8 = 16;
@@ -27,6 +28,11 @@ fn main() {
2728
fn id_u16(n: u16) -> u16 { n }
2829
fn id_u32(n: u32) -> u32 { n }
2930
fn id_u64(n: u64) -> u64 { n }
31+
fn id_usize(n: usize) -> usize { n }
32+
33+
// Values for testing *size
34+
let asize: isize = 1;
35+
let bsize: usize = 3;
3036

3137
id_i8(a8); // ok
3238
id_i8(a16);
@@ -38,6 +44,9 @@ fn main() {
3844
id_i8(a64);
3945
//~^ ERROR mismatched types
4046
//~| expected `i8`, found `i64`
47+
id_i8(asize);
48+
//~^ ERROR mismatched types
49+
//~| expected `i8`, found `isize`
4150

4251
id_i16(a8);
4352
//~^ ERROR mismatched types
@@ -49,6 +58,9 @@ fn main() {
4958
id_i16(a64);
5059
//~^ ERROR mismatched types
5160
//~| expected `i16`, found `i64`
61+
id_i16(asize);
62+
//~^ ERROR mismatched types
63+
//~| expected `i16`, found `isize`
5264

5365
id_i32(a8);
5466
//~^ ERROR mismatched types
@@ -60,6 +72,9 @@ fn main() {
6072
id_i32(a64);
6173
//~^ ERROR mismatched types
6274
//~| expected `i32`, found `i64`
75+
id_i32(asize);
76+
//~^ ERROR mismatched types
77+
//~| expected `i32`, found `isize`
6378

6479
id_i64(a8);
6580
//~^ ERROR mismatched types
@@ -71,6 +86,23 @@ fn main() {
7186
//~^ ERROR mismatched types
7287
//~| expected `i64`, found `i32`
7388
id_i64(a64); // ok
89+
id_i64(asize);
90+
//~^ ERROR mismatched types
91+
//~| expected `i64`, found `isize`
92+
93+
id_isize(a8);
94+
//~^ ERROR mismatched types
95+
//~| expected `isize`, found `i8`
96+
id_isize(a16);
97+
//~^ ERROR mismatched types
98+
//~| expected `isize`, found `i16`
99+
id_isize(a32);
100+
//~^ ERROR mismatched types
101+
//~| expected `isize`, found `i32`
102+
id_isize(a64);
103+
//~^ ERROR mismatched types
104+
//~| expected `isize`, found `i64`
105+
id_isize(asize); //ok
74106

75107
id_i8(c8); // ok
76108
id_i8(c16);
@@ -126,6 +158,9 @@ fn main() {
126158
id_u8(b64);
127159
//~^ ERROR mismatched types
128160
//~| expected `u8`, found `u64`
161+
id_u8(bsize);
162+
//~^ ERROR mismatched types
163+
//~| expected `u8`, found `usize`
129164

130165
id_u16(b8);
131166
//~^ ERROR mismatched types
@@ -137,6 +172,9 @@ fn main() {
137172
id_u16(b64);
138173
//~^ ERROR mismatched types
139174
//~| expected `u16`, found `u64`
175+
id_u16(bsize);
176+
//~^ ERROR mismatched types
177+
//~| expected `u16`, found `usize`
140178

141179
id_u32(b8);
142180
//~^ ERROR mismatched types
@@ -148,6 +186,9 @@ fn main() {
148186
id_u32(b64);
149187
//~^ ERROR mismatched types
150188
//~| expected `u32`, found `u64`
189+
id_u32(bsize);
190+
//~^ ERROR mismatched types
191+
//~| expected `u32`, found `usize`
151192

152193
id_u64(b8);
153194
//~^ ERROR mismatched types
@@ -159,4 +200,21 @@ fn main() {
159200
//~^ ERROR mismatched types
160201
//~| expected `u64`, found `u32`
161202
id_u64(b64); // ok
203+
id_u64(bsize);
204+
//~^ ERROR mismatched types
205+
//~| expected `u64`, found `usize`
206+
207+
id_usize(b8);
208+
//~^ ERROR mismatched types
209+
//~| expected `usize`, found `u8`
210+
id_usize(b16);
211+
//~^ ERROR mismatched types
212+
//~| expected `usize`, found `u16`
213+
id_usize(b32);
214+
//~^ ERROR mismatched types
215+
//~| expected `usize`, found `u32`
216+
id_usize(b64);
217+
//~^ ERROR mismatched types
218+
//~| expected `usize`, found `u64`
219+
id_usize(bsize); //ok
162220
}

0 commit comments

Comments
 (0)