Skip to content

Commit fc964fb

Browse files
committed
review comments
1 parent 7df4a09 commit fc964fb

File tree

2 files changed

+19
-34
lines changed

2 files changed

+19
-34
lines changed

compiler/rustc_hir_analysis/src/astconv/errors.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::errors::{
66
use crate::fluent_generated as fluent;
77
use crate::traits::error_reporting::report_object_safety_error;
88
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
9+
use rustc_data_structures::sorted_map::SortedMap;
910
use rustc_data_structures::unord::UnordMap;
1011
use rustc_errors::{pluralize, struct_span_code_err, Applicability, Diagnostic, ErrorGuaranteed};
1112
use rustc_hir as hir;
@@ -461,23 +462,22 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
461462
return err.emit();
462463
}
463464

464-
let mut bound_spans: FxHashMap<Span, Vec<String>> = Default::default();
465+
let mut bound_spans: SortedMap<Span, Vec<String>> = Default::default();
465466

466467
let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
467468
let msg = format!("`{}`", if obligation.len() > 50 { quiet } else { obligation });
468469
match &self_ty.kind() {
469470
// Point at the type that couldn't satisfy the bound.
470471
ty::Adt(def, _) => {
471-
bound_spans.entry(tcx.def_span(def.did())).or_default().push(msg)
472+
bound_spans.get_mut_or_insert_default(tcx.def_span(def.did())).push(msg)
472473
}
473474
// Point at the trait object that couldn't satisfy the bound.
474475
ty::Dynamic(preds, _, _) => {
475476
for pred in preds.iter() {
476477
match pred.skip_binder() {
477478
ty::ExistentialPredicate::Trait(tr) => {
478479
bound_spans
479-
.entry(tcx.def_span(tr.def_id))
480-
.or_default()
480+
.get_mut_or_insert_default(tcx.def_span(tr.def_id))
481481
.push(msg.clone());
482482
}
483483
ty::ExistentialPredicate::Projection(_)
@@ -488,8 +488,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
488488
// Point at the closure that couldn't satisfy the bound.
489489
ty::Closure(def_id, _) => {
490490
bound_spans
491-
.entry(tcx.def_span(*def_id))
492-
.or_default()
491+
.get_mut_or_insert_default(tcx.def_span(*def_id))
493492
.push(format!("`{quiet}`"));
494493
}
495494
_ => {}
@@ -559,21 +558,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
559558
format!("associated type cannot be referenced on `{self_ty}` due to unsatisfied trait bounds")
560559
);
561560

562-
let mut bound_spans: Vec<(Span, Vec<String>)> = bound_spans
563-
.into_iter()
564-
.map(|(span, mut bounds)| {
565-
bounds.sort();
566-
bounds.dedup();
567-
(span, bounds)
568-
})
569-
.collect();
570-
bound_spans.sort_by_key(|(span, _)| *span);
571-
for (span, bounds) in bound_spans {
561+
for (span, mut bounds) in bound_spans {
572562
if !tcx.sess.source_map().is_span_accessible(span) {
573563
continue;
574564
}
565+
bounds.sort();
566+
bounds.dedup();
575567
let msg = match &bounds[..] {
576568
[bound] => format!("doesn't satisfy {bound}"),
569+
bounds if bounds.len() > 4 => format!("doesn't satisfy {} bounds", bounds.len()),
577570
[bounds @ .., last] => format!("doesn't satisfy {} or {last}", bounds.join(", ")),
578571
[] => unreachable!(),
579572
};

compiler/rustc_hir_typeck/src/method/suggest.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use crate::Expectation;
99
use crate::FnCtxt;
1010
use rustc_ast::ast::Mutability;
1111
use rustc_attr::parse_confusables;
12-
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
12+
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
13+
use rustc_data_structures::sorted_map::SortedMap;
1314
use rustc_data_structures::unord::UnordSet;
1415
use rustc_errors::StashKey;
1516
use rustc_errors::{
@@ -538,7 +539,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
538539
);
539540
}
540541

541-
let mut bound_spans: FxHashMap<Span, Vec<String>> = Default::default();
542+
let mut bound_spans: SortedMap<Span, Vec<String>> = Default::default();
542543
let mut restrict_type_params = false;
543544
let mut unsatisfied_bounds = false;
544545
if item_name.name == sym::count && self.is_slice_ty(rcvr_ty, span) {
@@ -637,16 +638,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
637638
match &self_ty.kind() {
638639
// Point at the type that couldn't satisfy the bound.
639640
ty::Adt(def, _) => {
640-
bound_spans.entry(tcx.def_span(def.did())).or_default().push(msg)
641+
bound_spans.get_mut_or_insert_default(tcx.def_span(def.did())).push(msg)
641642
}
642643
// Point at the trait object that couldn't satisfy the bound.
643644
ty::Dynamic(preds, _, _) => {
644645
for pred in preds.iter() {
645646
match pred.skip_binder() {
646647
ty::ExistentialPredicate::Trait(tr) => {
647648
bound_spans
648-
.entry(tcx.def_span(tr.def_id))
649-
.or_default()
649+
.get_mut_or_insert_default(tcx.def_span(tr.def_id))
650650
.push(msg.clone());
651651
}
652652
ty::ExistentialPredicate::Projection(_)
@@ -657,8 +657,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
657657
// Point at the closure that couldn't satisfy the bound.
658658
ty::Closure(def_id, _) => {
659659
bound_spans
660-
.entry(tcx.def_span(*def_id))
661-
.or_default()
660+
.get_mut_or_insert_default(tcx.def_span(*def_id))
662661
.push(format!("`{quiet}`"));
663662
}
664663
_ => {}
@@ -1167,20 +1166,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11671166

11681167
self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_name);
11691168

1170-
#[allow(rustc::potential_query_instability)] // We immediately sort the resulting Vec.
1171-
let mut bound_spans: Vec<(Span, Vec<String>)> = bound_spans
1172-
.into_iter()
1173-
.map(|(span, mut bounds)| {
1174-
bounds.sort();
1175-
bounds.dedup();
1176-
(span, bounds)
1177-
})
1178-
.collect();
1179-
bound_spans.sort_by_key(|(span, _)| *span);
1180-
for (span, bounds) in bound_spans {
1169+
for (span, mut bounds) in bound_spans {
11811170
if !tcx.sess.source_map().is_span_accessible(span) {
11821171
continue;
11831172
}
1173+
bounds.sort();
1174+
bounds.dedup();
11841175
let pre = if Some(span) == ty_span {
11851176
ty_span.take();
11861177
format!(
@@ -1192,6 +1183,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11921183
};
11931184
let msg = match &bounds[..] {
11941185
[bound] => format!("{pre}doesn't satisfy {bound}"),
1186+
bounds if bounds.len() > 4 => format!("doesn't satisfy {} bounds", bounds.len()),
11951187
[bounds @ .., last] => {
11961188
format!("{pre}doesn't satisfy {} or {last}", bounds.join(", "))
11971189
}

0 commit comments

Comments
 (0)