Description
There are two related issues with error messages generated with cargo fix
when the code starts in a broken state.
Example 1
If the code starts with both an error, and suggestion diagnostics, cargo fix --broken-code
prints a large message that tells you there is a bug and to use --broken-code
. That is not a good suggestion since the user is already using --broken-code
, and there isn't a bug.
// WARNING: unused_imports suggestion will remove this line
use std::mem;
fn main() {
// ERROR: mismatched types
let x: i32 = "foo";
}
results in:
Checking foo v0.1.0 (/Users/eric/Temp/foo)
warning: failed to automatically apply fixes suggested by rustc to crate `foo`
after fixes were automatically applied the compiler reported errors within these files:
* src/main.rs
This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag
The following errors were reported:
error[E0308]: mismatched types
--> src/main.rs:6:18
|
6 | let x: i32 = "foo";
| --- ^^^^^ expected `i32`, found `&str`
| |
| expected due to this
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.
error[E0308]: mismatched types
--> src/main.rs:6:18
|
6 | let x: i32 = "foo";
| --- ^^^^^ expected `i32`, found `&str`
| |
| expected due to this
For more information about this error, try `rustc --explain E0308`.
error: could not compile `foo` due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `foo` due to previous error
If the first pass running rustc
fails, then I don't think PLEASE_REPORT_THIS_BUG should be displayed. Instead, a different error message should be used to explain that the code is starting in a broken state and cargo fix
cannot handle this. This message may need to be customized whether or not --broken-code
is used.
Example 2
Similar to above, if the code does not have any suggestions, and is a broken state, then it displays a relatively terse output.
fn main() {
this_does_not_compile(); // ERROR
}
cargo fix
displays (with or without --broken-code
):
Checking foo v0.1.0 (/Users/eric/Temp/foo)
error[E0425]: cannot find function `this_does_not_compile` in this scope
--> src/main.rs:2:5
|
2 | this_does_not_compile(); // ERROR
| ^^^^^^^^^^^^^^^^^^^^^ not found in this scope
For more information about this error, try `rustc --explain E0425`.
error: could not compile `foo` due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `foo` due to previous error
I think in this situation, it might be good to display some kind of message explaining that the code is starting in a broken state and that cargo fix
needs to start with passing code. This message may need to be customized based on whether --broken-code
is used.
Notes
These two examples may have the same solution.
A closely related issue is #8806 where Cargo does not tell you that non-machine-applicable suggestions aren't applied. This can cause confusion as to why suggestions aren't being applied, even with --broken-code
.
Meta
cargo 1.64.0-nightly (85b500cca 2022-07-24)
release: 1.64.0-nightly
commit-hash: 85b500ccad8cd0b63995fd94a03ddd4b83f7905b
commit-date: 2022-07-24
host: x86_64-apple-darwin
libgit2: 1.4.2 (sys:0.14.2 vendored)
libcurl: 7.79.1 (sys:0.4.55+curl-7.83.1 system ssl:(SecureTransport) LibreSSL/3.3.6)
os: Mac OS 12.4.0 [64-bit]