raz / commit b5aa492d

commit
b5aa492d04fd5c89dd82375be05bd9cd828ca6f9
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
fa020166
U8, I8 and I16 widen to INT, U8 and U16 to UINT (report 2026-10.31)

smallWidens, in coerce and op2: the value is loaded as its own type,
extended, and only the type changes. What an operator gives is marked
made and doesn't widen (madeNarrow); a conversion clears it. A U8 into
an I64 says to write I64(x). The report says return f(x) passes its
results on as they are. Tests in conform/, messages in test/msg.

patch · log

18 files changed, +201 -7

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

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

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

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

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

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

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

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

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

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

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

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

conform/run/SmallWiden.expect (history) added +1 -0

@@ -0,0 +1 @@
1+7319 300 44 -5 255 -30000 88 200 205 -5 65535 200

conform/run/SmallWiden.raz (history) added +52 -0

@@ -0,0 +1,52 @@
1+(* A U8, I8 or I16 widens to INT, and a U8 or U16 to UINT, where it is
2+ moved: assigned, passed, returned, as a bound or a step, and where it
3+ meets an INT in an operator; a conversion's result too (report
4+ 2026-10.31, sections 5.10 and 6.1). *)
5+module SmallWiden;
6+import Out;
7+
8+var g: U8; h: I16;
9+
10+proc show(n: INT);
11+begin
12+ Out.int(n); Out.str(" ");
13+end;
14+
15+proc byte(i: INT): U8;
16+begin
17+ return U8(i);
18+end;
19+
20+proc twice(n: INT): INT;
21+begin
22+ return 2 * n;
23+end;
24+
25+proc wide(b: U8): INT;
26+begin
27+ return b; (* returned *)
28+end;
29+
30+proc main*;
31+var s: []U8; a, b: U8; c: I8; w: U16; n, i: INT; u: UINT;
32+begin
33+ s := "7319";
34+ for i := 0 to LEN(s) - 1 do n := n * 10 + s[i] - '0'; end;
35+ show(n); (* an element, met by an INT *)
36+ a := 200; b := 100;
37+ show(INT(a) + b); (* 300: the sum made in INT *)
38+ show(U8(a + b)); (* 44: made in U8, then converted *)
39+ c := -5; n := c; show(n); (* a signed one keeps its sign *)
40+ g := 255; h := -30000;
41+ show(g); show(h); (* passed *)
42+ show(twice(byte(300))); (* a call's result: 88 *)
43+ show(wide(a));
44+ if (a > n) and (c < n) then Out.str("compared "); end;
45+ n := 0; INC(n, a); DEC(n, c); show(n); (* 205 *)
46+ for i := c to c do show(i); end; (* bounds *)
47+ w := 65535; u := w; show(INT(u));
48+ u := a; show(INT(u));
49+ Out.ln;
50+end;
51+
52+end SmallWiden.

doc/compiler.md (history) modified +18 -2

@@ -19,7 +19,7 @@
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,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames |
22-| `src/Item.raz` | 1,986 | 1,617 | Items: types, constants, variables, operators, conversions, calls |
22+| `src/Item.raz` | 2,037 | 1,651 | Items: types, constants, variables, operators, conversions, calls |
2323 | `src/Parse.raz` | 4,049 | 3,518 | 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 |
@@ -1303,7 +1303,23 @@
13031303 an `I64` in an operator is `op2`'s, which gives the result the `I64`;
13041304 `return f(x)` takes results that widen by `resultsFit`. None of it
13051305 makes code: an `INT` and an `I64` are the same 64 bits on x86-64, so
1306-only the item's type changes. `assign` stores, or
1306+only the item's type changes.
1307+
1308+A `U8`, `I8` or `I16` goes into an `INT`, and a `U8` or `U16` into a
1309+`UINT`, by `smallWidens`, in `coerce` and `op2` alike (report 2026-10.31).
1310+Gen keeps every integer on its stack extended to 64 bits as its type
1311+says, `movzx` for a `U8` and `movsx` for an `I8`, so `coerce` loads the
1312+value as its own type (`value`) and then changes only the type:
1313+
1314+ before: x is a U8 variable; the INT it is wanted as is an error
1315+ after: value(x) loads it, movzx r8d, byte [...]; x.typ := INT
1316+
1317+What an operator gives doesn't widen, since it was computed in the
1318+narrow type and may have wrapped: `op2`, `neg` and `complement` mark
1319+their result `made`, which `coerce` and `op2` refuse (`madeNarrow`),
1320+and `convert` clears, so that `U8(a + b)` widens. `return f(x)` passes
1321+results on as they are, so `resultsFit` takes only `INT` to `I64`.
1322+`assign` stores, or
13071323 copies records and arrays. `convert` is the written conversion, `T(x)`.
13081324
13091325 Views, `v.(T)` (report section 6.9): a view is the item of `v` with

doc/raz.md (history) modified +3 -2

@@ -682,8 +682,9 @@
682682 A `U8`, `I8` or `I16` widens to `INT`, and a `U8` or `U16` to `UINT`,
683683 in the same places, as an `INT` to `I64`: assigned, passed, returned,
684684 in `a, b := f(x)`, to a `for`'s bounds and `INC` and `DEC`'s step, and
685-in an expression, where it meets an `INT` or a `UINT` (section 6.1).
686-They widen only that far: a `U8` meets an `I64` only as `INT(b)` or
685+in an expression, where it meets an `INT` or a `UINT` (section 6.1);
686+not in `return f(x)`, which passes `f`'s results on as they are. They
687+widen only that far: a `U8` meets an `I64` only as `INT(b)` or
687688 `I64(b)`. And they widen only as values that are moved: a variable, an
688689 element, a field, a call's result or a conversion's. What an operator
689690 gives on them doesn't: it was computed in their type, and has wrapped

src/Item.raz (history) modified +51 -0

@@ -60,6 +60,9 @@
6060 anything else: lies in that frame, a local, a
6161 value parameter or a temporary, or part of one *)
6262 pos*: Diag.Pos; (* where it is in the source, for messages *)
63+ made*: BOOL; (* an integer an operator gave, computed in its own
64+ type, where it may have wrapped: a U8 made so
65+ doesn't widen to INT (report section 5.10) *)
6366 end;
6467
6568 const
@@ -492,6 +495,33 @@
492495 return ((s = Sym.tInt) and Sym.same(t, Sym.tI64)) or ((s = Sym.tUInt) and Sym.same(t, Sym.tU64));
493496 end;
494497
498+(* A U8, I8 or I16 goes silently into an INT, and a U8 or U16 into a
499+ UINT: an INT or a UINT holds every value of theirs on any target,
500+ since it is at least 16 bits (report sections 5.1 and 5.10). Only
501+ one step: a U8 doesn't go into an I64. On the stack of values an
502+ integer is kept extended to 64 bits as its type says (Gen), so the
503+ value a U8 loads as is already the INT; only the type changes. *)
504+proc smallWidens*(s, t: ^Sym.Type): BOOL;
505+begin
506+ if Sym.same(t, Sym.tInt) then
507+ return Sym.same(s, Sym.tU8) or Sym.same(s, Sym.tI8) or Sym.same(s, Sym.tI16);
508+ elsif Sym.same(t, Sym.tUInt) then
509+ return Sym.same(s, Sym.tU8) or Sym.same(s, Sym.tU16);
510+ end;
511+ return false;
512+end;
513+
514+(* An error at x, a U8, I8, I16 or U16 an operator made, which would
515+ otherwise widen to t: the operator worked in x's type, and its
516+ result may have wrapped, so the program says where to widen. *)
517+proc madeNarrow(x: Item; t: ^Sym.Type): ERROR;
518+begin
519+ return Scan.markHelp(x.pos,
520+ Diag.say("can't use {} as {}: an operator made it, in {}, where it may have wrapped",
521+ [Fmt.str(Sym.typeName(x.typ)), Fmt.str(Sym.typeName(t)), Fmt.str(Sym.typeName(x.typ))]),
522+ say1("convert an operand to {} first, so that the operator works in it", Sym.typeName(t)));
523+end;
524+
495525 (* Whether the several results s may be passed on as t: as many, each
496526 of the same type or one that widens to it, as in return f(x) (report
497527 section 5.10). An INT and an I64 have one layout here, so the values
@@ -559,6 +589,10 @@
559589 if x.mode in {mType, mBuiltin, mNone, mCase} then return notValue(x); end;
560590 if Sym.same(s, t) then return nil; end;
561591 if widens(s, t) then x.typ := t; (* the same bits, on a 64-bit target *)
592+ elsif smallWidens(s, t) then
593+ if x.made then return madeNarrow(x, t); end;
594+ if x.mode # mConst then try value(x); end; (* loaded as s, extended *)
595+ x.typ := t;
562596 elsif (s = Sym.tRConst) and (t^.form = Sym.fReal) then realTo(x, t);
563597 elsif (s^.form = Sym.fConst) and (t^.form = Sym.fInt) then
564598 if (x.mode = mConst) and not Sym.fits(x.val, t) then
@@ -585,6 +619,11 @@
585619 return Scan.markHelp(x.pos,
586620 say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)),
587621 say1("to call it, write {}()", Diag.source(x.pos)));
622+ elsif (widens(Sym.tInt, t) and smallWidens(s, Sym.tInt)) or (widens(Sym.tUInt, t) and smallWidens(s, Sym.tUInt)) then
623+ (* a U8 widens one step, to INT, not on to an I64 *)
624+ return Scan.markHelp(x.pos, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)),
625+ Diag.say("{} widens to {} only as written, {}({})", [Fmt.str(Sym.typeName(s)),
626+ Fmt.str(Sym.typeName(t)), Fmt.str(Sym.typeName(t)), Fmt.str(Diag.source(x.pos))]));
588627 elsif widens(t, s) then
589628 (* the other way, an I64 into an INT, is written: it may drop bits *)
590629 return Scan.markHelp(x.pos, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)),
@@ -906,6 +945,7 @@
906945 return Scan.markAt(x.pos, "the constant is too big for I64");
907946 elsif x.mode = mConst then x.val := cut(-x.val, x.typ);
908947 else try value(x); Gen.neg(Gen.kWord, intSize(x.typ), signed(x.typ)); end;
948+ x.made := true;
909949 else return Scan.markAt(x.pos, say1("can't negate {}", Sym.typeName(x.typ))); end;
910950 return nil;
911951 end;
@@ -937,6 +977,7 @@
937977 end;
938978 if x.mode = mConst then x.val := cut(~x.val, x.typ);
939979 else try value(x); Gen.complement(intSize(x.typ), signed(x.typ)); end;
980+ x.made := true;
940981 return nil;
941982 end;
942983
@@ -1270,6 +1311,13 @@
12701311 (* an INT meeting an I64 widens to it, a UINT to a U64 (report
12711312 section 6.1): the same 64 bits here, so only the type changes *)
12721313 if widens(xt, yt) then t := yt; else t := xt; end;
1314+ elsif smallWidens(xt, yt) or smallWidens(yt, xt) then
1315+ (* a U8, I8 or I16 meeting an INT widens to it, unless an
1316+ operator made it (report section 6.1); each operand is
1317+ loaded as its own type, extended, so t is all that changes *)
1318+ if x.made and smallWidens(xt, yt) then return madeNarrow(x, yt); end;
1319+ if y.made and smallWidens(yt, xt) then return madeNarrow(y, xt); end;
1320+ if smallWidens(xt, yt) then t := yt; else t := xt; end;
12731321 else return mismatchOps(x, y, xt, yt); end;
12741322 if not intOp(op) then return doesntApply(op, x, y, t); end;
12751323 elsif (xt^.form = Sym.fReal) and (yt^.form = Sym.fReal) then
@@ -1337,6 +1385,7 @@
13371385 else
13381386 v := try foldInt(op, x.val, y.val, t, Diag.join(x.pos, y.pos));
13391387 makeConst(x, t, cut(v, t));
1388+ x.made := true;
13401389 end;
13411390 return nil;
13421391 end;
@@ -1375,6 +1424,7 @@
13751424 end;
13761425 x.mode := mStack;
13771426 if isRel(op) or (op = Scan.sIn) then x.typ := Sym.tBool; else x.typ := t; end;
1427+ x.made := true;
13781428 return nil;
13791429 end;
13801430
@@ -1393,6 +1443,7 @@
13931443 if x.mode = mStr then return Scan.markAt(x.pos, "can't convert a string"); end;
13941444 return notValue(x);
13951445 end;
1446+ x.made := false; (* what a conversion gives widens, U8(a + b) too *)
13961447 if Sym.same(s, t) then return nil; end;
13971448 if (Sym.isInt(s) or (s^.form in {Sym.fEnum, Sym.fSet})) and (t^.form = Sym.fInt) then
13981449 if x.mode = mConst then x.val := cut(x.val, t);

