Skip to content

Commit c92b8f5

Browse files
committed
Auto merge of rust-lang#122832 - oli-obk:no_ord_def_id3, r=<try>
Remove `DefId`'s `Partial/Ord` impls work towards rust-lang#90317 based on rust-lang#122824 and rust-lang#122820 r? `@michaelwoerister`
2 parents 2627e9f + e7667c7 commit c92b8f5

File tree

100 files changed

+692
-733
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+692
-733
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4236,6 +4236,7 @@ name = "rustc_middle"
42364236
version = "0.0.0"
42374237
dependencies = [
42384238
"bitflags 2.4.2",
4239+
"derivative",
42394240
"either",
42404241
"field-offset",
42414242
"gsgdt",

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::FxIndexMap;
12
use rustc_infer::infer::outlives::components::{push_outlives_components, Component};
23
use rustc_middle::ty::{self, Region, Ty, TyCtxt};
34
use rustc_middle::ty::{GenericArg, GenericArgKind};
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+
FxIndexMap<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/method/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub struct NoMatchData<'tcx> {
8181

8282
// A pared down enum describing just the places from which a method
8383
// candidate can arise. Used for error reporting only.
84-
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
84+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
8585
pub enum CandidateSource {
8686
Impl(DefId),
8787
Trait(DefId /* trait id */),

compiler/rustc_hir_typeck/src/method/suggest.rs

+12-31
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ use std::borrow::Cow;
4949
use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
5050
use super::{CandidateSource, MethodError, NoMatchData};
5151
use rustc_hir::intravisit::Visitor;
52-
use std::cmp::{self, Ordering};
5352
use std::iter;
5453

5554
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -1189,7 +1188,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11891188
})
11901189
.collect::<Vec<_>>();
11911190
if !inherent_impls_candidate.is_empty() {
1192-
inherent_impls_candidate.sort();
1191+
inherent_impls_candidate.sort_by_key(|id| self.tcx.def_path_str(id));
11931192
inherent_impls_candidate.dedup();
11941193

11951194
// number of types to show at most
@@ -1570,7 +1569,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15701569
sources: &mut Vec<CandidateSource>,
15711570
sugg_span: Option<Span>,
15721571
) {
1573-
sources.sort();
1572+
sources.sort_by_key(|source| match source {
1573+
CandidateSource::Trait(id) => (0, self.tcx.def_path_str(id)),
1574+
CandidateSource::Impl(id) => (1, self.tcx.def_path_str(id)),
1575+
});
15741576
sources.dedup();
15751577
// Dynamic limit to avoid hiding just one candidate, which is silly.
15761578
let limit = if sources.len() == 5 { 5 } else { 4 };
@@ -2552,7 +2554,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25522554
_ => None,
25532555
})
25542556
.collect();
2555-
preds.sort_by_key(|pred| (pred.def_id(), pred.self_ty()));
2557+
preds.sort_by_key(|pred| pred.trait_ref.to_string());
25562558
let def_ids = preds
25572559
.iter()
25582560
.filter_map(|pred| match pred.self_ty().kind() {
@@ -2666,7 +2668,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26662668
traits.push(trait_pred.def_id());
26672669
}
26682670
}
2669-
traits.sort();
2671+
traits.sort_by_key(|id| self.tcx.def_path_str(id));
26702672
traits.dedup();
26712673

