about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorDavid Wood <david@davidtw.co>2020-07-02 14:13:39 +0100
committerDavid Wood <david@davidtw.co>2020-07-02 16:24:09 +0100
commitbddb266089deb0cb879caeff1bb9aa860746e062 (patch)
tree3597b0358ef4d06ddd246ae581365dbcb2af3c57 /src
parent9491f18c5de3ff1c4bf9c3fdacf52d9859e26f7c (diff)
downloadrust-bddb266089deb0cb879caeff1bb9aa860746e062.tar.gz
rust-bddb266089deb0cb879caeff1bb9aa860746e062.zip
typeck: check for infer before type impls trait
This commit checks that the target type of the cast (an error related
to which is being reported) does not have types to be inferred before
checking if it implements the `From` trait.

Signed-off-by: David Wood <david@davidtw.co>
Diffstat (limited to 'src')
-rw-r--r--src/librustc_typeck/check/cast.rs1
-rw-r--r--src/test/ui/issues/issue-73886.rs6
-rw-r--r--src/test/ui/issues/issue-73886.stderr15
3 files changed, 22 insertions, 0 deletions
diff --git a/src/librustc_typeck/check/cast.rs b/src/librustc_typeck/check/cast.rs
index 1ea7bf25ef2..8948e5a3e00 100644
--- a/src/librustc_typeck/check/cast.rs
+++ b/src/librustc_typeck/check/cast.rs
@@ -387,6 +387,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
                             // Check for infer types because cases like `Option<{integer}>` would
                             // panic otherwise.
                             if !expr_ty.has_infer_types()
+                                && !ty.has_infer_types()
                                 && fcx.tcx.type_implements_trait((
                                     from_trait,
                                     ty,
diff --git a/src/test/ui/issues/issue-73886.rs b/src/test/ui/issues/issue-73886.rs
new file mode 100644
index 00000000000..2f1ec8c6d62
--- /dev/null
+++ b/src/test/ui/issues/issue-73886.rs
@@ -0,0 +1,6 @@
+fn main() {
+    let _ = &&[0] as &[_];
+    //~^ ERROR non-primitive cast: `&&[i32; 1]` as `&[_]`
+    let _ = 7u32 as Option<_>;
+    //~^ ERROR non-primitive cast: `u32` as `std::option::Option<_>`
+}
diff --git a/src/test/ui/issues/issue-73886.stderr b/src/test/ui/issues/issue-73886.stderr
new file mode 100644
index 00000000000..e8ab7db6b82
--- /dev/null
+++ b/src/test/ui/issues/issue-73886.stderr
@@ -0,0 +1,15 @@
+error[E0605]: non-primitive cast: `&&[i32; 1]` as `&[_]`
+  --> $DIR/issue-73886.rs:2:13
+   |
+LL |     let _ = &&[0] as &[_];
+   |             ^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
+
+error[E0605]: non-primitive cast: `u32` as `std::option::Option<_>`
+  --> $DIR/issue-73886.rs:4:13
+   |
+LL |     let _ = 7u32 as Option<_>;
+   |             ^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0605`.