-
Notifications
You must be signed in to change notification settings - Fork 13.4k
make mem::discriminant
const
#69825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
make mem::discriminant
const
#69825
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6d03bbd
constify `mem::discriminant`
lcnr 22f2385
prevent potential promotion in const_discriminant
lcnr 6bbb9b8
test discriminant of enum with uninhabited variant
lcnr 4b724e8
allow dead code in discriminant test
lcnr 314da73
discrimant test must not be inlined!
lcnr 7b3e3ff
explain the use of a custom identity function
lcnr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
// run-pass | ||
#![feature(const_discriminant)] | ||
#![allow(dead_code)] | ||
|
||
use std::mem::{discriminant, Discriminant}; | ||
|
||
#[inline(never)] | ||
fn identity<T>(x: T) -> T { x } | ||
lcnr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
enum Test { | ||
A(u8), | ||
B, | ||
C { a: u8, b: u8 }, | ||
} | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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))); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.