26722674
let len = traits.len();
@@ -2889,7 +2891,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28892891
) -> bool {
28902892
if !valid_out_of_scope_traits.is_empty() {
28912893
let mut candidates = valid_out_of_scope_traits;
2892-
candidates.sort();
2894+
candidates.sort_by_key(|id| self.tcx.def_path_str(id));
28932895
candidates.dedup();
28942896

28952897
// `TryFrom` and `FromIterator` have no methods
@@ -3215,8 +3217,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32153217
}
32163218

32173219
if !candidates.is_empty() {
3218-
// Sort from most relevant to least relevant.
3219-
candidates.sort_by_key(|&info| cmp::Reverse(info));
3220+
// Sort local crate results before others
3221+
candidates
3222+
.sort_by_key(|&info| (!info.def_id.is_local(), self.tcx.def_path_str(info.def_id)));
32203223
candidates.dedup();
32213224

32223225
let param_type = match rcvr_ty.kind() {
@@ -3564,33 +3567,11 @@ pub enum SelfSource<'a> {
35643567
MethodCall(&'a hir::Expr<'a> /* rcvr */),
35653568
}
35663569

3567-
#[derive(Copy, Clone)]
3570+
#[derive(Copy, Clone, PartialEq, Eq)]
35683571
pub struct TraitInfo {
35693572
pub def_id: DefId,
35703573
}
35713574

3572-
impl PartialEq for TraitInfo {
3573-
fn eq(&self, other: &TraitInfo) -> bool {
3574-
self.cmp(other) == Ordering::Equal
3575-
}
3576-
}
3577-
impl Eq for TraitInfo {}
3578-
impl PartialOrd for TraitInfo {
3579-
fn partial_cmp(&self, other: &TraitInfo) -> Option<Ordering> {
3580-
Some(self.cmp(other))
3581-
}
3582-
}
3583-
impl Ord for TraitInfo {
3584-
fn cmp(&self, other: &TraitInfo) -> Ordering {
3585-
// Local crates are more important than remote ones (local:
3586-
// `cnum == 0`), and otherwise we throw in the defid for totality.
3587-
3588-
let lhs = (other.def_id.krate, other.def_id);
3589-
let rhs = (self.def_id.krate, self.def_id);
3590-
lhs.cmp(&rhs)
3591-
}
3592-
}
3593-
35943575
/// Retrieves all traits in this crate and any dependent crates,
35953576
/// and wraps them into `TraitInfo` for custom sorting.
35963577
pub fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
10741074
let (sig, reg) = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS)
10751075
.name_all_regions(sig)
10761076
.unwrap();
1077-
let lts: Vec<String> = reg.into_values().map(|kind| kind.to_string()).collect();
1077+
let lts: Vec<String> =
1078+
reg.into_items().map(|(_, kind)| kind.to_string()).into_sorted_stable_ord();
10781079
(if lts.is_empty() { String::new() } else { format!("for<{}> ", lts.join(", ")) }, sig)
10791080
};
10801081

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

+10-8
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
139139
let mut var_data = self.construct_var_data();
140140

141141
// Deduplicating constraints is shown to have a positive perf impact.
142-
self.data.constraints.sort_by_key(|(constraint, _)| *constraint);
143-
self.data.constraints.dedup_by_key(|(constraint, _)| *constraint);
142+
let mut seen = FxHashSet::default();
143+
self.data.constraints.retain(|(constraint, _)| seen.insert(*constraint));
144144

145145
if cfg!(debug_assertions) {
146146
self.dump_constraints();
@@ -888,12 +888,14 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
888888
}
889889

890890
Constraint::RegSubVar(region, _) | Constraint::VarSubReg(_, region) => {
891-
let constraint_idx =
892-
this.constraints.binary_search_by(|(c, _)| c.cmp(&edge.data)).unwrap();
893-
state.result.push(RegionAndOrigin {
894-
region,
895-
origin: this.constraints[constraint_idx].1.clone(),
896-
});
891+
let origin = this
892+
.constraints
893+
.iter()
894+
.find(|(c, _)| *c == edge.data)
895+
.unwrap()
896+
.1
897+
.clone();
898+
state.result.push(RegionAndOrigin { region, origin });
897899
}
898900

899901
Constraint::RegSubReg(..) => panic!(

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub struct RegionConstraintData<'tcx> {
104104
}
105105

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

compiler/rustc_middle/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
[dependencies]
77
# tidy-alphabetical-start
88
bitflags = "2.4.1"
9+
derivative = "2.2.0"
910
either = "1.5.0"
1011
field-offset = "0.3.5"
1112
gsgdt = "0.1.2"

compiler/rustc_middle/src/mir/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ impl<'tcx> Const<'tcx> {
456456
}
457457

458458
/// An unevaluated (potentially generic) constant used in MIR.
459-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
459+
#[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable)]
460460
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable, Lift)]
461461
pub struct UnevaluatedConst<'tcx> {
462462
pub def: DefId,

