summaryrefslogtreecommitdiffstats
path: root/tests/ui/enum-discriminant
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui/enum-discriminant')
-rw-r--r--tests/ui/enum-discriminant/actually_not_an_enum-discriminant.rs49
-rw-r--r--tests/ui/enum-discriminant/arbitrary_enum_discriminant-no-repr.rs8
-rw-r--r--tests/ui/enum-discriminant/arbitrary_enum_discriminant-no-repr.stderr9
-rw-r--r--tests/ui/enum-discriminant/arbitrary_enum_discriminant.rs43
-rw-r--r--tests/ui/enum-discriminant/discriminant_size.rs54
-rw-r--r--tests/ui/enum-discriminant/discriminant_size.stderr11
-rw-r--r--tests/ui/enum-discriminant/discriminant_value-wrapper.rs20
-rw-r--r--tests/ui/enum-discriminant/discriminant_value.rs82
-rw-r--r--tests/ui/enum-discriminant/forbidden-discriminant-kind-impl.rs14
-rw-r--r--tests/ui/enum-discriminant/forbidden-discriminant-kind-impl.stderr9
-rw-r--r--tests/ui/enum-discriminant/get_discr.rs114
-rw-r--r--tests/ui/enum-discriminant/issue-104519.rs36
-rw-r--r--tests/ui/enum-discriminant/issue-43398.rs14
-rw-r--r--tests/ui/enum-discriminant/issue-43398.stderr11
-rw-r--r--tests/ui/enum-discriminant/issue-46519.rs30
-rw-r--r--tests/ui/enum-discriminant/issue-51582.rs18
-rw-r--r--tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice-2.rs16
-rw-r--r--tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice-2.stderr11
-rw-r--r--tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice.rs17
-rw-r--r--tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice.stderr21
-rw-r--r--tests/ui/enum-discriminant/issue-70453-polymorphic-ctfe.rs16
-rw-r--r--tests/ui/enum-discriminant/issue-70453-polymorphic-ctfe.stderr11
-rw-r--r--tests/ui/enum-discriminant/issue-70509-partial_eq.rs18
-rw-r--r--tests/ui/enum-discriminant/issue-70509-partial_eq.stderr11
-rw-r--r--tests/ui/enum-discriminant/issue-72554.rs22
-rw-r--r--tests/ui/enum-discriminant/issue-72554.stderr17
-rw-r--r--tests/ui/enum-discriminant/issue-90038.rs21
-rw-r--r--tests/ui/enum-discriminant/niche-prefer-zero.rs14
-rw-r--r--tests/ui/enum-discriminant/niche.rs109
-rw-r--r--tests/ui/enum-discriminant/repr128.rs45
-rw-r--r--tests/ui/enum-discriminant/repr128.stderr11
31 files changed, 882 insertions, 0 deletions
diff --git a/tests/ui/enum-discriminant/actually_not_an_enum-discriminant.rs b/tests/ui/enum-discriminant/actually_not_an_enum-discriminant.rs
new file mode 100644
index 000000000..6a566ab3a
--- /dev/null
+++ b/tests/ui/enum-discriminant/actually_not_an_enum-discriminant.rs
@@ -0,0 +1,49 @@
+// run-pass
+#![feature(core_intrinsics)]
+
+use std::intrinsics::discriminant_value;
+
+struct Zst;
+
+struct Struct {
+ _a: u32,
+}
+
+union Union {
+ _a: u32,
+}
+
+fn check(v: u8) {
+ assert_eq!(v, 0);
+}
+
+pub fn generic<T>()
+where
+ for<'a> T: Fn(&'a isize),
+{
+ let v: Vec<T> = Vec::new();
+ let _: u8 = discriminant_value(&v);
+}
+
+fn main() {
+ // check that we use `u8` as the discriminant value
+ // for everything that is not an enum.
+ check(discriminant_value(&true));
+ check(discriminant_value(&'a'));
+ check(discriminant_value(&7));
+ check(discriminant_value(&7.0));
+ check(discriminant_value(&Zst));
+ check(discriminant_value(&Struct { _a: 7 }));
+ check(discriminant_value(&Union { _a: 7 }));
+ check(discriminant_value(&[7, 77]));
+ check(discriminant_value(&(7 as *const ())));
+ check(discriminant_value(&(7 as *mut ())));
+ check(discriminant_value(&&7));
+ check(discriminant_value(&&mut 7));
+ check(discriminant_value(&check));
+ let fn_ptr: fn(u8) = check;
+ check(discriminant_value(&fn_ptr));
+ let hrtb: for<'a> fn(&'a str) -> &'a str = |x| x;
+ check(discriminant_value(&hrtb));
+ check(discriminant_value(&(7, 77, 777)));
+}
diff --git a/tests/ui/enum-discriminant/arbitrary_enum_discriminant-no-repr.rs b/tests/ui/enum-discriminant/arbitrary_enum_discriminant-no-repr.rs
new file mode 100644
index 000000000..a6e5f70fd
--- /dev/null
+++ b/tests/ui/enum-discriminant/arbitrary_enum_discriminant-no-repr.rs
@@ -0,0 +1,8 @@
+#![crate_type="lib"]
+
+enum Enum {
+//~^ ERROR `#[repr(inttype)]` must be specified
+ Unit = 1,
+ Tuple() = 2,
+ Struct{} = 3,
+}
diff --git a/tests/ui/enum-discriminant/arbitrary_enum_discriminant-no-repr.stderr b/tests/ui/enum-discriminant/arbitrary_enum_discriminant-no-repr.stderr
new file mode 100644
index 000000000..8cee74696
--- /dev/null
+++ b/tests/ui/enum-discriminant/arbitrary_enum_discriminant-no-repr.stderr
@@ -0,0 +1,9 @@
+error[E0732]: `#[repr(inttype)]` must be specified
+ --> $DIR/arbitrary_enum_discriminant-no-repr.rs:3:1
+ |
+LL | enum Enum {
+ | ^^^^^^^^^
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0732`.
diff --git a/tests/ui/enum-discriminant/arbitrary_enum_discriminant.rs b/tests/ui/enum-discriminant/arbitrary_enum_discriminant.rs
new file mode 100644
index 000000000..83e74a6e6
--- /dev/null
+++ b/tests/ui/enum-discriminant/arbitrary_enum_discriminant.rs
@@ -0,0 +1,43 @@
+// run-pass
+#![feature(test)]
+
+extern crate test;
+
+use test::black_box;
+
+#[allow(dead_code)]
+#[repr(u8)]
+enum Enum {
+ Unit = 3,
+ Tuple(u16) = 2,
+ Struct {
+ a: u8,
+ b: u16,
+ } = 1,
+}
+
+impl Enum {
+ const unsafe fn tag(&self) -> u8 {
+ *(self as *const Self as *const u8)
+ }
+}
+
+fn main() {
+ const UNIT: Enum = Enum::Unit;
+ const TUPLE: Enum = Enum::Tuple(5);
+ const STRUCT: Enum = Enum::Struct{a: 7, b: 11};
+
+ // Ensure discriminants are correct during runtime execution
+ assert_eq!(3, unsafe { black_box(UNIT).tag() });
+ assert_eq!(2, unsafe { black_box(TUPLE).tag() });
+ assert_eq!(1, unsafe { black_box(STRUCT).tag() });
+
+ // Ensure discriminants are correct during CTFE
+ const UNIT_TAG: u8 = unsafe { UNIT.tag() };
+ const TUPLE_TAG: u8 = unsafe { TUPLE.tag() };
+ const STRUCT_TAG: u8 = unsafe { STRUCT.tag() };
+
+ assert_eq!(3, UNIT_TAG);
+ assert_eq!(2, TUPLE_TAG);
+ assert_eq!(1, STRUCT_TAG);
+}
diff --git a/tests/ui/enum-discriminant/discriminant_size.rs b/tests/ui/enum-discriminant/discriminant_size.rs
new file mode 100644
index 000000000..b939a70df
--- /dev/null
+++ b/tests/ui/enum-discriminant/discriminant_size.rs
@@ -0,0 +1,54 @@
+// run-pass
+#![feature(core_intrinsics, repr128)]
+//~^ WARN the feature `repr128` is incomplete
+
+use std::intrinsics::discriminant_value;
+
+enum E1 {
+ A,
+ B,
+}
+
+#[repr(i8)]
+enum E2 {
+ A = 7,
+ B = -2,
+}
+
+#[repr(C)]
+enum E3 {
+ A = 42,
+ B = 100,
+}
+
+#[repr(i128)]
+enum E4 {
+ A = 0x1223_3445_5667_7889,
+ B = -0x1223_3445_5667_7889,
+}
+
+fn main() {
+ let mut target: [isize; 3] = [0, 0, 0];
+ target[1] = discriminant_value(&E1::A);
+ assert_eq!(target, [0, 0, 0]);
+ target[1] = discriminant_value(&E1::B);
+ assert_eq!(target, [0, 1, 0]);
+
+ let mut target: [i8; 3] = [0, 0, 0];
+ target[1] = discriminant_value(&E2::A);
+ assert_eq!(target, [0, 7, 0]);
+ target[1] = discriminant_value(&E2::B);
+ assert_eq!(target, [0, -2, 0]);
+
+ let mut target: [isize; 3] = [0, 0, 0];
+ target[1] = discriminant_value(&E3::A);
+ assert_eq!(target, [0, 42, 0]);
+ target[1] = discriminant_value(&E3::B);
+ assert_eq!(target, [0, 100, 0]);
+
+ let mut target: [i128; 3] = [0, 0, 0];
+ target[1] = discriminant_value(&E4::A);
+ assert_eq!(target, [0, 0x1223_3445_5667_7889, 0]);
+ target[1] = discriminant_value(&E4::B);
+ assert_eq!(target, [0, -0x1223_3445_5667_7889, 0]);
+}
diff --git a/tests/ui/enum-discriminant/discriminant_size.stderr b/tests/ui/enum-discriminant/discriminant_size.stderr
new file mode 100644
index 000000000..9b1505b5c
--- /dev/null
+++ b/tests/ui/enum-discriminant/discriminant_size.stderr
@@ -0,0 +1,11 @@
+warning: the feature `repr128` is incomplete and may not be safe to use and/or cause compiler crashes
+ --> $DIR/discriminant_size.rs:2:29
+ |
+LL | #![feature(core_intrinsics, repr128)]
+ | ^^^^^^^
+ |
+ = note: see issue #56071 <https://github.com/rust-lang/rust/issues/56071> for more information
+ = note: `#[warn(incomplete_features)]` on by default
+
+warning: 1 warning emitted
+
diff --git a/tests/ui/enum-discriminant/discriminant_value-wrapper.rs b/tests/ui/enum-discriminant/discriminant_value-wrapper.rs
new file mode 100644
index 000000000..8e162d5c4
--- /dev/null
+++ b/tests/ui/enum-discriminant/discriminant_value-wrapper.rs
@@ -0,0 +1,20 @@
+// run-pass
+
+#![allow(enum_intrinsics_non_enums)]
+
+use std::mem;
+
+#[allow(unused_tuple_struct_fields)]
+enum ADT {
+ First(u32, u32),
+ Second(u64)
+}
+
+pub fn main() {
+ assert!(mem::discriminant(&ADT::First(0,0)) == mem::discriminant(&ADT::First(1,1)));
+ assert!(mem::discriminant(&ADT::Second(5)) == mem::discriminant(&ADT::Second(6)));
+ assert!(mem::discriminant(&ADT::First(2,2)) != mem::discriminant(&ADT::Second(2)));
+
+ let _ = mem::discriminant(&10);
+ let _ = mem::discriminant(&"test");
+}
diff --git a/tests/ui/enum-discriminant/discriminant_value.rs b/tests/ui/enum-discriminant/discriminant_value.rs
new file mode 100644
index 000000000..f3dfac298
--- /dev/null
+++ b/tests/ui/enum-discriminant/discriminant_value.rs
@@ -0,0 +1,82 @@
+// run-pass
+#![allow(stable_features)]
+#![feature(core, core_intrinsics)]
+
+extern crate core;
+use core::intrinsics::discriminant_value;
+
+enum CLike1 {
+ A,
+ B,
+ C,
+ D
+}
+
+enum CLike2 {
+ A = 5,
+ B = 2,
+ C = 19,
+ D
+}
+
+#[repr(i8)]
+enum CLike3 {
+ A = 5,
+ B,
+ C = -1,
+ D
+}
+
+#[allow(unused_tuple_struct_fields)]
+enum ADT {
+ First(u32, u32),
+ Second(u64)
+}
+
+enum NullablePointer {
+ Something(#[allow(unused_tuple_struct_fields)] &'static u32),
+ Nothing
+}
+
+static CONST : u32 = 0xBEEF;
+
+#[allow(dead_code)]
+#[repr(isize)]
+enum Mixed {
+ Unit = 3,
+ Tuple(u16) = 2,
+ Struct {
+ a: u8,
+ b: u16,
+ } = 1,
+}
+
+pub fn main() {
+ assert_eq!(discriminant_value(&CLike1::A), 0isize);
+ assert_eq!(discriminant_value(&CLike1::B), 1);
+ assert_eq!(discriminant_value(&CLike1::C), 2);
+ assert_eq!(discriminant_value(&CLike1::D), 3);
+
+ assert_eq!(discriminant_value(&CLike2::A), 5isize);
+ assert_eq!(discriminant_value(&CLike2::B), 2);
+ assert_eq!(discriminant_value(&CLike2::C), 19);
+ assert_eq!(discriminant_value(&CLike2::D), 20);
+
+ assert_eq!(discriminant_value(&CLike3::A), 5i8);
+ assert_eq!(discriminant_value(&CLike3::B), 6);
+ assert_eq!(discriminant_value(&CLike3::C), -1);
+ assert_eq!(discriminant_value(&CLike3::D), 0);
+
+ assert_eq!(discriminant_value(&ADT::First(0,0)), 0isize);
+ assert_eq!(discriminant_value(&ADT::Second(5)), 1);
+
+ assert_eq!(discriminant_value(&NullablePointer::Nothing), 1isize);
+ assert_eq!(discriminant_value(&NullablePointer::Something(&CONST)), 0);
+
+ assert_eq!(discriminant_value(&10), 0u8);
+ assert_eq!(discriminant_value(&"test"), 0u8);
+
+ assert_eq!(discriminant_value(&Mixed::Unit), 3isize);
+ assert_eq!(discriminant_value(&Mixed::Tuple(5)), 2);
+ assert_eq!(discriminant_value(&Mixed::Struct{a: 7, b: 11}), 1);
+}
diff --git a/tests/ui/enum-discriminant/forbidden-discriminant-kind-impl.rs b/tests/ui/enum-discriminant/forbidden-discriminant-kind-impl.rs
new file mode 100644
index 000000000..4760ca548
--- /dev/null
+++ b/tests/ui/enum-discriminant/forbidden-discriminant-kind-impl.rs
@@ -0,0 +1,14 @@
+#![feature(discriminant_kind)]
+
+use std::marker::DiscriminantKind;
+
+enum Uninhabited {}
+
+struct NewType;
+
+impl DiscriminantKind for NewType {
+ //~^ ERROR explicit impls for the `DiscriminantKind` trait are not permitted
+ type Discriminant = Uninhabited;
+}
+
+fn main() {}
diff --git a/tests/ui/enum-discriminant/forbidden-discriminant-kind-impl.stderr b/tests/ui/enum-discriminant/forbidden-discriminant-kind-impl.stderr
new file mode 100644
index 000000000..38cfd13b9
--- /dev/null
+++ b/tests/ui/enum-discriminant/forbidden-discriminant-kind-impl.stderr
@@ -0,0 +1,9 @@
+error[E0322]: explicit impls for the `DiscriminantKind` trait are not permitted
+ --> $DIR/forbidden-discriminant-kind-impl.rs:9:1
+ |
+LL | impl DiscriminantKind for NewType {
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of `DiscriminantKind` not allowed
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0322`.
diff --git a/tests/ui/enum-discriminant/get_discr.rs b/tests/ui/enum-discriminant/get_discr.rs
new file mode 100644
index 000000000..71eea4e0f
--- /dev/null
+++ b/tests/ui/enum-discriminant/get_discr.rs
@@ -0,0 +1,114 @@
+// run-pass
+
+// Now that there are several variations on the code generated in
+// `codegen_get_discr`, let's make sure the various cases yield the correct
+// result.
+
+// To get the discriminant of an E<X1> value, there are no shortcuts - we must
+// do the full algorithm.
+#[repr(u8)]
+pub enum X1 {
+ _1 = 1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16,
+ _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32,
+ _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48,
+ _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64,
+ _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, _77, _78, _79, _80,
+ _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91, _92, _93, _94, _95, _96,
+ _97, _98, _99, _100, _101, _102, _103, _104, _105, _106, _107, _108, _109, _110, _111, _112,
+ _113, _114, _115, _116, _117, _118, _119, _120, _121, _122, _123, _124, _125, _126, _127, _128,
+ _129, _130, _131, _132, _133, _134, _135, _136, _137, _138, _139, _140, _141, _142, _143, _144,
+ _145, _146, _147, _148, _149, _150, _151, _152, _153, _154, _155, _156, _157, _158, _159, _160,
+ _161, _162, _163, _164, _165, _166, _167, _168, _169, _170, _171, _172, _173, _174, _175, _176,
+ _177, _178, _179, _180, _181, _182, _183, _184, _185, _186, _187, _188, _189, _190, _191, _192,
+ _193, _194, _195, _196, _197, _198, _199, _200, _201, _202, _203, _204, _205, _206, _207, _208,
+ _209, _210, _211, _212, _213, _214, _215, _216, _217, _218, _219, _220, _221, _222, _223, _224,
+ _225, _226, _227, _228, _229, _230, _231, _232, _233, _234, _235, _236, _237, _238, _239, _240,
+ _241, _242, _243, _244, _245, _246, _247, _248, _249, _250, _251, _252, _253, _254,
+}
+
+#[repr(i8)]
+pub enum X2 {
+ _1 = -1, _2 = 0, _3 = 1,
+}
+
+#[repr(i8)]
+pub enum X3 {
+ _1 = -128, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16,
+ _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32,
+ _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48,
+ _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64,
+ _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, _77, _78, _79, _80,
+ _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91, _92, _93, _94, _95, _96,
+ _97, _98, _99, _100, _101, _102, _103, _104, _105, _106, _107, _108, _109, _110, _111, _112,
+ _113, _114, _115, _116, _117, _118, _119, _120, _121, _122, _123, _124, _125, _126, _127, _128,
+ _129, _130, _131, _132, _133, _134, _135, _136, _137, _138, _139, _140, _141, _142, _143, _144,
+ _145, _146, _147, _148, _149, _150, _151, _152, _153, _154, _155, _156, _157, _158, _159, _160,
+ _161, _162, _163, _164, _165, _166, _167, _168, _169, _170, _171, _172, _173, _174, _175, _176,
+ _177, _178, _179, _180, _181, _182, _183, _184, _185, _186, _187, _188, _189, _190, _191, _192,
+ _193, _194, _195, _196, _197, _198, _199, _200, _201, _202, _203, _204, _205, _206, _207, _208,
+ _209, _210, _211, _212, _213, _214, _215, _216, _217, _218, _219, _220, _221, _222, _223, _224,
+ _225, _226, _227, _228, _229, _230, _231, _232, _233, _234, _235, _236, _237, _238, _239, _240,
+ _241, _242, _243, _244, _245, _246, _247, _248, _249, _250, _251, _252, _253, _254,
+}
+
+#[repr(i8)]
+pub enum X4 {
+ _1 = -126, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16,
+ _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32,
+ _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48,
+ _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64,
+ _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, _77, _78, _79, _80,
+ _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91, _92, _93, _94, _95, _96,
+ _97, _98, _99, _100, _101, _102, _103, _104, _105, _106, _107, _108, _109, _110, _111, _112,
+ _113, _114, _115, _116, _117, _118, _119, _120, _121, _122, _123, _124, _125, _126, _127, _128,
+ _129, _130, _131, _132, _133, _134, _135, _136, _137, _138, _139, _140, _141, _142, _143, _144,
+ _145, _146, _147, _148, _149, _150, _151, _152, _153, _154, _155, _156, _157, _158, _159, _160,
+ _161, _162, _163, _164, _165, _166, _167, _168, _169, _170, _171, _172, _173, _174, _175, _176,
+ _177, _178, _179, _180, _181, _182, _183, _184, _185, _186, _187, _188, _189, _190, _191, _192,
+ _193, _194, _195, _196, _197, _198, _199, _200, _201, _202, _203, _204, _205, _206, _207, _208,
+ _209, _210, _211, _212, _213, _214, _215, _216, _217, _218, _219, _220, _221, _222, _223, _224,
+ _225, _226, _227, _228, _229, _230, _231, _232, _233, _234, _235, _236, _237, _238, _239, _240,
+ _241, _242, _243, _244, _245, _246, _247, _248, _249, _250, _251, _252, _253, _254,
+}
+
+pub enum E<X> {
+ A(X),
+ B,
+ C,
+}
+
+pub fn match_e<X>(e: E<X>) -> u8 {
+ use E::*;
+ match e {
+ A(_) => 0,
+ B => 1,
+ C => 2,
+ }
+}
+
+fn main() {
+ assert_eq!(match_e(E::A(X1::_1)), 0);
+ assert_eq!(match_e(E::A(X1::_2)), 0);
+ assert_eq!(match_e(E::A(X1::_254)), 0);
+ assert_eq!(match_e(E::<X1>::B), 1);
+ assert_eq!(match_e(E::<X1>::C), 2);
+ assert_eq!(match_e(E::A(X2::_1)), 0);
+ assert_eq!(match_e(E::A(X2::_2)), 0);
+ assert_eq!(match_e(E::A(X2::_3)), 0);
+ assert_eq!(match_e(E::<X2>::B), 1);
+ assert_eq!(match_e(E::<X2>::C), 2);
+ assert_eq!(match_e(E::A(X3::_1)), 0);
+ assert_eq!(match_e(E::A(X3::_2)), 0);
+ assert_eq!(match_e(E::A(X3::_254)), 0);
+ assert_eq!(match_e(E::<X3>::B), 1);
+ assert_eq!(match_e(E::<X3>::C), 2);
+ assert_eq!(match_e(E::A(X4::_1)), 0);
+ assert_eq!(match_e(E::A(X4::_2)), 0);
+ assert_eq!(match_e(E::A(X4::_254)), 0);
+ assert_eq!(match_e(E::<X4>::B), 1);
+ assert_eq!(match_e(E::<X4>::C), 2);
+ assert_eq!(match_e(E::A(false)), 0);
+ assert_eq!(match_e(E::A(true)), 0);
+ assert_eq!(match_e(E::<bool>::B), 1);
+ assert_eq!(match_e(E::<bool>::C), 2);
+}
diff --git a/tests/ui/enum-discriminant/issue-104519.rs b/tests/ui/enum-discriminant/issue-104519.rs
new file mode 100644
index 000000000..c4630f76b
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-104519.rs
@@ -0,0 +1,36 @@
+// run-pass
+#![allow(dead_code)]
+
+enum OpenResult {
+ Ok(()),
+ Err(()),
+ TransportErr(TransportErr),
+}
+
+#[repr(i32)]
+enum TransportErr {
+ UnknownMethod = -2,
+}
+
+#[inline(never)]
+fn some_match(result: OpenResult) -> u8 {
+ match result {
+ OpenResult::Ok(()) => 0,
+ _ => 1,
+ }
+}
+
+fn main() {
+ let result = OpenResult::Ok(());
+ assert_eq!(some_match(result), 0);
+
+ let result = OpenResult::Ok(());
+ match result {
+ OpenResult::Ok(()) => (),
+ _ => unreachable!("message a"),
+ }
+ match result {
+ OpenResult::Ok(()) => (),
+ _ => unreachable!("message b"),
+ }
+}
diff --git a/tests/ui/enum-discriminant/issue-43398.rs b/tests/ui/enum-discriminant/issue-43398.rs
new file mode 100644
index 000000000..581db033f
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-43398.rs
@@ -0,0 +1,14 @@
+// run-pass
+
+#![feature(core_intrinsics)]
+#![feature(repr128)]
+//~^ WARN the feature `repr128` is incomplete
+
+#[repr(i128)]
+enum Big { A, B }
+
+fn main() {
+ println!("{} {:?}",
+ std::intrinsics::discriminant_value(&Big::A),
+ std::mem::discriminant(&Big::B));
+}
diff --git a/tests/ui/enum-discriminant/issue-43398.stderr b/tests/ui/enum-discriminant/issue-43398.stderr
new file mode 100644
index 000000000..fc7bbd062
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-43398.stderr
@@ -0,0 +1,11 @@
+warning: the feature `repr128` is incomplete and may not be safe to use and/or cause compiler crashes
+ --> $DIR/issue-43398.rs:4:12
+ |
+LL | #![feature(repr128)]
+ | ^^^^^^^
+ |
+ = note: see issue #56071 <https://github.com/rust-lang/rust/issues/56071> for more information
+ = note: `#[warn(incomplete_features)]` on by default
+
+warning: 1 warning emitted
+
diff --git a/tests/ui/enum-discriminant/issue-46519.rs b/tests/ui/enum-discriminant/issue-46519.rs
new file mode 100644
index 000000000..0567923b7
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-46519.rs
@@ -0,0 +1,30 @@
+// run-pass
+// compile-flags:--test -O
+
+// needs-unwind
+
+#[test]
+#[should_panic(expected = "creating inhabited type")]
+fn test() {
+ FontLanguageOverride::system_font(SystemFont::new());
+}
+
+pub enum FontLanguageOverride {
+ Normal,
+ Override(&'static str),
+ System(SystemFont)
+}
+
+pub enum SystemFont {}
+
+impl FontLanguageOverride {
+ fn system_font(f: SystemFont) -> Self {
+ FontLanguageOverride::System(f)
+ }
+}
+
+impl SystemFont {
+ fn new() -> Self {
+ panic!("creating inhabited type")
+ }
+}
diff --git a/tests/ui/enum-discriminant/issue-51582.rs b/tests/ui/enum-discriminant/issue-51582.rs
new file mode 100644
index 000000000..40a70c623
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-51582.rs
@@ -0,0 +1,18 @@
+// run-pass
+#![feature(core_intrinsics)]
+
+#[repr(i8)]
+pub enum Enum {
+ VariantA,
+ VariantB,
+}
+
+fn make_b() -> Enum { Enum::VariantB }
+
+fn main() {
+ assert_eq!(1, make_b() as i8);
+ assert_eq!(1, make_b() as u8);
+ assert_eq!(1, make_b() as i32);
+ assert_eq!(1, make_b() as u32);
+ assert_eq!(1, std::intrinsics::discriminant_value(&make_b()));
+}
diff --git a/tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice-2.rs b/tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice-2.rs
new file mode 100644
index 000000000..ad9fcc25b
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice-2.rs
@@ -0,0 +1,16 @@
+#![feature(core_intrinsics)]
+
+extern crate core;
+use core::intrinsics::discriminant_value;
+
+#[repr(usize)]
+enum MyWeirdOption<T> {
+ None = 0,
+ Some(T) = std::mem::size_of::<T>(),
+ //~^ ERROR generic parameters may not be used in const operations
+}
+
+fn main() {
+ assert_eq!(discriminant_value(&MyWeirdOption::<u8>::None), 0);
+ assert_eq!(discriminant_value(&MyWeirdOption::Some(0u8)), 1);
+}
diff --git a/tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice-2.stderr b/tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice-2.stderr
new file mode 100644
index 000000000..e4e10468d
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice-2.stderr
@@ -0,0 +1,11 @@
+error: generic parameters may not be used in const operations
+ --> $DIR/issue-70453-generics-in-discr-ice-2.rs:9:35
+ |
+LL | Some(T) = std::mem::size_of::<T>(),
+ | ^ cannot perform const operation using `T`
+ |
+ = note: type parameters may not be used in const expressions
+ = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
+
+error: aborting due to previous error
+
diff --git a/tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice.rs b/tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice.rs
new file mode 100644
index 000000000..a0fb788a5
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice.rs
@@ -0,0 +1,17 @@
+#![feature(core_intrinsics)]
+
+extern crate core;
+use core::intrinsics::discriminant_value;
+
+#[repr(usize)]
+enum MyWeirdOption<T> {
+//~^ ERROR parameter `T` is never used
+ None = 0,
+ Some = std::mem::size_of::<T>(),
+ //~^ ERROR generic parameters may not be used in const operations
+}
+
+fn main() {
+ assert_eq!(discriminant_value(&MyWeirdOption::<u8>::None), 0);
+ assert_eq!(discriminant_value(&MyWeirdOption::<u8>::Some), 1);
+}
diff --git a/tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice.stderr b/tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice.stderr
new file mode 100644
index 000000000..7ea8a3912
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-70453-generics-in-discr-ice.stderr
@@ -0,0 +1,21 @@
+error: generic parameters may not be used in const operations
+ --> $DIR/issue-70453-generics-in-discr-ice.rs:10:32
+ |
+LL | Some = std::mem::size_of::<T>(),
+ | ^ cannot perform const operation using `T`
+ |
+ = note: type parameters may not be used in const expressions
+ = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
+
+error[E0392]: parameter `T` is never used
+ --> $DIR/issue-70453-generics-in-discr-ice.rs:7:20
+ |
+LL | enum MyWeirdOption<T> {
+ | ^ unused parameter
+ |
+ = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
+ = help: if you intended `T` to be a const parameter, use `const T: usize` instead
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0392`.
diff --git a/tests/ui/enum-discriminant/issue-70453-polymorphic-ctfe.rs b/tests/ui/enum-discriminant/issue-70453-polymorphic-ctfe.rs
new file mode 100644
index 000000000..42a062239
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-70453-polymorphic-ctfe.rs
@@ -0,0 +1,16 @@
+#![feature(core_intrinsics)]
+
+extern crate core;
+use core::intrinsics::discriminant_value;
+
+#[repr(usize)]
+enum MyWeirdOption<T> {
+ None = 0,
+ Some(T) = core::mem::size_of::<*mut T>(),
+ //~^ ERROR generic parameters may not be used
+}
+
+fn main() {
+ assert_eq!(discriminant_value(&MyWeirdOption::<()>::None), 0);
+ assert_eq!(discriminant_value(&MyWeirdOption::Some(())), core::mem::size_of::<usize>());
+}
diff --git a/tests/ui/enum-discriminant/issue-70453-polymorphic-ctfe.stderr b/tests/ui/enum-discriminant/issue-70453-polymorphic-ctfe.stderr
new file mode 100644
index 000000000..0a7a63160
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-70453-polymorphic-ctfe.stderr
@@ -0,0 +1,11 @@
+error: generic parameters may not be used in const operations
+ --> $DIR/issue-70453-polymorphic-ctfe.rs:9:41
+ |
+LL | Some(T) = core::mem::size_of::<*mut T>(),
+ | ^ cannot perform const operation using `T`
+ |
+ = note: type parameters may not be used in const expressions
+ = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
+
+error: aborting due to previous error
+
diff --git a/tests/ui/enum-discriminant/issue-70509-partial_eq.rs b/tests/ui/enum-discriminant/issue-70509-partial_eq.rs
new file mode 100644
index 000000000..3adac7b72
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-70509-partial_eq.rs
@@ -0,0 +1,18 @@
+// run-pass
+#![feature(repr128)]
+//~^ WARN the feature `repr128` is incomplete
+
+#[derive(PartialEq, Debug)]
+#[repr(i128)]
+enum Test {
+ A(Box<u64>) = 0,
+ B(usize) = u64::MAX as i128 + 1,
+}
+
+fn main() {
+ assert_ne!(Test::A(Box::new(2)), Test::B(0));
+ // This previously caused a segfault.
+ //
+ // See https://github.com/rust-lang/rust/issues/70509#issuecomment-620654186
+ // for a detailed explanation.
+}
diff --git a/tests/ui/enum-discriminant/issue-70509-partial_eq.stderr b/tests/ui/enum-discriminant/issue-70509-partial_eq.stderr
new file mode 100644
index 000000000..2eef930c3
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-70509-partial_eq.stderr
@@ -0,0 +1,11 @@
+warning: the feature `repr128` is incomplete and may not be safe to use and/or cause compiler crashes
+ --> $DIR/issue-70509-partial_eq.rs:2:12
+ |
+LL | #![feature(repr128)]
+ | ^^^^^^^
+ |
+ = note: see issue #56071 <https://github.com/rust-lang/rust/issues/56071> for more information
+ = note: `#[warn(incomplete_features)]` on by default
+
+warning: 1 warning emitted
+
diff --git a/tests/ui/enum-discriminant/issue-72554.rs b/tests/ui/enum-discriminant/issue-72554.rs
new file mode 100644
index 000000000..54f7e9ac5
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-72554.rs
@@ -0,0 +1,22 @@
+use std::collections::BTreeSet;
+
+#[derive(Hash)]
+pub enum ElemDerived {
+ //~^ ERROR recursive type `ElemDerived` has infinite size
+ A(ElemDerived)
+}
+
+
+pub enum Elem {
+ Derived(ElemDerived)
+}
+
+pub struct Set(BTreeSet<Elem>);
+
+impl Set {
+ pub fn into_iter(self) -> impl Iterator<Item = Elem> {
+ self.0.into_iter()
+ }
+}
+
+fn main() {}
diff --git a/tests/ui/enum-discriminant/issue-72554.stderr b/tests/ui/enum-discriminant/issue-72554.stderr
new file mode 100644
index 000000000..d12be539f
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-72554.stderr
@@ -0,0 +1,17 @@
+error[E0072]: recursive type `ElemDerived` has infinite size
+ --> $DIR/issue-72554.rs:4:1
+ |
+LL | pub enum ElemDerived {
+ | ^^^^^^^^^^^^^^^^^^^^
+LL |
+LL | A(ElemDerived)
+ | ----------- recursive without indirection
+ |
+help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
+ |
+LL | A(Box<ElemDerived>)
+ | ++++ +
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0072`.
diff --git a/tests/ui/enum-discriminant/issue-90038.rs b/tests/ui/enum-discriminant/issue-90038.rs
new file mode 100644
index 000000000..5e98eccd9
--- /dev/null
+++ b/tests/ui/enum-discriminant/issue-90038.rs
@@ -0,0 +1,21 @@
+// run-pass
+
+#[repr(u32)]
+pub enum Foo {
+ // Greater than or equal to 2
+ A = 2,
+}
+
+pub enum Bar {
+ A(Foo),
+ // More than two const variants
+ B,
+ C,
+}
+
+fn main() {
+ match Bar::A(Foo::A) {
+ Bar::A(_) => (),
+ _ => unreachable!(),
+ }
+}
diff --git a/tests/ui/enum-discriminant/niche-prefer-zero.rs b/tests/ui/enum-discriminant/niche-prefer-zero.rs
new file mode 100644
index 000000000..f20607a89
--- /dev/null
+++ b/tests/ui/enum-discriminant/niche-prefer-zero.rs
@@ -0,0 +1,14 @@
+// Check that niche selection prefers zero.
+// See https://github.com/rust-lang/rust/pull/87794
+// run-pass
+#[repr(u8)]
+pub enum Size {
+ One = 1,
+ Two = 2,
+ Three = 3,
+}
+
+fn main() {
+ // check that `None` is zero
+ assert_eq!(0, unsafe { std::mem::transmute::<Option<Size>, u8>(None) });
+}
diff --git a/tests/ui/enum-discriminant/niche.rs b/tests/ui/enum-discriminant/niche.rs
new file mode 100644
index 000000000..8d3061050
--- /dev/null
+++ b/tests/ui/enum-discriminant/niche.rs
@@ -0,0 +1,109 @@
+// run-pass
+
+//! Make sure that we read and write enum discriminants correctly for corner cases caused
+//! by layout optimizations.
+
+const OVERFLOW: usize = {
+ // Tests for https://github.com/rust-lang/rust/issues/62138.
+ #[repr(u8)]
+ #[allow(dead_code)]
+ enum WithWraparoundInvalidValues {
+ X = 1,
+ Y = 254,
+ }
+
+ #[allow(dead_code)]
+ enum Foo {
+ A,
+ B,
+ C(WithWraparoundInvalidValues),
+ }
+
+ let x = Foo::B;
+ match x {
+ Foo::B => 0,
+ _ => panic!(),
+ }
+};
+
+const MORE_OVERFLOW: usize = {
+ pub enum Infallible {}
+
+ // The check that the `bool` field of `V1` is encoding a "niche variant"
+ // (i.e. not `V1`, so `V3` or `V4`) used to be mathematically incorrect,
+ // causing valid `V1` values to be interpreted as other variants.
+ #[allow(dead_code)]
+ pub enum E1 {
+ V1 { f: bool },
+ V2 { f: Infallible },
+ V3,
+ V4,
+ }
+
+ // Computing the discriminant used to be done using the niche type (here `u8`,
+ // from the `bool` field of `V1`), overflowing for variants with large enough
+ // indices (`V3` and `V4`), causing them to be interpreted as other variants.
+ #[allow(dead_code)]
+ pub enum E2<X> {
+ V1 { f: bool },
+
+ /*_00*/ _01(X), _02(X), _03(X), _04(X), _05(X), _06(X), _07(X),
+ _08(X), _09(X), _0A(X), _0B(X), _0C(X), _0D(X), _0E(X), _0F(X),
+ _10(X), _11(X), _12(X), _13(X), _14(X), _15(X), _16(X), _17(X),
+ _18(X), _19(X), _1A(X), _1B(X), _1C(X), _1D(X), _1E(X), _1F(X),
+ _20(X), _21(X), _22(X), _23(X), _24(X), _25(X), _26(X), _27(X),
+ _28(X), _29(X), _2A(X), _2B(X), _2C(X), _2D(X), _2E(X), _2F(X),
+ _30(X), _31(X), _32(X), _33(X), _34(X), _35(X), _36(X), _37(X),
+ _38(X), _39(X), _3A(X), _3B(X), _3C(X), _3D(X), _3E(X), _3F(X),
+ _40(X), _41(X), _42(X), _43(X), _44(X), _45(X), _46(X), _47(X),
+ _48(X), _49(X), _4A(X), _4B(X), _4C(X), _4D(X), _4E(X), _4F(X),
+ _50(X), _51(X), _52(X), _53(X), _54(X), _55(X), _56(X), _57(X),
+ _58(X), _59(X), _5A(X), _5B(X), _5C(X), _5D(X), _5E(X), _5F(X),
+ _60(X), _61(X), _62(X), _63(X), _64(X), _65(X), _66(X), _67(X),
+ _68(X), _69(X), _6A(X), _6B(X), _6C(X), _6D(X), _6E(X), _6F(X),
+ _70(X), _71(X), _72(X), _73(X), _74(X), _75(X), _76(X), _77(X),
+ _78(X), _79(X), _7A(X), _7B(X), _7C(X), _7D(X), _7E(X), _7F(X),
+ _80(X), _81(X), _82(X), _83(X), _84(X), _85(X), _86(X), _87(X),
+ _88(X), _89(X), _8A(X), _8B(X), _8C(X), _8D(X), _8E(X), _8F(X),
+ _90(X), _91(X), _92(X), _93(X), _94(X), _95(X), _96(X), _97(X),
+ _98(X), _99(X), _9A(X), _9B(X), _9C(X), _9D(X), _9E(X), _9F(X),
+ _A0(X), _A1(X), _A2(X), _A3(X), _A4(X), _A5(X), _A6(X), _A7(X),
+ _A8(X), _A9(X), _AA(X), _AB(X), _AC(X), _AD(X), _AE(X), _AF(X),
+ _B0(X), _B1(X), _B2(X), _B3(X), _B4(X), _B5(X), _B6(X), _B7(X),
+ _B8(X), _B9(X), _BA(X), _BB(X), _BC(X), _BD(X), _BE(X), _BF(X),
+ _C0(X), _C1(X), _C2(X), _C3(X), _C4(X), _C5(X), _C6(X), _C7(X),
+ _C8(X), _C9(X), _CA(X), _CB(X), _CC(X), _CD(X), _CE(X), _CF(X),
+ _D0(X), _D1(X), _D2(X), _D3(X), _D4(X), _D5(X), _D6(X), _D7(X),
+ _D8(X), _D9(X), _DA(X), _DB(X), _DC(X), _DD(X), _DE(X), _DF(X),
+ _E0(X), _E1(X), _E2(X), _E3(X), _E4(X), _E5(X), _E6(X), _E7(X),
+ _E8(X), _E9(X), _EA(X), _EB(X), _EC(X), _ED(X), _EE(X), _EF(X),
+ _F0(X), _F1(X), _F2(X), _F3(X), _F4(X), _F5(X), _F6(X), _F7(X),
+ _F8(X), _F9(X), _FA(X), _FB(X), _FC(X), _FD(X), _FE(X), _FF(X),
+
+ V3,
+ V4,
+ }
+
+ if let E1::V2 { .. } = (E1::V1 { f: true }) {
+ unreachable!()
+ }
+ if let E1::V1 { .. } = (E1::V1 { f: true }) {
+ } else {
+ unreachable!()
+ }
+
+ if let E2::V1 { .. } = E2::V3::<Infallible> {
+ unreachable!()
+ }
+ if let E2::V3 { .. } = E2::V3::<Infallible> {
+ } else {
+ unreachable!()
+ }
+
+ 0
+};
+
+fn main() {
+ assert_eq!(OVERFLOW, 0);
+ assert_eq!(MORE_OVERFLOW, 0);
+}
diff --git a/tests/ui/enum-discriminant/repr128.rs b/tests/ui/enum-discriminant/repr128.rs
new file mode 100644
index 000000000..00021a07b
--- /dev/null
+++ b/tests/ui/enum-discriminant/repr128.rs
@@ -0,0 +1,45 @@
+// run-pass
+#![feature(repr128, core_intrinsics, discriminant_kind)]
+//~^ WARN the feature `repr128` is incomplete
+
+use std::intrinsics::discriminant_value;
+use std::marker::DiscriminantKind;
+
+#[repr(i128)]
+enum Signed {
+ Zero = 0,
+ Staircase = 0x01_02_03_04_05_06_07_08_09_0a_0b_0c_0d_0e_0f,
+ U64Limit = u64::MAX as i128 + 1,
+ SmallNegative = -1,
+ BigNegative = i128::MIN,
+ Next,
+}
+
+#[repr(u128)]
+enum Unsigned {
+ Zero = 0,
+ Staircase = 0x01_02_03_04_05_06_07_08_09_0a_0b_0c_0d_0e_0f,
+ U64Limit = u64::MAX as u128 + 1,
+ Next,
+}
+
+fn discr<T, U>(v: T, value: U)
+where
+ <T as DiscriminantKind>::Discriminant: PartialEq<U>,
+{
+ assert!(discriminant_value(&v) == value);
+}
+
+fn main() {
+ discr(Signed::Zero, 0);
+ discr(Signed::Staircase, 0x01_02_03_04_05_06_07_08_09_0a_0b_0c_0d_0e_0f);
+ discr(Signed::U64Limit, u64::MAX as i128 + 1);
+ discr(Signed::SmallNegative, -1);
+ discr(Signed::BigNegative, i128::MIN);
+ discr(Signed::Next, i128::MIN + 1);
+
+ discr(Unsigned::Zero, 0);
+ discr(Unsigned::Staircase, 0x01_02_03_04_05_06_07_08_09_0a_0b_0c_0d_0e_0f);
+ discr(Unsigned::U64Limit, u64::MAX as u128 + 1);
+ discr(Unsigned::Next, u64::MAX as u128 + 2);
+}
diff --git a/tests/ui/enum-discriminant/repr128.stderr b/tests/ui/enum-discriminant/repr128.stderr
new file mode 100644
index 000000000..da8d75c11
--- /dev/null
+++ b/tests/ui/enum-discriminant/repr128.stderr
@@ -0,0 +1,11 @@
+warning: the feature `repr128` is incomplete and may not be safe to use and/or cause compiler crashes
+ --> $DIR/repr128.rs:2:12
+ |
+LL | #![feature(repr128, core_intrinsics, discriminant_kind)]
+ | ^^^^^^^
+ |
+ = note: see issue #56071 <https://github.com/rust-lang/rust/issues/56071> for more information
+ = note: `#[warn(incomplete_features)]` on by default
+
+warning: 1 warning emitted
+