Skip to content

Commit 5980a32

Browse files
committed
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.
1 parent aa82fd6 commit 5980a32

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

Diff for: compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2169,6 +2169,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21692169
// First, attempt to add note to this error with an async-await-specific
21702170
// message, and fall back to regular note otherwise.
21712171
if !self.maybe_note_obligation_cause_for_async_await(err, obligation) {
2172+
let mut long_ty_file = None;
21722173
self.note_obligation_cause_code(
21732174
obligation.cause.body_id,
21742175
err,
@@ -2177,7 +2178,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21772178
obligation.cause.code(),
21782179
&mut vec![],
21792180
&mut Default::default(),
2181+
&mut long_ty_file,
21802182
);
2183+
if let Some(file) = long_ty_file {
2184+
err.note(format!(
2185+
"the full name for the type has been written to '{}'",
2186+
file.display(),
2187+
));
2188+
err.note("consider using `--verbose` to print the full type name to the console");
2189+
}
21812190
self.suggest_unsized_bound_if_applicable(err, obligation);
21822191
if let Some(span) = err.span.primary_span()
21832192
&& let Some(mut diag) =

Diff for: compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
305305
if let ObligationCauseCode::WhereClause(..)
306306
| ObligationCauseCode::WhereClauseInExpr(..) = code
307307
{
308+
let mut long_ty_file = None;
308309
self.note_obligation_cause_code(
309310
error.obligation.cause.body_id,
310311
&mut diag,
@@ -313,7 +314,17 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
313314
code,
314315
&mut vec![],
315316
&mut Default::default(),
317+
&mut long_ty_file,
316318
);
319+
if let Some(file) = long_ty_file {
320+
diag.note(format!(
321+
"the full name for the type has been written to '{}'",
322+
file.display(),
323+
));
324+
diag.note(
325+
"consider using `--verbose` to print the full type name to the console",
326+
);
327+
}
317328
}
318329
diag.emit()
319330
}

Diff for: compiler/rustc_trait_selection/src/error_reporting/traits/overflow.rs

+11
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
144144
obligation.cause.span,
145145
suggest_increasing_limit,
146146
|err| {
147+
let mut long_ty_file = None;
147148
self.note_obligation_cause_code(
148149
obligation.cause.body_id,
149150
err,
@@ -152,7 +153,17 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
152153
obligation.cause.code(),
153154
&mut vec![],
154155
&mut Default::default(),
156+
&mut long_ty_file,
155157
);
158+
if let Some(file) = long_ty_file {
159+
err.note(format!(
160+
"the full name for the type has been written to '{}'",
161+
file.display(),
162+
));
163+
err.note(
164+
"consider using `--verbose` to print the full type name to the console",
165+
);
166+
}
156167
},
157168
);
158169
}

Diff for: compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

+18-20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::assert_matches::debug_assert_matches;
44
use std::borrow::Cow;
55
use std::iter;
6+
use std::path::PathBuf;
67

78
use itertools::{EitherOrBoth, Itertools};
89
use rustc_data_structures::fx::FxHashSet;
@@ -2703,6 +2704,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
27032704
// Add a note for the item obligation that remains - normally a note pointing to the
27042705
// bound that introduced the obligation (e.g. `T: Send`).
27052706
debug!(?next_code);
2707+
let mut long_ty_file = None;
27062708
self.note_obligation_cause_code(
27072709
obligation.cause.body_id,
27082710
err,
@@ -2711,6 +2713,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
27112713
next_code.unwrap(),
27122714
&mut Vec::new(),
27132715
&mut Default::default(),
2716+
&mut long_ty_file,
27142717
);
27152718
}
27162719