compiler/rustc_middle/src/mir/query.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,15 @@ rustc_data_structures::static_assert_size!(ConstraintCategory<'_>, 16);
284284
/// order of the category, thereby influencing diagnostic output.
285285
///
286286
/// See also `rustc_const_eval::borrow_check::constraints`.
287-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
287+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
288288
#[derive(TyEncodable, TyDecodable, HashStable, TypeVisitable, TypeFoldable)]
289+
#[derive(derivative::Derivative)]
290+
#[derivative(
291+
PartialOrd,
292+
Ord,
293+
PartialOrd = "feature_allow_slow_enum",
294+
Ord = "feature_allow_slow_enum"
295+
)]
289296
pub enum ConstraintCategory<'tcx> {
290297
Return(ReturnConstraint),
291298
Yield,
@@ -295,6 +302,7 @@ pub enum ConstraintCategory<'tcx> {
295302
Cast {
296303
/// Whether this is an unsizing cast and if yes, this contains the target type.
297304
/// Region variables are erased to ReErased.
305+
#[derivative(PartialOrd = "ignore", Ord = "ignore")]
298306
unsize_to: Option<Ty<'tcx>>,
299307
},
300308

@@ -304,7 +312,7 @@ pub enum ConstraintCategory<'tcx> {
304312
ClosureBounds,
305313

306314
/// Contains the function type if available.
307-
CallArgument(Option<Ty<'tcx>>),
315+
CallArgument(#[derivative(PartialOrd = "ignore", Ord = "ignore")] Option<Ty<'tcx>>),
308316
CopyBound,
309317
SizedBound,
310318
Assignment,

compiler/rustc_middle/src/thir.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,11 @@ impl<'tcx> PatRangeBoundary<'tcx> {
10181018
(Finite(mir::Const::Ty(a)), Finite(mir::Const::Ty(b)))
10191019
if matches!(ty.kind(), ty::Uint(_) | ty::Char) =>
10201020
{
1021-
return Some(a.kind().cmp(&b.kind()));
1021+
if let Some(a) = a.try_to_valtree() {
1022+
if let Some(b) = b.try_to_valtree() {
1023+
return Some(a.cmp(&b));
1024+
}
1025+
}
10221026
}
10231027
(
10241028
Finite(mir::Const::Val(mir::ConstValue::Scalar(Scalar::Int(a)), _)),

compiler/rustc_middle/src/ty/adt.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use rustc_span::symbol::sym;
1818
use rustc_target::abi::{ReprOptions, VariantIdx, FIRST_VARIANT};
1919

2020
use std::cell::RefCell;
21-
use std::cmp::Ordering;
2221
use std::hash::{Hash, Hasher};
2322
use std::ops::Range;
2423
use std::str;
@@ -102,20 +101,6 @@ pub struct AdtDefData {
102101
repr: ReprOptions,
103102
}
104103

105-
impl PartialOrd for AdtDefData {
106-
fn partial_cmp(&self, other: &AdtDefData) -> Option<Ordering> {
107-
Some(self.cmp(other))
108-
}
109-
}
110-
111-
/// There should be only one AdtDef for each `did`, therefore
112-
/// it is fine to implement `Ord` only based on `did`.
113-
impl Ord for AdtDefData {
114-
fn cmp(&self, other: &AdtDefData) -> Ordering {
115-
self.did.cmp(&other.did)
116-
}
117-
}
118-
119104
impl PartialEq for AdtDefData {
120105
#[inline]
121106
fn eq(&self, other: &Self) -> bool {
@@ -180,7 +165,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for AdtDefData {
180165
}
181166
}
182167

183-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, HashStable)]
168+
#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable)]
184169
#[rustc_pass_by_value]
185170
pub struct AdtDef<'tcx>(pub Interned<'tcx, AdtDefData>);
186171

compiler/rustc_middle/src/ty/consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub use valtree::*;
2323
pub type ConstKind<'tcx> = IrConstKind<TyCtxt<'tcx>>;
2424

2525
/// Use this rather than `ConstData`, whenever possible.
26-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)]
26+
#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable)]
2727
#[rustc_pass_by_value]
2828
pub struct Const<'tcx>(pub(super) Interned<'tcx, WithCachedTypeInfo<ConstData<'tcx>>>);
2929

