1
Fork 0

Pass long type path into note_obligation_cause_code to avoid printing same path multiple times

Because `note_obligation_cause_code` is recursive, if multiple types are too
long to print to the terminal, a `long_ty_file` will be created. Before, one was
created *per recursion*. Now, it is passed in so it gets printed only once.

Part of #132013.
This commit is contained in:
Esteban Küber 2024-10-24 00:22:37 +00:00
parent aa82fd6d1d
commit 5980a32ef1
4 changed files with 49 additions and 20 deletions

View file

@ -2169,6 +2169,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
// First, attempt to add note to this error with an async-await-specific // First, attempt to add note to this error with an async-await-specific
// message, and fall back to regular note otherwise. // message, and fall back to regular note otherwise.
if !self.maybe_note_obligation_cause_for_async_await(err, obligation) { if !self.maybe_note_obligation_cause_for_async_await(err, obligation) {
let mut long_ty_file = None;
self.note_obligation_cause_code( self.note_obligation_cause_code(
obligation.cause.body_id, obligation.cause.body_id,
err, err,
@ -2177,7 +2178,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
obligation.cause.code(), obligation.cause.code(),
&mut vec![], &mut vec![],
&mut Default::default(), &mut Default::default(),
&mut long_ty_file,
); );
if let Some(file) = long_ty_file {
err.note(format!(
"the full name for the type has been written to '{}'",
file.display(),
));
err.note("consider using `--verbose` to print the full type name to the console");
}
self.suggest_unsized_bound_if_applicable(err, obligation); self.suggest_unsized_bound_if_applicable(err, obligation);
if let Some(span) = err.span.primary_span() if let Some(span) = err.span.primary_span()
&& let Some(mut diag) = && let Some(mut diag) =

View file

@ -305,6 +305,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if let ObligationCauseCode::WhereClause(..) if let ObligationCauseCode::WhereClause(..)
| ObligationCauseCode::WhereClauseInExpr(..) = code | ObligationCauseCode::WhereClauseInExpr(..) = code
{ {
let mut long_ty_file = None;
self.note_obligation_cause_code( self.note_obligation_cause_code(
error.obligation.cause.body_id, error.obligation.cause.body_id,
&mut diag, &mut diag,
@ -313,7 +314,17 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
code, code,
&mut vec![], &mut vec![],
&mut Default::default(), &mut Default::default(),
&mut long_ty_file,
); );
if let Some(file) = long_ty_file {
diag.note(format!(
"the full name for the type has been written to '{}'",
file.display(),
));
diag.note(
"consider using `--verbose` to print the full type name to the console",
);
}
} }
diag.emit() diag.emit()
} }

View file

@ -144,6 +144,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
obligation.cause.span, obligation.cause.span,
suggest_increasing_limit, suggest_increasing_limit,
|err| { |err| {
let mut long_ty_file = None;
self.note_obligation_cause_code( self.note_obligation_cause_code(
obligation.cause.body_id, obligation.cause.body_id,
err, err,
@ -152,7 +153,17 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
obligation.cause.code(), obligation.cause.code(),
&mut vec![], &mut vec![],
&mut Default::default(), &mut Default::default(),
&mut long_ty_file,
); );
if let Some(file) = long_ty_file {
err.note(format!(
"the full name for the type has been written to '{}'",
file.display(),
));
err.note(
"consider using `--verbose` to print the full type name to the console",
);
}
}, },
); );
} }

View file

