raz / commit aeb43a7c

commit
aeb43a7cb6b1f839f579950be849cc6bc803f1a2
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
aa755916
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.

patch · log

8 files changed, +95 -14

PLAN.md (history) modified +5 -1

@@ -17,4 +17,8 @@
1717 ## Wishes, for a later thaw
1818
1919 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)`.

README.md (history) modified +1 -1

@@ -24,7 +24,7 @@
2424 $ examples/Hello
2525 hello, world
2626
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
2828 intermediate form, no optimizer. It writes x86-64 machine code and the
2929 ELF executable itself, with no assembler or linker, and compiles about
3030 a million lines a second.

doc/compiler.md (history) modified +22 -8

@@ -1,6 +1,6 @@
11 # Reading the raz compiler
22
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.
44 This guide walks through them in the order they are best read: from the
55 characters of a source file to the bytes of an executable. It assumes
66 you know the language from `doc/raz.md`, and enough x86-64 to read a
@@ -8,14 +8,14 @@
88
99 | File | Lines | Code | What |
1010 |---|---|---|---|
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 |
1414 | `src/chunk.pas` | 276 | 217 | The compiled program: chunks, fixups, line notes |
1515 | `src/x64.pas` | 1,039 | 870 | The instruction encoder, jumps within a procedure, the assembler |
1616 | `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 |
1919 | `src/link.pas` | 234 | 180 | Tree shaking, layout, patching, the places |
2020 | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers |
2121 | `src/raz.pas` | 197 | 147 | The command line |
@@ -471,6 +471,14 @@
471471 for the half-made code: once there is an error, no executable is
472472 written.
473473
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+
474482 **Warnings.** `Declare` keeps each module's declarations in order in
475483 two lists, through `link`: its own and its procedures', parameters
476484 and a match's names for fields among them. `Named` marks an object
@@ -479,7 +487,9 @@
479487 whole before `:=`, which is written, not read. At the end of a module
480488 with no errors, `Warnings` merges the two lists by position and `Warn`
481489 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
483493 share its text, so only the first is warned of.
484494
485495 The built-in procedures are in `Builtin` (and `New_`, `Atomic`): each
@@ -592,7 +602,11 @@
592602 a pointer in its own declaration does. Assigning a variable whole
593603 (`x :=`, `a, b :=`) doesn't read it; anything else does. Warnings
594604 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.
596610 - **Several results** come back through a hidden pointer to the
597611 caller's slots; a code procedure returns two in rax and rdx.
598612 - **Reals.** A real constant is held as an F64. An `F32` in a register

src/parse.pas modified +25 -4

