-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Use discriminant_value intrinsic for derive(PartialOrd) #24270
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
41dd355
Implement `discriminant_value` intrinsic
Aatch 800c5f8
Add test for discriminant_value results
Aatch ea27391
Rebase discriminant_value test. Add case for a specialized repr.
pnkfelix 6118795
Change `derive` expansions to use `discriminant_value` intrinsic.
pnkfelix c44d40e
Test case for new `derive(PartialOrd)` expansion.
pnkfelix afb7acf
Re-add a fixme after some investigation into what's going on.
pnkfelix 781fc90
Incorporate repr-attr into deriving(PartialOrd) to avoid truncation e…
pnkfelix 47016f9
Test case for 64-bit corner cases where truncation occurred before pr…
pnkfelix 847a897
fix some comments.
pnkfelix 05aaad1
Remove pretty-expanded from 2 tests; deriving(Ord) uses unstable intr…
pnkfelix 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 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
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,77 @@ | ||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(core)] | ||
|
||
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 | ||
} | ||
|
||
enum ADT { | ||
First(u32, u32), | ||
Second(u64) | ||
} | ||
|
||
enum NullablePointer { | ||
Something(&'static u32), | ||
Nothing | ||
} | ||
|
||
static CONST : u32 = 0xBEEF; | ||
|
||
pub fn main() { | ||
unsafe { | ||
|
||
assert_eq!(discriminant_value(&CLike1::A), 0); | ||
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), 5); | ||
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), 5); | ||
assert_eq!(discriminant_value(&CLike3::B), 6); | ||
assert_eq!(discriminant_value(&CLike3::C), -1_i8 as u64); | ||
assert_eq!(discriminant_value(&CLike3::D), 0); | ||
|
||
assert_eq!(discriminant_value(&ADT::First(0,0)), 0); | ||
assert_eq!(discriminant_value(&ADT::Second(5)), 1); | ||
|
||
assert_eq!(discriminant_value(&NullablePointer::Nothing), 1); | ||
assert_eq!(discriminant_value(&NullablePointer::Something(&CONST)), 0); | ||
|
||
assert_eq!(discriminant_value(&10), 0); | ||
assert_eq!(discriminant_value(&"test"), 0); | ||
} | ||
} |
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,48 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Issue 15523: derive(PartialOrd) should use the provided | ||
// discriminant values for the derived ordering. | ||
// | ||
// This test is checking corner cases that arise when you have | ||
// 64-bit values in the variants. | ||
|
||
#[derive(PartialEq, PartialOrd)] | ||
#[repr(u64)] | ||
enum Eu64 { | ||
Pos2 = 2, | ||
PosMax = !0, | ||
Pos1 = 1, | ||
} | ||
|
||
#[derive(PartialEq, PartialOrd)] | ||
#[repr(i64)] | ||
enum Ei64 { | ||
Pos2 = 2, | ||
Neg1 = -1, | ||
NegMin = 1 << 63, | ||
PosMax = !(1 << 63), | ||
Pos1 = 1, | ||
} | ||
|
||
fn main() { | ||
assert!(Eu64::Pos2 > Eu64::Pos1); | ||
assert!(Eu64::Pos2 < Eu64::PosMax); | ||
assert!(Eu64::Pos1 < Eu64::PosMax); | ||
|
||
|
||
assert!(Ei64::Pos2 > Ei64::Pos1); | ||
assert!(Ei64::Pos2 > Ei64::Neg1); | ||
assert!(Ei64::Pos1 > Ei64::Neg1); | ||
assert!(Ei64::Pos2 > Ei64::NegMin); | ||
assert!(Ei64::Pos1 > Ei64::NegMin); | ||
assert!(Ei64::Pos2 < Ei64::PosMax); | ||
assert!(Ei64::Pos1 < Ei64::PosMax); | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like there's a chance that this could miss a repr attribute that is applied by a later pass of expansion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very interesting, I'm not even sure how we can handle that ... without some somewhat serious revisions to the expansion infrastructure (e.g. some way to schedule an expansion "at the end" ...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The most conservative solution would be to change the intrinsic to return
(sign, abs-value)
to avoid having to figure out the repr at all, but that does make the comparisons a bit less efficient depending on how smart LLVM can be.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a simpler option. The intrinsic could be:
compare_variant_order<T:Reflect>(t: &T, u: &T)
which would yield a -1,0,1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(well, we may still want the
discriminant_value
intrinsic itself independently, but I am inclined to agree that this is an elegant way to resolve all of the issues here.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yeah, that's a good call.