]> git.lizzy.rs Git - rust.git/commitdiff
Don't bug if we're trying to cast dyn* to a nother type
authorMichael Goulet <michael@errs.io>
Wed, 14 Dec 2022 18:00:56 +0000 (18:00 +0000)
committerMichael Goulet <michael@errs.io>
Wed, 14 Dec 2022 18:00:56 +0000 (18:00 +0000)
compiler/rustc_hir_typeck/src/cast.rs
src/test/ui/dyn-star/dyn-to-rigid.rs [new file with mode: 0644]
src/test/ui/dyn-star/dyn-to-rigid.stderr [new file with mode: 0644]

index b050ad20afbdbcca92547e224feda97a9d8af8b8..042a50f2fd42eb89906167fde85ed9b0e4eb1622 100644 (file)
@@ -847,13 +847,15 @@ pub fn do_check(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError> {
 
             (Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
 
-            (_, DynStar) | (DynStar, _) => {
+            (_, DynStar) => {
                 if fcx.tcx.features().dyn_star {
                     bug!("should be handled by `try_coerce`")
                 } else {
                     Err(CastError::IllegalCast)
                 }
             }
+
+            (DynStar, _) => Err(CastError::IllegalCast),
         }
     }
 
diff --git a/src/test/ui/dyn-star/dyn-to-rigid.rs b/src/test/ui/dyn-star/dyn-to-rigid.rs
new file mode 100644 (file)
index 0000000..e80ee15
--- /dev/null
@@ -0,0 +1,11 @@
+#![feature(dyn_star)]
+#![allow(incomplete_features)]
+
+trait Tr {}
+
+fn f(x: dyn* Tr) -> usize {
+    x as usize
+    //~^ ERROR casting `(dyn* Tr + 'static)` as `usize` is invalid
+}
+
+fn main() {}
diff --git a/src/test/ui/dyn-star/dyn-to-rigid.stderr b/src/test/ui/dyn-star/dyn-to-rigid.stderr
new file mode 100644 (file)
index 0000000..588e6d9
--- /dev/null
@@ -0,0 +1,9 @@
+error[E0606]: casting `(dyn* Tr + 'static)` as `usize` is invalid
+  --> $DIR/dyn-to-rigid.rs:7:5
+   |
+LL |     x as usize
+   |     ^^^^^^^^^^
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0606`.