Skip to content

Commit a1dbb61

Browse files
committed
Unify long type name file and note in note_obligation_cause_code
1 parent c0ce0f3 commit a1dbb61

File tree

4 files changed

+46
-48
lines changed

4 files changed

+46
-48
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_session::lint::builtin::UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES;
1616
use rustc_span::symbol::{kw, sym, Symbol};
1717
use rustc_span::Span;
1818
use std::iter;
19+
use std::path::PathBuf;
1920

2021
use crate::errors::{
2122
EmptyOnClauseInOnUnimplemented, InvalidOnClauseInOnUnimplemented, NoValueInOnUnimplemented,
@@ -111,6 +112,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
111112
trait_ref: ty::PolyTraitRef<'tcx>,
112113
obligation: &PredicateObligation<'tcx>,
113114
) -> OnUnimplementedNote {
115+
let mut long_ty_file = None;
116+
114117
let (def_id, args) = self
115118
.impl_similar_to(trait_ref, obligation)
116119
.unwrap_or_else(|| (trait_ref.def_id(), trait_ref.skip_binder().args));
@@ -265,7 +268,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
265268
}));
266269

267270
if let Ok(Some(command)) = OnUnimplementedDirective::of_item(self.tcx, def_id) {
268-
command.evaluate(self.tcx, trait_ref, &flags)
271+
command.evaluate(self.tcx, trait_ref, &flags, &mut long_ty_file)
269272
} else {
270273
OnUnimplementedNote::default()
271274
}
@@ -657,6 +660,7 @@ impl<'tcx> OnUnimplementedDirective {
657660
tcx: TyCtxt<'tcx>,
658661
trait_ref: ty::TraitRef<'tcx>,
659662
options: &[(Symbol, Option<String>)],
663+
long_ty_file: &mut Option<PathBuf>,
660664
) -> OnUnimplementedNote {
661665
let mut message = None;
662666
let mut label = None;
@@ -681,7 +685,12 @@ impl<'tcx> OnUnimplementedDirective {
681685
span: cfg.span,
682686
is_diagnostic_namespace_variant: false
683687
}
684-
.format(tcx, trait_ref, &options_map)
688+
.format(
689+
tcx,
690+
trait_ref,
691+
&options_map,
692+
long_ty_file
693+
)
685694
)
686695
});
687696

@@ -710,10 +719,14 @@ impl<'tcx> OnUnimplementedDirective {
710719
}
711720

