Skip to content

Fix confusing compiler diagnostic messages and error code docs - #159315

Open
axeroofficialtournament-del wants to merge 1 commit into
rust-lang:mainfrom
axeroofficialtournament-del:fixing_annotations
Open

Fix confusing compiler diagnostic messages and error code docs#159315
axeroofficialtournament-del wants to merge 1 commit into
rust-lang:mainfrom
axeroofficialtournament-del:fixing_annotations

Conversation

@axeroofficialtournament-del

Copy link
Copy Markdown

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.

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

rustbot commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@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 Jul 15, 2026
@rustbot

rustbot commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

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 (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 21 candidates

@rustbot

rustbot commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[TIMING:end] tool::ToolBuild { build_compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu, tool: "tidy", path: "src/tools/tidy", mode: ToolBootstrap, source_type: InTree, extra_features: [], allow_features: "", cargo_args: [], artifact_kind: Binary } -- 10.946
[TIMING:end] tool::Tidy { compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu } -- 0.000
fmt check
Diff in /checkout/compiler/rustc_parse/src/diagnostics.rs:1332:
 #[derive(Diagnostic)]
 #[diag("found a documentation comment that doesn't document anything", code = E0585)]
 #[help("doc comments must come before what they document, if a comment was intended use `//`")]
-#[note("if the following item has a syntax error, fix that first — a syntax error can cause this warning to appear even when the doc comment placement is correct")]
+#[note(
+    "if the following item has a syntax error, fix that first — a syntax error can cause this warning to appear even when the doc comment placement is correct"
+)]
 pub(crate) struct DocCommentDoesNotDocumentAnything {
     #[primary_span]
     pub span: Span,
fmt: checked 7020 files
Bootstrap failed while executing `test src/tools/tidy tidyselftest --extra-checks=py,cpp,js,spellcheck`
Currently active steps:
test::Tidy {  } at src/bootstrap/src/core/build_steps/test.rs:1566
Build completed unsuccessfully in 0:00:48

@workingjubilee workingjubilee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This PR fixes a bunch of long standing issues

Thank you for your PR, please separate the error code submissions from the other two changes.

View changes since this review

#[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")]

@workingjubilee workingjubilee Jul 15, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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")]

@workingjubilee workingjubilee Jul 15, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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")]

@workingjubilee workingjubilee Jul 15, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

View changes since the review

@mati865

mati865 commented Jul 15, 2026

Copy link
Copy Markdown
Member

r? workingjubilee since they already did partial review? feel free to reroll

@rustbot rustbot assigned workingjubilee and unassigned mati865 Jul 15, 2026
@rustbot

rustbot commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

workingjubilee is currently at their maximum review capacity.
They may take a while to respond.

@mati865

mati865 commented Jul 15, 2026

Copy link
Copy Markdown
Member

Also CI is failing. @rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 15, 2026
@rustbot

rustbot commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@workingjubilee

Copy link
Copy Markdown
Member

The new --explain submissions are probably something we can just decide to r+ or not, with only a review for correctness. I only have strong opinions on the things that relate to triage of stuff.

@rust-bors

rust-bors Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #160774) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

6 participants