@@ -52,7 +52,7 @@ impl<'tcx> ConstTy<TyCtxt<'tcx>> for Const<'tcx> {
5252
}
5353

5454
/// Typed constant value.
55-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
55+
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
5656
#[derive(HashStable, TyEncodable, TyDecodable)]
5757
pub struct ConstData<'tcx> {
5858
pub ty: Ty<'tcx>,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::def_id::DefId;
77
use rustc_macros::HashStable;
88

99
/// An unevaluated (potentially generic) constant used in the type-system.
10-
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
10+
#[derive(Copy, Clone, Eq, PartialEq, TyEncodable, TyDecodable)]
1111
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
1212
pub struct UnevaluatedConst<'tcx> {
1313
pub def: DefId,
@@ -62,7 +62,7 @@ impl<'tcx> UnevaluatedConst<'tcx> {
6262
}
6363
}
6464

65-
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
65+
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
6666
#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
6767
pub enum Expr<'tcx> {
6868
Binop(mir::BinOp, Const<'tcx>, Const<'tcx>),

compiler/rustc_middle/src/ty/fold.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use crate::ty::{self, Binder, BoundTy, Ty, TyCtxt, TypeVisitableExt};
22
use rustc_data_structures::fx::FxIndexMap;
33
use rustc_hir::def_id::DefId;
44

5-
use std::collections::BTreeMap;
6-
75
pub use rustc_type_ir::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldable};
86

97
///////////////////////////////////////////////////////////////////////////
@@ -254,12 +252,12 @@ impl<'tcx> TyCtxt<'tcx> {
254252
self,
255253
value: Binder<'tcx, T>,
256254
mut fld_r: F,
257-
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
255+
) -> (T, FxIndexMap<ty::BoundRegion, ty::Region<'tcx>>)
258256
where
259257
F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
260258
T: TypeFoldable<TyCtxt<'tcx>>,
261259
{
262-
let mut region_map = BTreeMap::new();
260+
let mut region_map = FxIndexMap::default();
263261
let real_fld_r = |br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
264262
let value = self.instantiate_bound_regions_uncached(value, real_fld_r);
265263
(value, region_map)

compiler/rustc_middle/src/ty/generic_args.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc_type_ir::WithCachedTypeInfo;
1717
use smallvec::SmallVec;
1818

1919
use core::intrinsics;
20-
use std::cmp::Ordering;
2120
use std::marker::PhantomData;
2221
use std::mem;
2322
use std::num::NonZero;
@@ -68,7 +67,7 @@ const TYPE_TAG: usize = 0b00;
6867
const REGION_TAG: usize = 0b01;
6968
const CONST_TAG: usize = 0b10;
7069

71-
#[derive(Debug, TyEncodable, TyDecodable, PartialEq, Eq, PartialOrd, Ord, HashStable)]
70+
#[derive(Debug, TyEncodable, TyDecodable, PartialEq, Eq, HashStable)]
7271
pub enum GenericArgKind<'tcx> {
7372
Lifetime(ty::Region<'tcx>),
7473
Type(Ty<'tcx>),
@@ -100,18 +99,6 @@ impl<'tcx> GenericArgKind<'tcx> {
10099
}
101100
}
102101

103-
impl<'tcx> Ord for GenericArg<'tcx> {
104-
fn cmp(&self, other: &GenericArg<'tcx>) -> Ordering {
105-
self.unpack().cmp(&other.unpack())
106-
}
107-
}
108-
109-
impl<'tcx> PartialOrd for GenericArg<'tcx> {
110-
fn partial_cmp(&self, other: &GenericArg<'tcx>) -> Option<Ordering> {
111-
Some(self.cmp(other))
112-
}
113-
}
114-
115102
impl<'tcx> From<ty::Region<'tcx>> for GenericArg<'tcx> {
116103
#[inline]
117104
fn from(r: ty::Region<'tcx>) -> GenericArg<'tcx> {

0 commit comments

Comments
 (0)