raz / commit f4ad1385

commit
f4ad1385e5443c9f4dc87f35587332c5016a4636
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
7cf6e50c
An INT widens to an I64 wherever it goes to one, in an expression too

patch · log

28 files changed, +216 -81

CLAUDE.md modified +5 -3

@@ -190,9 +190,11 @@
190190 (SIGFPE), as 6.1 says, `mod` as `div`; for a type narrower than 64
191191 bits the code makes the division fault, as the machine wouldn't. In
192192 a constant it is an error, as dividing by zero is.
193-- `INT` widens to `I64` (and `UINT` to `U64`) wherever a value is
194- assigned, passed or returned, `return a, b` included; not in `a, b :=
195- f(x)`, `return f(x)`, a `for` or `INC`/`DEC` (in the report, 5.10).
193+- `INT` widens to `I64` (and `UINT` to `U64`) wherever its value goes
194+ to one: assigned, passed, returned, in `a, b := f(x)` and `return
195+ f(x)`, a `for`'s bounds, `INC`/`DEC`'s step, and where it meets an
196+ `I64` in an operator (report 2026-10.26, 5.10 and 6.1). Never to a
197+ `U64`, nor an `I64` back to an `INT`.
196198 - An `extern` module variable is read-only for an importer, as the
197199 report says of every exported variable (3).
198200 - Warnings come only for a module with no errors. A procedure naming

conform/err/IntFor.raz deleted +0 -7

@@ -1,7 +0,0 @@
1-module IntFor;
2-proc main*;
3-var i: I64; s: []U8;
4-begin
5- for i := 0 to LEN(s) - 1 do end;
6-end;
7-end IntFor.

conform/err/IntInc.raz deleted +0 -7

@@ -1,7 +0,0 @@
1-module IntInc;
2-proc main*;
3-var i: I64; s: []U8;
4-begin
5- INC(i, LEN(s));
6-end;
7-end IntInc.

conform/err/IntIncNarrow.raz (history) added +7 -0

@@ -0,0 +1,7 @@
1+module IntIncNarrow;
2+proc main*;
3+var n: INT; a: I64;
4+begin
5+ INC(n, a);
6+end;
7+end IntIncNarrow.

conform/err/IntMix.raz deleted +0 -7

@@ -1,7 +0,0 @@
1-module IntMix;
2-proc main*;
3-var i: I64; s: []U8;
4-begin
5- if i < LEN(s) then end;
6-end;
7-end IntMix.

conform/err/IntNarrowSum.raz (history) added +7 -0

@@ -0,0 +1,7 @@
1+module IntNarrowSum;
2+proc main*;
3+var n: INT; a: I64;
4+begin
5+ n := a + n;
6+end;
7+end IntNarrowSum.

conform/err/IntPassOnNarrow.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module IntPassOnNarrow;
2+proc two: (I64, I64); begin return 1, 2; end;
3+proc twoNarrow: (INT, INT); begin return two(); end;
4+proc main*;
5+begin
6+ _, _ := twoNarrow();
7+end;
8+end IntPassOnNarrow.

conform/err/IntResults.raz deleted +0 -8

@@ -1,8 +0,0 @@
1-module IntResults;
2-proc two: (INT, INT); begin return 1, 2; end;
3-proc main*;
4-var a, b: I64;
5-begin
6- a, b := two();
7-end;
8-end IntResults.

conform/err/IntResultsNarrow.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module IntResultsNarrow;
2+proc two: (I64, I64); begin return 1, 2; end;
3+proc main*;
4+var a, b: INT;
5+begin
6+ a, b := two();
7+end;
8+end IntResultsNarrow.

conform/err/IntUnsigned.raz (history) added +7 -0

@@ -0,0 +1,7 @@
1+module IntUnsigned;
2+proc main*;
3+var a: U64; b: INT;
4+begin
5+ a := 5 + (a - b);
6+end;
7+end IntUnsigned.

conform/err/IntVarParam.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module IntVarParam;
2+proc put(var x: I64); begin x := 1; end;
3+proc main*;
4+var n: INT;
5+begin
6+ put(n);
7+end;
8+end IntVarParam.

conform/run/IntWiden.expect (history) added +14 -0

@@ -0,0 +1,14 @@
1+a + n 8
2+n * a -20
3+5 + (a - n) 17
4+a + LEN(s) 15
5+n * n + a 14
6+n < a
7+LEN(s) = a - 5
8+sum of 0 to LEN(s) - 1 10
9+a after INC and DEC 17
10+a, b := two() 34
11+twoWide 34
12+threeWide 567
13+0000000000000006
14+w > u

conform/run/IntWiden.raz (history) added +68 -0

@@ -0,0 +1,68 @@
1+(* An INT widens to an I64, and a UINT to a U64, wherever its value
2+ goes to one: in an expression, a relation, a for loop's bounds, the
3+ step of INC and DEC, and several results passed on (report sections
4+ 5.10, 6.1 and 7.2). *)
5+module IntWiden;
6+import Out;
7+
8+proc two: (INT, INT);
9+begin
10+ return 3, 4;
11+end;
12+
13+proc three: (INT, INT, INT);
14+begin
15+ return 5, 6, 7;
16+end;
17+
18+(* passed on whole: two results come back in registers, three through
19+ memory *)
20+proc twoWide: (I64, I64);
21+begin
22+ return two();
23+end;
24+
25+proc threeWide: (I64, I64, I64);
26+begin
27+ return three();
28+end;
29+
30+proc show(label: []U8; v: I64);
31+begin
32+ Out.str(label); Out.str(" "); Out.int(v); Out.str("\n");
33+end;
34+
35+proc main*;
36+var
37+ a, b, c, i: I64; n: INT; s: []U8; u: U64; w: UINT;
38+begin
39+ s := "hello";
40+ n := -2;
41+ a := 10;
42+ show("a + n", a + n);
43+ show("n * a", n * a);
44+ show("5 + (a - n)", 5 + (a - n));
45+ show("a + LEN(s)", a + LEN(s));
46+ show("n * n + a", n * n + a);
47+ if n < a then Out.str("n < a\n"); end;
48+ if LEN(s) = a - 5 then Out.str("LEN(s) = a - 5\n"); end;
49+ c := 0;
50+ for i := 0 to LEN(s) - 1 do c := c + i; end;
51+ show("sum of 0 to LEN(s) - 1", c);
52+ INC(a, LEN(s));
53+ DEC(a, n);
54+ show("a after INC and DEC", a);
55+ a, b := two();
56+ show("a, b := two()", a * 10 + b);
57+ a, b := twoWide();
58+ show("twoWide", a * 10 + b);
59+ a, b, c := threeWide();
60+ show("threeWide", a * 100 + b * 10 + c);
61+ w := 7;
62+ u := 0FFFFFFFFFFFFFFFFH;
63+ u := u + w; (* wraps around, as U64 does *)
64+ Out.hex(u, 16); Out.str("\n");
65+ if w > u then Out.str("w > u\n"); end;
66+end;
67+
68+end IntWiden.

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

