From d0db3279ab251db3612204cc1e3f386e8beae996 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 14 Dec 2022 18:00:56 +0000 Subject: [PATCH] Don't bug if we're trying to cast dyn* to a nother type --- compiler/rustc_hir_typeck/src/cast.rs | 4 +++- src/test/ui/dyn-star/dyn-to-rigid.rs | 11 +++++++++++ src/test/ui/dyn-star/dyn-to-rigid.stderr | 9 +++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/dyn-star/dyn-to-rigid.rs create mode 100644 src/test/ui/dyn-star/dyn-to-rigid.stderr diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index b050ad20afb..042a50f2fd4 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -847,13 +847,15 @@ impl<'a, 'tcx> CastCheck<'tcx> { (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 index 00000000000..e80ee15902e --- /dev/null +++ b/src/test/ui/dyn-star/dyn-to-rigid.rs @@ -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 index 00000000000..588e6d97e5c --- /dev/null +++ b/src/test/ui/dyn-star/dyn-to-rigid.stderr @@ -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`.