Skip to content

Commit 2c6feb5

Browse files
authored
Rollup merge of #96955 - Aaron1011:pretty-print-sort, r=petrochenkov
Remove (transitive) reliance on sorting by DefId in pretty-printer This moves us a step closer to removing the `PartialOrd/`Ord` impls for `DefId`. See #90317
2 parents 97f4d7b + 36ccdbe commit 2c6feb5

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ty::{
55
TypeSuperFoldable,
66
};
77
use rustc_apfloat::ieee::{Double, Single};
8-
use rustc_data_structures::fx::FxHashMap;
8+
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
99
use rustc_data_structures::sso::SsoHashSet;
1010
use rustc_hir as hir;
1111
use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
@@ -779,8 +779,8 @@ pub trait PrettyPrinter<'tcx>:
779779
// by looking up the projections associated with the def_id.
780780
let bounds = self.tcx().bound_explicit_item_bounds(def_id);
781781

782-
let mut traits = BTreeMap::new();
783-
let mut fn_traits = BTreeMap::new();
782+
let mut traits = FxIndexMap::default();
783+
let mut fn_traits = FxIndexMap::default();
784784
let mut is_sized = false;
785785

786786
for predicate in bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) {
@@ -970,11 +970,11 @@ pub trait PrettyPrinter<'tcx>:
970970
&mut self,
971971
trait_ref: ty::PolyTraitRef<'tcx>,
972972
proj_ty: Option<(DefId, ty::Binder<'tcx, Term<'tcx>>)>,
973-
traits: &mut BTreeMap<
973+
traits: &mut FxIndexMap<
974974
ty::PolyTraitRef<'tcx>,
975-
BTreeMap<DefId, ty::Binder<'tcx, Term<'tcx>>>,
975+
FxIndexMap<DefId, ty::Binder<'tcx, Term<'tcx>>>,
976976
>,
977-
fn_traits: &mut BTreeMap<ty::PolyTraitRef<'tcx>, OpaqueFnEntry<'tcx>>,
977+
fn_traits: &mut FxIndexMap<ty::PolyTraitRef<'tcx>, OpaqueFnEntry<'tcx>>,
978978
) {
979979
let trait_def_id = trait_ref.def_id();
980980

@@ -1110,19 +1110,18 @@ pub trait PrettyPrinter<'tcx>:
11101110
// Builtin bounds.
11111111
// FIXME(eddyb) avoid printing twice (needed to ensure
11121112
// that the auto traits are sorted *and* printed via cx).
1113-
let mut auto_traits: Vec<_> =
1114-
predicates.auto_traits().map(|did| (self.tcx().def_path_str(did), did)).collect();
1113+
let mut auto_traits: Vec<_> = predicates.auto_traits().collect();
11151114

11161115
// The auto traits come ordered by `DefPathHash`. While
11171116
// `DefPathHash` is *stable* in the sense that it depends on
11181117
// neither the host nor the phase of the moon, it depends
11191118
// "pseudorandomly" on the compiler version and the target.
11201119
//
1121-
// To avoid that causing instabilities in compiletest
1120+
// To avoid causing instabilities in compiletest
11221121
// output, sort the auto-traits alphabetically.
1123-
auto_traits.sort();
1122+
auto_traits.sort_by_cached_key(|did| self.tcx().def_path_str(*did));
11241123

1125-
for (_, def_id) in auto_traits {
1124+
for def_id in auto_traits {
11261125
if !first {
11271126
p!(" + ");
11281127
}

src/test/ui/associated-types/issue-87261.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,17 @@ fn main() {
8383
//~^ ERROR type mismatch resolving `<impl DerivedTrait as Trait>::Associated == ()`
8484

8585
accepts_trait(returns_opaque_foo());
86-
//~^ ERROR type mismatch resolving `<impl Foo + Trait as Trait>::Associated == ()`
86+
//~^ ERROR type mismatch resolving `<impl Trait + Foo as Trait>::Associated == ()`
8787

8888
accepts_trait(returns_opaque_derived_foo());
89-
//~^ ERROR type mismatch resolving `<impl Foo + DerivedTrait as Trait>::Associated == ()`
89+
//~^ ERROR type mismatch resolving `<impl DerivedTrait + Foo as Trait>::Associated == ()`
9090

9191
accepts_generic_trait(returns_opaque_generic());
9292
//~^ ERROR type mismatch resolving `<impl GenericTrait<()> as GenericTrait<()>>::Associated == ()`
9393

9494
accepts_generic_trait(returns_opaque_generic_foo());
95-
//~^ ERROR type mismatch resolving `<impl Foo + GenericTrait<()> as GenericTrait<()>>::Associated == ()`
95+
//~^ ERROR type mismatch resolving `<impl GenericTrait<()> + Foo as GenericTrait<()>>::Associated == ()`
9696

9797
accepts_generic_trait(returns_opaque_generic_duplicate());
98-
//~^ ERROR type mismatch resolving `<impl GenericTrait<u8> + GenericTrait<()> as GenericTrait<()>>::Associated == ()`
98+
//~^ ERROR type mismatch resolving `<impl GenericTrait<()> + GenericTrait<u8> as GenericTrait<()>>::Associated == ()`
9999
}

src/test/ui/associated-types/issue-87261.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ help: consider constraining the associated type `<impl DerivedTrait as Trait>::A
160160
LL | fn returns_opaque_derived() -> impl DerivedTrait<Associated = ()> + 'static {
161161
| +++++++++++++++++
162162

