From aeb43a7cb6b1f839f579950be849cc6bc803f1a2 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 11:13:25 +0100 Subject: [PATCH] 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. --- diff --git a/PLAN.md b/PLAN.md index 2b610ca..bf0e5c5 100644 --- a/PLAN.md +++ b/PLAN.md @@ -17,4 +17,8 @@ ## Wishes, for a later thaw What the language or the library might have, met while writing raz; -none is built while the freeze holds. None waits now. +none is built while the freeze holds. + +- A warning for a call whose result is dropped. Code drops results on + purpose (`SYSCALL`, `Sys.write`; about 40 places in `lib/`), so it + needs a way to say so first, such as `_ := f(x)`. diff --git a/README.md b/README.md index 3ed708e..e74c741 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ $ examples/Hello hello, world -The compiler is about 7,600 lines of Free Pascal: one pass, no +The compiler is about 7,800 lines of Free Pascal: one pass, no intermediate form, no optimizer. It writes x86-64 machine code and the ELF executable itself, with no assembler or linker, and compiles about a million lines a second. diff --git a/doc/compiler.md b/doc/compiler.md index 12e66b7..8637ff3 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1,6 +1,6 @@ # Reading the raz compiler -The compiler is eleven files of Free Pascal, under 7,700 lines of code. +The compiler is eleven files of Free Pascal, under 7,800 lines of code. This guide walks through them in the order they are best read: from the characters of a source file to the bytes of an executable. It assumes you know the language from `doc/raz.md`, and enough x86-64 to read a @@ -8,14 +8,14 @@ | File | Lines | Code | What | |---|---|---|---| -| `src/diag.pas` | 296 | 218 | Messages: positions, source excerpts, colour, counts | -| `src/scan.pas` | 855 | 722 | Symbols, literals, comments, positions | -| `src/sym.pas` | 602 | 497 | Types, objects, scopes, layout | +| `src/diag.pas` | 310 | 229 | Messages: positions, source excerpts, colour, counts | +| `src/scan.pas` | 884 | 745 | Symbols, literals, comments, positions | +| `src/sym.pas` | 615 | 510 | Types, objects, scopes, layout | | `src/chunk.pas` | 276 | 217 | The compiled program: chunks, fixups, line notes | | `src/x64.pas` | 1,039 | 870 | The instruction encoder, jumps within a procedure, the assembler | | `src/gen.pas` | 1,332 | 1,039 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/item.pas` | 1,320 | 1,100 | Items: types, constants, variables, operators, conversions, calls | -| `src/parse.pas` | 2,578 | 2,291 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/item.pas` | 1,342 | 1,118 | Items: types, constants, variables, operators, conversions, calls | +| `src/parse.pas` | 2,653 | 2,354 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 234 | 180 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers | | `src/raz.pas` | 197 | 147 | The command line | @@ -471,6 +471,14 @@ for the half-made code: once there is an error, no executable is written. +**Hints.** Some messages guess at the mistake from the layout. A +block statement passes `CloseEnd` its start and how far its line is +indented; an `end` further left was most likely meant for a block +around it, so the first such statement is kept, and `EndMissing` +notes it when a procedure's own `end` turns out to be missing. A type +error in an argument goes through `Mismatch`, which notes the +parameter it goes to (`passing`, set by `PassArg`). + **Warnings.** `Declare` keeps each module's declarations in order in two lists, through `link`: its own and its procedures', parameters and a match's names for fields among them. `Named` marks an object @@ -479,7 +487,9 @@ whole before `:=`, which is written, not read. At the end of a module with no errors, `Warnings` merges the two lists by position and `Warn` reports what isn't exported, doesn't start with `_`, and was never -used, or, for a variable, only assigned. A generic module's instances +used, or, for a variable, only assigned, and a procedure with results +that may reach its end (`openEnd`, from `ends`, which each statement +sets). A generic module's instances share its text, so only the first is warned of. The built-in procedures are in `Builtin` (and `New_`, `Atomic`): each @@ -592,7 +602,11 @@ a pointer in its own declaration does. Assigning a variable whole (`x :=`, `a, b :=`) doesn't read it; anything else does. Warnings come only for a module with no errors, and a generic module's only - for its first instance. `-Werror` makes each an error. + for its first instance. A procedure with results is warned of if + it may reach its end without `return` (`ends`, `parse.pas`): each + way through its statements must end in a `return`, an `if` with an + `else`, a `match`, a `while true` or a `repeat ... until false` + with no `break`. `-Werror` makes each an error. - **Several results** come back through a hidden pointer to the caller's slots; a code procedure returns two in rax and rdx. - **Reals.** A real constant is held as an F64. An `F32` in a register diff --git a/src/parse.pas b/src/parse.pas index d4fd68e..113c4aa 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -91,6 +91,8 @@ stray: boolean; { an "end" in this procedure stood left of its statement's line } strayAt: TPos; { ... the first such statement } elseIf: boolean; { an "else if" in this procedure } + ends: boolean; { the statements just read never go on past + their end: each way through them returns } useAt: TPos; { ... which is here } procedure Expression(var x: TItem; want: PType); forward; @@ -1197,7 +1199,7 @@ end; procedure IfStatement(const start: TPos; ind: longint); -var x: TItem; L, endL: longint; +var x: TItem; L, endL: longint; all: boolean; begin Next; Expression(x, nil); @@ -1205,6 +1207,7 @@ L := NoLabel; CondJump(x, L); Statements; + all := ends; endL := NoLabel; while tok = sElsif do begin Jump(endL); @@ -1215,6 +1218,7 @@ L := NoLabel; CondJump(x, L); Statements; + all := all and ends; end; if tok = sElse then begin Jump(endL); @@ -1222,17 +1226,23 @@ Next; if tok = sIf then elseIf := true; Statements; - end else Fix(L); + all := all and ends; + end else begin + Fix(L); + all := false; + end; CloseEnd(start, ind); Fix(endL); + ends := all; end; procedure WhileStatement(const start: TPos; ind: longint); -var x: TItem; top, L: longint; lp: TLoop; old: PLoop; +var x: TItem; top, L: longint; lp: TLoop; old: PLoop; forever: boolean; begin Next; top := pc; Expression(x, nil); + forever := (x.mode = mConst) and (x.val <> 0); Expect(sDo); L := NoLabel; CondJump(x, L); @@ -1240,6 +1250,7 @@ Statements; JumpBack(top); CloseEnd(start, ind); + ends := forever and (lp.brk = NoLabel); Fix(L); Fix(lp.brk); curLoop := old; @@ -1255,6 +1266,7 @@ curLoop := old; Expect(sUntil); Expression(x, nil); + ends := (x.mode = mConst) and (x.val = 0) and (lp.brk = NoLabel); { until false } L := NoLabel; CondJump(x, L); FixTo(L, top); @@ -1372,7 +1384,7 @@ procedure MatchStatement(const start: TPos; ind: longint); var x: TItem; t: PType; c, f, o: PObj; p: int64; L, endL, n: longint; - seen: array[0..255] of boolean; hasElse: boolean; endAt: TPos; ls: TLabels; + seen: array[0..255] of boolean; hasElse, all: boolean; endAt: TPos; ls: TLabels; begin Next; Expression(x, nil); @@ -1382,6 +1394,7 @@ Expect(sOf); FillChar(seen, SizeOf(seen), 0); hasElse := false; + all := true; endL := NoLabel; ls.n := 0; while tok = sBar do begin @@ -1431,9 +1444,11 @@ Jump(endL); Fix(L); end; + all := all and ends; end; endAt := TokPos; CloseEnd(start, ind); + ends := all; if (t^.form = fInt) and not hasElse then MarkHelp(endAt, 'a match on an integer needs an else arm', 'it may be empty: | else:'); if not hasElse then begin @@ -1563,6 +1578,7 @@ first := id; NoteLine(CurLine, CurCol); m := TempMark; + ends := false; case tok of sIdent: begin LeftSide(x, called); @@ -1610,6 +1626,7 @@ PushVar(FrameAdr(deferSlot), 8, false, KindOf(curResult)); end else RunDefers(0); ReturnJump; + ends := true; end; end; sBreak: begin @@ -1649,6 +1666,7 @@ base := numDefers; old := directDefer; directDefer := false; + ends := false; while not (tok in [sEnd, sElse, sElsif, sUntil, sBar, sEof]) do begin Statement; if not skipped or (tok = sSemicolon) then Expect(sSemicolon); @@ -1745,6 +1763,7 @@ EnterBody(p^.chunk, line); stray := false; elseIf := false; Statements; + p^.openEnd := (curResult <> nil) and not ends; NoteLine(CurLine, CurCol); Expect(sEnd); EndProc(p^.chunk); @@ -2187,6 +2206,8 @@ var s: shortstring; begin s := NameOf(o^.name); + if (o^.cls = oProc) and o^.openEnd then + Warning(o^.pos, s + ' may reach its end without return, and then returns zero'); if o^.exported or (s[1] = '_') then exit; if o^.used and (o^.read or (o^.cls <> oVar) or o^.isVar or o^.ext) then exit; case o^.cls of diff --git a/src/sym.pas b/src/sym.pas index 4480423..6214105 100644 --- a/src/sym.pas +++ b/src/sym.pas @@ -71,6 +71,7 @@ read: boolean; { oVar: named other than as what := assigns } fwd: boolean; { oProc: declared forward, no body yet } isCode: boolean; { oProc: a code procedure } + openEnd: boolean; { oProc: has results, and may reach its end } ext: boolean; { oProc, oVar: C's, reached through its chunk, which holds its address } fields: PObj; { oCase: its fields } diff --git a/test/err/OpenEnd.args b/test/err/OpenEnd.args new file mode 100644 index 0000000..2f41be6 --- /dev/null +++ b/test/err/OpenEnd.args @@ -0,0 +1 @@ +-Werror diff --git a/test/err/OpenEnd.err b/test/err/OpenEnd.err new file mode 100644 index 0000000..53831fa --- /dev/null +++ b/test/err/OpenEnd.err @@ -0,0 +1,7 @@ +test/err/OpenEnd.raz:3:6: error: sign may reach its end without return, and then returns zero + 3 | proc sign(n: I64): I64; + | ^^^^ +test/err/OpenEnd.raz:23:6: error: brk may reach its end without return, and then returns zero + 23 | proc brk(n: I64): I64; + | ^^^ +raz: 2 errors diff --git a/test/err/OpenEnd.raz b/test/err/OpenEnd.raz new file mode 100644 index 0000000..9a786cd --- /dev/null +++ b/test/err/OpenEnd.raz @@ -0,0 +1,33 @@ +module OpenEnd; +import Out; +proc sign(n: I64): I64; +begin + if n > 0 then + return 1; + end; +end; +proc ok(n: I64): I64; +begin + if n > 0 then + return 1; + else + return 0; + end; +end; +proc loop(n: I64): I64; +begin + while true do + if n > 0 then return n; end; + end; +end; +proc brk(n: I64): I64; +begin + while true do + if n > 0 then break; end; + end; +end; +proc main*; +begin + Out.int(sign(-1) + ok(1) + loop(1) + brk(1)); +end; +end OpenEnd.