Skip to content
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

Display short types for unimplemented trait #121739

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,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 Down Expand Up @@ -824,7 +825,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, &mut None)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you should modify format to take an extra file: &mut None parameter to pass in here and modify OnUnimplementedNote to carry file as well. You can see how we present that in other places, which makes it so that if the user is interested in looking at the full type for whatever reason, they can. Ideally we'd have a single file for the entire error, and pass that around everywhere that we use short_ty_string for E0277, particularly note_obligation_cause_code, but that doesn't need to be addressed in this PR, but since you're adding another use there it might be a good idea to do so. Then we can remove all of the redundant "the full type name has been written to '{}'" notes and push them to the top-most call of note_obligation_cause_code.

Copy link
Contributor Author

@jieyouxu jieyouxu Feb 28, 2024

Choose a reason for hiding this comment

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

Not-within-the-scope-of-this-PR remark: the long type names when written to disk is... still incredibly long. I previously saw some issue proposing that the long types written to disk should be pretty printed (but now I cannot find it) and I think that might be good as an option (not necessarily default-behavior because it's probably easier to diff two long types when they are in single lines).

Some issue like #54923 but it's a different one specifically about when written to disk.

} 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 @@ -3507,7 +3507,8 @@ 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 =
with_forced_trimmed_paths!(self.tcx.short_ty_string(expr_ty, &mut None));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can remove the macro annotation if we're using short_ty_string already:

Suggested change
let expr_ty =
with_forced_trimmed_paths!(self.tcx.short_ty_string(expr_ty, &mut None));
let expr_ty = self.tcx.short_ty_string(expr_ty, &mut None);

Copy link
Contributor

Choose a reason for hiding this comment

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

(The other comment about &mut None applies here too.)

err.span_label(
expr_span,
with_forced_trimmed_paths!(format!(
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
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() {}
21 changes: 21 additions & 0 deletions tests/ui/traits/on_unimplemented_long_types.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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

error: aborting due to 1 previous error

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