Sub topic 13
Cascading errors: fix the first one first
A single mistake in a Rust program can produce a long list of errors. This chapter explains why that happens, how to find the error that matters, and how to avoid chasing errors that will disappear on their own. It also covers the opposite case, where fixing one error makes new ones appear.
This chapter assumes you can read a rustc diagnostic (code, message, span, labels, help) and run cargo check. It also assumes you know the basic error families: mismatched types, moved values, and unresolved names.
Why one mistake produces many errors
The compiler does not report mistakes one at a time. It works in phases, and each phase can report several errors before the compiler stops.
- Parsing turns text into a syntax tree. A syntax error here usually stops the compiler before later phases run.
- Name resolution connects each name to a definition. Unresolved names and imports are reported here.
- Type checking infers and checks types and trait bounds for each function body.
- Borrow checking verifies ownership, moves, borrows, and lifetimes. It generally runs on a function only when that function type-checked cleanly.
Two effects follow from this design.
- Downstream damage. One wrong definition breaks every place that uses it. A changed function signature produces one error per call. These are separate diagnostics with one cause.
- Hidden errors. Later phases do not run on code that failed an earlier phase. Your list may be short only because the compiler has not reached the harder checks yet.
The compiler also suppresses some follow-on errors. After a failed lookup, it gives the binding an internal "error type" and stays quiet about later uses of it. This reduces noise but is not complete. Do not expect every secondary error to be hidden.
One cause, several errors
Here is a struct whose field was renamed from age to years. Two functions still use the old name.
struct User {
name: String,
years: u32,
}
fn describe(user: &User) -> String {
format!("{} is {}", user.name, user.age)
}
fn is_adult(user: &User) -> bool {
user.age >= 18
}
fn main() {
let u = User {
name: String::from("Ada"),
years: 36,
};
println!("{}", describe(&u));
println!("{}", is_adult(&u));
}cargo check reports E0609, "no field age on type &User", once for each use. Two errors appear, but there is one underlying decision: the field is now called years. You fix it by changing the two uses (or by renaming the field back). The count of errors tells you how many places the mistake touches. It does not tell you how much work remains.
struct User {
name: String,
years: u32,
}
fn describe(user: &User) -> String {
format!("{} is {}", user.name, user.years)
}
fn is_adult(user: &User) -> bool {
user.years >= 18
}
fn main() {
let u = User {
name: String::from("Ada"),
years: 36,
};
println!("{}", describe(&u));
println!("{}", is_adult(&u));
}Read the first error, not the last
The compiler prints errors in the order it finds them, and the final lines are only a summary:
error: aborting due to N previous errorsgives a count.Some errors have detailed explanations: E0308, E0609.suggests commands to run.
Neither line tells you what to change. Scroll to the top of the output and start with the first error[...] block.
Long output scrolls past your terminal buffer, and the first error is the one you lose. Two commands help.
cargo check 2>&1 | head -40
cargo check --message-format shortThe first command shows only the top of the output. The second prints one line per diagnostic, in the form file:line:column: error[CODE]: message. That form works well as a table of contents. You can then run plain cargo check to read the first one in full.
Why the first error is usually the best starting point
- Later errors are often consequences of earlier ones, such as a bad definition used in many places.
- Within one phase, diagnostics tend to appear in source order, so the first error is often the earliest line in the file.
- Earlier-phase errors always come before later-phase errors. A name-resolution error is reported before a type error.
When the first error is not the root cause
Sometimes the first line in the output is not the best place to start.
- Dependencies. If a dependency fails to compile, its errors appear before your crate's. Fix or update the dependency first.
- Macros. An error inside a macro expansion points at the macro call. The real fix may be the arguments you passed, or the macro's definition.
- Multiple files. Errors in different modules do not follow a fixed order. If an error mentions a type defined elsewhere, check that definition first.
- Warnings. Warnings print between errors. Ignore them while there are errors. An
unused variablewarning is often a side effect of the error you are fixing.
Fixing can reveal new errors
Fixing a syntax error often makes errors appear in code that you thought was fine. This does not mean you made things worse. The compiler can now see more of the program.
This program has a syntax error and a type error.
fn main() {
let items = vec![1, 2, 3];
let total: i32 = items.iter().sum();
let label: String = "total";
println!("{label}: {total}";
}The println! call is missing its closing parenthesis. The compiler reports the syntax error and stops. It does not mention label, because type checking has not run. After you add the ), the compiler reports E0308 for label: it expected String and found &str. Its help text suggests calling .to_string().
fn main() {
let items = vec![1, 2, 3];
let total: i32 = items.iter().sum();
let label: String = "total".to_string();
println!("{label}: {total}");
}The same effect happens between type checking and borrow checking. A function with a type error is generally not borrow-checked, so E0382 or E0502 errors can show up only after the type errors are gone. Plan for several rounds.
| What you see | Likely root cause | Where to look |
|---|---|---|
| The same error code repeated at many call sites | A definition changed (signature, field, type) | The definition, not the calls |
| Many unresolved names for one item | A missing `use` or a misspelled import | The first `E0432` or `E0433` |
| Only one syntax error, and the rest of the file looks fine | Later phases have not run yet | Fix the syntax, then re-run |
| Type errors vanish when you fix one `let` | The wrong type annotation flowed into later code | The first binding in the chain |
| Borrow errors appear only after type errors are fixed | Borrow checking was skipped for that function | Treat them as a new round |
A repeatable workflow
- Run
cargo check. If the output is long, useheador--message-format short. - Read only the first error. Use its code with
rustc --explainif the message is unclear. - Make one change that addresses that cause. If several errors share the cause, one edit may clear them all.
- Run
cargo checkagain. Do not batch many unrelated edits, because you lose the ability to tell which edit helped. - Repeat. Treat new errors as progress, not as failure.
Small steps matter most when you are unsure. If you fix three things at once and the output gets longer, you cannot tell which edit caused the new errors.
When the list does not shrink
If you fix the first error and the count stays the same or grows, check these before changing anything else.
- You fixed a consequence. The error you edited was caused by something earlier. Go back and re-read the very first error.
- A new phase ran. The count grew because the compiler reached type checking or borrow checking. Look at whether the new errors are of a different kind.
- The fix introduced a mismatch. For example, changing a type annotation can make other lines wrong. This is a real new error, so treat it as the next first error.
- Stale state. Rarely, an editor shows old diagnostics from a language server. Trust
cargo checkin the terminal.
If an error stays stubborn after these checks, the next chapter shows how to cut the program down to a minimal reproduction.
Exercise
A project prints 12 errors, and every one is E0061 ("this function takes 2 arguments but 3 arguments were supplied") for calls to area(w, h, d). You recently changed the definition of area to take only w and h. Which error should you read first, and what is the fastest fix if the old three-argument version was correct?
Show answer
Read the first E0061 and check the definition of area, because all 12 errors share one cause: the signature change. If the three-argument version was correct, restore the third parameter in the definition of area. That one edit should clear all 12 errors. Editing the 12 calls would be unnecessary work.
Exercise
You fix a missing closing parenthesis, and cargo check now reports a new E0308 error that was not there before. Did your fix cause it? What should you do next?
Show answer
No. The syntax error stopped the compiler before type checking, so the type error was always present but not yet reported. Treat the E0308 error as the new first error: read it in full, apply the suggested fix (or rustc --explain E0308), and re-run cargo check.