Sub topic 1
Anatomy of a compiler diagnostic
Prerequisites
This chapter assumes you can write basic Rust: functions, let bindings, structs, enums, and match. You should be able to run cargo build and cargo run in a terminal. You should also have met ownership and references once, though you do not need to master them yet.
Why read the whole diagnostic
A diagnostic is the structured message rustc prints when it rejects your code. Most of the fix is already in the message. Learners who read only the first line often guess at fixes. Learners who read all of it usually find the answer quickly.
Here is a small program with one error. The trailing semicolon turns the last expression into a statement, so the function returns () instead of i32.
fn double(x: i32) -> i32 {
x * 2;
}Compiling it prints a diagnostic shaped like this (exact wording varies by compiler version):
error[E0308]: mismatched types
--> src/main.rs:1:25
|
1 | fn double(x: i32) -> i32 {
| ------ ^^^ expected `i32`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
2 | x * 2;
| - help: remove this semicolon to return this valueThe parts of a diagnostic
Every diagnostic is built from the same pieces.
| Part | Example | What it tells you |
|---|---|---|
| Severity | `error` | `error` stops compilation. `warning` does not. |
| Error code | `E0308` | A stable identifier you can look up. |
| Message | `mismatched types` | A short statement of what went wrong. |
| Location | `src/main.rs:1:25` | File, line, and column of the primary span. |
| Primary span | `^^^` | The code the compiler blames most. |
| Labels | `expected i32, found ()` | Short notes attached to spans in your code. |
| Notes | `= note: ...` | Extra background about the rule that was broken. |
| Help | `help: remove this semicolon` | A suggested change. |
The primary span is underlined with carets (^). Secondary spans are underlined with dashes (-). The primary span is where the compiler detected the problem. The real cause is sometimes in a secondary span, as it is here: the semicolon on line 2 is the actual mistake.
How the pieces fit together
A reliable reading order:
- Check the severity and the code. Errors block the build, so start there.
- Read the message to learn the category of problem.
- Read the primary span and every label. Compare "expected" against "found".
- Read notes for the rule behind the error.
- Read the help line and decide whether the suggestion matches your intent.
Help text is a suggestion, not a command
The compiler cannot know what you meant. In the example, removing the semicolon is right. In another function, you might have meant to add a return or change the return type. Check that the suggestion matches your intent before you apply it.
Looking at a second example
An undefined name gives a diagnostic with the same parts.
fn main() {
let count = 3;
println!("{}", cnt);
}error[E0425]: cannot find value `cnt` in this scope
--> src/main.rs:3:20
|
3 | println!("{}", cnt);
| ^^^ help: a local variable with a similar name exists: `count`The code is E0425, the message names the missing value, and the label offers a close match. The fix is to write count.
Exercise
In a diagnostic, what is the difference between the primary span and a secondary span, and which one always contains the real mistake?
Show answer
The primary span (underlined with carets) is where the compiler detected the problem. Secondary spans (underlined with dashes) point to related code. Neither always holds the real mistake. In the double example, the primary span is the return type, but the actual mistake is the semicolon in a secondary span.
Exercise
A diagnostic reads error[E0425]: cannot find value total in this scope and the label says a similar name totals exists. List the parts you used to decide on a fix.
Show answer
The severity (error) and code (E0425) identify the category. The message names the missing value total. The label suggests totals. Before accepting it, confirm that totals is the variable you intended.