about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSamrat Man Singh <samratmansingh@gmail.com>2020-04-28 00:13:37 +0530
committerSamrat Man Singh <samratmansingh@gmail.com>2020-04-28 00:43:20 +0530
commit1f0a864570d79807c7843ebb995fdcdd384ff3c9 (patch)
tree6076d6f01de52e7b74b75db5fb7eaa6c6d342eb2
parent46ec74e60f238f694b46c976d6217e7cf8d4cf1a (diff)
Suggest `into` instead of `try_into` if possible with int types
If it is possible to convert an integer type into another using
`into`, don't suggest `try_into`. This commit changes the suggested
method to convert from one integer type to another for the following
cases:

- u{n} -> i{m} where n < m
- u8 -> isize
- i{n} -> isize where n <= 16
- u{n} -> usize where n <= 16
-rw-r--r--src/librustc_typeck/check/demand.rs11
-rw-r--r--src/test/ui/suggestions/integer-into.rs17
-rw-r--r--src/test/ui/suggestions/integer-into.stderr43
3 files changed, 70 insertions, 1 deletions
diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs
index c75283e419a..980aefa710f 100644
--- a/src/librustc_typeck/check/demand.rs
+++ b/src/librustc_typeck/check/demand.rs
@@ -767,7 +767,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                     suggest_to_change_suffix_or_into(err, is_fallible);
                     true
                 }
-                (&ty::Int(_), &ty::Uint(_)) | (&ty::Uint(_), &ty::Int(_)) => {
+                (&ty::Int(exp), &ty::Uint(found)) => {
+                    let is_fallible = match (exp.bit_width(), found.bit_width()) {
+                        (Some(exp), Some(found)) if found < exp => false,
+                        (None, Some(found)) if found <= 16 => false,
+                        _ => true
+                    };
+                    suggest_to_change_suffix_or_into(err, is_fallible);
+                    true
+                },
+                (&ty::Uint(_), &ty::Int(_)) => {
                     suggest_to_change_suffix_or_into(err, true);
                     true
                 }
diff --git a/src/test/ui/suggestions/integer-into.rs b/src/test/ui/suggestions/integer-into.rs
new file mode 100644
index 00000000000..409b27a4eab
--- /dev/null
+++ b/src/test/ui/suggestions/integer-into.rs
@@ -0,0 +1,17 @@
+fn main() {
+    let a = 1u8;
+    let _: i64 = a;
+    //~^ ERROR mismatched types
+
+    let b = 1i8;
+    let _: isize = b;
+    //~^ ERROR mismatched types
+
+    let c = 1u8;
+    let _: isize = c;
+    //~^ ERROR mismatched types
+
+    let d = 1u8;
+    let _: usize = d;
+    //~^ ERROR mismatched types
+}
diff --git a/src/test/ui/suggestions/integer-into.stderr b/src/test/ui/suggestions/integer-into.stderr
new file mode 100644
index 00000000000..a15cf81f71f
--- /dev/null
+++ b/src/test/ui/suggestions/integer-into.stderr
@@ -0,0 +1,43 @@
+error[E0308]: mismatched types
+  --> $DIR/integer-into.rs:3:18
+   |
+LL |     let _: i64 = a;
+   |            ---   ^
+   |            |     |
+   |            |     expected `i64`, found `u8`
+   |            |     help: you can convert an `u8` to `i64`: `a.into()`
+   |            expected due to this
+
+error[E0308]: mismatched types
+  --> $DIR/integer-into.rs:7:20
+   |
+LL |     let _: isize = b;
+   |            -----   ^
+   |            |       |
+   |            |       expected `isize`, found `i8`
+   |            |       help: you can convert an `i8` to `isize`: `b.into()`
+   |            expected due to this
+
+error[E0308]: mismatched types
+  --> $DIR/integer-into.rs:11:20
+   |
+LL |     let _: isize = c;
+   |            -----   ^
+   |            |       |
+   |            |       expected `isize`, found `u8`
+   |            |       help: you can convert an `u8` to `isize`: `c.into()`
+   |            expected due to this
+
+error[E0308]: mismatched types
+  --> $DIR/integer-into.rs:15:20
+   |
+LL |     let _: usize = d;
+   |            -----   ^
+   |            |       |
+   |            |       expected `usize`, found `u8`
+   |            |       help: you can convert an `u8` to `usize`: `d.into()`
+   |            expected due to this
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0308`.