@@ -91,6 +91,8 @@
9191 stray: boolean; { an "end" in this procedure stood left of its statement's line }
9292 strayAt: TPos; { ... the first such statement }
9393 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 }
9496 useAt: TPos; { ... which is here }
9597
9698 procedure Expression(var x: TItem; want: PType); forward;
@@ -1197,7 +1199,7 @@
11971199 end;
11981200
11991201 procedure IfStatement(const start: TPos; ind: longint);
1200-var x: TItem; L, endL: longint;
1202+var x: TItem; L, endL: longint; all: boolean;
12011203 begin
12021204 Next;
12031205 Expression(x, nil);
@@ -1205,6 +1207,7 @@
12051207 L := NoLabel;
12061208 CondJump(x, L);
12071209 Statements;
1210+ all := ends;
12081211 endL := NoLabel;
12091212 while tok = sElsif do begin
12101213 Jump(endL);
@@ -1215,6 +1218,7 @@
12151218 L := NoLabel;
12161219 CondJump(x, L);
12171220 Statements;
1221+ all := all and ends;
12181222 end;
12191223 if tok = sElse then begin
12201224 Jump(endL);
@@ -1222,17 +1226,23 @@
12221226 Next;
12231227 if tok = sIf then elseIf := true;
12241228 Statements;
1225- end else Fix(L);
1229+ all := all and ends;
1230+ end else begin
1231+ Fix(L);
1232+ all := false;
1233+ end;
12261234 CloseEnd(start, ind);
12271235 Fix(endL);
1236+ ends := all;
12281237 end;
12291238
12301239 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;
12321241 begin
12331242 Next;
12341243 top := pc;
12351244 Expression(x, nil);
1245+ forever := (x.mode = mConst) and (x.val <> 0);
12361246 Expect(sDo);
12371247 L := NoLabel;
12381248 CondJump(x, L);
@@ -1240,6 +1250,7 @@
12401250 Statements;
12411251 JumpBack(top);
12421252 CloseEnd(start, ind);
1253+ ends := forever and (lp.brk = NoLabel);
12431254 Fix(L);
12441255 Fix(lp.brk);
12451256 curLoop := old;
@@ -1255,6 +1266,7 @@
12551266 curLoop := old;
12561267 Expect(sUntil);
12571268 Expression(x, nil);
1269+ ends := (x.mode = mConst) and (x.val = 0) and (lp.brk = NoLabel); { until false }
12581270 L := NoLabel;
12591271 CondJump(x, L);
12601272 FixTo(L, top);
@@ -1372,7 +1384,7 @@
13721384 procedure MatchStatement(const start: TPos; ind: longint);
13731385 var
13741386 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;
13761388 begin
13771389 Next;
13781390 Expression(x, nil);
@@ -1382,6 +1394,7 @@
13821394 Expect(sOf);
13831395 FillChar(seen, SizeOf(seen), 0);
13841396 hasElse := false;
1397+ all := true;
13851398 endL := NoLabel;
13861399 ls.n := 0;
13871400 while tok = sBar do begin
@@ -1431,9 +1444,11 @@
14311444 Jump(endL);
14321445 Fix(L);
14331446 end;
1447+ all := all and ends;
14341448 end;
14351449 endAt := TokPos;
14361450 CloseEnd(start, ind);
1451+ ends := all;
14371452 if (t^.form = fInt) and not hasElse then
14381453 MarkHelp(endAt, 'a match on an integer needs an else arm', 'it may be empty: | else:');
14391454 if not hasElse then begin
@@ -1563,6 +1578,7 @@
15631578 first := id;
15641579 NoteLine(CurLine, CurCol);
15651580 m := TempMark;
1581+ ends := false;
15661582 case tok of
15671583 sIdent: begin
15681584 LeftSide(x, called);
@@ -1610,6 +1626,7 @@
16101626 PushVar(FrameAdr(deferSlot), 8, false, KindOf(curResult));
16111627 end else RunDefers(0);
16121628 ReturnJump;
1629+ ends := true;
16131630 end;
16141631 end;
16151632 sBreak: begin
@@ -1649,6 +1666,7 @@
16491666 base := numDefers;
16501667 old := directDefer;
16511668 directDefer := false;
1669+ ends := false;
16521670 while not (tok in [sEnd, sElse, sElsif, sUntil, sBar, sEof]) do begin
16531671 Statement;
16541672 if not skipped or (tok = sSemicolon) then Expect(sSemicolon);
@@ -1745,6 +1763,7 @@
17451763 EnterBody(p^.chunk, line);
17461764 stray := false; elseIf := false;
17471765 Statements;
1766+ p^.openEnd := (curResult <> nil) and not ends;
17481767 NoteLine(CurLine, CurCol);
17491768 Expect(sEnd);
17501769 EndProc(p^.chunk);
@@ -2187,6 +2206,8 @@
21872206 var s: shortstring;
21882207 begin
21892208 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');
21902211 if o^.exported or (s[1] = '_') then exit;
21912212 if o^.used and (o^.read or (o^.cls <> oVar) or o^.isVar or o^.ext) then exit;
21922213 case o^.cls of

src/sym.pas modified +1 -0

@@ -71,6 +71,7 @@
7171 read: boolean; { oVar: named other than as what := assigns }
7272 fwd: boolean; { oProc: declared forward, no body yet }
7373 isCode: boolean; { oProc: a code procedure }
74+ openEnd: boolean; { oProc: has results, and may reach its end }
7475 ext: boolean; { oProc, oVar: C's, reached through its chunk, which
7576 holds its address }
7677 fields: PObj; { oCase: its fields }

test/err/OpenEnd.args (history) added +1 -0

@@ -0,0 +1 @@
1+-Werror

test/err/OpenEnd.err (history) added +7 -0

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

test/err/OpenEnd.raz (history) added +33 -0

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