-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #24270 - pnkfelix:use-disr-val-for-derive-ord, r=brson
Use `discriminant_value` intrinsic for `derive(PartialOrd)` [breaking-change] This is a [breaking-change] because it can change the result of comparison operators when enum discriminants have been explicitly assigned. Notably in a case like: ```rust #[derive(PartialOrd)] enum E { A = 2, B = 1} ``` Under the old deriving, `A < B` held, because `A` came before `B` in the order of declaration. But now we use the ordering according to the provided values, and thus `A > B`. (However, this change is very unlikely to break much, if any, code, since the orderings themselves should all remain well-defined, total, etc.) Fix #15523
- Loading branch information
Showing
10 changed files
with
264 additions
and
33 deletions.
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.