163-
error[E0271]: type mismatch resolving `<impl Foo + Trait as Trait>::Associated == ()`
163+
error[E0271]: type mismatch resolving `<impl Trait + Foo as Trait>::Associated == ()`
164164
--> $DIR/issue-87261.rs:85:5
165165
|
166166
LL | fn returns_opaque_foo() -> impl Trait + Foo {
@@ -170,18 +170,18 @@ LL | accepts_trait(returns_opaque_foo());
170170
| ^^^^^^^^^^^^^ expected `()`, found associated type
171171
|
172172
= note: expected unit type `()`
173-
found associated type `<impl Foo + Trait as Trait>::Associated`
173+
found associated type `<impl Trait + Foo as Trait>::Associated`
174174
note: required by a bound in `accepts_trait`
175175
--> $DIR/issue-87261.rs:43:27
176176
|
177177
LL | fn accepts_trait<T: Trait<Associated = ()>>(_: T) {}
178178
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_trait`
179-
help: consider constraining the associated type `<impl Foo + Trait as Trait>::Associated` to `()`
179+
help: consider constraining the associated type `<impl Trait + Foo as Trait>::Associated` to `()`
180180
|
181181
LL | fn returns_opaque_foo() -> impl Trait<Associated = ()> + Foo {
182182
| +++++++++++++++++
183183

184-
error[E0271]: type mismatch resolving `<impl Foo + DerivedTrait as Trait>::Associated == ()`
184+
error[E0271]: type mismatch resolving `<impl DerivedTrait + Foo as Trait>::Associated == ()`
185185
--> $DIR/issue-87261.rs:88:5
186186
|
187187
LL | fn returns_opaque_derived_foo() -> impl DerivedTrait + Foo {
@@ -191,8 +191,8 @@ LL | accepts_trait(returns_opaque_derived_foo());
191191
| ^^^^^^^^^^^^^ expected `()`, found associated type
192192
|
193193
= note: expected unit type `()`
194-
found associated type `<impl Foo + DerivedTrait as Trait>::Associated`
195-
= help: consider constraining the associated type `<impl Foo + DerivedTrait as Trait>::Associated` to `()`
194+
found associated type `<impl DerivedTrait + Foo as Trait>::Associated`
195+
= help: consider constraining the associated type `<impl DerivedTrait + Foo as Trait>::Associated` to `()`
196196
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
197197
note: required by a bound in `accepts_trait`
198198
--> $DIR/issue-87261.rs:43:27
@@ -221,7 +221,7 @@ help: consider constraining the associated type `<impl GenericTrait<()> as Gener
221221
LL | fn returns_opaque_generic() -> impl GenericTrait<(), Associated = ()> + 'static {
222222
| +++++++++++++++++
223223

224-
error[E0271]: type mismatch resolving `<impl Foo + GenericTrait<()> as GenericTrait<()>>::Associated == ()`
224+
error[E0271]: type mismatch resolving `<impl GenericTrait<()> + Foo as GenericTrait<()>>::Associated == ()`
225225
--> $DIR/issue-87261.rs:94:5
226226
|
227227
LL | fn returns_opaque_generic_foo() -> impl GenericTrait<()> + Foo {
@@ -231,18 +231,18 @@ LL | accepts_generic_trait(returns_opaque_generic_foo());
231231
| ^^^^^^^^^^^^^^^^^^^^^ expected `()`, found associated type
232232
|
233233
= note: expected unit type `()`
234-
found associated type `<impl Foo + GenericTrait<()> as GenericTrait<()>>::Associated`
234+
found associated type `<impl GenericTrait<()> + Foo as GenericTrait<()>>::Associated`
235235
note: required by a bound in `accepts_generic_trait`
236236
--> $DIR/issue-87261.rs:44:46
237237
|
238238
LL | fn accepts_generic_trait<T: GenericTrait<(), Associated = ()>>(_: T) {}
239239
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_generic_trait`
240-
help: consider constraining the associated type `<impl Foo + GenericTrait<()> as GenericTrait<()>>::Associated` to `()`
240+
help: consider constraining the associated type `<impl GenericTrait<()> + Foo as GenericTrait<()>>::Associated` to `()`
241241
|
242242
LL | fn returns_opaque_generic_foo() -> impl GenericTrait<(), Associated = ()> + Foo {
243243
| +++++++++++++++++
244244

245-
error[E0271]: type mismatch resolving `<impl GenericTrait<u8> + GenericTrait<()> as GenericTrait<()>>::Associated == ()`
245+
error[E0271]: type mismatch resolving `<impl GenericTrait<()> + GenericTrait<u8> as GenericTrait<()>>::Associated == ()`
246246
--> $DIR/issue-87261.rs:97:5
247247
|
248248
LL | fn returns_opaque_generic_duplicate() -> impl GenericTrait<()> + GenericTrait<u8> {
@@ -252,8 +252,8 @@ LL | accepts_generic_trait(returns_opaque_generic_duplicate());
252252
| ^^^^^^^^^^^^^^^^^^^^^ expected `()`, found associated type
253253
|
254254
= note: expected unit type `()`
255-
found associated type `<impl GenericTrait<u8> + GenericTrait<()> as GenericTrait<()>>::Associated`
256-
= help: consider constraining the associated type `<impl GenericTrait<u8> + GenericTrait<()> as GenericTrait<()>>::Associated` to `()`
255+
found associated type `<impl GenericTrait<()> + GenericTrait<u8> as GenericTrait<()>>::Associated`
256+
= help: consider constraining the associated type `<impl GenericTrait<()> + GenericTrait<u8> as GenericTrait<()>>::Associated` to `()`
257257
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
258258
note: required by a bound in `accepts_generic_trait`
259259
--> $DIR/issue-87261.rs:44:46

0 commit comments

Comments
 (0)