src/check.sh modified +6 -3

@@ -14,8 +14,9 @@
1414 # whose tests are conform/*/View*.raz, and which Math, examples/Elf.raz
1515 # and, through Math, test/bench/NBody.raz use; and returning a slice of
1616 # the frame as an error (report 2026-10.27); an array constant typed
17-# []T (report 2026-10.29); and [] as the empty slice, seen through a
18-# view (report 2026-10.30).
17+# []T (report 2026-10.29); [] as the empty slice, seen through a view
18+# (report 2026-10.30); and U8, I8 and I16 widening to INT (report
19+# 2026-10.31).
1920 newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz
2021 conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz
2122 conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz
@@ -25,7 +26,9 @@
2526 lib/Math.raz examples/Elf.raz test/bench/NBody.raz
2627 conform/err/FrameSlice.raz
2728 conform/run/ArrayConstLen.raz conform/err/ArrayConstLenEmpty.raz conform/err/ArrayConstLenLiteral.raz
28- conform/run/EmptySlice.raz"
29+ conform/run/EmptySlice.raz
30+ conform/run/SmallWiden.raz conform/err/SmallWidenMade.raz conform/err/SmallWidenNeg.raz
31+ conform/err/SmallWidenI64.raz conform/err/SmallWidenMadeOp.raz"
2932 newer() {
3033 case $1 in conform/*/View*.raz) return 0 ;; esac
3134 for g in $newer; do [ $g = $1 ] && return 0; done

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

@@ -0,0 +1,4 @@
1+conform/err/SmallWidenI32.raz:5:10: error: can't use I32 as INT
2+ 5 | n := a;
3+ | ^
4+raz: 1 error

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

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

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

@@ -0,0 +1,5 @@
1+conform/err/SmallWidenMade.raz:5:10: error: can't use U8 as INT: an operator made it, in U8, where it may have wrapped
2+ 5 | n := a + b;
3+ | ^^^^^
4+help: convert an operand to INT first, so that the operator works in it
5+raz: 1 error

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

@@ -0,0 +1,5 @@
1+conform/err/SmallWidenMadeOp.raz:5:14: error: can't use U8 as INT: an operator made it, in U8, where it may have wrapped
2+ 5 | n := n + (a * b);
3+ | ^^^^^^^
4+help: convert an operand to INT first, so that the operator works in it
5+raz: 1 error

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

@@ -0,0 +1,5 @@
1+conform/err/SmallWidenNeg.raz:5:10: error: can't use I8 as INT: an operator made it, in I8, where it may have wrapped
2+ 5 | n := -c;
3+ | ^^
4+help: convert an operand to INT first, so that the operator works in it
5+raz: 1 error

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

@@ -0,0 +1,4 @@
1+conform/err/SmallWidenU16.raz:5:10: error: can't use U16 as INT
2+ 5 | n := a;
3+ | ^
4+raz: 1 error