raz / commit 81cb2aee

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

patch · log

6 files changed, +123 -7

CLAUDE.md modified +3 -0

@@ -389,6 +389,9 @@
389389 (`chooseRegs`). A pointer that isn't a variable, `ADR(x)`, is still
390390 taken as the old form until v0.24.0; a pointer variable is the new
391391 form, so `XADD(p, 1)` is an error whose help says `p^`.
392+- `raz -strict` (temporary, until v0.24.0 makes it the default) refuses
393+ every old form of report 2026-10.37: see `doc/compiler.md`. The
394+ library and every program are mended against it first.
392395 - `import SYSTEM` (report 2026-10.37) binds to `Sym.system`, no file,
393396 whose scope holds `ADR`, `PTR`, `SYSCALL` and `ARGV`; it is never
394397 warned of as unused, and an alias is refused. In v0.23.0 nothing is

doc/compiler.md (history) modified +12 -0

@@ -1885,6 +1885,18 @@
18851885 which its type's fingerprint has.
18861886
18871887 Last, modules, at the end of the file. `compileFile` reads one module.
1888+`raz -strict`, until v0.24.0 makes it the rule, holds report 2026-10.37
1889+whole (`Item.strict`): `needSystem` refuses, in a module whose
1890+`Sym.Module.system` isn't set, a pointer's arithmetic, indexing and
1891+slicing, pointer and procedure conversions, views and `BYTES` that make
1892+or change an address (`Sym.isPlain`, the item's `viewOver`, checked in
1893+`notReadOnly`), a string as a `^U8`, `extern`, `link`, `check off` and
1894+code procedures; `builtin` refuses `ADR`, `PTR`, `SYSCALL` and `ARGV`
1895+not reached through `SYSTEM` (their objects' `home` tells them apart);
1896+a string, a slice of a constant, `MSG` and a slice of a `const`
1897+parameter's array are `[]const`; and an atomic given a pointer is
1898+refused.
1899+
18881900 `import SYSTEM` (report 2026-10.37) finds no file: the import binds the
18891901 name to `Sym.system`, a module made once in `Sym`'s body whose scope
18901902 holds four of the built-ins, `ADR`, `PTR`, `SYSCALL` and `ARGV` (made by

src/Item.raz (history) modified +62 -5

@@ -54,6 +54,9 @@
5454 of one, which only that module changes *)
5555 elemRo*: BOOL; (* mVar: an element of a []const slice, or part of
5656 one, read through it and not written *)
57+ viewOver*: BOOL; (* a view over a variable that holds more than
58+ integers and reals, written only in a
59+ module that imports SYSTEM *)
5760 deref*: BOOL; (* mVar: at an address from a pointer, which may be
5861 nil and isn't tested yet (nilTest) *)
5962 obj*: ^Sym.Obj; (* mProc, mBuiltin, mCase; mVar: the variable named *)
@@ -80,6 +83,8 @@
8083 procedure is compiled again, the variable in memory *)
8184 checks*: BOOL; (* the module's index and slice checks are made: it
8285 doesn't say check off *)
86+ strict*: BOOL; (* raz -strict: report 2026-10.37 held whole, as v0.24.0
87+ will hold it; until then, its old forms are taken *)
8388 result: ^Sym.Type; (* the result of the procedure being compiled, or nil *)
8489 passing: ^Sym.Obj; (* the parameter the argument at passingAt goes to *)
8590 passingAt: Diag.Pos;
@@ -94,6 +99,11 @@
9499 checks := yes;
95100 end;
96101
102+proc setStrict*(yes: BOOL);
103+begin
104+ strict := yes;
105+end;
106+
97107 (* The comparison that holds when op doesn't *)
98108 proc negated(op: Scan.Sym): Scan.Sym;
99109 begin
@@ -127,6 +137,18 @@
127137 return Diag.say(f, [Fmt.str(a)]);
128138 end;
129139
140+(* Under -strict, what reaches memory other than through a variable,
141+ what, is refused at pos in a module that doesn't import SYSTEM
142+ (report 3) *)
143+proc needSystem*(pos: Diag.Pos; what: []U8): ERROR;
144+begin
145+ if strict and not Sym.curModule^.system then
146+ return Scan.markHelp(pos, say1("{} needs import SYSTEM", what),
147+ "it reaches memory other than through a variable: say import SYSTEM after the module's imports");
148+ end;
149+ return nil;
150+end;
151+
130152 proc say2(f, a, b: []U8): []U8;
131153 begin
132154 return Diag.say(f, [Fmt.str(a), Fmt.str(b)]);
@@ -550,6 +572,12 @@
550572 return (x.mode = mVar) and x.ro and (x.obj # nil) and x.obj^.isConst;
551573 end;
552574
575+(* Whether x is a constant, or part of one, held in read-only data *)
576+proc ofConst(const x: Item): BOOL;
577+begin
578+ return (x.mode = mVar) and (x.obj # nil) and (x.obj^.cls = Sym.oConst);
579+end;
580+
553581 (* Another module's exported variable can be read, not changed, nor
554582 handed to anything that could change it (report section 3); nor can
555583 what is only read, above. *)
@@ -559,6 +587,7 @@
559587 return Scan.markHelp(x.pos, say1("{} is read through a []const slice, and can't be changed", Diag.source(x.pos)),
560588 "a []const slice's elements are only read: write them through a []T");
561589 end;
590+ if x.view and x.viewOver then try needSystem(x.pos, "writing a view over what holds more than integers and reals"); end;
562591 if onlyRead(x) then
563592 return Scan.markHelp(x.pos, say1("{} is only read here, and can't be changed", Diag.source(x.pos)),
564593 "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 @@
611640 if x.mode = mCond then try value(x); end;
612641 s := x.typ;
613642 if x.mode = mStr then
643+ if strict and (t^.form = Sym.fSlice) and (t^.base = Sym.tU8) and not t^.rdonly then
644+ return mismatchHelp(x, say1("a string is a []const U8, not {}", Sym.typeName(t)),
645+ "its bytes are constant: take a []const U8 where they are only read, or copy them");
646+ end;
614647 if (t^.form = Sym.fSlice) and (t^.base = Sym.tU8) then
615648 try address(x); sliceOf(x, t, x.val);
616649 elsif (t^.form = Sym.fPtr) and (t^.base = Sym.tU8) then
650+ try needSystem(x.pos, "a string as a ^U8");
617651 try address(x); x.mode := mStack; x.typ := t;
618652 elsif (t^.form = Sym.fArray) and (t^.base = Sym.tU8) and (t^.len >= x.val) then
619653 tmp := Gen.allocTemp(t^.size, 1);
@@ -651,6 +685,10 @@
651685 (* a slice of what is only read may be made, to read, as of a
652686 constant; another module's array, or one read through a
653687 []const, only as a []const *)
688+ if strict and (onlyRead(x) or ofConst(x)) and not t^.rdonly then
689+ return mismatchHelp(x, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)),
690+ say1("{} is only read: a slice of it is a []const", Diag.source(x.pos)));
691+ end;
654692 if x.ro and not onlyRead(x) and not t^.rdonly then
655693 return Scan.markHelp(x.pos, Diag.say("{} is {}'s, and can't be changed outside it",
656694 [Fmt.str(Diag.source(x.pos)), Fmt.str(Scan.nameOf(x.obj^.home^.name))]),
@@ -765,7 +803,9 @@
765803 proc indexPrep*(var x: Item): ERROR;
766804 begin
767805 nilTest(x);
768- if x.mode = mStr then try coerce(x, Sym.tBytes); end;
806+ if x.mode = mStr then
807+ if strict then try coerce(x, Sym.tConstBytes); else try coerce(x, Sym.tBytes); end;
808+ end;
769809 if (x.typ^.form = Sym.fPtr) and (x.typ^.base^.form = Sym.fArray) then
770810 return Scan.markAt(x.pos, "a pointer to an array is indexed as p^[i]");
771811 end;
@@ -794,6 +834,7 @@
794834 return Scan.markAt(y.pos, say1("an index is an integer, found {}", what(y)));
795835 end;
796836 if t^.form = Sym.fPtr then
837+ try needSystem(Diag.join(x.pos, y.pos), "indexing through a pointer");
797838 isConst := y.mode = mConst;
798839 try value(y);
799840 if isConst or not ((s = 1) or (s = 2) or (s = 4) or (s = 8)) then
@@ -848,7 +889,10 @@
848889 (* a slice of another module's array, or of one read through a
849890 []const, is a []const; of a const parameter's, a slice to read *)
850891 roSlice := (t^.form = Sym.fSlice) and t^.rdonly;
851- if t^.form = Sym.fArray then roSlice := (x.ro and not onlyRead(x)) or x.elemRo; end;
892+ if t^.form = Sym.fArray then
893+ roSlice := (x.ro and not onlyRead(x)) or x.elemRo or (strict and (onlyRead(x) or ofConst(x)));
894+ end;
895+ if t^.form = Sym.fPtr then try needSystem(x.pos, "a slice of a pointer"); end;
852896 try value(hi);
853897 inFrame := x.frame and (t^.form # Sym.fPtr);
854898 tmp := Gen.allocTemp(16, 8);
@@ -952,7 +996,10 @@
952996 "a view may be misaligned, which can't be one access: reach a device through a ^VOLATILE pointer to a record");
953997 end;
954998 u := notPlain(t);
955- if u = nil then return nil; end;
999+ if u = nil then
1000+ if not Sym.isPlain(t) then try needSystem(x.pos, say1("a view as {}, which holds an address,", Sym.typeName(t))); end;
1001+ return nil;
1002+ end;
9561003 if u # t then
9571004 text := say2("can't view bytes as {}, which holds {}", Sym.typeName(t), Sym.typeName(u));
9581005 else
@@ -1015,6 +1062,7 @@
10151062 inFrame := x.frame;
10161063 if t^.form = Sym.fSlice then
10171064 try bytesType(x, t^.base);
1065+ if not t^.rdonly and not Sym.isPlain(t^.base) then try needSystem(x.pos, "BYTES of what holds an address"); end;
10181066 if (x.mode # mVar) or (x.base = Gen.bStack) then try copyToTemp(x); end;
10191067 y := x;
10201068 y.typ := Sym.tInt;
@@ -1050,6 +1098,7 @@
10501098 if x.odd then return Scan.markAt(x.pos, "BYTES can't take a field of a packed record, which has no address"); end;
10511099 if x.view then return noAddress(x, "BYTES can't take a view"); end;
10521100 try bytesType(x, t);
1101+ if not Sym.isPlain(t) then try needSystem(x.pos, "BYTES of what holds an address"); end;
10531102 try address(x);
10541103 sliceOf(x, Sym.tBytes, t^.size);
10551104 x.frame := inFrame;
@@ -1068,13 +1117,16 @@
10681117
10691118 (* v.(T), where v is not an element: T must fit in v itself. *)
10701119 proc view*(var x: Item; t: ^Sym.Type): ERROR;
1120+var over: BOOL;
10711121 begin
10721122 try viewable(x, t);
1123+ over := not Sym.isPlain(x.typ);
10731124 if t^.size > x.typ^.size then
10741125 return Scan.markAt(x.pos, Diag.say("a view of {} bytes doesn't fit in {}, of {}",
10751126 [Fmt.int(t^.size), Fmt.str(Sym.typeName(x.typ)), Fmt.int(x.typ^.size)]));
10761127 end;
10771128 becomeView(x, t);
1129+ x.viewOver := over;
10781130 return nil;
10791131 end;
10801132
@@ -1093,7 +1145,7 @@
10931145 below it, is at least k. Through a pointer, no check: p[i] has none
10941146 (report section 6.9). *)
10951147 proc indexView*(var x, y: Item; t: ^Sym.Type): ERROR;
1096-var at: ^Sym.Type; s, k: I64;
1148+var at: ^Sym.Type; s, k: I64; over: BOOL;
10971149 begin
10981150 at := x.typ;
10991151 s := at^.base^.size;
@@ -1129,7 +1181,9 @@
11291181 try index(x, y);
11301182 end;
11311183 try viewable(x, t);
1184+ over := not Sym.isPlain(x.typ); (* the element's *)
11321185 becomeView(x, t);
1186+ x.viewOver := over;
11331187 return nil;
11341188 end;
11351189
@@ -1542,6 +1596,7 @@
15421596 if (op # Scan.sEql) and (op # Scan.sNeq) then return doesntApply(op, x, y, xt); end;
15431597 t := Sym.tBool;
15441598 elsif (xt^.form = Sym.fPtr) and Sym.isInt(yt) and ((op = Scan.sPlus) or (op = Scan.sMinus)) then
1599+ try needSystem(Diag.join(x.pos, y.pos), "a pointer's arithmetic");
15451600 t := xt; scale := INT(xt^.base^.size); isPtr := true;
15461601 if y.mode = mConst then y.typ := Sym.tInt; end;
15471602 elsif (xt^.form = Sym.fPtr) and (yt^.form = Sym.fPtr) and (op = Scan.sMinus) then
@@ -1845,8 +1900,10 @@
18451900 try value(x); Gen.realToInt(Gen.kindOf(s)); Gen.narrow(INT(t^.size), t^.signed);
18461901 end;
18471902 elsif (isWord(s) or (s^.form in {Sym.fConst, Sym.fPtr, Sym.fProc, Sym.fNil})) and (t^.form in {Sym.fPtr, Sym.fProc}) then
1903+ if s^.form # Sym.fNil then try needSystem(x.pos, say1("a conversion to {}", Sym.typeName(t))); end;
18481904 if x.mode # mConst then try value(x); end;
18491905 elsif (s^.form in {Sym.fPtr, Sym.fProc}) and isWord(t) then
1906+ try needSystem(x.pos, say1("a conversion of a pointer to {}", Sym.typeName(t)));
18501907 try value(x);
18511908 elsif Sym.isInt(s) and (t^.form = Sym.fBool) then
18521909 return Scan.markHelp(x.pos, say1("can't convert {} to BOOL", Sym.typeName(s)),
@@ -2494,7 +2551,7 @@
24942551 try value(x);
24952552 s := Gen.allocTemp(16, 8);
24962553 Gen.errorText(s);
2497- tempItem(x, Sym.tBytes, s);
2554+ if strict then tempItem(x, Sym.tConstBytes, s); else tempItem(x, Sym.tBytes, s); end; (* the message is constant *)
24982555 return nil;
24992556 end;
25002557

src/Parse.raz (history) modified +19 -0

@@ -1128,6 +1128,10 @@
11281128 if p.typ^.form # Sym.fPtr then
11291129 return Scan.markAt(p.pos, say1("expected a variable, found {}", Item.what(p)));
11301130 end;
1131+ if Item.strict then
1132+ return Scan.markHelp(p.pos, say1("{} takes a variable, not a pointer to it", atomicName(which)),
1133+ "give it the variable itself: XADD(n, 1), or XADD(p^, 1) through a pointer");
1134+ end;
11311135 t := p.typ^.base;
11321136 if not atomicType(t, which) then
11331137 if which = Sym.bXadd then
@@ -1260,6 +1264,11 @@
12601264 var which, n, start: INT; y: TItem; t: PType; f: PObj; at: Diag.Pos;
12611265 begin
12621266 which := INT(x.obj^.val);
1267+ if Item.strict and (x.obj^.home # Sym.system)
1268+ and ((which = Sym.bAdr) or (which = Sym.bPtr) or (which = Sym.bSyscall) or (which = Sym.bArgv)) then
1269+ return Scan.markHelp(x.pos, say2("{} is SYSTEM's, and written SYSTEM.{}", nameOf(x.obj^.name), nameOf(x.obj^.name)),
1270+ "say import SYSTEM after the module's imports, and write the name after SYSTEM.");
1271+ end;
12631272 if (which = Sym.bFence) or (which = Sym.bArgc) or (which = Sym.bArgv) then
12641273 if Scan.tok = Scan.sLparen then Scan.next; try expect(Scan.sRparen); end;
12651274 Item.makeConst(x, Sym.tNone, 0);
@@ -1481,6 +1490,7 @@
14811490 end;
14821491 try Item.makeItem(x, o);
14831492 if x.mode = Item.mBuiltin then
1493+ x.pos := Scan.spanFromAt(start); (* its name, for a message *)
14841494 try builtin(x);
14851495 called := true;
14861496 elsif x.mode = Item.mCase then
@@ -3540,6 +3550,7 @@
35403550 end;
35413551 p^.fwd := false;
35423552 if Scan.tok = Scan.sCode then
3553+ try Item.needSystem(Scan.tokPos(), "a code procedure");
35433554 Scan.next;
35443555 p^.isCode := true;
35453556 try codeBody(p, sig, at);
@@ -3564,6 +3575,7 @@
35643575 proc externDecl: ERROR;
35653576 var n: INT; exp: BOOL; o, q: PObj; t: PType; at: Diag.Pos;
35663577 begin
3578+ try Item.needSystem(Scan.tokPos(), "extern");
35673579 Scan.next;
35683580 Sym.curModule^.usesC := true;
35693581 if Scan.tok = Scan.sProc then
@@ -3776,6 +3788,10 @@
37763788 return Scan.mark(say1("expected {}, found an array literal", Sym.typeName(t)));
37773789 end;
37783790 try expression(e, t);
3791+ if Item.strict and (t^.form = Sym.fSlice) and not t^.rdonly then
3792+ return Scan.markHelp(e.pos, say1("a constant's slice is a []const, not {}", Sym.typeName(t)),
3793+ "a constant can't be changed: declare the slice []const");
3794+ end;
37793795 if (e.mode = Item.mStr) and ((t^.form = Sym.fSlice) or (t^.form = Sym.fArray)) and (t^.base = Sym.tU8) then
37803796 if t^.form = Sym.fSlice then
37813797 try constFix(off, e.chunk);
@@ -4400,6 +4416,7 @@
44004416 "every module that reaches memory through SYSTEM says so by that name: write import SYSTEM");
44014417 end;
44024418 o^.home := Sym.system;
4419+ m^.system := true;
44034420 named(o); (* not warned of when only its features are used *)
44044421 elsif not generic then
44054422 o^.home := try importModule(mname, iargs);
@@ -4426,6 +4443,7 @@
44264443 if (Scan.tok = Scan.sIdent) and (Scan.id = nameStatic) then
44274444 m^.linkStatic := true;
44284445 elsif Scan.tok = Scan.sString then
4446+ try Item.needSystem(Scan.tokPos(), "a link of a library");
44294447 NEW(lib, Heap.heap);
44304448 lib^.name := internName(Scan.strBytes());
44314449 lib^.next := m^.libs;
@@ -4440,6 +4458,7 @@
44404458 (* check off;: the module's index and slice checks left out; neither
44414459 word is reserved *)
44424460 if (Scan.tok = Scan.sIdent) and (Scan.id = nameCheck) then
4461+ try Item.needSystem(Scan.tokPos(), "check off");
44434462 Scan.next;
44444463 if (Scan.tok # Scan.sIdent) or (Scan.id # nameOff) then return nil, expected("\"off\""); end;
44454464 Scan.next;

src/Raz.raz (history) modified +4 -1

@@ -29,7 +29,7 @@
2929 Leaves out: the work itself; the other units compile, link and
3030 write, and this only reads the command line and runs them in order. *)
3131 module Raz;
32-import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf;
32+import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Item, Parse, Link, Elf;
3333
3434 const
3535 Report = "2026-10.37"; (* the version of doc/raz.md it follows *)
@@ -271,6 +271,9 @@
271271 outPath := Files.arg(i);
272272 elsif s = "-Werror" then
273273 Diag.setWerror(true);
274+ elsif s = "-strict" then
275+ (* until v0.24.0: report 2026-10.37 held whole, its old forms refused *)
276+ Item.setStrict(true);
274277 elsif s = "-S" then
275278 listing := true;
276279 X64.setListing(true);

src/Sym.raz (history) modified +23 -1

@@ -136,6 +136,7 @@
136136 used*: BOOL; (* imported, through others, by the main module *)
137137 usesC*: BOOL; (* has an extern, or links a library *)
138138 linkStatic*: BOOL; (* says link static *)
139+ system*: BOOL; (* says import SYSTEM (report 3) *)
139140 libs*: ^Lib; (* the libraries it links *)
140141 next*: ^Module; (* modules in the order they were finished *)
141142 end;
@@ -153,7 +154,7 @@
153154 var
154155 tI8*, tI16*, tI32*, tI64*, tU8*, tU16*, tU32*, tU64*: ^Type;
155156 tInt*, tUInt*: ^Type; (* INT and UINT, as big as an address: 8 bytes here *)
156- tBool*, tF32*, tReal*, tRConst*, tError*, tConst*, tNil*, tStr*, tNone*, tPtrU8*, tBytes*: ^Type;
157+ tBool*, tF32*, tReal*, tRConst*, tError*, tConst*, tNil*, tStr*, tNone*, tPtrU8*, tBytes*, tConstBytes*: ^Type;
157158 tPoison*: ^Type; (* of a name reported as not declared *)
158159 curModule*: ^Module;
159160 (* SYSTEM, the module the language gives, of what reaches memory
@@ -463,6 +464,23 @@
463464 return false;
464465 end;
465466
467+(* Whether any bytes at all are a value of t that holds no address: an
468+ integer or a real, or a record or array of these alone (report 3 and
469+ 6.9: what a view may write over outside a module that imports SYSTEM) *)
470+proc isPlain*(t: ^Type): BOOL;
471+var f: ^Obj;
472+begin
473+ if t^.form in {fInt, fReal} then return true; end;
474+ if t^.form = fArray then return isPlain(t^.base); end;
475+ if t^.form # fRecord then return false; end;
476+ f := t^.fields;
477+ while f # nil do
478+ if not isPlain(f^.typ) then return false; end;
479+ f := f^.next;
480+ end;
481+ return true;
482+end;
483+
466484 (* Whether a result field f is a single value of at most a word, not a real *)
467485 proc oneWord(f: ^Obj): BOOL;
468486 begin
@@ -707,6 +725,7 @@
707725 o := newObj(cls, internOf(name), t);
708726 o^.val := v;
709727 o^.exported := true;
728+ if s # 0 then o^.home := system; end; (* one of SYSTEM's, told from the universe's *)
710729 _ := insert(o, s);
711730 end;
712731
@@ -747,6 +766,9 @@
747766 tPtrU8^.base := tU8;
748767 tBytes := newType(fSlice, 16, 8);
749768 tBytes^.base := tU8;
769+ tConstBytes := newType(fSlice, 16, 8); (* []const U8 *)
770+ tConstBytes^.base := tU8;
771+ tConstBytes^.rdonly := true;
750772 tPoison := newType(fInt, 8, 8);
751773 tPoison^.signed := true;
752774 enter("true", oConst, tBool, 1);