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

Enforce short type string in more errors #136898

Closed
wants to merge 1 commit into from

Conversation

estebank
Copy link
Contributor

error[E0529]: expected an array or slice, found `(..., ..., ..., ...)`
  --> $DIR/long-E0529.rs:10:9
   |
LL |     let [] = x;
   |         ^^ pattern cannot match with input type `(..., ..., ..., ...)`
   |
   = note: the full name for the type has been written to '$TEST_BUILD_DIR/$FILE.long-type-hash.txt'
   = note: consider using `--verbose` to print the full type name to the console
error[E0609]: no field `field` on type `(..., ..., ..., ...)`
  --> $DIR/long-E0609.rs:10:7
   |
LL |     x.field;
   |       ^^^^^ unknown field
   |
   = note: the full name for the type has been written to '$TEST_BUILD_DIR/$FILE.long-type-hash.txt'
   = note: consider using `--verbose` to print the full type name to the console
error[E0614]: type `(..., ..., ..., ...)` cannot be dereferenced
  --> $DIR/long-E0614.rs:10:5
   |
LL |     *x;
   |     ^^
   |
   = note: the full name for the type has been written to '$TEST_BUILD_DIR/$FILE.long-type-hash.txt'
   = note: consider using `--verbose` to print the full type name to the console
error[E0618]: expected function, found `(..., ..., ..., ...)`
  --> $DIR/long-E0618.rs:10:5
   |
LL | fn foo(x: D) {
   |        - `x` has type `(..., ..., ..., ...)`
LL |     x();
   |     ^--
   |     |
   |     call expression requires function
   |
   = note: the full name for the type has been written to '$TEST_BUILD_DIR/$FILE.long-type-hash.txt'
   = note: consider using `--verbose` to print the full type name to the console

Use multipart suggestion for cast -> into:

error[E0604]: only `u8` can be cast as `char`, not `u32`
  --> $DIR/E0604.rs:2:5
   |
LL |     1u32 as char;
   |     ^^^^^^^^^^^^ invalid cast
   |
help: try `char::from_u32` instead
   |
LL -     1u32 as char;
LL +     char::from_u32(1u32);
   |

CC #135919.

```
error[E0529]: expected an array or slice, found `(..., ..., ..., ...)`
  --> $DIR/long-E0529.rs:10:9
   |
LL |     let [] = x;
   |         ^^ pattern cannot match with input type `(..., ..., ..., ...)`
   |
   = note: the full name for the type has been written to '$TEST_BUILD_DIR/$FILE.long-type-hash.txt'
   = note: consider using `--verbose` to print the full type name to the console
```
```
error[E0609]: no field `field` on type `(..., ..., ..., ...)`
  --> $DIR/long-E0609.rs:10:7
   |
LL |     x.field;
   |       ^^^^^ unknown field
   |
   = note: the full name for the type has been written to '$TEST_BUILD_DIR/$FILE.long-type-hash.txt'
   = note: consider using `--verbose` to print the full type name to the console
```
```
error[E0614]: type `(..., ..., ..., ...)` cannot be dereferenced
  --> $DIR/long-E0614.rs:10:5
   |
LL |     *x;
   |     ^^
   |
   = note: the full name for the type has been written to '$TEST_BUILD_DIR/$FILE.long-type-hash.txt'
   = note: consider using `--verbose` to print the full type name to the console
```
```
error[E0618]: expected function, found `(..., ..., ..., ...)`
  --> $DIR/long-E0618.rs:10:5
   |
LL | fn foo(x: D) {
   |        - `x` has type `(..., ..., ..., ...)`
LL |     x();
   |     ^--
   |     |
   |     call expression requires function
   |
   = note: the full name for the type has been written to '$TEST_BUILD_DIR/$FILE.long-type-hash.txt'
   = note: consider using `--verbose` to print the full type name to the console
```

Use multipart suggestion for cast -> into:

```
error[E0604]: only `u8` can be cast as `char`, not `u32`
  --> $DIR/E0604.rs:2:5
   |
LL |     1u32 as char;
   |     ^^^^^^^^^^^^ invalid cast
   |
help: try `char::from_u32` instead
   |
LL -     1u32 as char;
LL +     char::from_u32(1u32);
   |
```
@rustbot
Copy link
Collaborator

rustbot commented Feb 12, 2025

r? @compiler-errors

rustbot has assigned @compiler-errors.
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 12, 2025
@compiler-errors
Copy link
Member

r? compiler

@rustbot rustbot assigned oli-obk and unassigned compiler-errors Feb 12, 2025
@@ -1222,13 +1223,14 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
),
};
let prefix = if !self.implements_clone(ty) {
let msg = format!("`{ty}` doesn't implement `Copy` or `Clone`");
let t = self.infcx.tcx.short_string(ty, err.long_ty_path());
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems a bit awful to have to do this everywhere. Can't we do something similar to the other global settings that change formatting for a scope?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I agree. My initial hope was to make it so only main messages needed to use this, but an even better approach would be to continue moving towards structured diagnostics and making the machinery handle the printing of Tys to always use short strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@oli-obk would you prefer we came up with a "hands off" solution that doesn't require as much redundant logic first, or land this while I work on that?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea, I think the added verbosity to diagnostics impls that will get reverted later is not worth it now and would def prefer going for another solution first

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm encountering a small issue: TyCtxt contains the DiagCtxt, which gets constructed early. In order to properly shorten types, you need a FmtPrinter, which has a TyCtxt within it. I don't think that we can make it so DiagCtxt contains an optional TyCtxt (I'm assuming if I go down that route I'll end up with a cycle), which instead would require me to pass a tcx to every create_err and emit_err. There are >1500 of these. I created a new trait with two impls, one that shortens and one on () that does nothing, so when you don't have a TyCtxt it doesn't attempt to shorten, but I would still have to touch a significant part of the codebase to introduce this. And it would still only work for structured diagnostics. I think that for raw access I could introduce a macro so that instead of doing err.span_label(span, format!("{ty}")) we would do span_label!(tcx, err, span, "{ty}"), which should potentially allow me to customize the printing behavior when it comes to things that can be shortened, but whatever pattern I can think of, it will be intrusive one way or another :-/

Copy link
Contributor

Choose a reason for hiding this comment

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

Huh. The other schemes just use TLS I think?

@estebank estebank closed this Feb 18, 2025
fmease added a commit to fmease/rust that referenced this pull request Feb 25, 2025
…, r=oli-obk

Teach structured errors to display short `Ty<'_>`

Make it so that in every structured error annotated with `#[derive(Diagnostic)]` that has a field of type `Ty<'_>`, the printing of that value into a `String` will look at the thread-local storage `TyCtxt` in order to shorten to a length appropriate with the terminal width. When this happen, the resulting error will have a note with the file where the full type name was written to.

```
error[E0618]: expected function, found `((..., ..., ..., ...), ..., ..., ...)``
 --> long.rs:7:5
  |
6 | fn foo(x: D) { //~ `x` has type `(...
  |        - `x` has type `((..., ..., ..., ...), ..., ..., ...)`
7 |     x(); //~ ERROR expected function, found `(...
  |     ^--
  |     |
  |     call expression requires function
  |
  = note: the full name for the type has been written to 'long.long-type-14182675702747116984.txt'
  = note: consider using `--verbose` to print the full type name to the console
```

Follow up to and response to the comments on rust-lang#136898.

r? `@oli-obk`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants