From 6d03bbd480ef9bef09321fbfdc00229a03091b11 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sun, 8 Mar 2020 14:24:32 +0100 Subject: [PATCH] constify `mem::discriminant` --- src/libcore/intrinsics.rs | 1 + src/libcore/lib.rs | 1 + src/libcore/mem/mod.rs | 3 ++- src/librustc_mir/interpret/intrinsics.rs | 5 +++++ src/librustc_span/symbol.rs | 1 + src/test/ui/consts/const_discriminant.rs | 22 ++++++++++++++++++++++ 6 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/consts/const_discriminant.rs diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs index a889eff75c0..63c5d782e65 100644 --- a/src/libcore/intrinsics.rs +++ b/src/libcore/intrinsics.rs @@ -1852,6 +1852,7 @@ extern "rust-intrinsic" { /// /// The stabilized version of this intrinsic is /// [`std::mem::discriminant`](../../std/mem/fn.discriminant.html) + #[rustc_const_unstable(feature = "const_discriminant", issue = "69821")] pub fn discriminant_value(v: &T) -> u64; /// Rust's "try catch" construct which invokes the function pointer `f` with diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 41fb4a77c7a..cac61c2c674 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -72,6 +72,7 @@ #![feature(concat_idents)] #![feature(const_ascii_ctype_on_intrinsics)] #![feature(const_alloc_layout)] +#![feature(const_discriminant)] #![feature(const_if_match)] #![feature(const_loop)] #![feature(const_checked_int_methods)] diff --git a/src/libcore/mem/mod.rs b/src/libcore/mem/mod.rs index 90144d11dc9..c23d0adab5c 100644 --- a/src/libcore/mem/mod.rs +++ b/src/libcore/mem/mod.rs @@ -864,6 +864,7 @@ impl fmt::Debug for Discriminant { /// assert_ne!(mem::discriminant(&Foo::B(3)), mem::discriminant(&Foo::C(3))); /// ``` #[stable(feature = "discriminant_value", since = "1.21.0")] -pub fn discriminant(v: &T) -> Discriminant { +#[rustc_const_unstable(feature = "const_discriminant", issue = "69821")] +pub const fn discriminant(v: &T) -> Discriminant { Discriminant(intrinsics::discriminant_value(v), PhantomData) } diff --git a/src/librustc_mir/interpret/intrinsics.rs b/src/librustc_mir/interpret/intrinsics.rs index 891afbf437f..1e5ed76c467 100644 --- a/src/librustc_mir/interpret/intrinsics.rs +++ b/src/librustc_mir/interpret/intrinsics.rs @@ -216,6 +216,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { }; self.write_scalar(val, dest)?; } + sym::discriminant_value => { + let place = self.deref_operand(args[0])?; + let discr_val = self.read_discriminant(place.into())?.0; + self.write_scalar(Scalar::from_uint(discr_val, dest.layout.size), dest)?; + } sym::unchecked_shl | sym::unchecked_shr | sym::unchecked_add diff --git a/src/librustc_span/symbol.rs b/src/librustc_span/symbol.rs index c39f9f360c0..0d37e9c2c7b 100644 --- a/src/librustc_span/symbol.rs +++ b/src/librustc_span/symbol.rs @@ -265,6 +265,7 @@ symbols! { derive, diagnostic, direct, + discriminant_value, doc, doc_alias, doc_cfg, diff --git a/src/test/ui/consts/const_discriminant.rs b/src/test/ui/consts/const_discriminant.rs new file mode 100644 index 00000000000..5a32ff29158 --- /dev/null +++ b/src/test/ui/consts/const_discriminant.rs @@ -0,0 +1,22 @@ +// run-pass +#![feature(const_discriminant)] + +use std::mem::{discriminant, Discriminant}; + +enum Test { + A(u8), + B, + C { a: u8, b: u8 }, +} + +const TEST_A: Discriminant = discriminant(&Test::A(5)); +const TEST_A_OTHER: Discriminant = discriminant(&Test::A(17)); +const TEST_B: Discriminant = discriminant(&Test::B); + +fn main() { + assert_eq!(TEST_A, TEST_A_OTHER); + assert_eq!(TEST_A, discriminant(&Test::A(17))); + assert_eq!(TEST_B, discriminant(&Test::B)); + assert_ne!(TEST_A, TEST_B); + assert_ne!(TEST_B, discriminant(&Test::C { a: 42, b: 7 })); +}