review comments

This commit is contained in:
Esteban Küber 2024-01-29 23:33:02 +00:00
parent 7df4a09fc4
commit fc964fb439
2 changed files with 19 additions and 34 deletions

View file

@ -6,6 +6,7 @@ use crate::errors::{
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use crate::traits::error_reporting::report_object_safety_error; use crate::traits::error_reporting::report_object_safety_error;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet}; use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::sorted_map::SortedMap;
use rustc_data_structures::unord::UnordMap; use rustc_data_structures::unord::UnordMap;
use rustc_errors::{pluralize, struct_span_code_err, Applicability, Diagnostic, ErrorGuaranteed}; use rustc_errors::{pluralize, struct_span_code_err, Applicability, Diagnostic, ErrorGuaranteed};
use rustc_hir as hir; use rustc_hir as hir;
@ -461,14 +462,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
return err.emit(); 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 mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
let msg = format!("`{}`", if obligation.len() > 50 { quiet } else { obligation }); let msg = format!("`{}`", if obligation.len() > 50 { quiet } else { obligation });
match &self_ty.kind() { match &self_ty.kind() {
// Point at the type that couldn't satisfy the bound. // Point at the type that couldn't satisfy the bound.
ty::Adt(def, _) => { 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. // Point at the trait object that couldn't satisfy the bound.
ty::Dynamic(preds, _, _) => { ty::Dynamic(preds, _, _) => {
@ -476,8 +477,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
match pred.skip_binder() { match pred.skip_binder() {
ty::ExistentialPredicate::Trait(tr) => { ty::ExistentialPredicate::Trait(tr) => {
bound_spans bound_spans
.entry(tcx.def_span(tr.def_id)) .get_mut_or_insert_default(tcx.def_span(tr.def_id))
.or_default()
.push(msg.clone()); .push(msg.clone());
} }
ty::ExistentialPredicate::Projection(_) ty::ExistentialPredicate::Projection(_)
@ -488,8 +488,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
// Point at the closure that couldn't satisfy the bound. // Point at the closure that couldn't satisfy the bound.
ty::Closure(def_id, _) => { ty::Closure(def_id, _) => {
bound_spans bound_spans
.entry(tcx.def_span(*def_id)) .get_mut_or_insert_default(tcx.def_span(*def_id))
.or_default()
.push(format!("`{quiet}`")); .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") format!("associated type cannot be referenced on `{self_ty}` due to unsatisfied trait bounds")
); );
let mut bound_spans: Vec<(Span, Vec<String>)> = bound_spans for (span, mut bounds) in 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 {
if !tcx.sess.source_map().is_span_accessible(span) { if !tcx.sess.source_map().is_span_accessible(span) {
continue; continue;
} }
bounds.sort();
bounds.dedup();
let msg = match &bounds[..] { let msg = match &bounds[..] {
[bound] => format!("doesn't satisfy {bound}"), [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(", ")), [bounds @ .., last] => format!("doesn't satisfy {} or {last}", bounds.join(", ")),
[] => unreachable!(), [] => unreachable!(),
}; };

View file

@ -9,7 +9,8 @@ use crate::Expectation;
use crate::FnCtxt; use crate::FnCtxt;
use rustc_ast::ast::Mutability; use rustc_ast::ast::Mutability;
use rustc_attr::parse_confusables; 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_data_structures::unord::UnordSet;
use rustc_errors::StashKey; use rustc_errors::StashKey;
use rustc_errors::{ 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 restrict_type_params = false;
let mut unsatisfied_bounds = false; let mut unsatisfied_bounds = false;
if item_name.name == sym::count && self.is_slice_ty(rcvr_ty, span) { 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() { match &self_ty.kind() {
// Point at the type that couldn't satisfy the bound. // Point at the type that couldn't satisfy the bound.
ty::Adt(def, _) => { 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. // Point at the trait object that couldn't satisfy the bound.
ty::Dynamic(preds, _, _) => { ty::Dynamic(preds, _, _) => {
@ -645,8 +646,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match pred.skip_binder() { match pred.skip_binder() {
ty::ExistentialPredicate::Trait(tr) => { ty::ExistentialPredicate::Trait(tr) => {
bound_spans bound_spans
.entry(tcx.def_span(tr.def_id)) .get_mut_or_insert_default(tcx.def_span(tr.def_id))
.or_default()
.push(msg.clone()); .push(msg.clone());
} }
ty::ExistentialPredicate::Projection(_) ty::ExistentialPredicate::Projection(_)
@ -657,8 +657,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Point at the closure that couldn't satisfy the bound. // Point at the closure that couldn't satisfy the bound.
ty::Closure(def_id, _) => { ty::Closure(def_id, _) => {
bound_spans bound_spans
.entry(tcx.def_span(*def_id)) .get_mut_or_insert_default(tcx.def_span(*def_id))
.or_default()
.push(format!("`{quiet}`")); .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); self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_name);
#[allow(rustc::potential_query_instability)] // We immediately sort the resulting Vec. for (span, mut bounds) in bound_spans {
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 {
if !tcx.sess.source_map().is_span_accessible(span) { if !tcx.sess.source_map().is_span_accessible(span) {
continue; continue;
} }
bounds.sort();
bounds.dedup();
let pre = if Some(span) == ty_span { let pre = if Some(span) == ty_span {
ty_span.take(); ty_span.take();
format!( format!(
@ -1192,6 +1183,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}; };
let msg = match &bounds[..] { let msg = match &bounds[..] {
[bound] => format!("{pre}doesn't satisfy {bound}"), [bound] => format!("{pre}doesn't satisfy {bound}"),
bounds if bounds.len() > 4 => format!("doesn't satisfy {} bounds", bounds.len()),
[bounds @ .., last] => { [bounds @ .., last] => {
format!("{pre}doesn't satisfy {} or {last}", bounds.join(", ")) format!("{pre}doesn't satisfy {} or {last}", bounds.join(", "))
} }