Fix confusing compiler diagnostic messages and error code docs - #159315
Fix confusing compiler diagnostic messages and error code docs#159315axeroofficialtournament-del wants to merge 1 commit into
Conversation
This commit addresses confusing, misleading, and incomplete error messages across several Rust compiler error codes, as documented in issues rust-lang#115015, rust-lang#82788, rust-lang#92531, rust-lang#46807, rust-lang#104739, rust-lang#60917, rust-lang#42226, rust-lang#34721, rust-lang#65254, rust-lang#71982. CATEGORY 1: ERROR MESSAGE WORDING - E0521: Rewrite explanation to define 'escapes' clearly (the reference outlives the closure body), explain why it is a lifetime error rather than a style issue, fix grammar ('Consider to drop it' -> 'Consider dropping it'), and add a concrete example showing the lifetime mismatch. - E0603: Add note about the #[non_exhaustive] false-positive case where 'unit variant is private' appears on enum variants that cannot actually be private; cross-link to E0638. - E0638: Expand documentation with clear rules for downstream crates (enum wildcards, struct .., variant matching), concrete before/after examples, and a cross-link back to E0603. - E0382: Add a dedicated section for the nested-loop 'value moved in previous iteration' variant; improve the 'reinitialization might get skipped' wording; show three fix options (continue 'outer, borrow, clone). - E0038: Add a leading 'What is a trait object?' explanation; add a new 'Common mistake' section for using a trait as a direct parameter type (the Into<String> case), with impl Trait / generic fixes. - E0308: Add a 'semicolon makes a function return ()' section explaining what () is and why removing the semicolon fixes it; add a 'function argument has wrong type' section showing &String vs &str coercion. - E0277: Add a dedicated '? operator and From trait' section explaining that ? implicitly calls From::from, showing the expansion, and providing three fix options (impl From, map_err, Box<dyn Error>). - E0283: Add an 'Annotating into() — turbofish does not work on trait methods' section; show the three correct annotation approaches; cross-link to E0035. - E0035: Create new documentation file explaining that turbofish cannot be used on trait methods, with the correct alternatives and a cross-link to E0283. CATEGORY 2: CONTRADICTORY/INCORRECT TERMINOLOGY - E0658: Add a note clarifying that let is a statement (not an expression) and cannot be used in arbitrary expression positions; mention let-chains. - rustc_parse diagnostics: Add a note to DocCommentDoesNotDocumentAnything that a syntax error in the following item can cause the warning to appear even when the doc comment placement is correct (masking the real error). CATEGORY 3: ERROR CODE DOCUMENTATION - E0117: Add 'Understanding local vs foreign types' section; document the three specific cases that trigger the orphan rule error with runnable examples; add a newtype fix pattern. CATEGORY 4: DIAGNOSTIC INFRASTRUCTURE - session_diagnostics (ICE): Augment the 'unexpectedly panicked' diagnostic struct with notes directing users to file a bug report, set RUST_BACKTRACE=1, and try cargo clean before reporting.
|
The parser was modified, potentially altering the grammar of (stable) Rust cc @fmease |
|
Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @mati865 (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
Why was this reviewer chosen?The reviewer was selected based on:
|
|
|
The job Click to see the possible cause of the failure (guessed by this bot) |
| #[diag("the compiler unexpectedly panicked. This is a bug")] | ||
| #[note("we would appreciate a bug report with a minimal reproduction at the URL below")] | ||
| #[note("set `RUST_BACKTRACE=1` environment variable to display a backtrace")] | ||
| #[note("try running `cargo clean` and rebuilding; transient failures do sometimes occur")] |
There was a problem hiding this comment.
We want to know when incremental errors are repeatedly recurring, and this feels like it tacitly encourages people to not report them. We could say something but I'm not sure it is this sort of "don't worry about it" thing?
|
|
||
| #[derive(Diagnostic)] | ||
| #[diag("the compiler unexpectedly panicked. This is a bug")] | ||
| #[note("we would appreciate a bug report with a minimal reproduction at the URL below")] |
There was a problem hiding this comment.
We do not require a minimal reproduction. We need any reproducer. And the link itself contains explanation of what we are hoping for in an ideal bug report.
|
|
||
| #[derive(Diagnostic)] | ||
| #[diag("the compiler unexpectedly panicked. This is a bug")] | ||
| #[note("we would appreciate a bug report with a minimal reproduction at the URL below")] |
There was a problem hiding this comment.
The "URL below" is on a line that says:
note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md
So this is not really new information?
|
r? workingjubilee since they already did partial review? feel free to reroll |
|
|
|
Also CI is failing. @rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
|
The new |
|
☔ The latest upstream changes (presumably #160774) made this pull request unmergeable. Please resolve the merge conflicts by rebasing. |
This PR fixes a bunch of long standing issues in rust’s error messages. Many were baffling or even misleading or simply didn't tell me what the real problem was. I have been waiting to clean a while and there were a lot of open issues around these.
What I changed to improve personally.
Slightly better wording of error messages:
The old “escapes the closure body” message is never clear:
E0521. I changed the explanation to actually say what “escapes” means (the reference outlives the scope it was borrowed in). Also I indicated that it is not a style suggestion, but actually a mistake and corrected the problematic “Consider to drop it” grammar.
E0603 : Sometimes we were emitting “unit variant is private”, which is wrong enum variants are not private! I made a comment about the false positive, and directed the user to the correct error (E0638).
E0638 : updated the documentation so that the rules it must follow are explained and includes concrete match examples on what breaks and fixes the document. I also created a cross link to E0603.
The case for the nested loop is a completely different from the simple move error one and was hidden within the same docs – The nested loop case is (this reinitialization might get skipped). I dedicated a section for it so it's not mixed in with the other sections, and included the continue 'outer fix and clone/borrow alternatives.
For E0038, I finally put an explanation at the very top of this, because many of the questions are confusing because of lack of understanding of what an “object” is. I also included a note about the common error of using Into as a parameter type, rather than impl Into.
E0308 : I've added special cases for two common beginner traps: function argument mismatch and the semicolon traps expression into the parentheses. The latter is something nearly everyone new to the language doesn't understand what it is.
E0277 : From not implemented — No mention of the ? operator. I included a section of explaining the implicit conversion, what ? expands to, and 3 ways to remedy it.
E0283: This is a common error where the people are trying to call .into::() on y, with no helpful error message. I added a section to the reasons why turbofish doesn't work with trait methods and how it should be done.
Fixed contradictory terminology:
E0658 / parser : The old “let expressions are not supported here” message is in contradiction with the Book which rightfully refers to “let a statement”. A note was added to E0658 to remind people about the difference between statement and expression (which had already been used by the parser in the new diagnostic).
Avoid the actual syntax error by getting the doc comment diagnostic instead of “this doc comment doesn’t document anything”. I added a note to explain that a syntax error in the next item can cause this warning, so that users will know to scroll down.
Filled documentation gaps:
E0117 : Orphan rule was explained, but people were not helped to recognise the three cases where the rule applies. I've included the explainer of “local vs foreign” with runnable examples for both cases, along with the common fix of the “newtype” pattern.
There was no .md file at all found in the directory with this error code: E0035 I wrote one to explain why turbofish does not work with trait methods, and what to do about that. I also cross linked it from E0283.
Better ICE messages:
session_diagnostics : Panic by the compiler did not provide any clue for the user on what to do next. To the basic, I added helpful notes to direct people to set RUST_BACKTRACE=1, cargo clean and report a bug; all of these things people need to know, but they have to google.