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

Conversation

jieyouxu
Copy link
Member

@jieyouxu jieyouxu commented Feb 28, 2024

Shortens unimplemented trait diagnostics. Now shows:

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`.

I'm not 100% sure if this is desirable, or if we should just let the long types remain long. This is also kinda a short-term bandaid solution. The real long term solution is to properly migrate rustc_trait_selection's error reporting to use translatable diagnostics and then properly handle type name printing.

Fixes #121687.

@rustbot
Copy link
Collaborator

rustbot commented Feb 28, 2024

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 28, 2024
Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

r? @estebank

Thank you for taking this on!

I have a bunch of nitpicks that I'd like to address. In particular, shortening the type with no way to see the full type anywhere (which we stored to disk) can cause issues for people. Most of the time people don't care about the full type, but when they do, they really do. Also if we can try and pass the file being written to so that we only create one file per error instead of one per shortened type, that will be less disruptive, both on the user's disk as well as in the terminal output (because we'd only have a single note: full type written to... to display, instead of multiple).

@petrochenkov, I know you have a bunch of things on your plate, taking it on to ease your load.

Comment on lines 3510 to 3511
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.)

@@ -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
Member 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.

@rustbot rustbot assigned estebank and unassigned petrochenkov Feb 28, 2024
@estebank estebank added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Feb 28, 2024
@jieyouxu
Copy link
Member Author

Thanks for reviewing!

I have a bunch of nitpicks that I'd like to address.

I will try to address them one-by-one.

In particular, shortening the type with no way to see the full type anywhere (which we stored to
disk) can cause issues for people. Most of the time people don't care about the full type, but
when they do, they really do.

I strongly agree with this, because I have ran into issues with big types too.

Also if we can try and pass the file being written to so that we only create one file per error
instead of one per shortened type, that will be less disruptive, both on the user's disk as well
as in the terminal output (because we'd only have a single note: full type written to... to
display, instead of multiple).

Yeah, I need to fix that. I noticed multiple loooong-type files being created for the same type
when making the change, which was already annoying.

@jieyouxu
Copy link
Member Author

jieyouxu commented Feb 28, 2024

I've unified (I think) the long type output file in note_obligation_cause_code, and moved the filename note to the end of note_obligation_cause_code. The long type output file probably should be passed even further up the call chain. Although further up in the call chain, there are also various instances of scattered output file / filename notes (esp. in rustc_hir_typeck) which probably should eventually be somehow unified too (probably not in this PR).

@jieyouxu jieyouxu removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 29, 2024
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 29, 2024
@@ -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

Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

Other than one (seemingly?) missing note, r=me

@estebank
Copy link
Contributor

estebank commented Mar 1, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Mar 1, 2024

📌 Commit 62baa67 has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 1, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 2, 2024
Display short types for unimplemented trait

Shortens unimplemented trait diagnostics. Now shows:

```
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`.
```

I'm not 100% sure if this is desirable, or if we should just let the long types remain long. This is also kinda a short-term bandaid solution. The real long term solution is to properly migrate `rustc_trait_selection`'s error reporting to use translatable diagnostics and then properly handle type name printing.

Fixes rust-lang#121687.
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 2, 2024
…iaskrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#109263 (fix typo in documentation for std::fs::Permissions)
 - rust-lang#120684 (Update E0716.md for clarity)
 - rust-lang#121739 (Display short types for unimplemented trait)
 - rust-lang#121749 (Don't lint on executable crates with `non_snake_case` names)
 - rust-lang#121758 (Move thread local implementation to `sys`)
 - rust-lang#121815 (Move `gather_comments`.)
 - rust-lang#121835 (Move `HandleStore` into `server.rs`.)
 - rust-lang#121847 (Remove hidden use of Global)
 - rust-lang#121861 (Use the guaranteed precision of a couple of float functions in docs)
 - rust-lang#121875 ( Account for unmet T: !Copy in E0277 message)

r? `@ghost`
`@rustbot` modify labels: rollup
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Mar 2, 2024
Display short types for unimplemented trait

Shortens unimplemented trait diagnostics. Now shows:

```
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`.
```

I'm not 100% sure if this is desirable, or if we should just let the long types remain long. This is also kinda a short-term bandaid solution. The real long term solution is to properly migrate `rustc_trait_selection`'s error reporting to use translatable diagnostics and then properly handle type name printing.

Fixes rust-lang#121687.
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 2, 2024
Rollup of 9 pull requests

Successful merges:

 - rust-lang#109263 (fix typo in documentation for std::fs::Permissions)
 - rust-lang#117156 (Convert `Unix{Datagram,Stream}::{set_}passcred()` to per-OS traits)
 - rust-lang#120684 (Update E0716.md for clarity)
 - rust-lang#121739 (Display short types for unimplemented trait)
 - rust-lang#121815 (Move `gather_comments`.)
 - rust-lang#121835 (Move `HandleStore` into `server.rs`.)
 - rust-lang#121847 (Remove hidden use of Global)
 - rust-lang#121861 (Use the guaranteed precision of a couple of float functions in docs)
 - rust-lang#121875 ( Account for unmet T: !Copy in E0277 message)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 2, 2024
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#109263 (fix typo in documentation for std::fs::Permissions)
 - rust-lang#120684 (Update E0716.md for clarity)
 - rust-lang#121715 (match lowering: pre-simplify or-patterns too)
 - rust-lang#121739 (Display short types for unimplemented trait)
 - rust-lang#121815 (Move `gather_comments`.)
 - rust-lang#121835 (Move `HandleStore` into `server.rs`.)
 - rust-lang#121847 (Remove hidden use of Global)
 - rust-lang#121861 (Use the guaranteed precision of a couple of float functions in docs)
 - rust-lang#121875 ( Account for unmet T: !Copy in E0277 message)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3432d1b into rust-lang:master Mar 2, 2024
11 checks passed
@rustbot rustbot added this to the 1.78.0 milestone Mar 2, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 2, 2024
Rollup merge of rust-lang#121739 - jieyouxu:loooong-typename, r=estebank

Display short types for unimplemented trait

Shortens unimplemented trait diagnostics. Now shows:

```
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`.
```

I'm not 100% sure if this is desirable, or if we should just let the long types remain long. This is also kinda a short-term bandaid solution. The real long term solution is to properly migrate `rustc_trait_selection`'s error reporting to use translatable diagnostics and then properly handle type name printing.

Fixes rust-lang#121687.
@jieyouxu jieyouxu deleted the loooong-typename branch August 20, 2024 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Very wide type shows up in error output
5 participants