about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-01-29 23:33:02 +0000
committerEsteban Küber <esteban@kuber.com.ar>2024-01-29 23:33:02 +0000
commitfc964fb439473c84a47444f89e5cf4c570b98f46 (patch)
treeb1c64f9c7a8843e3c577062530fc76d1149b9434
parent7df4a09fc4e236f854273202e124747d47246ba5 (diff)
downloadrust-fc964fb439473c84a47444f89e5cf4c570b98f46.tar.gz
rust-fc964fb439473c84a47444f89e5cf4c570b98f46.zip
review comments
-rw-r--r--compiler/rustc_hir_analysis/src/astconv/errors.rs25
-rw-r--r--compiler/rustc_hir_typeck/src/method/suggest.rs28
2 files changed, 19 insertions, 34 deletions
diff --git a/compiler/rustc_hir_analysis/src/astconv/errors.rs b/compiler/rustc_hir_analysis/src/astconv/errors.rs
index b2d5d3885d9..fa7d9799dbd 100644
--- a/compiler/rustc_hir_analysis/src/astconv/errors.rs
+++ b/compiler/rustc_hir_analysis/src/astconv/errors.rs
@@ -6,6 +6,7 @@ use crate::errors::{
 use crate::fluent_generated as fluent;
 use crate::traits::error_reporting::report_object_safety_error;
 use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
+use rustc_data_structures::sorted_map::SortedMap;
 use rustc_data_structures::unord::UnordMap;
 use rustc_errors::{pluralize, struct_span_code_err, Applicability, Diagnostic, ErrorGuaranteed};
 use rustc_hir as hir;
@@ -461,14 +462,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
             return err.emit();
         }
 
-        let mut bound_spans: FxHashMap<Span, Vec<String>> = Default::default();
+        let mut bound_spans: SortedMap<Span, Vec<String>> = Default::default();
 
         let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
             let msg = format!("`{}`", if obligation.len() > 50 { quiet } else { obligation });
             match &self_ty.kind() {
                 // Point at the type that couldn't satisfy the bound.
                 ty::Adt(def, _) => {
-                    bound_spans.entry(tcx.def_span(def.did())).or_default().push(msg)
+                    bound_spans.get_mut_or_insert_default(tcx.def_span(def.did())).push(msg)
                 }
                 // Point at the trait object that couldn't satisfy the bound.
                 ty::Dynamic(preds, _, _) => {
@@ -476,8 +477,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
                         match pred.skip_binder() {
                             ty::ExistentialPredicate::Trait(tr) => {
                                 bound_spans
-                                    .entry(tcx.def_span(tr.def_id))
-                                    .or_default()
+                                    .get_mut_or_insert_default(tcx.def_span(tr.def_id))
                                     .push(msg.clone());
                             }
                             ty::ExistentialPredicate::Projection(_)
@@ -488,8 +488,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
                 // Point at the closure that couldn't satisfy the bound.
                 ty::Closure(def_id, _) => {
                     bound_spans
-                        .entry(tcx.def_span(*def_id))
-                        .or_default()
+                        .get_mut_or_insert_default(tcx.def_span(*def_id))
                         .push(format!("`{quiet}`"));
                 }
                 _ => {}
@@ -559,21 +558,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
             format!("associated type cannot be referenced on `{self_ty}` due to unsatisfied trait bounds")
         );
 
-        let mut bound_spans: Vec<(Span, Vec<String>)> = bound_spans
-            .into_iter()
-            .map(|(span, mut bounds)| {
-                bounds.sort();
-                bounds.dedup();
-                (span, bounds)
-            })
-            .collect();
-        bound_spans.sort_by_key(|(span, _)| *span);
-        for (span, bounds) in bound_spans {
+        for (span, mut bounds) in bound_spans {
             if !tcx.sess.source_map().is_span_accessible(span) {
                 continue;
             }
+            bounds.sort();
+            bounds.dedup();
             let msg = match &bounds[..] {
                 [bound] => format!("doesn't satisfy {bound}"),
+                bounds if bounds.len() > 4 => format!("doesn't satisfy {} bounds", bounds.len()),
                 [bounds @ .., last] => format!("doesn't satisfy {} or {last}", bounds.join(", ")),
                 [] => unreachable!(),
             };
diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs
index 0499a46dbb8..677d2240651 100644
--- a/compiler/rustc_hir_typeck/src/method/suggest.rs
+++ b/compiler/rustc_hir_typeck/src/method/suggest.rs
@@ -9,7 +9,8 @@ use crate::Expectation;
 use crate::FnCtxt;
 use rustc_ast::ast::Mutability;
 use rustc_attr::parse_confusables;
-use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
+use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
+use rustc_data_structures::sorted_map::SortedMap;
 use rustc_data_structures::unord::UnordSet;
 use rustc_errors::StashKey;
 use rustc_errors::{
@@ -538,7 +539,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
             );
         }
 
-        let mut bound_spans: FxHashMap<Span, Vec<String>> = Default::default();
+        let mut bound_spans: SortedMap<Span, Vec<String>> = Default::default();
         let mut restrict_type_params = false;
         let mut unsatisfied_bounds = false;
         if item_name.name == sym::count && self.is_slice_ty(rcvr_ty, span) {
@@ -637,7 +638,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 match &self_ty.kind() {
                     // Point at the type that couldn't satisfy the bound.
                     ty::Adt(def, _) => {
-                        bound_spans.entry(tcx.def_span(def.did())).or_default().push(msg)
+                        bound_spans.get_mut_or_insert_default(tcx.def_span(def.did())).push(msg)
                     }
                     // Point at the trait object that couldn't satisfy the bound.
                     ty::Dynamic(preds, _, _) => {
@@ -645,8 +646,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                             match pred.skip_binder() {
                                 ty::ExistentialPredicate::Trait(tr) => {
                                     bound_spans
-                                        .entry(tcx.def_span(tr.def_id))
-                                        .or_default()
+                                        .get_mut_or_insert_default(tcx.def_span(tr.def_id))
                                         .push(msg.clone());
                                 }
                                 ty::ExistentialPredicate::Projection(_)
@@ -657,8 +657,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                     // Point at the closure that couldn't satisfy the bound.
                     ty::Closure(def_id, _) => {
                         bound_spans
-                            .entry(tcx.def_span(*def_id))
-                            .or_default()
+                            .get_mut_or_insert_default(tcx.def_span(*def_id))
                             .push(format!("`{quiet}`"));
                     }
                     _ => {}
@@ -1167,20 +1166,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
 
         self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_name);
 
-        #[allow(rustc::potential_query_instability)] // We immediately sort the resulting Vec.
-        let mut bound_spans: Vec<(Span, Vec<String>)> = bound_spans
-            .into_iter()
-            .map(|(span, mut bounds)| {
-                bounds.sort();
-                bounds.dedup();
-                (span, bounds)
-            })
-            .collect();
-        bound_spans.sort_by_key(|(span, _)| *span);
-        for (span, bounds) in bound_spans {
+        for (span, mut bounds) in bound_spans {
             if !tcx.sess.source_map().is_span_accessible(span) {
                 continue;
             }
+            bounds.sort();
+            bounds.dedup();
             let pre = if Some(span) == ty_span {
                 ty_span.take();
                 format!(
@@ -1192,6 +1183,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
             };
             let msg = match &bounds[..] {
                 [bound] => format!("{pre}doesn't satisfy {bound}"),
+                bounds if bounds.len() > 4 => format!("doesn't satisfy {} bounds", bounds.len()),
                 [bounds @ .., last] => {
                     format!("{pre}doesn't satisfy {} or {last}", bounds.join(", "))
                 }