@@ -19,8 +19,8 @@
1919 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2020 | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler |
2121 | `src/Gen.raz` | 2,516 | 1,800 | The stack machine, for x86-64: values, operations, checks, calls, frames |
22-| `src/Item.raz` | 1,782 | 1,473 | Items: types, constants, variables, operators, conversions, calls |
23-| `src/Parse.raz` | 3,949 | 3,441 | Declarations, statements, expressions, built-ins, recovery, warnings |
22+| `src/Item.raz` | 1,792 | 1,478 | Items: types, constants, variables, operators, conversions, calls |
23+| `src/Parse.raz` | 3,960 | 3,445 | Declarations, statements, expressions, built-ins, recovery, warnings |
2424 | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
2525 | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers |
2626 | `src/Raz.raz` | 305 | 238 | The command line |
@@ -1274,8 +1274,12 @@
12741274 Assignment and conversion: `coerce` makes a value fit the type it's
12751275 going to, or reports that it can't; it's where a string literal becomes
12761276 a slice, an array a slice, an untyped constant a typed one, and an `INT`
1277-an `I64` (`widens`). `coerceExact` is for where the types must be the
1278-same, as in a `for` loop, `INC` and several results. `assign` stores, or
1277+an `I64` (`widens`), wherever it goes: a `for` loop's bounds, `INC`'s
1278+step and several results pass through `coerce` too. An `INT` meeting
1279+an `I64` in an operator is `op2`'s, which gives the result the `I64`;
1280+`return f(x)` takes results that widen by `resultsFit`. None of it
1281+makes code: an `INT` and an `I64` are the same 64 bits on x86-64, so
1282+only the item's type changes. `assign` stores, or
12791283 copies records and arrays. `convert` is the written conversion, `T(x)`.
12801284
12811285 Calls: `beginCall`, then `passArg` for each argument, then `endCall`,

src/Item.raz (history) modified +29 -19

