From 81cb2aee9a1195de6e84394eeb67310f2ee15230 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 13:00:04 +0000 Subject: [PATCH] raz -strict: report 2026-10.37 held whole, until v0.24.0 makes it the rule Item.needSystem refuses, outside a module that imports SYSTEM (the module's system flag), a pointer's arithmetic, indexing and slicing, pointer and procedure conversions, views and BYTES that make or change an address, a string as a ^U8, extern, link, check off and code procedures; builtin refuses ADR, PTR, SYSCALL and ARGV not written through SYSTEM; a string, a constant's slice, MSG and a const parameter's array slice are []const; an atomic given a pointer is refused. The library and programs are mended against it next. --- diff --git a/CLAUDE.md b/CLAUDE.md index 17101c5..d971929 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -389,6 +389,9 @@ (`chooseRegs`). A pointer that isn't a variable, `ADR(x)`, is still taken as the old form until v0.24.0; a pointer variable is the new form, so `XADD(p, 1)` is an error whose help says `p^`. +- `raz -strict` (temporary, until v0.24.0 makes it the default) refuses + every old form of report 2026-10.37: see `doc/compiler.md`. The + library and every program are mended against it first. - `import SYSTEM` (report 2026-10.37) binds to `Sym.system`, no file, whose scope holds `ADR`, `PTR`, `SYSCALL` and `ARGV`; it is never warned of as unused, and an alias is refused. In v0.23.0 nothing is diff --git a/doc/compiler.md b/doc/compiler.md index d0a8f0d..8d6f893 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1885,6 +1885,18 @@ which its type's fingerprint has. Last, modules, at the end of the file. `compileFile` reads one module. +`raz -strict`, until v0.24.0 makes it the rule, holds report 2026-10.37 +whole (`Item.strict`): `needSystem` refuses, in a module whose +`Sym.Module.system` isn't set, a pointer's arithmetic, indexing and +slicing, pointer and procedure conversions, views and `BYTES` that make +or change an address (`Sym.isPlain`, the item's `viewOver`, checked in +`notReadOnly`), a string as a `^U8`, `extern`, `link`, `check off` and +code procedures; `builtin` refuses `ADR`, `PTR`, `SYSCALL` and `ARGV` +not reached through `SYSTEM` (their objects' `home` tells them apart); +a string, a slice of a constant, `MSG` and a slice of a `const` +parameter's array are `[]const`; and an atomic given a pointer is +refused. + `import SYSTEM` (report 2026-10.37) finds no file: the import binds the name to `Sym.system`, a module made once in `Sym`'s body whose scope holds four of the built-ins, `ADR`, `PTR`, `SYSCALL` and `ARGV` (made by diff --git a/src/Item.raz b/src/Item.raz index 3e3507b..230aa56 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -54,6 +54,9 @@ of one, which only that module changes *) elemRo*: BOOL; (* mVar: an element of a []const slice, or part of one, read through it and not written *) + viewOver*: BOOL; (* a view over a variable that holds more than + integers and reals, written only in a + module that imports SYSTEM *) deref*: BOOL; (* mVar: at an address from a pointer, which may be nil and isn't tested yet (nilTest) *) obj*: ^Sym.Obj; (* mProc, mBuiltin, mCase; mVar: the variable named *) @@ -80,6 +83,8 @@ procedure is compiled again, the variable in memory *) checks*: BOOL; (* the module's index and slice checks are made: it doesn't say check off *) + strict*: BOOL; (* raz -strict: report 2026-10.37 held whole, as v0.24.0 + will hold it; until then, its old forms are taken *) result: ^Sym.Type; (* the result of the procedure being compiled, or nil *) passing: ^Sym.Obj; (* the parameter the argument at passingAt goes to *) passingAt: Diag.Pos; @@ -94,6 +99,11 @@ checks := yes; end; +proc setStrict*(yes: BOOL); +begin + strict := yes; +end; + (* The comparison that holds when op doesn't *) proc negated(op: Scan.Sym): Scan.Sym; begin @@ -127,6 +137,18 @@ return Diag.say(f, [Fmt.str(a)]); end; +(* Under -strict, what reaches memory other than through a variable, + what, is refused at pos in a module that doesn't import SYSTEM + (report 3) *) +proc needSystem*(pos: Diag.Pos; what: []U8): ERROR; +begin + if strict and not Sym.curModule^.system then + return Scan.markHelp(pos, say1("{} needs import SYSTEM", what), + "it reaches memory other than through a variable: say import SYSTEM after the module's imports"); + end; + return nil; +end; + proc say2(f, a, b: []U8): []U8; begin return Diag.say(f, [Fmt.str(a), Fmt.str(b)]); @@ -550,6 +572,12 @@ return (x.mode = mVar) and x.ro and (x.obj # nil) and x.obj^.isConst; end; +(* Whether x is a constant, or part of one, held in read-only data *) +proc ofConst(const x: Item): BOOL; +begin + return (x.mode = mVar) and (x.obj # nil) and (x.obj^.cls = Sym.oConst); +end; + (* Another module's exported variable can be read, not changed, nor handed to anything that could change it (report section 3); nor can what is only read, above. *) @@ -559,6 +587,7 @@ return Scan.markHelp(x.pos, say1("{} is read through a []const slice, and can't be changed", Diag.source(x.pos)), "a []const slice's elements are only read: write them through a []T"); end; + if x.view and x.viewOver then try needSystem(x.pos, "writing a view over what holds more than integers and reals"); end; if onlyRead(x) then return Scan.markHelp(x.pos, say1("{} is only read here, and can't be changed", Diag.source(x.pos)), "a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it"); @@ -611,9 +640,14 @@ if x.mode = mCond then try value(x); end; s := x.typ; if x.mode = mStr then + if strict and (t^.form = Sym.fSlice) and (t^.base = Sym.tU8) and not t^.rdonly then + return mismatchHelp(x, say1("a string is a []const U8, not {}", Sym.typeName(t)), + "its bytes are constant: take a []const U8 where they are only read, or copy them"); + end; if (t^.form = Sym.fSlice) and (t^.base = Sym.tU8) then try address(x); sliceOf(x, t, x.val); elsif (t^.form = Sym.fPtr) and (t^.base = Sym.tU8) then + try needSystem(x.pos, "a string as a ^U8"); try address(x); x.mode := mStack; x.typ := t; elsif (t^.form = Sym.fArray) and (t^.base = Sym.tU8) and (t^.len >= x.val) then tmp := Gen.allocTemp(t^.size, 1); @@ -651,6 +685,10 @@ (* a slice of what is only read may be made, to read, as of a constant; another module's array, or one read through a []const, only as a []const *) + if strict and (onlyRead(x) or ofConst(x)) and not t^.rdonly then + return mismatchHelp(x, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)), + say1("{} is only read: a slice of it is a []const", Diag.source(x.pos))); + end; if x.ro and not onlyRead(x) and not t^.rdonly then return Scan.markHelp(x.pos, Diag.say("{} is {}'s, and can't be changed outside it", [Fmt.str(Diag.source(x.pos)), Fmt.str(Scan.nameOf(x.obj^.home^.name))]), @@ -765,7 +803,9 @@ proc indexPrep*(var x: Item): ERROR; begin nilTest(x); - if x.mode = mStr then try coerce(x, Sym.tBytes); end; + if x.mode = mStr then + if strict then try coerce(x, Sym.tConstBytes); else try coerce(x, Sym.tBytes); end; + end; if (x.typ^.form = Sym.fPtr) and (x.typ^.base^.form = Sym.fArray) then return Scan.markAt(x.pos, "a pointer to an array is indexed as p^[i]"); end; @@ -794,6 +834,7 @@ return Scan.markAt(y.pos, say1("an index is an integer, found {}", what(y))); end; if t^.form = Sym.fPtr then + try needSystem(Diag.join(x.pos, y.pos), "indexing through a pointer"); isConst := y.mode = mConst; try value(y); if isConst or not ((s = 1) or (s = 2) or (s = 4) or (s = 8)) then @@ -848,7 +889,10 @@ (* a slice of another module's array, or of one read through a []const, is a []const; of a const parameter's, a slice to read *) roSlice := (t^.form = Sym.fSlice) and t^.rdonly; - if t^.form = Sym.fArray then roSlice := (x.ro and not onlyRead(x)) or x.elemRo; end; + if t^.form = Sym.fArray then + roSlice := (x.ro and not onlyRead(x)) or x.elemRo or (strict and (onlyRead(x) or ofConst(x))); + end; + if t^.form = Sym.fPtr then try needSystem(x.pos, "a slice of a pointer"); end; try value(hi); inFrame := x.frame and (t^.form # Sym.fPtr); tmp := Gen.allocTemp(16, 8); @@ -952,7 +996,10 @@ "a view may be misaligned, which can't be one access: reach a device through a ^VOLATILE pointer to a record"); end; u := notPlain(t); - if u = nil then return nil; end; + if u = nil then + if not Sym.isPlain(t) then try needSystem(x.pos, say1("a view as {}, which holds an address,", Sym.typeName(t))); end; + return nil; + end; if u # t then text := say2("can't view bytes as {}, which holds {}", Sym.typeName(t), Sym.typeName(u)); else @@ -1015,6 +1062,7 @@ inFrame := x.frame; if t^.form = Sym.fSlice then try bytesType(x, t^.base); + if not t^.rdonly and not Sym.isPlain(t^.base) then try needSystem(x.pos, "BYTES of what holds an address"); end; if (x.mode # mVar) or (x.base = Gen.bStack) then try copyToTemp(x); end; y := x; y.typ := Sym.tInt; @@ -1050,6 +1098,7 @@ if x.odd then return Scan.markAt(x.pos, "BYTES can't take a field of a packed record, which has no address"); end; if x.view then return noAddress(x, "BYTES can't take a view"); end; try bytesType(x, t); + if not Sym.isPlain(t) then try needSystem(x.pos, "BYTES of what holds an address"); end; try address(x); sliceOf(x, Sym.tBytes, t^.size); x.frame := inFrame; @@ -1068,13 +1117,16 @@ (* v.(T), where v is not an element: T must fit in v itself. *) proc view*(var x: Item; t: ^Sym.Type): ERROR; +var over: BOOL; begin try viewable(x, t); + over := not Sym.isPlain(x.typ); if t^.size > x.typ^.size then return Scan.markAt(x.pos, Diag.say("a view of {} bytes doesn't fit in {}, of {}", [Fmt.int(t^.size), Fmt.str(Sym.typeName(x.typ)), Fmt.int(x.typ^.size)])); end; becomeView(x, t); + x.viewOver := over; return nil; end; @@ -1093,7 +1145,7 @@ below it, is at least k. Through a pointer, no check: p[i] has none (report section 6.9). *) proc indexView*(var x, y: Item; t: ^Sym.Type): ERROR; -var at: ^Sym.Type; s, k: I64; +var at: ^Sym.Type; s, k: I64; over: BOOL; begin at := x.typ; s := at^.base^.size; @@ -1129,7 +1181,9 @@ try index(x, y); end; try viewable(x, t); + over := not Sym.isPlain(x.typ); (* the element's *) becomeView(x, t); + x.viewOver := over; return nil; end; @@ -1542,6 +1596,7 @@ if (op # Scan.sEql) and (op # Scan.sNeq) then return doesntApply(op, x, y, xt); end; t := Sym.tBool; elsif (xt^.form = Sym.fPtr) and Sym.isInt(yt) and ((op = Scan.sPlus) or (op = Scan.sMinus)) then + try needSystem(Diag.join(x.pos, y.pos), "a pointer's arithmetic"); t := xt; scale := INT(xt^.base^.size); isPtr := true; if y.mode = mConst then y.typ := Sym.tInt; end; elsif (xt^.form = Sym.fPtr) and (yt^.form = Sym.fPtr) and (op = Scan.sMinus) then @@ -1845,8 +1900,10 @@ try value(x); Gen.realToInt(Gen.kindOf(s)); Gen.narrow(INT(t^.size), t^.signed); end; elsif (isWord(s) or (s^.form in {Sym.fConst, Sym.fPtr, Sym.fProc, Sym.fNil})) and (t^.form in {Sym.fPtr, Sym.fProc}) then + if s^.form # Sym.fNil then try needSystem(x.pos, say1("a conversion to {}", Sym.typeName(t))); end; if x.mode # mConst then try value(x); end; elsif (s^.form in {Sym.fPtr, Sym.fProc}) and isWord(t) then + try needSystem(x.pos, say1("a conversion of a pointer to {}", Sym.typeName(t))); try value(x); elsif Sym.isInt(s) and (t^.form = Sym.fBool) then return Scan.markHelp(x.pos, say1("can't convert {} to BOOL", Sym.typeName(s)), @@ -2494,7 +2551,7 @@ try value(x); s := Gen.allocTemp(16, 8); Gen.errorText(s); - tempItem(x, Sym.tBytes, s); + if strict then tempItem(x, Sym.tConstBytes, s); else tempItem(x, Sym.tBytes, s); end; (* the message is constant *) return nil; end; diff --git a/src/Parse.raz b/src/Parse.raz index 4224ba9..66d0200 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -1128,6 +1128,10 @@ if p.typ^.form # Sym.fPtr then return Scan.markAt(p.pos, say1("expected a variable, found {}", Item.what(p))); end; + if Item.strict then + return Scan.markHelp(p.pos, say1("{} takes a variable, not a pointer to it", atomicName(which)), + "give it the variable itself: XADD(n, 1), or XADD(p^, 1) through a pointer"); + end; t := p.typ^.base; if not atomicType(t, which) then if which = Sym.bXadd then @@ -1260,6 +1264,11 @@ var which, n, start: INT; y: TItem; t: PType; f: PObj; at: Diag.Pos; begin which := INT(x.obj^.val); + if Item.strict and (x.obj^.home # Sym.system) + and ((which = Sym.bAdr) or (which = Sym.bPtr) or (which = Sym.bSyscall) or (which = Sym.bArgv)) then + return Scan.markHelp(x.pos, say2("{} is SYSTEM's, and written SYSTEM.{}", nameOf(x.obj^.name), nameOf(x.obj^.name)), + "say import SYSTEM after the module's imports, and write the name after SYSTEM."); + end; if (which = Sym.bFence) or (which = Sym.bArgc) or (which = Sym.bArgv) then if Scan.tok = Scan.sLparen then Scan.next; try expect(Scan.sRparen); end; Item.makeConst(x, Sym.tNone, 0); @@ -1481,6 +1490,7 @@ end; try Item.makeItem(x, o); if x.mode = Item.mBuiltin then + x.pos := Scan.spanFromAt(start); (* its name, for a message *) try builtin(x); called := true; elsif x.mode = Item.mCase then @@ -3540,6 +3550,7 @@ end; p^.fwd := false; if Scan.tok = Scan.sCode then + try Item.needSystem(Scan.tokPos(), "a code procedure"); Scan.next; p^.isCode := true; try codeBody(p, sig, at); @@ -3564,6 +3575,7 @@ proc externDecl: ERROR; var n: INT; exp: BOOL; o, q: PObj; t: PType; at: Diag.Pos; begin + try Item.needSystem(Scan.tokPos(), "extern"); Scan.next; Sym.curModule^.usesC := true; if Scan.tok = Scan.sProc then @@ -3776,6 +3788,10 @@ return Scan.mark(say1("expected {}, found an array literal", Sym.typeName(t))); end; try expression(e, t); + if Item.strict and (t^.form = Sym.fSlice) and not t^.rdonly then + return Scan.markHelp(e.pos, say1("a constant's slice is a []const, not {}", Sym.typeName(t)), + "a constant can't be changed: declare the slice []const"); + end; if (e.mode = Item.mStr) and ((t^.form = Sym.fSlice) or (t^.form = Sym.fArray)) and (t^.base = Sym.tU8) then if t^.form = Sym.fSlice then try constFix(off, e.chunk); @@ -4400,6 +4416,7 @@ "every module that reaches memory through SYSTEM says so by that name: write import SYSTEM"); end; o^.home := Sym.system; + m^.system := true; named(o); (* not warned of when only its features are used *) elsif not generic then o^.home := try importModule(mname, iargs); @@ -4426,6 +4443,7 @@ if (Scan.tok = Scan.sIdent) and (Scan.id = nameStatic) then m^.linkStatic := true; elsif Scan.tok = Scan.sString then + try Item.needSystem(Scan.tokPos(), "a link of a library"); NEW(lib, Heap.heap); lib^.name := internName(Scan.strBytes()); lib^.next := m^.libs; @@ -4440,6 +4458,7 @@ (* check off;: the module's index and slice checks left out; neither word is reserved *) if (Scan.tok = Scan.sIdent) and (Scan.id = nameCheck) then + try Item.needSystem(Scan.tokPos(), "check off"); Scan.next; if (Scan.tok # Scan.sIdent) or (Scan.id # nameOff) then return nil, expected("\"off\""); end; Scan.next; diff --git a/src/Raz.raz b/src/Raz.raz index 3c2c9be..81a4d0a 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -29,7 +29,7 @@ Leaves out: the work itself; the other units compile, link and write, and this only reads the command line and runs them in order. *) module Raz; -import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; +import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Item, Parse, Link, Elf; const Report = "2026-10.37"; (* the version of doc/raz.md it follows *) @@ -271,6 +271,9 @@ outPath := Files.arg(i); elsif s = "-Werror" then Diag.setWerror(true); + elsif s = "-strict" then + (* until v0.24.0: report 2026-10.37 held whole, its old forms refused *) + Item.setStrict(true); elsif s = "-S" then listing := true; X64.setListing(true); diff --git a/src/Sym.raz b/src/Sym.raz index 07bc1cc..b45f1e4 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -136,6 +136,7 @@ used*: BOOL; (* imported, through others, by the main module *) usesC*: BOOL; (* has an extern, or links a library *) linkStatic*: BOOL; (* says link static *) + system*: BOOL; (* says import SYSTEM (report 3) *) libs*: ^Lib; (* the libraries it links *) next*: ^Module; (* modules in the order they were finished *) end; @@ -153,7 +154,7 @@ var tI8*, tI16*, tI32*, tI64*, tU8*, tU16*, tU32*, tU64*: ^Type; tInt*, tUInt*: ^Type; (* INT and UINT, as big as an address: 8 bytes here *) - tBool*, tF32*, tReal*, tRConst*, tError*, tConst*, tNil*, tStr*, tNone*, tPtrU8*, tBytes*: ^Type; + tBool*, tF32*, tReal*, tRConst*, tError*, tConst*, tNil*, tStr*, tNone*, tPtrU8*, tBytes*, tConstBytes*: ^Type; tPoison*: ^Type; (* of a name reported as not declared *) curModule*: ^Module; (* SYSTEM, the module the language gives, of what reaches memory @@ -463,6 +464,23 @@ return false; end; +(* Whether any bytes at all are a value of t that holds no address: an + integer or a real, or a record or array of these alone (report 3 and + 6.9: what a view may write over outside a module that imports SYSTEM) *) +proc isPlain*(t: ^Type): BOOL; +var f: ^Obj; +begin + if t^.form in {fInt, fReal} then return true; end; + if t^.form = fArray then return isPlain(t^.base); end; + if t^.form # fRecord then return false; end; + f := t^.fields; + while f # nil do + if not isPlain(f^.typ) then return false; end; + f := f^.next; + end; + return true; +end; + (* Whether a result field f is a single value of at most a word, not a real *) proc oneWord(f: ^Obj): BOOL; begin @@ -707,6 +725,7 @@ o := newObj(cls, internOf(name), t); o^.val := v; o^.exported := true; + if s # 0 then o^.home := system; end; (* one of SYSTEM's, told from the universe's *) _ := insert(o, s); end; @@ -747,6 +766,9 @@ tPtrU8^.base := tU8; tBytes := newType(fSlice, 16, 8); tBytes^.base := tU8; + tConstBytes := newType(fSlice, 16, 8); (* []const U8 *) + tConstBytes^.base := tU8; + tConstBytes^.rdonly := true; tPoison := newType(fInt, 8, 8); tPoison^.signed := true; enter("true", oConst, tBool, 1);