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.
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +7319 300 44 -5 255 -30000 88 200 205 -5 65535 200 |
| @@ -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. |
| @@ -19,7 +19,7 @@ | ||
| 19 | 19 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 20 | 20 | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | |
| 21 | 21 | | `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 | | |
| 23 | 23 | | `src/Parse.raz` | 4,049 | 3,518 | Declarations, statements, expressions, built-ins, recovery, warnings | |
| 24 | 24 | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | |
| 25 | 25 | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | |
| @@ -1303,7 +1303,23 @@ | ||
| 1303 | 1303 | an `I64` in an operator is `op2`'s, which gives the result the `I64`; |
| 1304 | 1304 | `return f(x)` takes results that widen by `resultsFit`. None of it |
| 1305 | 1305 | 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 | |
| 1307 | 1323 | copies records and arrays. `convert` is the written conversion, `T(x)`. |
| 1308 | 1324 | |
| 1309 | 1325 | Views, `v.(T)` (report section 6.9): a view is the item of `v` with |
| @@ -682,8 +682,9 @@ | ||
| 682 | 682 | A `U8`, `I8` or `I16` widens to `INT`, and a `U8` or `U16` to `UINT`, |
| 683 | 683 | in the same places, as an `INT` to `I64`: assigned, passed, returned, |
| 684 | 684 | 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 | |
| 687 | 688 | `I64(b)`. And they widen only as values that are moved: a variable, an |
| 688 | 689 | element, a field, a call's result or a conversion's. What an operator |
| 689 | 690 | gives on them doesn't: it was computed in their type, and has wrapped |
| @@ -60,6 +60,9 @@ | ||
| 60 | 60 | anything else: lies in that frame, a local, a |
| 61 | 61 | value parameter or a temporary, or part of one *) |
| 62 | 62 | 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) *) | |
| 63 | 66 | end; |
| 64 | 67 | |
| 65 | 68 | const |
| @@ -492,6 +495,33 @@ | ||
| 492 | 495 | return ((s = Sym.tInt) and Sym.same(t, Sym.tI64)) or ((s = Sym.tUInt) and Sym.same(t, Sym.tU64)); |
| 493 | 496 | end; |
| 494 | 497 | |
| 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 | + | |
| 495 | 525 | (* Whether the several results s may be passed on as t: as many, each |
| 496 | 526 | of the same type or one that widens to it, as in return f(x) (report |
| 497 | 527 | section 5.10). An INT and an I64 have one layout here, so the values |
| @@ -559,6 +589,10 @@ | ||
| 559 | 589 | if x.mode in {mType, mBuiltin, mNone, mCase} then return notValue(x); end; |
| 560 | 590 | if Sym.same(s, t) then return nil; end; |
| 561 | 591 | 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; | |
| 562 | 596 | elsif (s = Sym.tRConst) and (t^.form = Sym.fReal) then realTo(x, t); |
| 563 | 597 | elsif (s^.form = Sym.fConst) and (t^.form = Sym.fInt) then |
| 564 | 598 | if (x.mode = mConst) and not Sym.fits(x.val, t) then |
| @@ -585,6 +619,11 @@ | ||
| 585 | 619 | return Scan.markHelp(x.pos, |
| 586 | 620 | say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)), |
| 587 | 621 | 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))])); | |
| 588 | 627 | elsif widens(t, s) then |
| 589 | 628 | (* the other way, an I64 into an INT, is written: it may drop bits *) |
| 590 | 629 | return Scan.markHelp(x.pos, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)), |
| @@ -906,6 +945,7 @@ | ||
| 906 | 945 | return Scan.markAt(x.pos, "the constant is too big for I64"); |
| 907 | 946 | elsif x.mode = mConst then x.val := cut(-x.val, x.typ); |
| 908 | 947 | else try value(x); Gen.neg(Gen.kWord, intSize(x.typ), signed(x.typ)); end; |
| 948 | + x.made := true; | |
| 909 | 949 | else return Scan.markAt(x.pos, say1("can't negate {}", Sym.typeName(x.typ))); end; |
| 910 | 950 | return nil; |
| 911 | 951 | end; |
| @@ -937,6 +977,7 @@ | ||
| 937 | 977 | end; |
| 938 | 978 | if x.mode = mConst then x.val := cut(~x.val, x.typ); |
| 939 | 979 | else try value(x); Gen.complement(intSize(x.typ), signed(x.typ)); end; |
| 980 | + x.made := true; | |
| 940 | 981 | return nil; |
| 941 | 982 | end; |
| 942 | 983 | |
| @@ -1270,6 +1311,13 @@ | ||
| 1270 | 1311 | (* an INT meeting an I64 widens to it, a UINT to a U64 (report |
| 1271 | 1312 | section 6.1): the same 64 bits here, so only the type changes *) |
| 1272 | 1313 | 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; | |
| 1273 | 1321 | else return mismatchOps(x, y, xt, yt); end; |
| 1274 | 1322 | if not intOp(op) then return doesntApply(op, x, y, t); end; |
| 1275 | 1323 | elsif (xt^.form = Sym.fReal) and (yt^.form = Sym.fReal) then |
| @@ -1337,6 +1385,7 @@ | ||
| 1337 | 1385 | else |
| 1338 | 1386 | v := try foldInt(op, x.val, y.val, t, Diag.join(x.pos, y.pos)); |
| 1339 | 1387 | makeConst(x, t, cut(v, t)); |
| 1388 | + x.made := true; | |
| 1340 | 1389 | end; |
| 1341 | 1390 | return nil; |
| 1342 | 1391 | end; |
| @@ -1375,6 +1424,7 @@ | ||
| 1375 | 1424 | end; |
| 1376 | 1425 | x.mode := mStack; |
| 1377 | 1426 | if isRel(op) or (op = Scan.sIn) then x.typ := Sym.tBool; else x.typ := t; end; |
| 1427 | + x.made := true; | |
| 1378 | 1428 | return nil; |
| 1379 | 1429 | end; |
| 1380 | 1430 | |
| @@ -1393,6 +1443,7 @@ | ||
| 1393 | 1443 | if x.mode = mStr then return Scan.markAt(x.pos, "can't convert a string"); end; |
| 1394 | 1444 | return notValue(x); |
| 1395 | 1445 | end; |
| 1446 | + x.made := false; (* what a conversion gives widens, U8(a + b) too *) | |
| 1396 | 1447 | if Sym.same(s, t) then return nil; end; |
| 1397 | 1448 | if (Sym.isInt(s) or (s^.form in {Sym.fEnum, Sym.fSet})) and (t^.form = Sym.fInt) then |
| 1398 | 1449 | if x.mode = mConst then x.val := cut(x.val, t); |
| @@ -14,8 +14,9 @@ | ||
| 14 | 14 | # whose tests are conform/*/View*.raz, and which Math, examples/Elf.raz |
| 15 | 15 | # and, through Math, test/bench/NBody.raz use; and returning a slice of |
| 16 | 16 | # 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). | |
| 19 | 20 | newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz |
| 20 | 21 | conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz |
| 21 | 22 | conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz |
| @@ -25,7 +26,9 @@ | ||
| 25 | 26 | lib/Math.raz examples/Elf.raz test/bench/NBody.raz |
| 26 | 27 | conform/err/FrameSlice.raz |
| 27 | 28 | 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" | |
| 29 | 32 | newer() { |
| 30 | 33 | case $1 in conform/*/View*.raz) return 0 ;; esac |
| 31 | 34 | for g in $newer; do [ $g = $1 ] && return 0; done |
| @@ -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 |
| @@ -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 |
| @@ -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 |
| @@ -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 |
| @@ -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 |
| @@ -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 |