@@ -464,12 +464,29 @@
464464 end;
465465
466466 (* An INT goes silently into an I64, and a UINT into a U64, losing
467- nothing on any target (report section 5.10). *)
468-proc widens(s, t: ^Sym.Type): BOOL;
467+ nothing on any target, wherever its value goes to one (report
468+ section 5.10). *)
469+proc widens*(s, t: ^Sym.Type): BOOL;
469470 begin
470471 return ((s = Sym.tInt) and Sym.same(t, Sym.tI64)) or ((s = Sym.tUInt) and Sym.same(t, Sym.tU64));
471472 end;
472473
474+(* Whether the several results s may be passed on as t: as many, each
475+ of the same type or one that widens to it, as in return f(x) (report
476+ section 5.10). An INT and an I64 have one layout here, so the values
477+ pass as they are. *)
478+proc resultsFit*(s, t: ^Sym.Type): BOOL;
479+var f, g: ^Sym.Obj;
480+begin
481+ if not s^.tuple or not t^.tuple or (s^.len # t^.len) then return false; end;
482+ f := s^.fields; g := t^.fields;
483+ while f # nil do
484+ if not Sym.same(f^.typ, g^.typ) and not widens(f^.typ, g^.typ) then return false; end;
485+ f := f^.next; g := g^.next;
486+ end;
487+ return true;
488+end;
489+
473490 (* Another module's exported variable can be read, not changed, nor
474491 handed to anything that could change it (report section 3). *)
475492 proc notReadOnly*(x: Item): ERROR;
@@ -537,28 +554,17 @@
537554 return Scan.markHelp(x.pos,
538555 say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)),
539556 say1("to call it, write {}()", Diag.source(x.pos)));
557+ elsif widens(t, s) then
558+ (* the other way, an I64 into an INT, is written: it may drop bits *)
559+ return Scan.markHelp(x.pos, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)),
560+ Diag.say("{} becomes {} only as written, {}({})", [Fmt.str(Sym.typeName(s)),
561+ Fmt.str(Sym.typeName(t)), Fmt.str(Sym.typeName(t)), Fmt.str(Diag.source(x.pos))]));
540562 else
541563 return mismatch(x, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)));
542564 end;
543565 return nil;
544566 end;
545567
546-(* coerce where the types must be the same, as in a for loop, INC and
547- several results, which don't widen an INT to an I64 (report sections
548- 5.10, 6.1 and 7.2); help, if not empty, says why. *)
549-proc coerceExact*(var x: Item; t: ^Sym.Type; help: []U8): ERROR;
550-var h: []U8;
551-begin
552- if widens(x.typ, t) then
553- h := help;
554- if LEN(h) = 0 then
555- h := say2("they don't mix here: write {}({})", Sym.typeName(t), Diag.source(x.pos));
556- end;
557- return Scan.markHelp(x.pos, say2("expected {}, found {}", Sym.typeName(t), what(x)), h);
558- end;
559- return coerce(x, t);
560-end;
561-
562568 (* dst := src. The address of dst, if it has to be computed, is on the
563569 stack already, under src's. *)
564570 proc assign*(var dst, src: Item): ERROR;
@@ -1084,6 +1090,10 @@
10841090 end;
10851091 t := xt;
10861092 elsif Sym.same(xt, yt) then t := xt;
1093+ (* an INT meeting an I64 widens to it, a UINT to a U64 (report
1094+ section 6.1): the same 64 bits here, so only the type changes *)
1095+ elsif widens(xt, yt) then t := yt;
1096+ elsif widens(yt, xt) then t := xt;
10871097 else return mismatchOps(x, y, xt, yt); end;
10881098 if not intOp(op) then return doesntApply(op, x, y, t); end;
10891099 elsif (xt^.form = Sym.fReal) and (yt^.form = Sym.fReal) then
@@ -1723,7 +1733,7 @@
17231733 (* INC(v, k) or DEC: v's address is on the stack, from address. *)
17241734 proc incDec*(var v, k: Item; dec: BOOL): ERROR;
17251735 begin
1726- try coerceExact(k, v.typ, "");
1736+ try coerce(k, v.typ);
17271737 try value(k);
17281738 if v.base = Gen.bReg then Gen.incDecReg(v.chunk, scalarSize(v.typ), v.typ^.signed, dec);
17291739 else Gen.incDec(scalarSize(v.typ), v.typ^.signed, dec); end;

src/Parse.raz (history) modified +10 -4

@@ -1743,11 +1743,11 @@
17431743 try expect(Scan.sBecomes);
17441744 try Item.makeItem(v, o);
17451745 try expression(a, o^.typ);
1746- try Item.coerceExact(a, o^.typ, "");
1746+ try Item.coerce(a, o^.typ);
17471747 try Item.assign(v, a);
17481748 try expect(Scan.sTo);
17491749 try expression(b, o^.typ);
1750- try Item.coerceExact(b, o^.typ, "");
1750+ try Item.coerce(b, o^.typ);
17511751 (* the limit, evaluated once: a constant as it is; else, in an
17521752 innermost loop, in the register chooseRegs kept for it, as a
17531753 variable no one can name; else in a slot *)
@@ -2146,7 +2146,13 @@
21462146 if blanks[i] then
21472147 try Item.discard(z);
21482148 else
2149- try Item.coerceExact(z, d[i].typ, "several results go to variables of just their types");
2149+ (* an INT result widens to an I64 variable; an I64 result
2150+ into an INT is written, but not here, in the call *)
2151+ if Item.widens(d[i].typ, z.typ) then
2152+ return Scan.markAt(y.pos, Diag.say("result {} is {}, which can't go to {}",
2153+ [Fmt.int(i + 1), Fmt.str(Sym.typeName(z.typ)), Fmt.str(Sym.typeName(d[i].typ))]));
2154+ end;
2155+ try Item.coerce(z, d[i].typ);
21502156 try Item.assign(d[i], z);
21512157 end;
21522158 f := f^.next;
@@ -2163,7 +2169,7 @@
21632169 while f # nil do
21642170 try expression(x, f^.typ);
21652171 if x.typ^.tuple then
2166- if not Sym.same(x.typ, curResult) then
2172+ if not Sym.same(x.typ, curResult) and not Item.resultsFit(x.typ, curResult) then
21672173 return Scan.markAt(x.pos, say2("expected the results {}, found {}", Sym.typeName(curResult), Sym.typeName(x.typ)));
21682174 end;
21692175 return Item.returnAll(x);

src/check.sh modified +6 -2

@@ -7,11 +7,15 @@
77 b=build/src
88 # The seed predates raw strings and match on a string (report
99 # 2026-10.25): the files that use them are the compiler's alone, and
10-# so are those whose message says what match takes.
10+# so are those whose message says what match takes. It predates, too,
11+# an INT widened in an expression and the rest (report 2026-10.26),
12+# and the messages for an I64 that goes to an INT.
1113 newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz
1214 conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz
1315 conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz
14- examples/Sh.raz examples/Settings.raz"
16+ examples/Sh.raz examples/Settings.raz
17+ conform/run/IntWiden.raz conform/err/IntIncNarrow.raz conform/err/IntNarrow.raz conform/err/IntNarrowSum.raz
18+ conform/err/IntResultsNarrow.raz"
1519 newer() {
1620 for g in $newer; do [ $g = $1 ] && return 0; done
1721 return 1

test/msg/IntFor.err deleted +0 -5

@@ -1,5 +0,0 @@
1-conform/err/IntFor.raz:5:19: error: expected I64, found INT
2- 5 | for i := 0 to LEN(s) - 1 do end;
3- | ^^^^^^^^^^
4-help: they don't mix here: write I64(LEN(s) - 1)
5-raz: 1 error

test/msg/IntInc.err deleted +0 -5

@@ -1,5 +0,0 @@
1-conform/err/IntInc.raz:5:12: error: expected I64, found INT
2- 5 | INC(i, LEN(s));
3- | ^^^^^^
4-help: they don't mix here: write I64(LEN(s))
5-raz: 1 error

test/msg/IntIncNarrow.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/IntIncNarrow.raz:5:12: error: can't use I64 as INT
2+ 5 | INC(n, a);
3+ | ^
4+help: I64 becomes INT only as written, INT(a)
5+raz: 1 error

test/msg/IntMix.err deleted +0 -5

@@ -1,5 +0,0 @@
1-conform/err/IntMix.raz:5:8: error: I64 and INT are different types
2- 5 | if i < LEN(s) then end;
3- | ^^^^^^^^^^
4-help: convert one to the other's type, as in I64(LEN(s))
5-raz: 1 error

test/msg/IntNarrow.err (history) modified +1 -0

@@ -1,4 +1,5 @@
11 conform/err/IntNarrow.raz:5:10: error: can't use I64 as INT
22 5 | n := i;
33 | ^
4+help: I64 becomes INT only as written, INT(i)
45 raz: 1 error

test/msg/IntNarrowSum.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/IntNarrowSum.raz:5:10: error: can't use I64 as INT
2+ 5 | n := a + n;
3+ | ^^^^^
4+help: I64 becomes INT only as written, INT(a + n)
5+raz: 1 error

test/msg/IntPassOnNarrow.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/IntPassOnNarrow.raz:3:42: error: expected the results (INT, INT), found (I64, I64)
2+ 3 | proc twoNarrow: (INT, INT); begin return two(); end;
3+ | ^^^^^
4+raz: 1 error

test/msg/IntResults.err deleted +0 -5

@@ -1,5 +0,0 @@
1-conform/err/IntResults.raz:6:13: error: expected I64, found INT
2- 6 | a, b := two();
3- | ^^^^^
4-help: several results go to variables of just their types
5-raz: 1 error

test/msg/IntResultsNarrow.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/IntResultsNarrow.raz:6:13: error: result 1 is I64, which can't go to INT
2+ 6 | a, b := two();
3+ | ^^^^^
4+raz: 1 error

test/msg/IntUnsigned.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/IntUnsigned.raz:5:15: error: U64 and INT are different types
2+ 5 | a := 5 + (a - b);
3+ | ^^^^^
4+help: convert one to the other's type, as in U64(b)
5+raz: 1 error

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

@@ -0,0 +1,7 @@
1+conform/err/IntVarParam.raz:6:9: error: a var parameter needs exactly I64, found INT
2+ 6 | put(n);
3+ | ^
4+conform/err/IntVarParam.raz:2:14: note: it goes to x, declared here
5+ 2 | proc put(var x: I64); begin x := 1; end;
6+ | ^
7+raz: 1 error