Skip to content

Commit f5e1f88

Browse files
WaffleLapkindingxiangfei2009
authored andcommitted
remove implicit total orderings
1 parent 999ac5f commit f5e1f88

File tree

35 files changed

+194
-285
lines changed

35 files changed

+194
-285
lines changed

compiler/rustc_ast/src/ast.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -776,10 +776,9 @@ pub enum PatKind {
776776
MacCall(P<MacCall>),
777777
}
778778

779-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
779+
#[derive(Clone, PartialEq, Eq, Hash, Debug, Copy)]
780780
#[derive(HashStable_Generic, Encodable, Decodable)]
781781
pub enum Mutability {
782-
// N.B. Order is deliberate, so that Not < Mut
783782
Not,
784783
Mut,
785784
}

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -403,19 +403,22 @@ fn try_extract_error_from_region_constraints<'tcx>(
403403
mut region_var_origin: impl FnMut(RegionVid) -> RegionVariableOrigin,
404404
mut universe_of_region: impl FnMut(RegionVid) -> UniverseIndex,
405405
) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> {
406-
let (sub_region, cause) =
407-
region_constraints.constraints.iter().find_map(|(constraint, cause)| {
406+
let (_, _, sub_region, cause) = region_constraints
407+
.constraints
408+
.iter()
409+
.filter_map(|(constraint, cause)| {
408410
match *constraint {
409411
Constraint::RegSubReg(sub, sup) if sup == placeholder_region && sup != sub => {
410-
Some((sub, cause.clone()))
412+
Some((1u8, None, sub, cause.clone()))
411413
}
412414
// FIXME: Should this check the universe of the var?
413415
Constraint::VarSubReg(vid, sup) if sup == placeholder_region => {
414-
Some((infcx.tcx.mk_region(ty::ReVar(vid)), cause.clone()))
416+
Some((0u8, Some(vid), infcx.tcx.mk_region(ty::ReVar(vid)), cause.clone()))
415417
}
416418
_ => None,
417419
}
418-
})?;
420+
})
421+
.min_by_key(|(discr, vid, ..)| (*discr, *vid))?;
419422

420423
debug!(?sub_region, "cause = {:#?}", cause);
421424
let error = match (error_region, *sub_region) {

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
435435

436436
// Check if we can use one of the "nice region errors".
437437
if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
438+
debug!("report_region_error: nice report");
438439
let infer_err = self.infcx.err_ctxt();
439440
let nice = NiceRegionError::new_from_span(&infer_err, cause.span, o, f);
440441
if let Some(diag) = nice.try_report_from_nll() {

compiler/rustc_borrowck/src/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2173,7 +2173,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
21732173
// is in the same SCC or something. In that case, find what
21742174
// appears to be the most interesting point to report to the
21752175
// user via an even more ad-hoc guess.
2176-
categorized_path.sort_by(|p0, p1| p0.category.cmp(&p1.category));
2176+
categorized_path.sort_by_key(|p| p.category.cmp_discr());
21772177
debug!("sorted_path={:#?}", categorized_path);
21782178

21792179
(categorized_path.remove(0), extra_info)

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn
389389
let check_mutbl = |mt_a: ty::TypeAndMut<'tcx>,
390390
mt_b: ty::TypeAndMut<'tcx>,
391391
mk_ptr: &dyn Fn(Ty<'tcx>) -> Ty<'tcx>| {
392-
if mt_a.mutbl < mt_b.mutbl {
392+
if let (hir::Mutability::Not, hir::Mutability::Mut) = (mt_a.mutbl, mt_b.mutbl) {
393393
infcx
394394
.err_ctxt()
395395
.report_mismatched_types(

compiler/rustc_hir_analysis/src/outlives/utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
use rustc_data_structures::fx::FxHashMap;
12
use rustc_infer::infer::outlives::components::{push_outlives_components, Component};
23
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
34
use rustc_middle::ty::{self, Region, Ty, TyCtxt};
45
use rustc_span::Span;
56
use smallvec::smallvec;
6-
use std::collections::BTreeMap;
77

88
/// Tracks the `T: 'a` or `'a: 'a` predicates that we have inferred
99
/// must be added to the struct header.
1010
pub(crate) type RequiredPredicates<'tcx> =
11-
BTreeMap<ty::OutlivesPredicate<GenericArg<'tcx>, ty::Region<'tcx>>, Span>;
11+
FxHashMap<ty::OutlivesPredicate<GenericArg<'tcx>, ty::Region<'tcx>>, Span>;
1212

1313
/// Given a requirement `T: 'a` or `'b: 'a`, deduce the
1414
/// outlives_component and add it to `required_predicates`

compiler/rustc_hir_typeck/src/_match.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -575,5 +575,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
575575
}
576576

577577
fn arms_contain_ref_bindings<'tcx>(arms: &'tcx [hir::Arm<'tcx>]) -> Option<hir::Mutability> {
578-
arms.iter().filter_map(|a| a.pat.contains_explicit_ref_binding()).max()
578+
arms.iter().filter_map(|a| a.pat.contains_explicit_ref_binding()).max_by_key(|m| match m {
579+
hir::Mutability::Not => 0u8,
580+
hir::Mutability::Mut => 1,
581+
})
579582
}

compiler/rustc_hir_typeck/src/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
947947
m_cast: ty::TypeAndMut<'tcx>,
948948
) -> Result<CastKind, CastError> {
949949
// array-ptr-cast: allow mut-to-mut, mut-to-const, const-to-const
950-
if m_expr.mutbl >= m_cast.mutbl {
950+
if m_expr.mutbl == m_cast.mutbl || m_expr.mutbl == hir::Mutability::Mut {
951951
if let ty::Array(ety, _) = m_expr.ty.kind() {
952952
// Due to the limitations of LLVM global constants,
953953
// region pointers end up pointing at copies of

compiler/rustc_hir_typeck/src/coercion.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ fn coerce_mutbls<'tcx>(
110110
from_mutbl: hir::Mutability,
111111
to_mutbl: hir::Mutability,
112112
) -> RelateResult<'tcx, ()> {
113-
if from_mutbl >= to_mutbl { Ok(()) } else { Err(TypeError::Mutability) }
113+
if from_mutbl == to_mutbl || from_mutbl == hir::Mutability::Mut {
114+
Ok(())
115+
} else {
116+
Err(TypeError::Mutability)
117+
}
114118
}
115119

116120
/// Do not require any adjustments, i.e. coerce `x -> x`.

compiler/rustc_hir_typeck/src/demand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14201420
(
14211421
sp,
14221422
format!("{}{derefs}", if mutbl_a != mutbl_b { mutbl_b.prefix_str() } else { "" }),
1423-
if mutbl_b <= mutbl_a { Applicability::MachineApplicable } else { Applicability::MaybeIncorrect }
1423+
if mutbl_b == mutbl_a || mutbl_b == hir::Mutability::Not { Applicability::MachineApplicable } else { Applicability::MaybeIncorrect }
14241424
)
14251425
});
14261426

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19361936
_ => None,
19371937
})
19381938
.collect();
1939-
preds.sort_by_key(|pred| (pred.def_id(), pred.self_ty()));
1939+
preds.sort_by_key(|pred| pred.def_id());
19401940
let def_ids = preds
19411941
.iter()
19421942
.filter_map(|pred| match pred.self_ty().kind() {

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
6060
pub(super) fn try_report_placeholder_conflict(
6161
&self,
6262
) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> {
63+
debug!(?self.error, "try_report_placeholder_conflict");
6364
match &self.error {
6465
///////////////////////////////////////////////////////////////////////////
6566
// NB. The ordering of cases in this match is very

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,14 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
238238
// later use this to expand across vids.
239239
let mut constraints = IndexVec::from_elem_n(Vec::new(), var_values.values.len());
240240
// Tracks the changed region vids.
241-
let mut changes = Vec::new();
242-
for constraint in self.data.constraints.keys() {
241+
let mut changes = vec![];
242+
let mut input_constraints: Vec<_> = self.data.constraints.keys().collect();
243+
input_constraints.sort_by_key(|c| match c {
244+
Constraint::VarSubVar(..) => 0u8,
245+
Constraint::RegSubVar(..) => 1,
246+
Constraint::VarSubReg(..) | Constraint::RegSubReg(..) => 2,
247+
});
248+
for constraint in input_constraints {
243249
match *constraint {
244250
Constraint::RegSubVar(a_region, b_vid) => {
245251
let b_data = var_values.value_mut(b_vid);

compiler/rustc_infer/src/infer/region_constraints/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use super::{
77
InferCtxtUndoLogs, MiscVariable, RegionVariableOrigin, Rollback, Snapshot, SubregionOrigin,
88
};
99

10-
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
10+
use rustc_data_structures::fx::FxHashMap;
11+
use rustc_data_structures::fx::FxIndexMap;
12+
use rustc_data_structures::fx::FxIndexSet;
1113
use rustc_data_structures::intern::Interned;
1214
use rustc_data_structures::sync::Lrc;
1315
use rustc_data_structures::undo_log::UndoLogs;
@@ -20,7 +22,6 @@ use rustc_middle::ty::{ReLateBound, ReVar};
2022
use rustc_middle::ty::{Region, RegionVid};
2123
use rustc_span::Span;
2224

23-
use std::collections::BTreeMap;
2425
use std::ops::Range;
2526
use std::{cmp, fmt, mem};
2627

@@ -90,7 +91,7 @@ pub type VarInfos = IndexVec<RegionVid, RegionVariableInfo>;
9091
pub struct RegionConstraintData<'tcx> {
9192
/// Constraints of the form `A <= B`, where either `A` or `B` can
9293
/// be a region variable (or neither, as it happens).
93-
pub constraints: BTreeMap<Constraint<'tcx>, SubregionOrigin<'tcx>>,
94+
pub constraints: FxIndexMap<Constraint<'tcx>, SubregionOrigin<'tcx>>,
9495

9596
/// Constraints of the form `R0 member of [R1, ..., Rn]`, meaning that
9697
/// `R0` must be equal to one of the regions `R1..Rn`. These occur
@@ -127,7 +128,7 @@ pub struct RegionConstraintData<'tcx> {
127128
}
128129

129130
/// Represents a constraint that influences the inference process.
130-
#[derive(Clone, Copy, PartialEq, Eq, Debug, PartialOrd, Ord)]
131+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
131132
pub enum Constraint<'tcx> {
132133
/// A region variable is a subregion of another.
133134
VarSubVar(RegionVid, RegionVid),

compiler/rustc_lint/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ impl<'tcx> LateLintPass<'tcx> for MutableTransmutes {
12001200
if let Some((&ty::Ref(_, _, from_mutbl), &ty::Ref(_, _, to_mutbl))) =
12011201
get_transmute_from_to(cx, expr).map(|(ty1, ty2)| (ty1.kind(), ty2.kind()))
12021202
{
1203-
if from_mutbl < to_mutbl {
1203+
if let (hir::Mutability::Not, hir::Mutability::Mut) = (from_mutbl, to_mutbl) {
12041204
cx.emit_spanned_lint(MUTABLE_TRANSMUTES, expr.span, BuiltinMutablesTransmutes);
12051205
}
12061206
}

compiler/rustc_middle/src/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2580,7 +2580,7 @@ impl<'tcx> ConstantKind<'tcx> {
25802580
}
25812581

25822582
/// An unevaluated (potentially generic) constant used in MIR.
2583-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Lift)]
2583+
#[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, Lift)]
25842584
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
25852585
pub struct UnevaluatedConst<'tcx> {
25862586
pub def: ty::WithOptConstParam<DefId>,

compiler/rustc_middle/src/mir/query.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use std::fmt::{self, Debug};
1717

1818
use super::{Field, SourceInfo};
1919

20+
mod order;
21+
2022
#[derive(Copy, Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)]
2123
pub enum UnsafetyViolationKind {
2224
/// Unsafe operation outside `unsafe`.
@@ -336,7 +338,7 @@ rustc_data_structures::static_assert_size!(ConstraintCategory<'_>, 16);
336338
/// order of the category, thereby influencing diagnostic output.
337339
///
338340
/// See also `rustc_const_eval::borrow_check::constraints`.
339-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
341+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
340342
#[derive(TyEncodable, TyDecodable, HashStable, Lift, TypeVisitable, TypeFoldable)]
341343
pub enum ConstraintCategory<'tcx> {
342344
Return(ReturnConstraint),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use super::ConstraintCategory;
2+
3+
impl<'tcx> ConstraintCategory<'tcx> {
4+
pub fn cmp_discr(&self) -> u8 {
5+
use ConstraintCategory::*;
6+
match self {
7+
Return(_) => 0,
8+
Yield => 1,
9+
UseAsConst => 2,
10+
UseAsStatic => 3,
11+
TypeAnnotation => 4,
12+
Cast => 5,
13+
ClosureBounds => 6,
14+
CallArgument(_) => 7,
15+
CopyBound => 8,
16+
SizedBound => 9,
17+
Assignment => 10,
18+
Usage => 11,
19+
OpaqueType => 12,
20+
ClosureUpvar(_) => 13,
21+
Predicate(_) => 14,
22+
Boring => 15,
23+
BoringNoLocation => 16,
24+
Internal => 17,
25+
}
26+
}
27+
}

compiler/rustc_middle/src/ty/consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub use kind::*;
1515
pub use valtree::*;
1616

1717
/// Use this rather than `ConstData, whenever possible.
18-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)]
18+
#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable)]
1919
#[rustc_pass_by_value]
2020
pub struct Const<'tcx>(pub(super) Interned<'tcx, ConstData<'tcx>>);
2121

@@ -29,7 +29,7 @@ impl<'tcx> fmt::Debug for Const<'tcx> {
2929
}
3030

3131
/// Typed constant value.
32-
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, TyEncodable, TyDecodable)]
32+
#[derive(PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
3333
pub struct ConstData<'tcx> {
3434
pub ty: Ty<'tcx>,
3535
pub kind: ConstKind<'tcx>,

compiler/rustc_middle/src/ty/consts/kind.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_target::abi::Size;
1414
use super::ScalarInt;
1515

1616
/// An unevaluated (potentially generic) constant used in the type-system.
17-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Lift)]
17+
#[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, Lift)]
1818
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
1919
pub struct UnevaluatedConst<'tcx> {
2020
pub def: ty::WithOptConstParam<DefId>,
@@ -45,7 +45,7 @@ impl<'tcx> UnevaluatedConst<'tcx> {
4545
}
4646

4747
/// Represents a constant in Rust.
48-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
48+
#[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable)]
4949
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
5050
#[derive(derive_more::From)]
5151
pub enum ConstKind<'tcx> {
@@ -83,7 +83,7 @@ impl<'tcx> From<ty::ConstVid<'tcx>> for ConstKind<'tcx> {
8383
}
8484
}
8585

86-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
86+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
8787
#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
8888
pub enum Expr<'tcx> {
8989
Binop(mir::BinOp, Const<'tcx>, Const<'tcx>),
@@ -131,7 +131,7 @@ impl<'tcx> ConstKind<'tcx> {
131131
}
132132

133133
/// An inference variable for a const, for use in const generics.
134-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)]
134+
#[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, Hash)]
135135
pub enum InferConst<'tcx> {
136136
/// Infer the value of the const.
137137
Var(ty::ConstVid<'tcx>),

compiler/rustc_middle/src/ty/instance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct Instance<'tcx> {
2525
pub substs: SubstsRef<'tcx>,
2626
}
2727

28-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
28+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
2929
#[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable, Lift)]
3030
pub enum InstanceDef<'tcx> {
3131
/// A user-defined callable item.

compiler/rustc_middle/src/ty/list.rs

-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::arena::Arena;
22
use rustc_serialize::{Encodable, Encoder};
33
use std::alloc::Layout;
4-
use std::cmp::Ordering;
54
use std::fmt;
65
use std::hash::{Hash, Hasher};
76
use std::iter;
@@ -137,32 +136,6 @@ impl<T: PartialEq> PartialEq for List<T> {
137136

138137
impl<T: Eq> Eq for List<T> {}
139138

140-
impl<T> Ord for List<T>
141-
where
142-
T: Ord,
143-
{
144-
fn cmp(&self, other: &List<T>) -> Ordering {
145-
// Pointer equality implies list equality (due to the unique contents
146-
// assumption), but the contents must be compared otherwise.
147-
if self == other { Ordering::Equal } else { <[T] as Ord>::cmp(&**self, &**other) }
148-
}
149-
}
150-
151-
impl<T> PartialOrd for List<T>
152-
where
153-
T: PartialOrd,
154-
{
155-
fn partial_cmp(&self, other: &List<T>) -> Option<Ordering> {
156-
// Pointer equality implies list equality (due to the unique contents
157-
// assumption), but the contents must be compared otherwise.
158-
if self == other {
159-
Some(Ordering::Equal)
160-
} else {
161-
<[T] as PartialOrd>::partial_cmp(&**self, &**other)
162-
}
163-
}
164-
}
165-
166139
impl<T> Hash for List<T> {
167140
#[inline]
168141
fn hash<H: Hasher>(&self, s: &mut H) {

0 commit comments

Comments
 (0)