From 009ed88789d5351c763cd6d1056fe2c1a040bf33 Mon Sep 17 00:00:00 2001 From: Evgenii Zheltonozhskii Date: Mon, 20 Mar 2023 16:38:38 +0200 Subject: [PATCH] Add `known-bug` test for typeid unsoundness issue --- .../typeid-equality-by-subtyping.rs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/ui/const-generics/generic_const_exprs/typeid-equality-by-subtyping.rs diff --git a/tests/ui/const-generics/generic_const_exprs/typeid-equality-by-subtyping.rs b/tests/ui/const-generics/generic_const_exprs/typeid-equality-by-subtyping.rs new file mode 100644 index 00000000000..64317b9d39a --- /dev/null +++ b/tests/ui/const-generics/generic_const_exprs/typeid-equality-by-subtyping.rs @@ -0,0 +1,52 @@ +// check-pass +// known-bug: #97156 + +#![feature(const_type_id, generic_const_exprs)] +#![allow(incomplete_features)] + +use std::any::TypeId; +// `One` and `Two` are currently considered equal types, as both +// `One <: Two` and `One :> Two` holds. +type One = for<'a> fn(&'a (), &'a ()); +type Two = for<'a, 'b> fn(&'a (), &'b ()); +trait AssocCt { + const ASSOC: usize; +} +const fn to_usize() -> usize { + const WHAT_A_TYPE: TypeId = TypeId::of::(); + match TypeId::of::() { + WHAT_A_TYPE => 0, + _ => 1000, + } +} +impl AssocCt for T { + const ASSOC: usize = to_usize::(); +} + +trait WithAssoc { + type Assoc; +} +impl WithAssoc<()> for T where [(); ::ASSOC]: { + type Assoc = [u8; ::ASSOC]; +} + +fn generic(x: >::Assoc) -> >::Assoc +where + [(); ::ASSOC]:, + T: WithAssoc, +{ + x +} + + +fn unsound(x: >::Assoc) -> >::Assoc +where + One: WithAssoc, +{ + let x: >::Assoc = generic::(x); + x +} + +fn main() { + println!("{:?}", unsound::<()>([])); +}