From f4ad1385e5443c9f4dc87f35587332c5016a4636 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 08:08:44 +0000 Subject: [PATCH] An INT widens to an I64 wherever it goes to one, in an expression too --- diff --git a/CLAUDE.md b/CLAUDE.md index 76e3668..0c1880f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -190,9 +190,11 @@ (SIGFPE), as 6.1 says, `mod` as `div`; for a type narrower than 64 bits the code makes the division fault, as the machine wouldn't. In a constant it is an error, as dividing by zero is. -- `INT` widens to `I64` (and `UINT` to `U64`) wherever a value is - assigned, passed or returned, `return a, b` included; not in `a, b := - f(x)`, `return f(x)`, a `for` or `INC`/`DEC` (in the report, 5.10). +- `INT` widens to `I64` (and `UINT` to `U64`) wherever its value goes + to one: assigned, passed, returned, in `a, b := f(x)` and `return + f(x)`, a `for`'s bounds, `INC`/`DEC`'s step, and where it meets an + `I64` in an operator (report 2026-10.26, 5.10 and 6.1). Never to a + `U64`, nor an `I64` back to an `INT`. - An `extern` module variable is read-only for an importer, as the report says of every exported variable (3). - Warnings come only for a module with no errors. A procedure naming diff --git a/conform/err/IntFor.raz b/conform/err/IntFor.raz deleted file mode 100644 index 1df6e21..0000000 --- a/conform/err/IntFor.raz +++ /dev/null @@ -1,7 +0,0 @@ -module IntFor; -proc main*; -var i: I64; s: []U8; -begin - for i := 0 to LEN(s) - 1 do end; -end; -end IntFor. diff --git a/conform/err/IntInc.raz b/conform/err/IntInc.raz deleted file mode 100644 index 89f91d2..0000000 --- a/conform/err/IntInc.raz +++ /dev/null @@ -1,7 +0,0 @@ -module IntInc; -proc main*; -var i: I64; s: []U8; -begin - INC(i, LEN(s)); -end; -end IntInc. diff --git a/conform/err/IntIncNarrow.raz b/conform/err/IntIncNarrow.raz new file mode 100644 index 0000000..9912e05 --- /dev/null +++ b/conform/err/IntIncNarrow.raz @@ -0,0 +1,7 @@ +module IntIncNarrow; +proc main*; +var n: INT; a: I64; +begin + INC(n, a); +end; +end IntIncNarrow. diff --git a/conform/err/IntMix.raz b/conform/err/IntMix.raz deleted file mode 100644 index 557970d..0000000 --- a/conform/err/IntMix.raz +++ /dev/null @@ -1,7 +0,0 @@ -module IntMix; -proc main*; -var i: I64; s: []U8; -begin - if i < LEN(s) then end; -end; -end IntMix. diff --git a/conform/err/IntNarrowSum.raz b/conform/err/IntNarrowSum.raz new file mode 100644 index 0000000..83f48fc --- /dev/null +++ b/conform/err/IntNarrowSum.raz @@ -0,0 +1,7 @@ +module IntNarrowSum; +proc main*; +var n: INT; a: I64; +begin + n := a + n; +end; +end IntNarrowSum. diff --git a/conform/err/IntPassOnNarrow.raz b/conform/err/IntPassOnNarrow.raz new file mode 100644 index 0000000..9b63fe0 --- /dev/null +++ b/conform/err/IntPassOnNarrow.raz @@ -0,0 +1,8 @@ +module IntPassOnNarrow; +proc two: (I64, I64); begin return 1, 2; end; +proc twoNarrow: (INT, INT); begin return two(); end; +proc main*; +begin + _, _ := twoNarrow(); +end; +end IntPassOnNarrow. diff --git a/conform/err/IntResults.raz b/conform/err/IntResults.raz deleted file mode 100644 index 87900bf..0000000 --- a/conform/err/IntResults.raz +++ /dev/null @@ -1,8 +0,0 @@ -module IntResults; -proc two: (INT, INT); begin return 1, 2; end; -proc main*; -var a, b: I64; -begin - a, b := two(); -end; -end IntResults. diff --git a/conform/err/IntResultsNarrow.raz b/conform/err/IntResultsNarrow.raz new file mode 100644 index 0000000..a5cc514 --- /dev/null +++ b/conform/err/IntResultsNarrow.raz @@ -0,0 +1,8 @@ +module IntResultsNarrow; +proc two: (I64, I64); begin return 1, 2; end; +proc main*; +var a, b: INT; +begin + a, b := two(); +end; +end IntResultsNarrow. diff --git a/conform/err/IntUnsigned.raz b/conform/err/IntUnsigned.raz new file mode 100644 index 0000000..8a5698e --- /dev/null +++ b/conform/err/IntUnsigned.raz @@ -0,0 +1,7 @@ +module IntUnsigned; +proc main*; +var a: U64; b: INT; +begin + a := 5 + (a - b); +end; +end IntUnsigned. diff --git a/conform/err/IntVarParam.raz b/conform/err/IntVarParam.raz new file mode 100644 index 0000000..2b2f3c3 --- /dev/null +++ b/conform/err/IntVarParam.raz @@ -0,0 +1,8 @@ +module IntVarParam; +proc put(var x: I64); begin x := 1; end; +proc main*; +var n: INT; +begin + put(n); +end; +end IntVarParam. diff --git a/conform/run/IntWiden.expect b/conform/run/IntWiden.expect new file mode 100644 index 0000000..534c70e --- /dev/null +++ b/conform/run/IntWiden.expect @@ -0,0 +1,14 @@ +a + n 8 +n * a -20 +5 + (a - n) 17 +a + LEN(s) 15 +n * n + a 14 +n < a +LEN(s) = a - 5 +sum of 0 to LEN(s) - 1 10 +a after INC and DEC 17 +a, b := two() 34 +twoWide 34 +threeWide 567 +0000000000000006 +w > u diff --git a/conform/run/IntWiden.raz b/conform/run/IntWiden.raz new file mode 100644 index 0000000..0a452c3 --- /dev/null +++ b/conform/run/IntWiden.raz @@ -0,0 +1,68 @@ +(* An INT widens to an I64, and a UINT to a U64, wherever its value + goes to one: in an expression, a relation, a for loop's bounds, the + step of INC and DEC, and several results passed on (report sections + 5.10, 6.1 and 7.2). *) +module IntWiden; +import Out; + +proc two: (INT, INT); +begin + return 3, 4; +end; + +proc three: (INT, INT, INT); +begin + return 5, 6, 7; +end; + +(* passed on whole: two results come back in registers, three through + memory *) +proc twoWide: (I64, I64); +begin + return two(); +end; + +proc threeWide: (I64, I64, I64); +begin + return three(); +end; + +proc show(label: []U8; v: I64); +begin + Out.str(label); Out.str(" "); Out.int(v); Out.str("\n"); +end; + +proc main*; +var + a, b, c, i: I64; n: INT; s: []U8; u: U64; w: UINT; +begin + s := "hello"; + n := -2; + a := 10; + show("a + n", a + n); + show("n * a", n * a); + show("5 + (a - n)", 5 + (a - n)); + show("a + LEN(s)", a + LEN(s)); + show("n * n + a", n * n + a); + if n < a then Out.str("n < a\n"); end; + if LEN(s) = a - 5 then Out.str("LEN(s) = a - 5\n"); end; + c := 0; + for i := 0 to LEN(s) - 1 do c := c + i; end; + show("sum of 0 to LEN(s) - 1", c); + INC(a, LEN(s)); + DEC(a, n); + show("a after INC and DEC", a); + a, b := two(); + show("a, b := two()", a * 10 + b); + a, b := twoWide(); + show("twoWide", a * 10 + b); + a, b, c := threeWide(); + show("threeWide", a * 100 + b * 10 + c); + w := 7; + u := 0FFFFFFFFFFFFFFFFH; + u := u + w; (* wraps around, as U64 does *) + Out.hex(u, 16); Out.str("\n"); + if w > u then Out.str("w > u\n"); end; +end; + +end IntWiden. diff --git a/doc/compiler.md b/doc/compiler.md index c35ed40..79c4019 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -19,8 +19,8 @@ | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,516 | 1,800 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Item.raz` | 1,782 | 1,473 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 3,949 | 3,441 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Item.raz` | 1,792 | 1,478 | Items: types, constants, variables, operators, conversions, calls | +| `src/Parse.raz` | 3,960 | 3,445 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 305 | 238 | The command line | @@ -1274,8 +1274,12 @@ Assignment and conversion: `coerce` makes a value fit the type it's going to, or reports that it can't; it's where a string literal becomes a slice, an array a slice, an untyped constant a typed one, and an `INT` -an `I64` (`widens`). `coerceExact` is for where the types must be the -same, as in a `for` loop, `INC` and several results. `assign` stores, or +an `I64` (`widens`), wherever it goes: a `for` loop's bounds, `INC`'s +step and several results pass through `coerce` too. An `INT` meeting +an `I64` in an operator is `op2`'s, which gives the result the `I64`; +`return f(x)` takes results that widen by `resultsFit`. None of it +makes code: an `INT` and an `I64` are the same 64 bits on x86-64, so +only the item's type changes. `assign` stores, or copies records and arrays. `convert` is the written conversion, `T(x)`. Calls: `beginCall`, then `passArg` for each argument, then `endCall`, diff --git a/src/Item.raz b/src/Item.raz index 761d5e0..8314117 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -464,12 +464,29 @@ end; (* An INT goes silently into an I64, and a UINT into a U64, losing - nothing on any target (report section 5.10). *) -proc widens(s, t: ^Sym.Type): BOOL; + nothing on any target, wherever its value goes to one (report + section 5.10). *) +proc widens*(s, t: ^Sym.Type): BOOL; begin return ((s = Sym.tInt) and Sym.same(t, Sym.tI64)) or ((s = Sym.tUInt) and Sym.same(t, Sym.tU64)); end; +(* Whether the several results s may be passed on as t: as many, each + of the same type or one that widens to it, as in return f(x) (report + section 5.10). An INT and an I64 have one layout here, so the values + pass as they are. *) +proc resultsFit*(s, t: ^Sym.Type): BOOL; +var f, g: ^Sym.Obj; +begin + if not s^.tuple or not t^.tuple or (s^.len # t^.len) then return false; end; + f := s^.fields; g := t^.fields; + while f # nil do + if not Sym.same(f^.typ, g^.typ) and not widens(f^.typ, g^.typ) then return false; end; + f := f^.next; g := g^.next; + end; + return true; +end; + (* Another module's exported variable can be read, not changed, nor handed to anything that could change it (report section 3). *) proc notReadOnly*(x: Item): ERROR; @@ -537,28 +554,17 @@ return Scan.markHelp(x.pos, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)), say1("to call it, write {}()", Diag.source(x.pos))); + elsif widens(t, s) then + (* the other way, an I64 into an INT, is written: it may drop bits *) + return Scan.markHelp(x.pos, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)), + Diag.say("{} becomes {} only as written, {}({})", [Fmt.str(Sym.typeName(s)), + Fmt.str(Sym.typeName(t)), Fmt.str(Sym.typeName(t)), Fmt.str(Diag.source(x.pos))])); else return mismatch(x, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t))); end; return nil; end; -(* coerce where the types must be the same, as in a for loop, INC and - several results, which don't widen an INT to an I64 (report sections - 5.10, 6.1 and 7.2); help, if not empty, says why. *) -proc coerceExact*(var x: Item; t: ^Sym.Type; help: []U8): ERROR; -var h: []U8; -begin - if widens(x.typ, t) then - h := help; - if LEN(h) = 0 then - h := say2("they don't mix here: write {}({})", Sym.typeName(t), Diag.source(x.pos)); - end; - return Scan.markHelp(x.pos, say2("expected {}, found {}", Sym.typeName(t), what(x)), h); - end; - return coerce(x, t); -end; - (* dst := src. The address of dst, if it has to be computed, is on the stack already, under src's. *) proc assign*(var dst, src: Item): ERROR; @@ -1084,6 +1090,10 @@ end; t := xt; elsif Sym.same(xt, yt) then t := xt; + (* an INT meeting an I64 widens to it, a UINT to a U64 (report + section 6.1): the same 64 bits here, so only the type changes *) + elsif widens(xt, yt) then t := yt; + elsif widens(yt, xt) then t := xt; else return mismatchOps(x, y, xt, yt); end; if not intOp(op) then return doesntApply(op, x, y, t); end; elsif (xt^.form = Sym.fReal) and (yt^.form = Sym.fReal) then @@ -1723,7 +1733,7 @@ (* INC(v, k) or DEC: v's address is on the stack, from address. *) proc incDec*(var v, k: Item; dec: BOOL): ERROR; begin - try coerceExact(k, v.typ, ""); + try coerce(k, v.typ); try value(k); if v.base = Gen.bReg then Gen.incDecReg(v.chunk, scalarSize(v.typ), v.typ^.signed, dec); else Gen.incDec(scalarSize(v.typ), v.typ^.signed, dec); end; diff --git a/src/Parse.raz b/src/Parse.raz index f2d58fe..45cd158 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -1743,11 +1743,11 @@ try expect(Scan.sBecomes); try Item.makeItem(v, o); try expression(a, o^.typ); - try Item.coerceExact(a, o^.typ, ""); + try Item.coerce(a, o^.typ); try Item.assign(v, a); try expect(Scan.sTo); try expression(b, o^.typ); - try Item.coerceExact(b, o^.typ, ""); + try Item.coerce(b, o^.typ); (* the limit, evaluated once: a constant as it is; else, in an innermost loop, in the register chooseRegs kept for it, as a variable no one can name; else in a slot *) @@ -2146,7 +2146,13 @@ if blanks[i] then try Item.discard(z); else - try Item.coerceExact(z, d[i].typ, "several results go to variables of just their types"); + (* an INT result widens to an I64 variable; an I64 result + into an INT is written, but not here, in the call *) + if Item.widens(d[i].typ, z.typ) then + return Scan.markAt(y.pos, Diag.say("result {} is {}, which can't go to {}", + [Fmt.int(i + 1), Fmt.str(Sym.typeName(z.typ)), Fmt.str(Sym.typeName(d[i].typ))])); + end; + try Item.coerce(z, d[i].typ); try Item.assign(d[i], z); end; f := f^.next; @@ -2163,7 +2169,7 @@ while f # nil do try expression(x, f^.typ); if x.typ^.tuple then - if not Sym.same(x.typ, curResult) then + if not Sym.same(x.typ, curResult) and not Item.resultsFit(x.typ, curResult) then return Scan.markAt(x.pos, say2("expected the results {}, found {}", Sym.typeName(curResult), Sym.typeName(x.typ))); end; return Item.returnAll(x); diff --git a/src/check.sh b/src/check.sh index 33e904d..754b95a 100755 --- a/src/check.sh +++ b/src/check.sh @@ -7,11 +7,15 @@ b=build/src # The seed predates raw strings and match on a string (report # 2026-10.25): the files that use them are the compiler's alone, and -# so are those whose message says what match takes. +# so are those whose message says what match takes. It predates, too, +# an INT widened in an expression and the rest (report 2026-10.26), +# and the messages for an I64 that goes to an INT. newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz - examples/Sh.raz examples/Settings.raz" + examples/Sh.raz examples/Settings.raz + conform/run/IntWiden.raz conform/err/IntIncNarrow.raz conform/err/IntNarrow.raz conform/err/IntNarrowSum.raz + conform/err/IntResultsNarrow.raz" newer() { for g in $newer; do [ $g = $1 ] && return 0; done return 1 diff --git a/test/msg/IntFor.err b/test/msg/IntFor.err deleted file mode 100644 index db68f7f..0000000 --- a/test/msg/IntFor.err +++ /dev/null @@ -1,5 +0,0 @@ -conform/err/IntFor.raz:5:19: error: expected I64, found INT - 5 | for i := 0 to LEN(s) - 1 do end; - | ^^^^^^^^^^ -help: they don't mix here: write I64(LEN(s) - 1) -raz: 1 error diff --git a/test/msg/IntInc.err b/test/msg/IntInc.err deleted file mode 100644 index 241bf3b..0000000 --- a/test/msg/IntInc.err +++ /dev/null @@ -1,5 +0,0 @@ -conform/err/IntInc.raz:5:12: error: expected I64, found INT - 5 | INC(i, LEN(s)); - | ^^^^^^ -help: they don't mix here: write I64(LEN(s)) -raz: 1 error diff --git a/test/msg/IntIncNarrow.err b/test/msg/IntIncNarrow.err new file mode 100644 index 0000000..d5c64b4 --- /dev/null +++ b/test/msg/IntIncNarrow.err @@ -0,0 +1,5 @@ +conform/err/IntIncNarrow.raz:5:12: error: can't use I64 as INT + 5 | INC(n, a); + | ^ +help: I64 becomes INT only as written, INT(a) +raz: 1 error diff --git a/test/msg/IntMix.err b/test/msg/IntMix.err deleted file mode 100644 index a26e314..0000000 --- a/test/msg/IntMix.err +++ /dev/null @@ -1,5 +0,0 @@ -conform/err/IntMix.raz:5:8: error: I64 and INT are different types - 5 | if i < LEN(s) then end; - | ^^^^^^^^^^ -help: convert one to the other's type, as in I64(LEN(s)) -raz: 1 error diff --git a/test/msg/IntNarrow.err b/test/msg/IntNarrow.err index 1111982..a3eb19e 100644 --- a/test/msg/IntNarrow.err +++ b/test/msg/IntNarrow.err @@ -1,4 +1,5 @@ conform/err/IntNarrow.raz:5:10: error: can't use I64 as INT 5 | n := i; | ^ +help: I64 becomes INT only as written, INT(i) raz: 1 error diff --git a/test/msg/IntNarrowSum.err b/test/msg/IntNarrowSum.err new file mode 100644 index 0000000..7a38fe5 --- /dev/null +++ b/test/msg/IntNarrowSum.err @@ -0,0 +1,5 @@ +conform/err/IntNarrowSum.raz:5:10: error: can't use I64 as INT + 5 | n := a + n; + | ^^^^^ +help: I64 becomes INT only as written, INT(a + n) +raz: 1 error diff --git a/test/msg/IntPassOnNarrow.err b/test/msg/IntPassOnNarrow.err new file mode 100644 index 0000000..be9b76d --- /dev/null +++ b/test/msg/IntPassOnNarrow.err @@ -0,0 +1,4 @@ +conform/err/IntPassOnNarrow.raz:3:42: error: expected the results (INT, INT), found (I64, I64) + 3 | proc twoNarrow: (INT, INT); begin return two(); end; + | ^^^^^ +raz: 1 error diff --git a/test/msg/IntResults.err b/test/msg/IntResults.err deleted file mode 100644 index 617eb63..0000000 --- a/test/msg/IntResults.err +++ /dev/null @@ -1,5 +0,0 @@ -conform/err/IntResults.raz:6:13: error: expected I64, found INT - 6 | a, b := two(); - | ^^^^^ -help: several results go to variables of just their types -raz: 1 error diff --git a/test/msg/IntResultsNarrow.err b/test/msg/IntResultsNarrow.err new file mode 100644 index 0000000..e458188 --- /dev/null +++ b/test/msg/IntResultsNarrow.err @@ -0,0 +1,4 @@ +conform/err/IntResultsNarrow.raz:6:13: error: result 1 is I64, which can't go to INT + 6 | a, b := two(); + | ^^^^^ +raz: 1 error diff --git a/test/msg/IntUnsigned.err b/test/msg/IntUnsigned.err new file mode 100644 index 0000000..ca4b3fc --- /dev/null +++ b/test/msg/IntUnsigned.err @@ -0,0 +1,5 @@ +conform/err/IntUnsigned.raz:5:15: error: U64 and INT are different types + 5 | a := 5 + (a - b); + | ^^^^^ +help: convert one to the other's type, as in U64(b) +raz: 1 error diff --git a/test/msg/IntVarParam.err b/test/msg/IntVarParam.err new file mode 100644 index 0000000..7dc9ef4 --- /dev/null +++ b/test/msg/IntVarParam.err @@ -0,0 +1,7 @@ +conform/err/IntVarParam.raz:6:9: error: a var parameter needs exactly I64, found INT + 6 | put(n); + | ^ +conform/err/IntVarParam.raz:2:14: note: it goes to x, declared here + 2 | proc put(var x: I64); begin x := 1; end; + | ^ +raz: 1 error