-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Conversation
``` 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 has assigned @compiler-errors. Use |
r? compiler |
@@ -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()); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 Ty
s to always use short strings.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :-/
There was a problem hiding this comment.
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?
…, 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`
Use multipart suggestion for cast -> into:
CC #135919.