-
Notifications
You must be signed in to change notification settings - Fork 13k
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
implement Ord for OutlivesPredicate and other types #50930
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ use util::nodemap::{NodeSet, DefIdMap, FxHashMap}; | |
|
||
use serialize::{self, Encodable, Encoder}; | ||
use std::cell::RefCell; | ||
use std::cmp; | ||
use std::cmp::{self, Ordering}; | ||
use std::fmt; | ||
use std::hash::{Hash, Hasher}; | ||
use std::ops::Deref; | ||
|
@@ -491,6 +491,18 @@ pub struct TyS<'tcx> { | |
region_depth: u32, | ||
} | ||
|
||
impl<'tcx> Ord for TyS<'tcx> { | ||
fn cmp(&self, other: &TyS<'tcx>) -> Ordering { | ||
self.sty.cmp(&other.sty) | ||
} | ||
} | ||
|
||
impl<'tcx> PartialOrd for TyS<'tcx> { | ||
fn partial_cmp(&self, other: &TyS<'tcx>) -> Option<Ordering> { | ||
Some(self.sty.cmp(&other.sty)) | ||
} | ||
} | ||
|
||
impl<'tcx> PartialEq for TyS<'tcx> { | ||
#[inline] | ||
fn eq(&self, other: &TyS<'tcx>) -> bool { | ||
|
@@ -578,6 +590,22 @@ impl <'gcx: 'tcx, 'tcx> Canonicalize<'gcx, 'tcx> for Ty<'tcx> { | |
#[derive(Debug, RustcEncodable)] | ||
pub struct Slice<T>([T]); | ||
|
||
impl<T> Ord for Slice<T> where T: Ord { | ||
fn cmp(&self, other: &Slice<T>) -> Ordering { | ||
if self == other { Ordering::Equal } else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice optimization! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. credit goes to @nikomatsakis |
||
<[T] as Ord>::cmp(&self.0, &other.0) | ||
} | ||
} | ||
} | ||
|
||
impl<T> PartialOrd for Slice<T> where T: PartialOrd { | ||
fn partial_cmp(&self, other: &Slice<T>) -> Option<Ordering> { | ||
if self == other { Some(Ordering::Equal) } else { | ||
<[T] as PartialOrd>::partial_cmp(&self.0, &other.0) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I somehow missed that this was generic - seems fine, then. |
||
} | ||
} | ||
|
||
impl<T> PartialEq for Slice<T> { | ||
#[inline] | ||
fn eq(&self, other: &Slice<T>) -> bool { | ||
|
@@ -1104,7 +1132,7 @@ impl<'tcx> PolyTraitPredicate<'tcx> { | |
} | ||
} | ||
|
||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)] | ||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)] | ||
pub struct OutlivesPredicate<A,B>(pub A, pub B); // `A : B` | ||
pub type PolyOutlivesPredicate<A,B> = ty::Binder<OutlivesPredicate<A,B>>; | ||
pub type RegionOutlivesPredicate<'tcx> = OutlivesPredicate<ty::Region<'tcx>, | ||
|
@@ -1606,6 +1634,20 @@ pub struct AdtDef { | |
pub repr: ReprOptions, | ||
} | ||
|
||
impl PartialOrd for AdtDef { | ||
fn partial_cmp(&self, other: &AdtDef) -> Option<Ordering> { | ||
Some(self.cmp(&other)) | ||
} | ||
} | ||
|
||
/// There should be only one AdtDef for each `did`, therefore | ||
/// it is fine to implement `Ord` only based on `did`. | ||
impl Ord for AdtDef { | ||
fn cmp(&self, other: &AdtDef) -> Ordering { | ||
self.did.cmp(&other.did) | ||
} | ||
} | ||
|
||
impl PartialEq for AdtDef { | ||
// AdtDef are always interned and this is part of TyS equality | ||
#[inline] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ use rustc_data_structures::accumulate_vec::AccumulateVec; | |
use rustc_data_structures::array_vec::ArrayVec; | ||
|
||
use core::intrinsics; | ||
use std::cmp::Ordering; | ||
use std::fmt; | ||
use std::marker::PhantomData; | ||
use std::mem; | ||
|
@@ -70,6 +71,28 @@ impl<'tcx> UnpackedKind<'tcx> { | |
} | ||
} | ||
|
||
impl<'tcx> Ord for Kind<'tcx> { | ||
fn cmp(&self, other: &Kind) -> Ordering { | ||
match (self.unpack(), other.unpack()) { | ||
(UnpackedKind::Type(_), UnpackedKind::Lifetime(_)) => Ordering::Greater, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to implement all of this manually, just add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be unresolved? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops missed this one. Here is a pr for that #54074 |
||
|
||
(UnpackedKind::Type(ty1), UnpackedKind::Type(ty2)) => { | ||
ty1.sty.cmp(&ty2.sty) | ||
} | ||
|
||
(UnpackedKind::Lifetime(reg1), UnpackedKind::Lifetime(reg2)) => reg1.cmp(reg2), | ||
|
||
(UnpackedKind::Lifetime(_), UnpackedKind::Type(_)) => Ordering::Less, | ||
} | ||
} | ||
} | ||
|
||
impl<'tcx> PartialOrd for Kind<'tcx> { | ||
fn partial_cmp(&self, other: &Kind) -> Option<Ordering> { | ||
Some(self.cmp(&other)) | ||
} | ||
} | ||
|
||
impl<'tcx> From<ty::Region<'tcx>> for Kind<'tcx> { | ||
fn from(r: ty::Region<'tcx>) -> Kind<'tcx> { | ||
UnpackedKind::Lifetime(r).pack() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -332,7 +332,7 @@ impl AddAssign for Size { | |
/// Each field is a power of two, giving the alignment a maximum value | ||
/// of 2<sup>(2<sup>8</sup> - 1)</sup>, which is limited by LLVM to a | ||
/// maximum capacity of 2<sup>29</sup> or 536870912. | ||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)] | ||
#[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Debug, RustcEncodable, RustcDecodable)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a mistake, because this type does not have a defined ordering (see the I'll try to figure out if I can fix it locally (as part of a PR that would probably remove the need for using this type in most places, by replacing it with one alignment, instead of two). |
||
pub struct Align { | ||
abi_pow2: u8, | ||
pref_pow2: u8, | ||
|
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.
This could be
Some(self.cmp(other))
, to keep the implementation details in theOrd
impl.