@ -3,6 +3,7 @@
use std::assert_matches::debug_assert_matches; use std::assert_matches::debug_assert_matches;
use std::borrow::Cow; use std::borrow::Cow;
use std::iter; use std::iter;
use std::path::PathBuf;
use itertools::{EitherOrBoth, Itertools}; use itertools::{EitherOrBoth, Itertools};
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
@ -2703,6 +2704,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
// Add a note for the item obligation that remains - normally a note pointing to the // Add a note for the item obligation that remains - normally a note pointing to the
// bound that introduced the obligation (e.g. `T: Send`). // bound that introduced the obligation (e.g. `T: Send`).
debug!(?next_code); debug!(?next_code);
let mut long_ty_file = None;
self.note_obligation_cause_code( self.note_obligation_cause_code(
obligation.cause.body_id, obligation.cause.body_id,
err, err,
@ -2711,6 +2713,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
next_code.unwrap(), next_code.unwrap(),
&mut Vec::new(), &mut Vec::new(),
&mut Default::default(), &mut Default::default(),
&mut long_ty_file,
); );
} }
@ -2723,11 +2726,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
cause_code: &ObligationCauseCode<'tcx>, cause_code: &ObligationCauseCode<'tcx>,
obligated_types: &mut Vec<Ty<'tcx>>, obligated_types: &mut Vec<Ty<'tcx>>,
seen_requirements: &mut FxHashSet<DefId>, seen_requirements: &mut FxHashSet<DefId>,
long_ty_file: &mut Option<PathBuf>,
) where ) where
T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>, T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
{ {
let mut long_ty_file = None;
let tcx = self.tcx; let tcx = self.tcx;
let predicate = predicate.upcast(tcx); let predicate = predicate.upcast(tcx);
let suggest_remove_deref = |err: &mut Diag<'_, G>, expr: &hir::Expr<'_>| { let suggest_remove_deref = |err: &mut Diag<'_, G>, expr: &hir::Expr<'_>| {
@ -2957,9 +2959,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
} }
ObligationCauseCode::Coercion { source, target } => { ObligationCauseCode::Coercion { source, target } => {
let source = let source =
tcx.short_ty_string(self.resolve_vars_if_possible(source), &mut long_ty_file); tcx.short_ty_string(self.resolve_vars_if_possible(source), long_ty_file);
let target = let target =
tcx.short_ty_string(self.resolve_vars_if_possible(target), &mut long_ty_file); tcx.short_ty_string(self.resolve_vars_if_possible(target), long_ty_file);
err.note(with_forced_trimmed_paths!(format!( err.note(with_forced_trimmed_paths!(format!(
"required for the cast from `{source}` to `{target}`", "required for the cast from `{source}` to `{target}`",
))); )));
@ -3249,7 +3251,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}; };
if !is_upvar_tys_infer_tuple { if !is_upvar_tys_infer_tuple {
let ty_str = tcx.short_ty_string(ty, &mut long_ty_file); let ty_str = tcx.short_ty_string(ty, long_ty_file);
let msg = format!("required because it appears within the type `{ty_str}`"); let msg = format!("required because it appears within the type `{ty_str}`");
match ty.kind() { match ty.kind() {
ty::Adt(def, _) => match tcx.opt_item_ident(def.did()) { ty::Adt(def, _) => match tcx.opt_item_ident(def.did()) {
@ -3327,6 +3329,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&data.parent_code, &data.parent_code,
obligated_types, obligated_types,
seen_requirements, seen_requirements,
long_ty_file,
) )
}); });
} else { } else {
@ -3339,6 +3342,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
cause_code.peel_derives(), cause_code.peel_derives(),
obligated_types, obligated_types,
seen_requirements, seen_requirements,
long_ty_file,
) )
}); });
} }
@ -3347,8 +3351,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let mut parent_trait_pred = let mut parent_trait_pred =
self.resolve_vars_if_possible(data.derived.parent_trait_pred); self.resolve_vars_if_possible(data.derived.parent_trait_pred);
let parent_def_id = parent_trait_pred.def_id(); let parent_def_id = parent_trait_pred.def_id();
let self_ty_str = tcx let self_ty_str =
.short_ty_string(parent_trait_pred.skip_binder().self_ty(), &mut long_ty_file); tcx.short_ty_string(parent_trait_pred.skip_binder().self_ty(), long_ty_file);
let trait_name = parent_trait_pred.print_modifiers_and_trait_path().to_string(); let trait_name = parent_trait_pred.print_modifiers_and_trait_path().to_string();
let msg = format!("required for `{self_ty_str}` to implement `{trait_name}`"); let msg = format!("required for `{self_ty_str}` to implement `{trait_name}`");
let mut is_auto_trait = false; let mut is_auto_trait = false;
@ -3444,10 +3448,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
count, count,
pluralize!(count) pluralize!(count)
)); ));
let self_ty = tcx.short_ty_string( let self_ty = tcx
parent_trait_pred.skip_binder().self_ty(), .short_ty_string(parent_trait_pred.skip_binder().self_ty(), long_ty_file);
&mut long_ty_file,
);
err.note(format!( err.note(format!(
"required for `{self_ty}` to implement `{}`", "required for `{self_ty}` to implement `{}`",
parent_trait_pred.print_modifiers_and_trait_path() parent_trait_pred.print_modifiers_and_trait_path()
@ -3463,6 +3465,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&data.parent_code, &data.parent_code,
obligated_types, obligated_types,
seen_requirements, seen_requirements,
long_ty_file,
) )
}); });
} }
@ -3479,6 +3482,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&data.parent_code, &data.parent_code,
obligated_types, obligated_types,
seen_requirements, seen_requirements,
long_ty_file,
) )
}); });
} }
@ -3493,6 +3497,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
nested, nested,
obligated_types, obligated_types,
seen_requirements, seen_requirements,
long_ty_file,
) )
}); });
let mut multispan = MultiSpan::from(span); let mut multispan = MultiSpan::from(span);
@ -3523,6 +3528,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
parent_code, parent_code,
obligated_types, obligated_types,
seen_requirements, seen_requirements,
long_ty_file,
) )
}); });
} }
@ -3562,7 +3568,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
} }
ObligationCauseCode::OpaqueReturnType(expr_info) => { ObligationCauseCode::OpaqueReturnType(expr_info) => {
if let Some((expr_ty, hir_id)) = expr_info { if let Some((expr_ty, hir_id)) = expr_info {
let expr_ty = self.tcx.short_ty_string(expr_ty, &mut long_ty_file); let expr_ty = self.tcx.short_ty_string(expr_ty, long_ty_file);
let expr = self.infcx.tcx.hir().expect_expr(hir_id); let expr = self.infcx.tcx.hir().expect_expr(hir_id);
err.span_label( err.span_label(
expr.span, expr.span,
@ -3574,14 +3580,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
} }
} }
} }
if let Some(file) = long_ty_file {
err.note(format!(
"the full name for the type has been written to '{}'",
file.display(),
));
err.note("consider using `--verbose` to print the full type name to the console");
}
} }
#[instrument( #[instrument(