From a48b818d9c85807700ffa0e37034338b72477c3e Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 11:59:40 +0100 Subject: [PATCH] Warnings: an = or # of two BOOLs beside and or or, and \0 before a digit in a string --- diff --git a/doc/compiler.md b/doc/compiler.md index f40b981..f67f801 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -17,13 +17,13 @@ | File | Lines | Code | What | |---|---|---|---| | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts | -| `src/Scan.raz` | 1,148 | 949 | Symbols, literals, comments, positions | +| `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions | | `src/Sym.raz` | 715 | 573 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,584 | 1,304 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,592 | 1,843 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,223 | 1,785 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,323 | 3,735 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,396 | 3,796 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 418 | 333 | The command line | @@ -921,6 +921,7 @@ report's section 2), and leaving carriage returns out. `readReal` turns a real's digits into the nearest `F64` exactly, as a fraction of two big integers (`Big`), since a library's reader can be one bit off. +`zeroDigit` reads the text of the current string again, for a `\0` before a digit, which the parser warns of. Identifiers are *interned*: `intern` puts each distinct name once in a pool and hands back a small integer, so every later comparison of names @@ -1606,8 +1607,8 @@ reports what isn't exported, doesn't start with `_`, and was never used, or, for a variable, only assigned, and a procedure with results that may reach its end (`openEnd`, from `ends`, which each statement -sets). The calls that drop results are a third list, `drops`, in -order: `compileFile` notes where the module's own begin (`numDrops`), +sets). The calls that drop results, and the other doubts, are a third list, `doubts`, in +order: `compileFile` notes where the module's own begin (`numDoubts`), since the instances it makes along the way add theirs and take them back. A generic module's instances @@ -1837,8 +1838,19 @@ 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`. So is a call standing on its own that drops a - result (`drops`, kept by `noteDrop` and told by `dropped`); - `_ := f(x)` drops one on purpose. Warnings don't stop the compile; + result (`doubts`, kept by `noteDrop` and told by `dropped`); + `_ := f(x)` drops one on purpose. Two more are noted the same way, + by `noteDoubt`, which keeps the words to say. A relation binds + tighter than `and` and `or`, so `a # b and c` is `(a # b) and c`, as + a Pascal reader doesn't expect: `relation` sets `bareBoolRel` when it + read an `=` or `#` of two `BOOL`s (a parenthesised one is a factor, + and the relation around it sets it false), and `conjunct` and + `expression` ask `noteBoolRel` of each operand of an `and` or an + `or`. And a string with `\0` before a digit, `"\01"`, is a zero byte + and a `1`, not C's octal escape: `Scan.zeroDigit` looks for it in the + text of the string, and `factor` notes it (`noteZeroDigit`); `\x00` + keeps the two apart. A text the compiler reads twice, a `while`'s + condition, is noted once. Warnings don't stop the compile; `raz -Werror`, an option of this compiler's, makes each an error. - **Registers for variables.** Up to five of a procedure's single values, not reals, whose address is never taken, live in `rbx` and diff --git a/src/Parse.raz b/src/Parse.raz index 4815dc7..44df728 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -62,7 +62,7 @@ MaxNames = 64; (* names in one declaration, and the like *) MaxFwd = 256; (* pointers to types not yet declared *) MaxDefers = 64; (* defer statements in force *) - MaxDrops = 4096; (* calls that drop results, in one module *) + MaxDoubts = 4096; (* places to warn of, noted while a module is read *) MaxDeferred = 64; (* generic imports not yet made *) MaxLabels = 1024; (* labels of one match *) MaxElements = 1024; (* elements of an array literal *) @@ -108,12 +108,15 @@ stray: BOOL; (* an "end" in this procedure stood left of its statement's line *) strayAt: Diag.Pos; (* ... the first such statement *) elseIf: BOOL; (* an "else if" in this procedure *) - drops: [MaxDrops]record - at: Diag.Pos; (* a call standing on its own that drops results *) - n: INT; (* ... how many *) + doubts: [MaxDoubts]record + at: Diag.Pos; (* a place the module's end warns of *) + n: INT; (* a call standing on its own that drops this many results, or *) + msg, hint: []U8; (* any other doubt, in words (msg is empty for a drop) *) end; - numDrops: INT; (* the module being compiled has those from its + numDoubts: INT; (* the module being compiled has those from its first one, saved by compileFile, to here *) + bareBoolRel: BOOL; (* the relation just read is an = or # of two BOOLs, + not in parentheses *) ends: BOOL; (* the statements just read never go on past their end: each way through them returns *) useAt: Diag.Pos; (* ... which is here *) @@ -1548,6 +1551,33 @@ return nil; end; +(* A doubt other than a drop, to be warned of at p with msg and hint. + Text read twice, as a while's condition is, is noted once. *) +proc noteDoubt(p: Diag.Pos; msg, hint: []U8); +var i: INT; +begin + for i := 0 to numDoubts - 1 do + if (doubts[i].at.f = p.f) and (doubts[i].at.a = p.a) then return; end; + end; + if numDoubts < MaxDoubts then + doubts[numDoubts].at := p; + doubts[numDoubts].msg := msg; + doubts[numDoubts].hint := hint; + INC(numDoubts); + end; +end; + +(* Notes the string at the scanner if it has a \0 before a digit *) +proc noteZeroDigit; +var at: Diag.Pos; yes: BOOL; +begin + at, yes := Scan.zeroDigit(); + if yes then + noteDoubt(at, "\\0 is a zero byte and the digit after it is itself", + "write \\x00 to keep them apart"); + end; +end; + proc factor(var x: TItem; want: PType): ERROR; var called, vol, several: BOOL; t: PType; start: INT; s: []U8; begin @@ -1565,6 +1595,7 @@ Scan.next; | sString: Item.makeStr(x); + noteZeroDigit(); Scan.next; | sIdent: try designator(x, called); @@ -1684,33 +1715,54 @@ return nil; end; +(* A relation. It sets bareBoolRel, for the and or or that may follow: + a relation binds tighter than and and or, so the # in "a # b and c" + is read first, as (a # b) and c, which a Pascal reader doesn't expect. *) proc relation(var x: TItem; want: PType): ERROR; -var op: Scan.Sym; y: TItem; L: INT; +var op: Scan.Sym; y: TItem; L: INT; bools: BOOL; begin try simpleExpr(x, want); + bools := false; if isRelOp() then op := Scan.tok; Scan.next; try Item.op1(op, x, L); if op = Scan.sIn then try simpleExpr(y, nil); else try simpleExpr(y, x.typ); end; + bools := ((op = Scan.sEql) or (op = Scan.sNeq)) and (x.typ = Sym.tBool) and (y.typ = Sym.tBool); try Item.op2(op, x, y, L); x.pos := Diag.join(x.pos, y.pos); end; + bareBoolRel := bools; (* a relation in parentheses comes here as a factor, and sets it false *) return nil; end; +(* Notes x, an operand of the and or or (word) just read, if it is an + unparenthesised = or # of two BOOLs *) +proc noteBoolRel(const x: TItem; word: []U8); +begin + if bareBoolRel then + noteDoubt(x.pos, say2("{} is read first: = and # bind tighter than {}", Diag.source(x.pos), word), + "put the = or # of two BOOLs in parentheses"); + end; +end; + (* and binds tighter than or, and both below the relations *) proc conjunct(var x: TItem; want: PType): ERROR; var y: TItem; L: INT; start: INT; begin start := Scan.tokStart(); try relation(x, want); - while Scan.tok = Scan.sAnd do - Scan.next; - try Item.op1(Scan.sAnd, x, L); - try relation(y, nil); - try Item.op2(Scan.sAnd, x, y, L); - x.pos := Scan.spanFromAt(start); + if Scan.tok = Scan.sAnd then + noteBoolRel(x, "and"); + repeat + Scan.next; + try Item.op1(Scan.sAnd, x, L); + try relation(y, nil); + noteBoolRel(y, "and"); + try Item.op2(Scan.sAnd, x, y, L); + x.pos := Scan.spanFromAt(start); + until Scan.tok # Scan.sAnd; + bareBoolRel := false; (* x is a conjunction now *) end; return nil; end; @@ -1720,12 +1772,17 @@ begin start := Scan.tokStart(); try conjunct(x, want); - while Scan.tok = Scan.sOr do - Scan.next; - try Item.op1(Scan.sOr, x, L); - try conjunct(y, nil); - try Item.op2(Scan.sOr, x, y, L); - x.pos := Scan.spanFromAt(start); + if Scan.tok = Scan.sOr then + noteBoolRel(x, "or"); + repeat + Scan.next; + try Item.op1(Scan.sOr, x, L); + try conjunct(y, nil); + noteBoolRel(y, "or"); + try Item.op2(Scan.sOr, x, y, L); + x.pos := Scan.spanFromAt(start); + until Scan.tok # Scan.sOr; + bareBoolRel := false; (* x is a disjunction now *) end; return nil; end; @@ -2464,10 +2521,11 @@ to be warned of *) proc noteDrop(const x: TItem); begin - if (x.mode # Item.mNone) and (numDrops < MaxDrops) then - drops[numDrops].at := x.pos; - if x.typ^.tuple then drops[numDrops].n := INT(x.typ^.len); else drops[numDrops].n := 1; end; - INC(numDrops); + if (x.mode # Item.mNone) and (numDoubts < MaxDoubts) then + doubts[numDoubts].at := x.pos; + doubts[numDoubts].msg := ""; + if x.typ^.tuple then doubts[numDoubts].n := INT(x.typ^.len); else doubts[numDoubts].n := 1; end; + INC(numDoubts); end; end; @@ -2955,7 +3013,7 @@ proc body(p: PObj; sig: PType; line: INT): ERROR; var q, params, last, o, before: PObj; old: PType; st: Scan.State; - mark: Gen.CodeMark; drops0, errs: INT; + mark: Gen.CodeMark; doubts0, errs: INT; begin try Sym.openScope(Sym.newScope()); params := nil; last := nil; @@ -2986,7 +3044,7 @@ placeLocals(params, before); (* the body, made again if a variable in a register must be in memory *) Scan.saveScan(st); - Gen.markCode(mark); drops0 := numDrops; errs := Diag.errors; + Gen.markCode(mark); doubts0 := numDoubts; errs := Diag.errors; old := curResult; repeat Item.setDemoted(false); @@ -3008,7 +3066,7 @@ o := o^.next; end; end; - numDrops := drops0; + numDoubts := doubts0; Gen.redo(mark); placeLocals(params, before); Scan.restoreScan(st); @@ -3798,20 +3856,25 @@ (* The warnings of module m, by where each declaration is: its own declarations and its procedures' are two lists, each in order, and - firstDrop on are the calls that drop results. The + firstDoubt on are the calls that drop results and the other doubts. The instances of a generic module share its text, and so its warnings: only the first is warned of. *) -proc warnings(m: PModule; firstDrop: INT); +proc warnings(m: PModule; firstDoubt: INT); var a, b: PObj; n: PModule; i: INT; begin n := firstModule; while (n # nil) and (n^.name # m^.name) do n := n^.next; end; if n # nil then return; end; a := m^.first; b := m^.locals; - i := firstDrop; - while (a # nil) or (b # nil) or (i < numDrops) do - if (i < numDrops) and ((a = nil) or (drops[i].at.a < a^.pos.a)) and ((b = nil) or (drops[i].at.a < b^.pos.a)) then - dropped(drops[i].at, drops[i].n); + i := firstDoubt; + while (a # nil) or (b # nil) or (i < numDoubts) do + if (i < numDoubts) and ((a = nil) or (doubts[i].at.a < a^.pos.a)) and ((b = nil) or (doubts[i].at.a < b^.pos.a)) then + if LEN(doubts[i].msg) = 0 then + dropped(doubts[i].at, doubts[i].n); + else + Diag.warning(doubts[i].at, doubts[i].msg); + Diag.help(doubts[i].hint); + end; INC(i); elsif (b = nil) or ((a # nil) and (a^.pos.a < b^.pos.a)) then warn(a); @@ -3869,9 +3932,9 @@ st: Scan.State; saved: [Sym.MaxDepth]INT; d: INT; oldMod: PModule; oldCheck, oldChecks: BOOL; m: PModule; lib: ^Sym.Lib; start: Diag.Pos; alias, mname, line, oldPath: INT; o: PObj; bodyChunk: INT; iargs: Sym.Args; - generic: BOOL; argsAt: Scan.State; why: []U8; firstDrop: INT; + generic: BOOL; argsAt: Scan.State; why: []U8; firstDoubt: INT; begin - firstDrop := numDrops; + firstDoubt := numDoubts; Scan.saveScan(st); d := Sym.saveScopes(saved[0..Sym.MaxDepth]); oldMod := Sym.curModule; oldCheck := checkMode; oldPath := Chunk.curFilePath(); oldChecks := Item.checks; @@ -4020,8 +4083,8 @@ end; o := o^.chain; end; - if (Diag.errors = 0) and not checkOnly then warnings(m, firstDrop); end; - numDrops := firstDrop; + if (Diag.errors = 0) and not checkOnly then warnings(m, firstDoubt); end; + numDoubts := firstDoubt; if Diag.errors > 0 then Diag.finish(); end; (* a module with errors stops the compiler after it *) Sym.closeScope(); m^.done := true; diff --git a/src/Scan.raz b/src/Scan.raz index d888ba6..0032d1e 100644 --- a/src/Scan.raz +++ b/src/Scan.raz @@ -282,6 +282,27 @@ return {f: cur.f, a: cur.commentEnd - 2, b: cur.commentEnd}, true; end; +(* Whether the string at the scanner has a \0 before a + digit, '0' to '7': where, and true. A zero byte and the digit are two + characters, but a C reader sees one octal escape. A raw string has no + escapes. *) +proc zeroDigit*: (Diag.Pos, BOOL); +var i: INT; +begin + i := cur.tokPos; + if (cur.buf[i] # '"') and (cur.buf[i] # '\'') then return {}, false; end; + while i < cur.pos do + if cur.buf[i] # '\\' then + INC(i); + elsif (cur.buf[i + 1] = '0') and (cur.buf[i + 2] >= '0') and (cur.buf[i + 2] <= '7') then + return {f: cur.f, a: i, b: i + 3}, true; + else + INC(i, 2); (* an escape: the character after the backslash is not one *) + end; + end; + return {}, false; +end; + (* From the start of p to the end of the symbol before the current one: the whole of a construct that has just been read *) proc spanFrom*(p: Diag.Pos): Diag.Pos; diff --git a/test/err/BoolRel.args b/test/err/BoolRel.args new file mode 100644 index 0000000..2f41be6 --- /dev/null +++ b/test/err/BoolRel.args @@ -0,0 +1 @@ +-Werror diff --git a/test/err/BoolRel.err b/test/err/BoolRel.err new file mode 100644 index 0000000..2c099a8 --- /dev/null +++ b/test/err/BoolRel.err @@ -0,0 +1,9 @@ +test/err/BoolRel.raz:13:10: error: a # b is read first: = and # bind tighter than and + 13 | a := a # b and c; + | ^^^^^ +help: put the = or # of two BOOLs in parentheses +test/err/BoolRel.raz:14:15: error: b = c is read first: = and # bind tighter than or + 14 | a := a or b = c; + | ^^^^^ +help: put the = or # of two BOOLs in parentheses +raz: 2 errors diff --git a/test/err/BoolRel.raz b/test/err/BoolRel.raz new file mode 100644 index 0000000..78cbaae --- /dev/null +++ b/test/err/BoolRel.raz @@ -0,0 +1,16 @@ +module BoolRel; +(* A = or # of two BOOLs beside and or or binds first, as written; the + others, in parentheses or of integers, are quiet. *) +var a, b, c: BOOL; i, n: INT; +proc main*; +begin + a := (b = c) and a; + a := a and (b # c); + a := (b # c) or (a = b) and c; + a := (i < n) and (i # 0); + a := i < n and i # 0; + a := b = c; + a := a # b and c; + a := a or b = c; +end; +end BoolRel. diff --git a/test/err/ZeroDigit.args b/test/err/ZeroDigit.args new file mode 100644 index 0000000..2f41be6 --- /dev/null +++ b/test/err/ZeroDigit.args @@ -0,0 +1 @@ +-Werror diff --git a/test/err/ZeroDigit.err b/test/err/ZeroDigit.err new file mode 100644 index 0000000..f58a0ed --- /dev/null +++ b/test/err/ZeroDigit.err @@ -0,0 +1,5 @@ +test/err/ZeroDigit.raz:12:13: error: \0 is a zero byte and the digit after it is itself + 12 | s := "\0\01234567890-="; + | ^^^ +help: write \x00 to keep them apart +raz: 1 error diff --git a/test/err/ZeroDigit.raz b/test/err/ZeroDigit.raz new file mode 100644 index 0000000..135d380 --- /dev/null +++ b/test/err/ZeroDigit.raz @@ -0,0 +1,14 @@ +module ZeroDigit; +(* \0 before a digit is a warning; \x00, \0 at the end, \0 before a + letter, an escaped backslash before 0 and a raw string are quiet. *) +var s*: []U8; c*: U8; +proc main*; +begin + s := "a\x0012"; + s := "ab\0"; + s := "\0x\\01"; + s := `\01`; + c := '\0'; + s := "\0\01234567890-="; +end; +end ZeroDigit.