@@ -2723,11 +2726,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
27232726
cause_code: &ObligationCauseCode<'tcx>,
27242727
obligated_types: &mut Vec<Ty<'tcx>>,
27252728
seen_requirements: &mut FxHashSet<DefId>,
2729+
long_ty_file: &mut Option<PathBuf>,
27262730
) where
27272731
T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
27282732
{
2729-
let mut long_ty_file = None;
2730-
27312733
let tcx = self.tcx;
27322734
let predicate = predicate.upcast(tcx);
27332735
let suggest_remove_deref = |err: &mut Diag<'_, G>, expr: &hir::Expr<'_>| {
@@ -2957,9 +2959,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
29572959
}
29582960
ObligationCauseCode::Coercion { source, target } => {
29592961
let source =
2960-
tcx.short_ty_string(self.resolve_vars_if_possible(source), &mut long_ty_file);
2962+
tcx.short_ty_string(self.resolve_vars_if_possible(source), long_ty_file);
29612963
let target =
2962-
tcx.short_ty_string(self.resolve_vars_if_possible(target), &mut long_ty_file);
2964+
tcx.short_ty_string(self.resolve_vars_if_possible(target), long_ty_file);
29632965
err.note(with_forced_trimmed_paths!(format!(
29642966
"required for the cast from `{source}` to `{target}`",
29652967
)));
@@ -3249,7 +3251,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
32493251
};
32503252

32513253
if !is_upvar_tys_infer_tuple {
3252-
let ty_str = tcx.short_ty_string(ty, &mut long_ty_file);
3254+
let ty_str = tcx.short_ty_string(ty, long_ty_file);
32533255
let msg = format!("required because it appears within the type `{ty_str}`");
32543256
match ty.kind() {
32553257
ty::Adt(def, _) => match tcx.opt_item_ident(def.did()) {
@@ -3327,6 +3329,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
33273329
&data.parent_code,
33283330
obligated_types,
33293331
seen_requirements,
3332+
long_ty_file,
33303333
)
33313334
});
33323335
} else {
@@ -3339,6 +3342,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
33393342
cause_code.peel_derives(),
33403343
obligated_types,
33413344
seen_requirements,
3345+
long_ty_file,
33423346
)
33433347
});
33443348
}
@@ -3347,8 +3351,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
33473351
let mut parent_trait_pred =
33483352
self.resolve_vars_if_possible(data.derived.parent_trait_pred);
33493353
let parent_def_id = parent_trait_pred.def_id();
3350-
let self_ty_str = tcx
3351-
.short_ty_string(parent_trait_pred.skip_binder().self_ty(), &mut long_ty_file);
3354+
let self_ty_str =
3355+
tcx.short_ty_string(parent_trait_pred.skip_binder().self_ty(), long_ty_file);
33523356
let trait_name = parent_trait_pred.print_modifiers_and_trait_path().to_string();
33533357
let msg = format!("required for `{self_ty_str}` to implement `{trait_name}`");
33543358
let mut is_auto_trait = false;
@@ -3444,10 +3448,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
34443448
count,
34453449
pluralize!(count)
34463450
));
3447-
let self_ty = tcx.short_ty_string(
3448-
parent_trait_pred.skip_binder().self_ty(),
3449-
&mut long_ty_file,
3450-
);
3451+
let self_ty = tcx
3452+
.short_ty_string(parent_trait_pred.skip_binder().self_ty(), long_ty_file);
34513453
err.note(format!(
34523454
"required for `{self_ty}` to implement `{}`",
34533455
parent_trait_pred.print_modifiers_and_trait_path()
@@ -3463,6 +3465,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
34633465
&data.parent_code,
34643466
obligated_types,
34653467
seen_requirements,
3468+
long_ty_file,
34663469
)
34673470
});
34683471
}
@@ -3479,6 +3482,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
34793482
&data.parent_code,
34803483
obligated_types,
34813484
seen_requirements,
3485+
long_ty_file,
34823486
)
34833487
});
34843488
}
@@ -3493,6 +3497,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
34933497
nested,
34943498
obligated_types,
34953499
seen_requirements,
3500+
long_ty_file,
34963501
)
34973502
});
34983503
let mut multispan = MultiSpan::from(span);
@@ -3523,6 +3528,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
35233528
parent_code,
35243529
obligated_types,
35253530
seen_requirements,
3531+
long_ty_file,
35263532
)
35273533
});
35283534
}
@@ -3562,7 +3568,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
35623568
}
35633569
ObligationCauseCode::OpaqueReturnType(expr_info) => {
35643570
if let Some((expr_ty, hir_id)) = expr_info {
3565-
let expr_ty = self.tcx.short_ty_string(expr_ty, &mut long_ty_file);
3571+
let expr_ty = self.tcx.short_ty_string(expr_ty, long_ty_file);
35663572
let expr = self.infcx.tcx.hir().expect_expr(hir_id);
35673573
err.span_label(
35683574
expr.span,
@@ -3574,14 +3580,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
35743580
}
35753581
}
35763582
}
3577-
3578-
if let Some(file) = long_ty_file {
3579-
err.note(format!(
3580-
"the full name for the type has been written to '{}'",
3581-
file.display(),
3582-
));
3583-
err.note("consider using `--verbose` to print the full type name to the console");
3584-
}
35853583
}
35863584

35873585
#[instrument(

0 commit comments

Comments
 (0)