about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-08-25 02:44:59 +0200
committerGitHub <noreply@github.com>2019-08-25 02:44:59 +0200
commited8e13c2cbd7c27aec7666bf3f4a98fe6bc24f2b (patch)
tree8195506d91b345979092522e9ffb370c643bfe7c
parent5761fc759a283b8d8db8b5267fb94f16936e61a0 (diff)
parent1acb53753be395f6b5bd880617ddceaa4bfadcfa (diff)
downloadrust-ed8e13c2cbd7c27aec7666bf3f4a98fe6bc24f2b.tar.gz
rust-ed8e13c2cbd7c27aec7666bf3f4a98fe6bc24f2b.zip
Rollup merge of #63813 - estebank:int-from, r=varkor
Do not suggest `.try_into()` on `i32::from(x)`

Fix #63697.
-rw-r--r--src/librustc_typeck/check/demand.rs24
-rw-r--r--src/test/ui/suggestions/mismatched-types-numeric-from.rs3
-rw-r--r--src/test/ui/suggestions/mismatched-types-numeric-from.stderr9
3 files changed, 36 insertions, 0 deletions
diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs
index de5ba8bc8eb..0efc433341c 100644
--- a/src/librustc_typeck/check/demand.rs
+++ b/src/librustc_typeck/check/demand.rs
@@ -587,6 +587,30 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 return false;
             }
         }
+        if let hir::ExprKind::Call(path, args) = &expr.node {
+            if let (
+                hir::ExprKind::Path(hir::QPath::TypeRelative(base_ty, path_segment)),
+                1,
+            ) = (&path.node, args.len()) {
+                // `expr` is a conversion like `u32::from(val)`, do not suggest anything (#63697).
+                if let (
+                    hir::TyKind::Path(hir::QPath::Resolved(None, base_ty_path)),
+                    sym::from,
+                ) = (&base_ty.node, path_segment.ident.name) {
+                    if let Some(ident) = &base_ty_path.segments.iter().map(|s| s.ident).next() {
+                        match ident.name {
+                            sym::i128 | sym::i64 | sym::i32 | sym::i16 | sym::i8 |
+                            sym::u128 | sym::u64 | sym::u32 | sym::u16 | sym::u8 |
+                            sym::isize | sym::usize
+                            if base_ty_path.segments.len() == 1 => {
+                                return false;
+                            }
+                            _ => {}
+                        }
+                    }
+                }
+            }
+        }
 
         let msg = format!("you can convert an `{}` to `{}`", checked_ty, expected_ty);
         let cast_msg = format!("you can cast an `{} to `{}`", checked_ty, expected_ty);
diff --git a/src/test/ui/suggestions/mismatched-types-numeric-from.rs b/src/test/ui/suggestions/mismatched-types-numeric-from.rs
new file mode 100644
index 00000000000..56549da9c73
--- /dev/null
+++ b/src/test/ui/suggestions/mismatched-types-numeric-from.rs
@@ -0,0 +1,3 @@
+fn main() {
+    let _: u32 = i32::from(0_u8); //~ ERROR mismatched types
+}
diff --git a/src/test/ui/suggestions/mismatched-types-numeric-from.stderr b/src/test/ui/suggestions/mismatched-types-numeric-from.stderr
new file mode 100644
index 00000000000..223b6747322
--- /dev/null
+++ b/src/test/ui/suggestions/mismatched-types-numeric-from.stderr
@@ -0,0 +1,9 @@
+error[E0308]: mismatched types
+  --> $DIR/mismatched-types-numeric-from.rs:2:18
+   |
+LL |     let _: u32 = i32::from(0_u8);
+   |                  ^^^^^^^^^^^^^^^ expected u32, found i32
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.