forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#69825 - lcnr:discriminant, r=oli-obk
make `mem::discriminant` const implements rust-lang#69821, which could be used as a tracking issue for `const_discriminant`. Should this be added to the meta tracking issue rust-lang#57563? @Lokathor
- Loading branch information
Showing
6 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -265,6 +265,7 @@ symbols! { | |
derive, | ||
diagnostic, | ||
direct, | ||
discriminant_value, | ||
doc, | ||
doc_alias, | ||
doc_cfg, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// run-pass | ||
#![feature(const_discriminant)] | ||
#![allow(dead_code)] | ||
|
||
use std::mem::{discriminant, Discriminant}; | ||
|
||
// `discriminant(const_expr)` may get const-propagated. | ||
// As we want to check that const-eval is equal to ordinary exection, | ||
// we wrap `const_expr` with a function which is not const to prevent this. | ||
#[inline(never)] | ||
fn identity<T>(x: T) -> T { x } | ||
|
||
enum Test { | ||
A(u8), | ||
B, | ||
C { a: u8, b: u8 }, | ||
} | ||
|
||
const TEST_A: Discriminant<Test> = discriminant(&Test::A(5)); | ||
const TEST_A_OTHER: Discriminant<Test> = discriminant(&Test::A(17)); | ||
const TEST_B: Discriminant<Test> = discriminant(&Test::B); | ||
|
||
enum Void {} | ||
|
||
enum SingleVariant { | ||
V, | ||
Never(Void), | ||
} | ||
|
||
const TEST_V: Discriminant<SingleVariant> = discriminant(&SingleVariant::V); | ||
|
||
fn main() { | ||
assert_eq!(TEST_A, TEST_A_OTHER); | ||
assert_eq!(TEST_A, discriminant(identity(&Test::A(17)))); | ||
assert_eq!(TEST_B, discriminant(identity(&Test::B))); | ||
assert_ne!(TEST_A, TEST_B); | ||
assert_ne!(TEST_B, discriminant(identity(&Test::C { a: 42, b: 7 }))); | ||
|
||
assert_eq!(TEST_V, discriminant(identity(&SingleVariant::V))); | ||
} |