4. Reading errors
In Chapter 3 the compiler refused about a dozen of our programs. That's normal. Most of the time a compiler spends with a program, the program is wrong, because you're in the middle of writing it. So it pays to get fast at reading what the compiler says: where to look, what the message is telling you, and when the suggested fix is the right one.
In this chapter we'll take a compiler error apart piece by piece, see how far coax0 gets through a broken program before it stops, and then fix a program with six mistakes in it, one round at a time, until it runs. We'll finish with the errors that happen while a program runs, and with the two features meant for tools: coax explain and --json.
Note: Every diagnostic in this chapter was produced by running
coax00.10.0 on the program shown.
The parts of an error
Here's a short program with four mistakes in it:
Filename: half.coax
#import "Basic";half :: (n: int) -> int { if n > 0 { return n / 2; }}main :: () { name := "Kiki"; if name { print("%\n"); } x: int = name;}Listing 4-1: A program with four mistakes
coax check compiles a file without building or running it, which makes it the quickest way to see what the compiler thinks:
$ coax check half.coaxhalf.coax:3:1: error[E0338]: 'half' can reach its end without returning int half :: (n: int) -> int { ^ fix: add a return at the endhalf.coax:9:6: error[E0331]: condition must be bool, found string if name { print("%\n"); } ^half.coax:9:19: error[E0320]: format has 1 '%' slot but print was given 0 values if name { print("%\n"); } ^ fix: write '%%' for a literal percent signhalf.coax:10:12: error[E0325]: 'x' is int but is initialized with string x: int = name; ^4 errorsLet's read the first one line by line.
half.coax:3:1: error[E0338]: 'half' can reach its end without returning intThe line starts with where the problem is: file, line and column, half.coax:3:1. That's the format most editors and terminals already recognize, so in many setups you can click it to jump to the spot.
Next comes error[E0338]. Every diagnostic has a code like this: E, then four digits. The code names the kind of mistake, and it doesn't change between compiler versions even if the wording does. When a code stops being used, it's marked "retired" rather than handed to a different error. That's what lets a script, a test or an agent match on E0338 instead of on the English text.
After the code is the message itself, one sentence that names the things involved: the procedure 'half' and the type int. Good messages say "'x' is int but is initialized with string", not "type mismatch".
half :: (n: int) -> int { ^Then the compiler prints the source line and puts a caret under the column it's talking about. Here the caret is at the start of the procedure, because the problem belongs to half as a whole: some path through it reaches the closing brace with no return.
fix: add a return at the endLast, when the compiler knows a likely edit, it says so on a fix: line. Not every error has one. The second error, about if name, doesn't, because the compiler can't guess what you meant to test about a string.
After the last error, 4 errors gives the count. coax check exits with status 1 when there are errors, and when there aren't, it prints nothing at all and exits with status 0.
Fix lines are suggestions
Read the third error again:
half.coax:9:19: error[E0320]: format has 1 '%' slot but print was given 0 values if name { print("%\n"); } ^ fix: write '%%' for a literal percent signThe message is exactly right: the format has a % and there's no value for it. The fix assumes we meant a literal percent sign, which isn't what happened here. We forgot the value. The right edit is print("%\n", name);. A fix: line is the compiler's best guess at the most common cause. It's often right, and it's always worth reading, but the message above it is the part to trust.
Fix from the top
When you get several errors, start with the first one. Errors are listed in the order they appear in the file, and an early mistake sometimes explains a later one. Then run coax check again. It takes a fraction of a second, and seeing the new list is faster than reasoning about which old errors are still real.
Asking the compiler to explain
Every code has a longer explanation, which you can ask for with coax explain:
$ coax explain E0338E0338: missing return Some path reaches the end of a procedure that must return a value.$ coax explain E0331E0331: condition must be bool if, while and ifx take a bool. Compare explicitly: x != 0.The first line is the code's short name and the second is the rule behind it, often with the usual fix. Some explanations are more about the why:
$ coax explain E0204E0204: reserved word not supported yet The word is reserved for a planned feature, such as using or enum_flags. coax0 refuses it rather than guessing what it means.Codes are case-sensitive, and an unknown code says so:
$ coax explain E9999coax: no error code 'E9999'coax explain --all prints every code, and `coax explain --all --markdown` prints them as tables. That's how Appendix B is made, so the appendix and the compiler always list the same codes.
How far the compiler gets
coax0 reads a program in stages, and the first two digits of each code say which stage found the problem:
| Codes | Stage | What it does | After an error |
|---|---|---|---|
E01xx | Lexer | Turns characters into tokens | Reports every bad token, then lets the parser report its errors too |
E02xx | Parser | Turns tokens into a syntax tree | Reports one error per top-level declaration |
E03xx | Checker | Names, types, returns, formats | Reports everything it finds |
E04xx | Compile-time evaluation | Runs #run and #assert (Chapter 9) | Stops at the first failure |
E05xx | Descriptor imports | Reads protobuf descriptors (Chapter 18) | Stops at the first failure |
E06xx | Source loading | Admits the source graph and physical units (Chapter 10) | Reports each #load that fails, and keeps loading the rest |
The checker and everything after it only run if loading, lexing and parsing succeeded. The first error a stage reports is always the same one it would report alone, and the exit status is the same. You can see how far each stage goes with a program that has a bad character and a missing semicolon:
Filename: stages.coax
#import "Basic";main :: () { x := 1 $ 2; y := 3 z := 4;}$ coax check stages.coaxstages.coax:3:10: error[E0108]: unexpected character '$' x := 1 $ 2; ^stages.coax:3:9: error[E0201]: expected ';' after the declaration, found integer x := 1 $ 2; ^ fix: add ';' note: this may follow from the error at stages.coax:3:10; fix that one firstThe lexer drops the $ and reports it. The parser then reads what is left, x := 1 2;, and reports that too, with a note: the error may follow from the lexer's, so fix that one first. Here it does. Delete the $ and the line is fine.
The missing ; on line 4 isn't reported yet. After a syntax error, the parser doesn't try to guess where the next statement in the same procedure starts. When it guesses wrong it reports errors that aren't real, and chasing those wastes time; an agent fixing errors in a loop can waste a lot of it. Instead it skips to the next top-level declaration, a line that starts with name :: or with #import, #assert, #test, #forall, #run_steps or #authority in the first column, and parses again from there. So you get at most one syntax error per declaration, and errors in other declarations at once. Fix the $ here and the parser reports the missing ; on line 4.
So a short error list doesn't always mean you're nearly done. It may mean the compiler hasn't got that far yet.
When a skipped declaration doesn't close its braces, the parser's guess of where the next declaration starts may be wrong, so the first error after it gets the same kind of note. A note means "probably not a separate problem": fix the error it names, then check again. The lexer and the parser report at most 20 errors in all, then say coax: stopped after 20 errors.
The checker is the opposite of the parser. By the time it runs, the shape of the program is known, so each type error it finds is a real one, and you want them all at once.
The checker also avoids a different kind of cascade. When it can't work out a variable's type because of an earlier error, it stays quiet about later uses of that variable instead of reporting each one. You'll see that happen in the walkthrough below.
Fixing a program, end to end
Let's put this to work. Here's a program that's supposed to work out how many apples are left and how they'd split between friends. It has several mistakes, of the kind that come up every day:
Filename: pantry.coax
main :: () { apples := 12 eaten: u8 = 3; left := apples - eaten; if left { print("% % left\n", plural(left)); } for people: 0..4 { print("shared by %: % each\n", people, split(left, people)); }}share :: (count: int, people: int) -> int { return count / people;}plural :: (n: int) -> string { if n == 1 { return "apple"; } else if n > 1 { return "apples"; }}Listing 4-2: pantry.coax, before we fix it
Round 1: a missing semicolon
$ coax check pantry.coaxpantry.coax:2:15: error[E0201]: expected ';' after the declaration, found 'eaten' apples := 12 ^ fix: add ';'1 errorOne error. The parser reports at most one per top-level declaration, and both syntax mistakes in this file are in main, so the second one waits for the next round. Look at where the caret is: right after 12 on line 2, even though the parser only noticed something was wrong when it reached eaten on line 3. A missing ; is reported where the ; belongs. The message still tells you what it found instead (found 'eaten'), which helps when the real problem is something else, like a stray word.
We add the semicolon: apples := 12;.
Round 2: a closed range
$ coax check pantry.coaxpantry.coax:6:16: error[E0216]: ranges are half-open: write '..<' for people: 0..4 { ^ fix: for i: 0..<n { }1 errorThe parser got further into main this time and reported the next problem. As we saw in Chapter 3, Coax has only half-open ranges. We meant 0 to 3, so the loop becomes for people: 0..<4 {.
Round 3: the checker's turn
Now the program parses, and the checker reports everything at once:
$ coax check pantry.coaxpantry.coax:4:18: error[E0309]: '-' needs two numbers of one type, found int and u8 left := apples - eaten; ^ fix: convert the right side: cast(int) eatenpantry.coax:5:19: error[E0320]: format has 2 '%' slots but print was given 1 value if left { print("% % left\n", plural(left)); } ^ fix: write '%%' for a literal percent signpantry.coax:7:44: error[E0315]: procedure 'split' is not declared print("shared by %: % each\n", people, split(left, people)); ^pantry.coax:15:1: error[E0338]: 'plural' can reach its end without returning string plural :: (n: int) -> string { ^ fix: add a return at the end4 errorsFour errors, each with its own cause. Notice also what isn't here: the file has no #import "Basic"; line, and print works anyway, because Basic is imported by default.
The first error is the one Chapter 3 warned about: apples is an int and eaten is a u8, and - won't mix them. The fix line gives the exact cast, cast(int) eaten. That would work, but it treats the symptom. Nothing about a count of eaten apples needs to be a u8, so the better fix is to declare it like apples: eaten := 3;.
Notice what's missing from this list. Line 5 has if left, and left isn't a bool, but there's no E0331 for it. Because the subtraction on line 4 failed, the checker doesn't know what type left has, so it doesn't guess. Once we fix line 4, left becomes an int and we'll see the error then.
We change eaten. While we're at the top of the file we also add the #import "Basic"; line and a blank line after it, as the book's other listings have: it's optional, and it says where print comes from.
Round 4: what was hiding
$ coax check pantry.coaxpantry.coax:7:6: error[E0331]: condition must be bool, found int if left { print("% % left\n", plural(left)); } ^ fix: compare it: x != 0pantry.coax:7:19: error[E0320]: format has 2 '%' slots but print was given 1 value if left { print("% % left\n", plural(left)); } ^ fix: write '%%' for a literal percent signpantry.coax:9:44: error[E0315]: procedure 'split' is not declared print("shared by %: % each\n", people, split(left, people)); ^pantry.coax:17:1: error[E0338]: 'plural' can reach its end without returning string plural :: (n: int) -> string { ^ fix: add a return at the end4 errorsThe line numbers moved down by two because we added the import and a blank line. And there's the E0331 that was hiding. We'll take the four in order:
if leftneeds abool. Coax never treats a number as true or false. We want "if any are left", so we writeif left > 0.- The format has two slots, for the count and the word, but we only passed the word. Here the fix line's guess about a literal percent sign is wrong, and the message is what matters. We add the missing value:
print("% % left\n", left, plural(left));. splitisn't declared. We named the procedureshareand then called it by a different name. Coax doesn't suggest a near match, so when you seeE0315orE0306, check the spelling against the declaration. We change the call toshare(left, people).pluralhas tworeturns, both insideifs. Ifnis zero or negative, neither runs. The fix line says "add a return at the end", and that's right: the last case should be the default, so we replace theelse ifline withreturn "apples";.
Round 5: it compiles, then it stops
$ coax check pantry.coax$ coax run pantry.coax9 apples leftpantry.coax:14: division by zerocoax check printed nothing, which means no errors. Running it gets partway and then stops. This is a runtime error, which the compiler couldn't have caught: the program divided by zero while running. The message names the file and the line where it happened, line 14, which is the return count / people; in share.
The line where a program stops isn't always where the bug is. share did what it was told. The bug is in the caller: the loop starts at 0, and sharing between zero people makes no sense. So we fix the loop, not share, and start it at 1:
Filename: pantry.coax
#import "Basic";main :: () { apples := 12; eaten := 3; left := apples - eaten; if left > 0 { print("% % left\n", left, plural(left)); } for people: 1..<4 { print("shared by %: % each\n", people, share(left, people)); }}share :: (count: int, people: int) -> int { return count / people;}plural :: (n: int) -> string { if n == 1 { return "apple"; } return "apples";}Listing 4-3: pantry.coax, fixed
$ coax run pantry.coax9 apples leftshared by 1: 9 eachshared by 2: 4 eachshared by 3: 3 eachNice! Five rounds, and each round's error list told us what to do next. The order they came in is worth remembering: the lexer and parser report one problem at a time, so fix and re-run quickly; the checker reports all of its problems together, so read the whole list and look for errors that share a cause; and a program that compiles can still stop at run time, at a line that may not be where the mistake is.
"Not yet" is different from "no"
Coax reserves the vocabulary for the whole language now, including parts coax0 can't compile yet. When you use one of those, the message says "not supported by coax0 yet", which is different from the message for a name that doesn't exist:
Filename: types.coax
main :: () { x: flaot = 1.0; y: fixed32;}$ coax check types.coaxtypes.coax:2:6: error[E0302]: unknown type 'flaot' x: flaot = 1.0; ^types.coax:3:6: error[E0301]: type 'fixed32' is not supported by coax0 yet y: fixed32; ^2 errorsflaot is a typo, so it's an unknown type. fixed32 is a real Coax type that coax0 doesn't implement, and the compiler says exactly that. The same goes for planned keywords:
layers.coax:1:10: error[E0204]: 'enum_flags' is reserved and not supported by coax0 yet Layer :: enum_flags u32 { solid; visible; } ^1 errorThis tells you whether to fix your spelling or change your approach. It also keeps unsupported syntax from acquiring an accidental meaning before its feature arrives. enum_flags and fixed32 remain planned. Exhaustive switch over enums and tagged unions already works; Chapter 5 runs it in examples/ch05_tagged.coax. Chapter 28 lists the remaining work.
Errors while compiling your code
Some code runs while the program compiles: constants built with #run, and #assert checks, both in Chapter 9. When that code fails, the error points at the line that failed and then lists how the compiler got there:
Filename: slots.coax
#import "Basic";SLOTS :: #run slots_for(40);slots_for :: (rooms: int) -> int { return capacity(rooms) / (rooms - 40);}capacity :: (rooms: int) -> int { return rooms * 8; }main :: () { print("%\n", SLOTS); }$ coax check slots.coaxslots.coax:6:26: error[E0407]: division by zero during compile-time evaluation return capacity(rooms) / (rooms - 40); ^ note: compile-time call at slots.coax:3:151 errorThe note: lines are the call stack, innermost first. Here there's one: the division happened inside slots_for, which was called from the #run on line 3. In a deeper chain you'd see one note per call, with the outermost last. The list is cut off at 64 lines, so a runaway recursion can't flood the terminal.
Compile-time evaluation only runs on a program the checker accepted. If your file has type errors and a failing #assert, you'll see the type errors first, and the assertion once they're fixed.
When a limit runs out
The compiler's work is bounded, so a program can't make it run forever or use all the machine's memory. Each bound is a fixed amount of one resource:
| Resource | Limit | Error | How to raise it |
|---|---|---|---|
| Compile-time steps | 100,000 per compile; 20,000,000 per check | E0400 | #run_steps N;, --run-steps N; #steps N on a check, coax test --steps N |
| Compile-time traversal work | 134,217,728 value and field visits per compile or check | E0400 | --traversal N; #traversal N on a check |
| Compile-time value slots | 1,572,864 per compile; 33,554,432 for checks | E0400 | Fixed; coax test --slots N and coax mutate --slots N only lower it |
| Generic instances | 256, or one per 8 syntax nodes in a larger program | E0339 | Fixed |
| Checker node store | 16 times the program's own nodes | E0381 | Fixed |
When one runs out, the error names the resource, how much was needed and the limit, the parts of your program that used the most of it, and one change that would use less. Here's a check that passes a whole Game struct by value on every step of a loop:
Filename: page.coax
Game :: struct { cells: [1024] int; tick: int; }step :: (g: Game) -> int { return g.tick + 1; }#test "a page frame" { g: Game; t := 0; for i: 0..<200000 { t += step(g); } #assert t == 200000;}main :: () {}$ coax test page.coaxFAIL #test a page frame (1884 ms)1 check, 0 passed, 1 failedpage.coax:8:33: error[E0400]: compile-time aggregate traversal limit exceeded: 134217729 value and field visits needed, the limit is 134217728 for i: 0..<200000 { t += step(g); } ^ compile-time traversal work: 134217729 visits needed, the limit is 134217728 top users of compile-time traversal work: 134216702 visits page.coax:8:28 copying arguments into 'step' (130689 calls) 1026 visits page.coax:5:1 #test 'a page frame' itself fix: pass a view, an index or only the fields it needs instead of copying arguments into 'step' by value; or split the check (each check has its own traversal budget), or raise its budget with #traversal N on the check (or --traversal N for every check) note: in #test a page frameEach call copies the struct's 1,025 values, so the copies at line 8, column 28, used almost the whole budget. The compiler charges a by-value argument as a copy even when it can tell that step only reads g and lets it read the caller's storage: the visits and the report are the same, and only the time differs. The top users lines list at most five, largest first, each with its amount, where it is and what it is:
- For steps and traversal work, a procedure's own use (
in 'build', at its declaration; what its calls use is counted against them), the argument copies at a call site (copying arguments into 'step', at the call), and the top-level work of a constant, a#runor a check. - For value slots, the arrays and structs that were alive when the limit was reached, by the expression that made them, and the request that went over.
- For generic instances, each generic procedure or struct with its number of instances, at its declaration; for the node store, the nodes its instances copied.
The fix: line is one practical reduction, chosen from the top user: pass less by value, compute a table at run time or load it as data, split a check, bind a generic to fewer distinct types, or raise a budget that can be raised.
Near-limit notes
A command that uses more than three quarters of a limit still succeeds, and ends with a note that gives the same breakdown, so you can act before the next change crosses the limit:
$ coax test page.coax --traversal 300000000 --near-limit 60ok #test a page frame (1 case, 2201035 steps, 2846 ms)1 check, 1 passed, 0 failedcoax: note[E0400]: compile-time traversal work: 205401026 of 300000000 used (68%), above the near-limit threshold of 60%; more than 300000000 fails with E0400 top users of compile-time traversal work: 205400000 visits page.coax:8:28 copying arguments into 'step' (200000 calls) 1026 visits page.coax:5:1 #test 'a page frame' itself fix: pass a view, an index or only the fields it needs instead of copying arguments into 'step' by value; or split the check (each check has its own traversal budget), or raise its budget with #traversal N on the check (or --traversal N for every check)--near-limit P sets the threshold to P percent of each limit, from 1 to 100; the COAX_NEAR_LIMIT environment variable sets it for every command that doesn't pass the option. The default is 75. A check's own steps and traversal work, and the value slots the checks hold at their peak, are reported the same way as the compile's.
Errors while the program runs
Round 5 showed a runtime error. coax0 turns the operations that C leaves undefined into clean stops, each with the Coax source file and line, printed to standard error:
overflow.coax:6: integer overflow in '+'bounds_trap.coax:4: index out of boundsconversions.coax:12: cast out of rangeThe program exits with status 134, the status of an abort, so a script can tell a crash from a normal failure. Some of these checks are only in debug builds, and some stay in release builds too:
| What | Debug build | --release |
|---|---|---|
Integer overflow (+ - *, unary -) | Stops | Wraps |
Division or % by zero | Stops | Stops |
The smallest int divided by -1 | Stops | Stops |
| Index or slice out of bounds | Stops | Stops |
cast(T) of a value that doesn't fit | Stops | Stops |
| Shift by a negative count, or by the width or more | Stops | Stops |
Overflow wraps in release builds because that's what the hardware does and checking every addition has a cost. The others stop in both, because there's no reasonable value to carry on with. Chapter 7 covers handling failure on purpose, and Chapter 11 covers finding these bugs with checks before the program ever runs.
Errors for tools
Everything so far is formatted for a person in a terminal. Tools, including editors, test scripts and agents, want something they can parse. Add --json and each diagnostic comes out as one JSON object per line. Here's round 4 of the pantry program again:
$ coax check pantry.coax --json{"file": "pantry.coax", "line": 7, "col": 6, "code": "E0331", "message": "condition must be bool, found int", "fix": "compare it: x != 0"}{"file": "pantry.coax", "line": 7, "col": 19, "code": "E0320", "message": "format has 2 '%' slots but print was given 1 value", "fix": "write '%%' for a literal percent sign"}{"file": "pantry.coax", "line": 9, "col": 44, "code": "E0315", "message": "procedure 'split' is not declared", "fix": ""}{"file": "pantry.coax", "line": 17, "col": 1, "code": "E0338", "message": "'plural' can reach its end without returning string", "fix": "add a return at the end"}Each object has the same six fields: file, line and col (both counted from 1), code, message, and fix, which is an empty string when there's no suggestion. A limit that ran out adds a seventh, capacity, described below. There's no count line at the end; count the lines instead. The exit status is the same as without --json: 0 for a clean file and 1 for errors. Like the text form, the JSON goes to standard error.
A tool that fixes errors in a loop can work from just these fields: jump to file:line:col, decide by code what kind of edit is needed, apply the fix when there is one, then run coax check again. Because codes are stable, that logic doesn't break when the wording of a message improves. Chapter 22 looks at how agents use this.
A capacity refusal's object carries the breakdown from When a limit runs out in its capacity field:
"capacity": {"resource": "compile-time traversal work", "unit": "visits", "used": 134217729, "limit": 134217728, "near_percent": 75, "consumers": [{"what": "copying arguments into 'step' (130689 calls)", "amount": 134216702, "file": "page.coax", "line": 8, "col": 28}, ...], "suggestion": "pass a view, an index or ..."}--diagnostics json prints the richer coax.diagnostic.v1 records, where the same object has a span for each consumer in place of file, line and col, and each near-limit note is a record with severity note, a null span and the same capacity object. --timings FILE keeps every limit's highest use in its capacities array, each row with that capacity object.
Note: Two things don't come out as JSON yet. A compile-time error's
note:lines are printed after its JSON object as plain text, in the same form as the terminal output, so a strict JSON-lines reader should skip lines that don't start with{. And runtime errors such asdivision by zerocome from the running program, not the compiler, so they're always plain text.
Summary
A Coax error has a location, a stable code, a message that names what's involved, the source line with a caret, and sometimes a fix: line, which is a suggestion to weigh against the message. coax explain CODE gives the rule behind any code. The lexer and parser stop early, so fix-and-rerun is the fastest way through them; the checker reports everything at once and stays quiet about errors that follow from earlier ones. "Not supported by coax0 yet" means a planned feature, not a typo. Runtime errors name the line where the program stopped, which may not be the line with the bug. And --json gives tools the same information one object per line.
That's the end of Part I. You can write, run and fix small Coax programs. In Part II we'll start building bigger ones, beginning in Chapter 5 with structs, enums and unions.