Skip to content

Commit

Permalink
Avoid silently writing to a file when the involved ty is long
Browse files Browse the repository at this point in the history
  • Loading branch information
jieyouxu committed Mar 1, 2024
1 parent a1dbb61 commit 62baa67
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
15 changes: 13 additions & 2 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
bound_list.into_iter().map(|(_, path)| path).collect::<Vec<_>>().join("\n");
let actual_prefix = rcvr_ty.prefix_string(self.tcx);
info!("unimplemented_traits.len() == {}", unimplemented_traits.len());
let mut long_ty_file = None;
let (primary_message, label) = if unimplemented_traits.len() == 1
&& unimplemented_traits_only
{
Expand All @@ -1066,8 +1067,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Avoid crashing.
return (None, None);
}
let OnUnimplementedNote { message, label, .. } =
self.err_ctxt().on_unimplemented_note(trait_ref, &obligation);
let OnUnimplementedNote { message, label, .. } = self
.err_ctxt()
.on_unimplemented_note(trait_ref, &obligation, &mut long_ty_file);
(message, label)
})
.unwrap()
Expand All @@ -1081,6 +1083,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)
});
err.primary_message(primary_message);
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",
);
}
if let Some(label) = label {
custom_span_label = true;
err.span_label(span, label);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
&self,
trait_ref: ty::PolyTraitRef<'tcx>,
obligation: &PredicateObligation<'tcx>,
long_ty_file: &mut Option<PathBuf>,
) -> OnUnimplementedNote {
let mut long_ty_file = None;

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 @@ -268,7 +267,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}));

if let Ok(Some(command)) = OnUnimplementedDirective::of_item(self.tcx, def_id) {
command.evaluate(self.tcx, trait_ref, &flags, &mut long_ty_file)
command.evaluate(self.tcx, trait_ref, &flags, long_ty_file)
} else {
OnUnimplementedNote::default()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,16 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
file.display(),
));

let mut long_ty_file = None;

let OnUnimplementedNote {
message,
label,
notes,
parent_label,
append_const_msg,
} = self.on_unimplemented_note(trait_ref, &obligation);
} = self.on_unimplemented_note(trait_ref, &obligation, &mut long_ty_file);

let have_alt_message = message.is_some() || label.is_some();
let is_try_conversion = self.is_try_conversion(span, trait_ref.def_id());
let is_unsize =
Expand Down Expand Up @@ -506,6 +509,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {

let mut err = struct_span_code_err!(self.dcx(), span, E0277, "{}", err_msg);

if let Some(long_ty_file) = long_ty_file {
err.note(format!(
"the full name for the type has been written to '{}'",
long_ty_file.display(),
));
err.note("consider using `--verbose` to print the full type name to the console");
}
let mut suggested = false;
if is_try_conversion {
suggested = self.try_conversion_context(&obligation, trait_ref.skip_binder(), &mut err);
Expand Down Expand Up @@ -753,6 +763,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
return err.emit();
}



err
}

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/traits/on_unimplemented_long_types.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ LL | | ))))))))))),
LL | | )))))))))))
| |_______________- return type was inferred to be `Option<Option<Option<...>>>` here
|
= 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
= 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'
Expand Down

0 comments on commit 62baa67

Please sign in to comment.