From fc964fb439473c84a47444f89e5cf4c570b98f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 29 Jan 2024 23:33:02 +0000 Subject: [PATCH] review comments --- .../rustc_hir_analysis/src/astconv/errors.rs | 25 ++++++----------- .../rustc_hir_typeck/src/method/suggest.rs | 28 +++++++------------ 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> = Default::default(); + let mut bound_spans: SortedMap> = 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)> = 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> = Default::default(); + let mut bound_spans: SortedMap> = 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)> = 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(", ")) }