A warning for a procedure that may reach its end A procedure with results that can reach its end without return returns zeros (report 7.3), which is rarely meant. Each statement sets ends: return; an if with an else, or a match, whose every arm ends; a while true or repeat ... until false with no break. Warn reports a procedure whose body doesn't end. lib, examples and tools have none. doc/compiler.md: the warning, the hints of the last commit, the table of lines (7,761). PLAN.md: a warning for a dropped result waits for a way to drop one on purpose.
| @@ -17,4 +17,8 @@ | ||
| 17 | 17 | ## Wishes, for a later thaw |
| 18 | 18 | |
| 19 | 19 | What the language or the library might have, met while writing raz; |
| 20 | -none is built while the freeze holds. None waits now. | |
| 20 | +none is built while the freeze holds. | |
| 21 | + | |
| 22 | +- A warning for a call whose result is dropped. Code drops results on | |
| 23 | + purpose (`SYSCALL`, `Sys.write`; about 40 places in `lib/`), so it | |
| 24 | + needs a way to say so first, such as `_ := f(x)`. |
| @@ -24,7 +24,7 @@ | ||
| 24 | 24 | $ examples/Hello |
| 25 | 25 | hello, world |
| 26 | 26 | |
| 27 | -The compiler is about 7,600 lines of Free Pascal: one pass, no | |
| 27 | +The compiler is about 7,800 lines of Free Pascal: one pass, no | |
| 28 | 28 | intermediate form, no optimizer. It writes x86-64 machine code and the |
| 29 | 29 | ELF executable itself, with no assembler or linker, and compiles about |
| 30 | 30 | a million lines a second. |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Reading the raz compiler |
| 2 | 2 | |
| 3 | -The compiler is eleven files of Free Pascal, under 7,700 lines of code. | |
| 3 | +The compiler is eleven files of Free Pascal, under 7,800 lines of code. | |
| 4 | 4 | This guide walks through them in the order they are best read: from the |
| 5 | 5 | characters of a source file to the bytes of an executable. It assumes |
| 6 | 6 | you know the language from `doc/raz.md`, and enough x86-64 to read a |
| @@ -8,14 +8,14 @@ | ||
| 8 | 8 | |
| 9 | 9 | | File | Lines | Code | What | |
| 10 | 10 | |---|---|---|---| |
| 11 | -| `src/diag.pas` | 296 | 218 | Messages: positions, source excerpts, colour, counts | | |
| 12 | -| `src/scan.pas` | 855 | 722 | Symbols, literals, comments, positions | | |
| 13 | -| `src/sym.pas` | 602 | 497 | Types, objects, scopes, layout | | |
| 11 | +| `src/diag.pas` | 310 | 229 | Messages: positions, source excerpts, colour, counts | | |
| 12 | +| `src/scan.pas` | 884 | 745 | Symbols, literals, comments, positions | | |
| 13 | +| `src/sym.pas` | 615 | 510 | Types, objects, scopes, layout | | |
| 14 | 14 | | `src/chunk.pas` | 276 | 217 | The compiled program: chunks, fixups, line notes | |
| 15 | 15 | | `src/x64.pas` | 1,039 | 870 | The instruction encoder, jumps within a procedure, the assembler | |
| 16 | 16 | | `src/gen.pas` | 1,332 | 1,039 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 17 | -| `src/item.pas` | 1,320 | 1,100 | Items: types, constants, variables, operators, conversions, calls | | |
| 18 | -| `src/parse.pas` | 2,578 | 2,291 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 17 | +| `src/item.pas` | 1,342 | 1,118 | Items: types, constants, variables, operators, conversions, calls | | |
| 18 | +| `src/parse.pas` | 2,653 | 2,354 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 19 | 19 | | `src/link.pas` | 234 | 180 | Tree shaking, layout, patching, the places | |
| 20 | 20 | | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers | |
| 21 | 21 | | `src/raz.pas` | 197 | 147 | The command line | |
| @@ -471,6 +471,14 @@ | ||
| 471 | 471 | for the half-made code: once there is an error, no executable is |
| 472 | 472 | written. |
| 473 | 473 | |
| 474 | +**Hints.** Some messages guess at the mistake from the layout. A | |
| 475 | +block statement passes `CloseEnd` its start and how far its line is | |
| 476 | +indented; an `end` further left was most likely meant for a block | |
| 477 | +around it, so the first such statement is kept, and `EndMissing` | |
| 478 | +notes it when a procedure's own `end` turns out to be missing. A type | |
| 479 | +error in an argument goes through `Mismatch`, which notes the | |
| 480 | +parameter it goes to (`passing`, set by `PassArg`). | |
| 481 | + | |
| 474 | 482 | **Warnings.** `Declare` keeps each module's declarations in order in |
| 475 | 483 | two lists, through `link`: its own and its procedures', parameters |
| 476 | 484 | and a match's names for fields among them. `Named` marks an object |
| @@ -479,7 +487,9 @@ | ||
| 479 | 487 | whole before `:=`, which is written, not read. At the end of a module |
| 480 | 488 | with no errors, `Warnings` merges the two lists by position and `Warn` |
| 481 | 489 | reports what isn't exported, doesn't start with `_`, and was never |
| 482 | -used, or, for a variable, only assigned. A generic module's instances | |
| 490 | +used, or, for a variable, only assigned, and a procedure with results | |
| 491 | +that may reach its end (`openEnd`, from `ends`, which each statement | |
| 492 | +sets). A generic module's instances | |
| 483 | 493 | share its text, so only the first is warned of. |
| 484 | 494 | |
| 485 | 495 | The built-in procedures are in `Builtin` (and `New_`, `Atomic`): each |
| @@ -592,7 +602,11 @@ | ||
| 592 | 602 | a pointer in its own declaration does. Assigning a variable whole |
| 593 | 603 | (`x :=`, `a, b :=`) doesn't read it; anything else does. Warnings |
| 594 | 604 | come only for a module with no errors, and a generic module's only |
| 595 | - for its first instance. `-Werror` makes each an error. | |
| 605 | + for its first instance. A procedure with results is warned of if | |
| 606 | + it may reach its end without `return` (`ends`, `parse.pas`): each | |
| 607 | + way through its statements must end in a `return`, an `if` with an | |
| 608 | + `else`, a `match`, a `while true` or a `repeat ... until false` | |
| 609 | + with no `break`. `-Werror` makes each an error. | |
| 596 | 610 | - **Several results** come back through a hidden pointer to the |
| 597 | 611 | caller's slots; a code procedure returns two in rax and rdx. |
| 598 | 612 | - **Reals.** A real constant is held as an F64. An `F32` in a register |
| @@ -91,6 +91,8 @@ | ||
| 91 | 91 | stray: boolean; { an "end" in this procedure stood left of its statement's line } |
| 92 | 92 | strayAt: TPos; { ... the first such statement } |
| 93 | 93 | elseIf: boolean; { an "else if" in this procedure } |
| 94 | + ends: boolean; { the statements just read never go on past | |
| 95 | + their end: each way through them returns } | |
| 94 | 96 | useAt: TPos; { ... which is here } |
| 95 | 97 | |
| 96 | 98 | procedure Expression(var x: TItem; want: PType); forward; |
| @@ -1197,7 +1199,7 @@ | ||
| 1197 | 1199 | end; |
| 1198 | 1200 | |
| 1199 | 1201 | procedure IfStatement(const start: TPos; ind: longint); |
| 1200 | -var x: TItem; L, endL: longint; | |
| 1202 | +var x: TItem; L, endL: longint; all: boolean; | |
| 1201 | 1203 | begin |
| 1202 | 1204 | Next; |
| 1203 | 1205 | Expression(x, nil); |
| @@ -1205,6 +1207,7 @@ | ||
| 1205 | 1207 | L := NoLabel; |
| 1206 | 1208 | CondJump(x, L); |
| 1207 | 1209 | Statements; |
| 1210 | + all := ends; | |
| 1208 | 1211 | endL := NoLabel; |
| 1209 | 1212 | while tok = sElsif do begin |
| 1210 | 1213 | Jump(endL); |
| @@ -1215,6 +1218,7 @@ | ||
| 1215 | 1218 | L := NoLabel; |
| 1216 | 1219 | CondJump(x, L); |
| 1217 | 1220 | Statements; |
| 1221 | + all := all and ends; | |
| 1218 | 1222 | end; |
| 1219 | 1223 | if tok = sElse then begin |
| 1220 | 1224 | Jump(endL); |
| @@ -1222,17 +1226,23 @@ | ||
| 1222 | 1226 | Next; |
| 1223 | 1227 | if tok = sIf then elseIf := true; |
| 1224 | 1228 | Statements; |
| 1225 | - end else Fix(L); | |
| 1229 | + all := all and ends; | |
| 1230 | + end else begin | |
| 1231 | + Fix(L); | |
| 1232 | + all := false; | |
| 1233 | + end; | |
| 1226 | 1234 | CloseEnd(start, ind); |
| 1227 | 1235 | Fix(endL); |
| 1236 | + ends := all; | |
| 1228 | 1237 | end; |
| 1229 | 1238 | |
| 1230 | 1239 | procedure WhileStatement(const start: TPos; ind: longint); |
| 1231 | -var x: TItem; top, L: longint; lp: TLoop; old: PLoop; | |
| 1240 | +var x: TItem; top, L: longint; lp: TLoop; old: PLoop; forever: boolean; | |
| 1232 | 1241 | begin |
| 1233 | 1242 | Next; |
| 1234 | 1243 | top := pc; |
| 1235 | 1244 | Expression(x, nil); |
| 1245 | + forever := (x.mode = mConst) and (x.val <> 0); | |
| 1236 | 1246 | Expect(sDo); |
| 1237 | 1247 | L := NoLabel; |
| 1238 | 1248 | CondJump(x, L); |
| @@ -1240,6 +1250,7 @@ | ||
| 1240 | 1250 | Statements; |
| 1241 | 1251 | JumpBack(top); |
| 1242 | 1252 | CloseEnd(start, ind); |
| 1253 | + ends := forever and (lp.brk = NoLabel); | |
| 1243 | 1254 | Fix(L); |
| 1244 | 1255 | Fix(lp.brk); |
| 1245 | 1256 | curLoop := old; |
| @@ -1255,6 +1266,7 @@ | ||
| 1255 | 1266 | curLoop := old; |
| 1256 | 1267 | Expect(sUntil); |
| 1257 | 1268 | Expression(x, nil); |
| 1269 | + ends := (x.mode = mConst) and (x.val = 0) and (lp.brk = NoLabel); { until false } | |
| 1258 | 1270 | L := NoLabel; |
| 1259 | 1271 | CondJump(x, L); |
| 1260 | 1272 | FixTo(L, top); |
| @@ -1372,7 +1384,7 @@ | ||
| 1372 | 1384 | procedure MatchStatement(const start: TPos; ind: longint); |
| 1373 | 1385 | var |
| 1374 | 1386 | x: TItem; t: PType; c, f, o: PObj; p: int64; L, endL, n: longint; |
| 1375 | - seen: array[0..255] of boolean; hasElse: boolean; endAt: TPos; ls: TLabels; | |
| 1387 | + seen: array[0..255] of boolean; hasElse, all: boolean; endAt: TPos; ls: TLabels; | |
| 1376 | 1388 | begin |
| 1377 | 1389 | Next; |
| 1378 | 1390 | Expression(x, nil); |
| @@ -1382,6 +1394,7 @@ | ||
| 1382 | 1394 | Expect(sOf); |
| 1383 | 1395 | FillChar(seen, SizeOf(seen), 0); |
| 1384 | 1396 | hasElse := false; |
| 1397 | + all := true; | |
| 1385 | 1398 | endL := NoLabel; |
| 1386 | 1399 | ls.n := 0; |
| 1387 | 1400 | while tok = sBar do begin |
| @@ -1431,9 +1444,11 @@ | ||
| 1431 | 1444 | Jump(endL); |
| 1432 | 1445 | Fix(L); |
| 1433 | 1446 | end; |
| 1447 | + all := all and ends; | |
| 1434 | 1448 | end; |
| 1435 | 1449 | endAt := TokPos; |
| 1436 | 1450 | CloseEnd(start, ind); |
| 1451 | + ends := all; | |
| 1437 | 1452 | if (t^.form = fInt) and not hasElse then |
| 1438 | 1453 | MarkHelp(endAt, 'a match on an integer needs an else arm', 'it may be empty: | else:'); |
| 1439 | 1454 | if not hasElse then begin |
| @@ -1563,6 +1578,7 @@ | ||
| 1563 | 1578 | first := id; |
| 1564 | 1579 | NoteLine(CurLine, CurCol); |
| 1565 | 1580 | m := TempMark; |
| 1581 | + ends := false; | |
| 1566 | 1582 | case tok of |
| 1567 | 1583 | sIdent: begin |
| 1568 | 1584 | LeftSide(x, called); |
| @@ -1610,6 +1626,7 @@ | ||
| 1610 | 1626 | PushVar(FrameAdr(deferSlot), 8, false, KindOf(curResult)); |
| 1611 | 1627 | end else RunDefers(0); |
| 1612 | 1628 | ReturnJump; |
| 1629 | + ends := true; | |
| 1613 | 1630 | end; |
| 1614 | 1631 | end; |
| 1615 | 1632 | sBreak: begin |
| @@ -1649,6 +1666,7 @@ | ||
| 1649 | 1666 | base := numDefers; |
| 1650 | 1667 | old := directDefer; |
| 1651 | 1668 | directDefer := false; |
| 1669 | + ends := false; | |
| 1652 | 1670 | while not (tok in [sEnd, sElse, sElsif, sUntil, sBar, sEof]) do begin |
| 1653 | 1671 | Statement; |
| 1654 | 1672 | if not skipped or (tok = sSemicolon) then Expect(sSemicolon); |
| @@ -1745,6 +1763,7 @@ | ||
| 1745 | 1763 | EnterBody(p^.chunk, line); |
| 1746 | 1764 | stray := false; elseIf := false; |
| 1747 | 1765 | Statements; |
| 1766 | + p^.openEnd := (curResult <> nil) and not ends; | |
| 1748 | 1767 | NoteLine(CurLine, CurCol); |
| 1749 | 1768 | Expect(sEnd); |
| 1750 | 1769 | EndProc(p^.chunk); |
| @@ -2187,6 +2206,8 @@ | ||
| 2187 | 2206 | var s: shortstring; |
| 2188 | 2207 | begin |
| 2189 | 2208 | s := NameOf(o^.name); |
| 2209 | + if (o^.cls = oProc) and o^.openEnd then | |
| 2210 | + Warning(o^.pos, s + ' may reach its end without return, and then returns zero'); | |
| 2190 | 2211 | if o^.exported or (s[1] = '_') then exit; |
| 2191 | 2212 | if o^.used and (o^.read or (o^.cls <> oVar) or o^.isVar or o^.ext) then exit; |
| 2192 | 2213 | case o^.cls of |
| @@ -71,6 +71,7 @@ | ||
| 71 | 71 | read: boolean; { oVar: named other than as what := assigns } |
| 72 | 72 | fwd: boolean; { oProc: declared forward, no body yet } |
| 73 | 73 | isCode: boolean; { oProc: a code procedure } |
| 74 | + openEnd: boolean; { oProc: has results, and may reach its end } | |
| 74 | 75 | ext: boolean; { oProc, oVar: C's, reached through its chunk, which |
| 75 | 76 | holds its address } |
| 76 | 77 | fields: PObj; { oCase: its fields } |
| @@ -0,0 +1 @@ | ||
| 1 | +-Werror |
| @@ -0,0 +1,7 @@ | ||
| 1 | +test/err/OpenEnd.raz:3:6: error: sign may reach its end without return, and then returns zero | |
| 2 | + 3 | proc sign(n: I64): I64; | |
| 3 | + | ^^^^ | |
| 4 | +test/err/OpenEnd.raz:23:6: error: brk may reach its end without return, and then returns zero | |
| 5 | + 23 | proc brk(n: I64): I64; | |
| 6 | + | ^^^ | |
| 7 | +raz: 2 errors |
| @@ -0,0 +1,33 @@ | ||
| 1 | +module OpenEnd; | |
| 2 | +import Out; | |
| 3 | +proc sign(n: I64): I64; | |
| 4 | +begin | |
| 5 | + if n > 0 then | |
| 6 | + return 1; | |
| 7 | + end; | |
| 8 | +end; | |
| 9 | +proc ok(n: I64): I64; | |
| 10 | +begin | |
| 11 | + if n > 0 then | |
| 12 | + return 1; | |
| 13 | + else | |
| 14 | + return 0; | |
| 15 | + end; | |
| 16 | +end; | |
| 17 | +proc loop(n: I64): I64; | |
| 18 | +begin | |
| 19 | + while true do | |
| 20 | + if n > 0 then return n; end; | |
| 21 | + end; | |
| 22 | +end; | |
| 23 | +proc brk(n: I64): I64; | |
| 24 | +begin | |
| 25 | + while true do | |
| 26 | + if n > 0 then break; end; | |
| 27 | + end; | |
| 28 | +end; | |
| 29 | +proc main*; | |
| 30 | +begin | |
| 31 | + Out.int(sign(-1) + ok(1) + loop(1) + brk(1)); | |
| 32 | +end; | |
| 33 | +end OpenEnd. |