712721
OnUnimplementedNote {
713-
label: label.map(|l| l.format(tcx, trait_ref, &options_map)),
714-
message: message.map(|m| m.format(tcx, trait_ref, &options_map)),
715-
notes: notes.into_iter().map(|n| n.format(tcx, trait_ref, &options_map)).collect(),
716-
parent_label: parent_label.map(|e_s| e_s.format(tcx, trait_ref, &options_map)),
722+
label: label.map(|l| l.format(tcx, trait_ref, &options_map, long_ty_file)),
723+
message: message.map(|m| m.format(tcx, trait_ref, &options_map, long_ty_file)),
724+
notes: notes
725+
.into_iter()
726+
.map(|n| n.format(tcx, trait_ref, &options_map, long_ty_file))
727+
.collect(),
728+
parent_label: parent_label
729+
.map(|e_s| e_s.format(tcx, trait_ref, &options_map, long_ty_file)),
717730
append_const_msg,
718731
}
719732
}
@@ -815,6 +828,7 @@ impl<'tcx> OnUnimplementedFormatString {
815828
tcx: TyCtxt<'tcx>,
816829
trait_ref: ty::TraitRef<'tcx>,
817830
options: &FxHashMap<Symbol, String>,
831+
long_ty_file: &mut Option<PathBuf>,
818832
) -> String {
819833
let name = tcx.item_name(trait_ref.def_id);
820834
let trait_str = tcx.def_path_str(trait_ref.def_id);
@@ -826,7 +840,7 @@ impl<'tcx> OnUnimplementedFormatString {
826840
let value = match param.kind {
827841
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
828842
if let Some(ty) = trait_ref.args[param.index as usize].as_type() {
829-
tcx.short_ty_string(ty, &mut None)
843+
tcx.short_ty_string(ty, long_ty_file)
830844
} else {
831845
trait_ref.args[param.index as usize].to_string()
832846
}

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+22-40
Original file line numberDiff line numberDiff line change
@@ -2676,6 +2676,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
26762676
) where
26772677
T: ToPredicate<'tcx>,
26782678
{
2679+
let mut long_ty_file = None;
2680+
26792681
let tcx = self.tcx;
26802682
let predicate = predicate.to_predicate(tcx);
26812683
match *cause_code {
@@ -2858,21 +2860,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
28582860
}
28592861
}
28602862
ObligationCauseCode::Coercion { source, target } => {
2861-
let mut file = None;
2862-
let source = tcx.short_ty_string(self.resolve_vars_if_possible(source), &mut file);
2863-
let target = tcx.short_ty_string(self.resolve_vars_if_possible(target), &mut file);
2863+
let source =
2864+
tcx.short_ty_string(self.resolve_vars_if_possible(source), &mut long_ty_file);
2865+
let target =
2866+
tcx.short_ty_string(self.resolve_vars_if_possible(target), &mut long_ty_file);
28642867
err.note(with_forced_trimmed_paths!(format!(
28652868
"required for the cast from `{source}` to `{target}`",
28662869
)));
2867-
if let Some(file) = file {
2868-
err.note(format!(
2869-
"the full name for the type has been written to '{}'",
2870-
file.display(),
2871-
));
2872-
err.note(
2873-
"consider using `--verbose` to print the full type name to the console",
2874-
);
2875-
}
28762870
}
28772871
ObligationCauseCode::RepeatElementCopy {
28782872
is_constable,
@@ -3175,8 +3169,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
31753169
// Don't print the tuple of capture types
31763170
'print: {
31773171
if !is_upvar_tys_infer_tuple {
3178-
let mut file = None;
3179-
let ty_str = tcx.short_ty_string(ty, &mut file);
3172+
let ty_str = tcx.short_ty_string(ty, &mut long_ty_file);
31803173
let msg = format!("required because it appears within the type `{ty_str}`");
31813174
match ty.kind() {
31823175
ty::Adt(def, _) => match tcx.opt_item_ident(def.did()) {
@@ -3274,9 +3267,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
32743267
let mut parent_trait_pred =
32753268
self.resolve_vars_if_possible(data.derived.parent_trait_pred);
32763269
let parent_def_id = parent_trait_pred.def_id();
3277-
let mut file = None;
3278-
let self_ty_str =
3279-
tcx.short_ty_string(parent_trait_pred.skip_binder().self_ty(), &mut file);
3270+
let self_ty_str = tcx
3271+
.short_ty_string(parent_trait_pred.skip_binder().self_ty(), &mut long_ty_file);
32803272
let trait_name = parent_trait_pred.print_modifiers_and_trait_path().to_string();
32813273
let msg = format!("required for `{self_ty_str}` to implement `{trait_name}`");
32823274
let mut is_auto_trait = false;
@@ -3334,15 +3326,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
33343326
}
33353327
};
33363328

3337-
if let Some(file) = file {
3338-
err.note(format!(
3339-
"the full type name has been written to '{}'",
3340-
file.display(),
3341-
));
3342-
err.note(
3343-
"consider using `--verbose` to print the full type name to the console",
3344-
);
3345-
}
33463329
let mut parent_predicate = parent_trait_pred;
33473330
let mut data = &data.derived;
33483331
let mut count = 0;
@@ -3383,22 +3366,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
33833366
count,
33843367
pluralize!(count)
33853368
));
3386-
let mut file = None;
3387-
let self_ty =
3388-
tcx.short_ty_string(parent_trait_pred.skip_binder().self_ty(), &mut file);
3369+
let self_ty = tcx.short_ty_string(
3370+
parent_trait_pred.skip_binder().self_ty(),
3371+
&mut long_ty_file,
3372+
);
33893373
err.note(format!(
33903374
"required for `{self_ty}` to implement `{}`",
33913375
parent_trait_pred.print_modifiers_and_trait_path()
33923376
));
3393-
if let Some(file) = file {
3394-
err.note(format!(
3395-
"the full type name has been written to '{}'",
3396-
file.display(),
3397-
));
3398-
err.note(
3399-
"consider using `--verbose` to print the full type name to the console",
3400-
);
3401-
}
34023377
}
34033378
// #74711: avoid a stack overflow
34043379
ensure_sufficient_stack(|| {
@@ -3507,8 +3482,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
35073482
}
35083483
ObligationCauseCode::OpaqueReturnType(expr_info) => {
35093484
if let Some((expr_ty, expr_span)) = expr_info {
3510-
let expr_ty =
3511-
with_forced_trimmed_paths!(self.tcx.short_ty_string(expr_ty, &mut None));
3485+
let expr_ty = self.tcx.short_ty_string(expr_ty, &mut long_ty_file);
35123486
err.span_label(
35133487
expr_span,
35143488
with_forced_trimmed_paths!(format!(
@@ -3518,6 +3492,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
35183492
}
35193493
}
35203494
}
3495+
3496+
if let Some(file) = long_ty_file {
3497+
err.note(format!(
3498+
"the full name for the type has been written to '{}'",
3499+
file.display(),
3500+
));
3501+
err.note("consider using `--verbose` to print the full type name to the console");
3502+
}
35213503
}
35223504

35233505
#[instrument(

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
440440
)
441441
})
442442
.unwrap_or_default();
443-
let file_note = file.map(|file| format!(
443+
let file_note = file.as_ref().map(|file| format!(
444444
"the full trait has been written to '{}'",
445445
file.display(),
446446
));

tests/ui/traits/on_unimplemented_long_types.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ LL | | )))))))))))
1515
|
1616
= help: the trait `std::fmt::Display` is not implemented for `Option<Option<Option<...>>>`
1717
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
18+
= note: the full name for the type has been written to '$TEST_BUILD_DIR/traits/on_unimplemented_long_types/on_unimplemented_long_types.long-type-hash.txt'
19+
= note: consider using `--verbose` to print the full type name to the console
1820

1921
error: aborting due to 1 previous error
2022

0 commit comments

Comments
 (0)