From 36b1f4411db4a35fbbeaec58f0603e4e086e851f Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 22 Jan 2024 17:21:57 +0000 Subject: [PATCH] Add a test for `*const Tr` to `*const Tr` casts --- .../cast/ptr-to-trait-obj-different-args.rs | 30 +++++++++++++++++++ .../ptr-to-trait-obj-different-args.stderr | 11 +++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/ui/cast/ptr-to-trait-obj-different-args.rs create mode 100644 tests/ui/cast/ptr-to-trait-obj-different-args.stderr diff --git a/tests/ui/cast/ptr-to-trait-obj-different-args.rs b/tests/ui/cast/ptr-to-trait-obj-different-args.rs new file mode 100644 index 00000000000..2cb661ba016 --- /dev/null +++ b/tests/ui/cast/ptr-to-trait-obj-different-args.rs @@ -0,0 +1,30 @@ +// check-fail +// +// issue: + + +trait A {} +impl A for T {} +trait B {} +impl B for T {} + +trait Trait {} +struct X; +impl Trait for T {} +struct Y; +impl Trait for T {} + +fn main() { + let a: *const dyn A = &(); + let b: *const dyn B = a as _; //~ error: casting `*const dyn A` as `*const dyn B` is invalid + + let x: *const dyn Trait = &(); + let y: *const dyn Trait = x as _; + + _ = (b, y); +} + +fn generic(x: *const dyn Trait, t: *const dyn Trait) { + let _: *const dyn Trait = x as _; + let _: *const dyn Trait = t as _; +} diff --git a/tests/ui/cast/ptr-to-trait-obj-different-args.stderr b/tests/ui/cast/ptr-to-trait-obj-different-args.stderr new file mode 100644 index 00000000000..14e8a48ffd9 --- /dev/null +++ b/tests/ui/cast/ptr-to-trait-obj-different-args.stderr @@ -0,0 +1,11 @@ +error[E0606]: casting `*const dyn A` as `*const dyn B` is invalid + --> $DIR/ptr-to-trait-obj-different-args.rs:19:27 + | +LL | let b: *const dyn B = a as _; + | ^^^^^^ + | + = note: vtable kinds may not match + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0606`.