Skip to content

Display short types for unimplemented trait #121739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use rustc_session::lint::builtin::UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;
use std::iter;
use std::path::PathBuf;

use crate::errors::{
EmptyOnClauseInOnUnimplemented, InvalidOnClauseInOnUnimplemented, NoValueInOnUnimplemented,
Expand Down Expand Up @@ -111,6 +112,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
trait_ref: ty::PolyTraitRef<'tcx>,
obligation: &PredicateObligation<'tcx>,
) -> OnUnimplementedNote {
let mut long_ty_file = None;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the path of this file being printed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is a bit awkward, I tried to make sure I don't miss any silent type name being written to file, but now there seems to be a duplicate written to file note lol

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That... should be fine for now. We need to bubble the printing of the file (and passing the file down) more than we do now, but it being duplicated is better than out right losing it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That... should be fine for now. We need to bubble the printing of the file (and passing the file down) more than we do now, but it being duplicated is better than out right losing it.

Yeah, there's let mut file = None a bit all over the place right now lol


let (def_id, args) = self
.impl_similar_to(trait_ref, obligation)
.unwrap_or_else(|| (trait_ref.def_id(), trait_ref.skip_binder().args));
Expand Down Expand Up @@ -265,7 +268,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}));

if let Ok(Some(command)) = OnUnimplementedDirective::of_item(self.tcx, def_id) {
command.evaluate(self.tcx, trait_ref, &flags)
command.evaluate(self.tcx, trait_ref, &flags, &mut long_ty_file)
} else {
OnUnimplementedNote::default()
}
Expand Down Expand Up @@ -657,6 +660,7 @@ impl<'tcx> OnUnimplementedDirective {
tcx: TyCtxt<'tcx>,
trait_ref: ty::TraitRef<'tcx>,
options: &[(Symbol, Option<String>)],
long_ty_file: &mut Option<PathBuf>,
) -> OnUnimplementedNote {
let mut message = None;
let mut label = None;
Expand All @@ -669,6 +673,7 @@ impl<'tcx> OnUnimplementedDirective {
options.iter().filter_map(|(k, v)| v.clone().map(|v| (*k, v))).collect();

for command in self.subcommands.iter().chain(Some(self)).rev() {
debug!(?command);
if let Some(ref condition) = command.condition
&& !attr::eval_condition(condition, &tcx.sess, Some(tcx.features()), &mut |cfg| {
let value = cfg.value.map(|v| {
Expand All @@ -680,7 +685,12 @@ impl<'tcx> OnUnimplementedDirective {
span: cfg.span,
is_diagnostic_namespace_variant: false
}
.format(tcx, trait_ref, &options_map)
.format(
tcx,
trait_ref,
&options_map,
long_ty_file
)
)
});

Expand Down Expand Up @@ -709,10 +719,14 @@ impl<'tcx> OnUnimplementedDirective {
}

OnUnimplementedNote {
label: label.map(|l| l.format(tcx, trait_ref, &options_map)),
message: message.map(|m| m.format(tcx, trait_ref, &options_map)),
notes: notes.into_iter().map(|n| n.format(tcx, trait_ref, &options_map)).collect(),
parent_label: parent_label.map(|e_s| e_s.format(tcx, trait_ref, &options_map)),
label: label.map(|l| l.format(tcx, trait_ref, &options_map, long_ty_file)),
message: message.map(|m| m.format(tcx, trait_ref, &options_map, long_ty_file)),
notes: notes
.into_iter()
.map(|n| n.format(tcx, trait_ref, &options_map, long_ty_file))
.collect(),
parent_label: parent_label
.map(|e_s| e_s.format(tcx, trait_ref, &options_map, long_ty_file)),
append_const_msg,
}
}
Expand Down Expand Up @@ -814,6 +828,7 @@ impl<'tcx> OnUnimplementedFormatString {
tcx: TyCtxt<'tcx>,
trait_ref: ty::TraitRef<'tcx>,
options: &FxHashMap<Symbol, String>,
long_ty_file: &mut Option<PathBuf>,
) -> String {
let name = tcx.item_name(trait_ref.def_id);
let trait_str = tcx.def_path_str(trait_ref.def_id);
Expand All @@ -824,7 +839,11 @@ impl<'tcx> OnUnimplementedFormatString {
.filter_map(|param| {
let value = match param.kind {
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
trait_ref.args[param.index as usize].to_string()
if let Some(ty) = trait_ref.args[param.index as usize].as_type() {
tcx.short_ty_string(ty, long_ty_file)
} else {
trait_ref.args[param.index as usize].to_string()
}
}
GenericParamDefKind::Lifetime => return None,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2676,6 +2676,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
) where
T: ToPredicate<'tcx>,
{
let mut long_ty_file = None;

let tcx = self.tcx;
let predicate = predicate.to_predicate(tcx);
match *cause_code {
Expand Down Expand Up @@ -2858,21 +2860,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}
ObligationCauseCode::Coercion { source, target } => {
let mut file = None;
let source = tcx.short_ty_string(self.resolve_vars_if_possible(source), &mut file);
let target = tcx.short_ty_string(self.resolve_vars_if_possible(target), &mut file);
let source =
tcx.short_ty_string(self.resolve_vars_if_possible(source), &mut long_ty_file);
let target =
tcx.short_ty_string(self.resolve_vars_if_possible(target), &mut long_ty_file);
err.note(with_forced_trimmed_paths!(format!(
"required for the cast from `{source}` to `{target}`",
)));
if let Some(file) = 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",
);
}
}
ObligationCauseCode::RepeatElementCopy {
is_constable,
Expand Down Expand Up @@ -3175,8 +3169,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// Don't print the tuple of capture types
'print: {
if !is_upvar_tys_infer_tuple {
let mut file = None;
let ty_str = tcx.short_ty_string(ty, &mut file);
let ty_str = tcx.short_ty_string(ty, &mut long_ty_file);
let msg = format!("required because it appears within the type `{ty_str}`");
match ty.kind() {
ty::Adt(def, _) => match tcx.opt_item_ident(def.did()) {
Expand Down Expand Up @@ -3274,9 +3267,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let mut parent_trait_pred =
self.resolve_vars_if_possible(data.derived.parent_trait_pred);
let parent_def_id = parent_trait_pred.def_id();
let mut file = None;
let self_ty_str =
tcx.short_ty_string(parent_trait_pred.skip_binder().self_ty(), &mut file);
let self_ty_str = tcx
.short_ty_string(parent_trait_pred.skip_binder().self_ty(), &mut long_ty_file);
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 mut is_auto_trait = false;
Expand Down Expand Up @@ -3334,15 +3326,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
};

if let Some(file) = file {
err.note(format!(
"the full type name has been written to '{}'",
file.display(),
));
err.note(
"consider using `--verbose` to print the full type name to the console",
);
}
let mut parent_predicate = parent_trait_pred;
let mut data = &data.derived;
let mut count = 0;
Expand Down Expand Up @@ -3383,22 +3366,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
count,
pluralize!(count)
));
let mut file = None;
let self_ty =
tcx.short_ty_string(parent_trait_pred.skip_binder().self_ty(), &mut file);
let self_ty = tcx.short_ty_string(
parent_trait_pred.skip_binder().self_ty(),
&mut long_ty_file,
);
err.note(format!(
"required for `{self_ty}` to implement `{}`",
parent_trait_pred.print_modifiers_and_trait_path()
));
if let Some(file) = file {
err.note(format!(
"the full type name has been written to '{}'",
file.display(),
));
err.note(
"consider using `--verbose` to print the full type name to the console",
);
}
}
// #74711: avoid a stack overflow
ensure_sufficient_stack(|| {
Expand Down Expand Up @@ -3507,7 +3482,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
ObligationCauseCode::OpaqueReturnType(expr_info) => {
if let Some((expr_ty, expr_span)) = expr_info {
let expr_ty = with_forced_trimmed_paths!(self.ty_to_string(expr_ty));
let expr_ty = self.tcx.short_ty_string(expr_ty, &mut long_ty_file);
err.span_label(
expr_span,
with_forced_trimmed_paths!(format!(
Expand All @@ -3517,6 +3492,14 @@ impl<'tcx> TypeErrCtxt<'_, '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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
kind: _,
} = *obligation.cause.code()
{
debug!("ObligationCauseCode::CompareImplItemObligation");
return self.report_extra_impl_obligation(
span,
impl_item_def_id,
Expand Down Expand Up @@ -439,7 +440,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
)
})
.unwrap_or_default();
let file_note = file.map(|file| format!(
let file_note = file.as_ref().map(|file| format!(
"the full trait has been written to '{}'",
file.display(),
));
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/traits/on_unimplemented_long_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ compile-flags: --diagnostic-width=60 -Z write-long-types-to-disk=yes
//@ normalize-stderr-test: "long-type-\d+" -> "long-type-hash"

pub fn foo() -> impl std::fmt::Display {
//~^ ERROR doesn't implement `std::fmt::Display`
Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(
Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(
Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(
Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(Some(
Some(Some(Some(Some(Some(Some(Some(Some(())))))))),
))))))))))),
))))))))))),
))))))))))),
)))))))))))
}

fn main() {}
23 changes: 23 additions & 0 deletions tests/ui/traits/on_unimplemented_long_types.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0277]: `Option<Option<Option<...>>>` doesn't implement `std::fmt::Display`
--> $DIR/on_unimplemented_long_types.rs:4:17
|
LL | pub fn foo() -> impl std::fmt::Display {
| ^^^^^^^^^^^^^^^^^^^^^^ `Option<Option<Option<...>>>` cannot be formatted with the default formatter
LL |
LL | / Some(Some(Some(Some(Some(Some(Some(Some(Some(S...
LL | | Some(Some(Some(Some(Some(Some(Some(Some(So...
LL | | Some(Some(Some(Some(Some(Some(Some(Som...
LL | | Some(Some(Some(Some(Some(Some(Some...
... |
LL | | ))))))))))),
LL | | )))))))))))
| |_______________- return type was inferred to be `Option<Option<Option<...>>>` here
|
= help: the trait `std::fmt::Display` is not implemented for `Option<Option<Option<...>>>`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= 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'
= note: consider using `--verbose` to print the full type name to the console

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.