raz / commit 9a94d612

commit
9a94d6127e5ca367eb6ce6ab0ae67475bd4125d9
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
71a95b30
The compiler: an address given for a var or const parameter says what to write

f(ADR(b)) for var b: Buf, or f(p) for a pointer p, is what every program
written before the library took var (report 2026-10.33) will meet. The
message now names the fix from the argument's own text: write b for
ADR(b), or p^ for p. test/err/VarGivenAddress.

patch · log

4 files changed, +57 -2

doc/compiler.md (history) modified +7 -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` | 2,091 | 1,682 | Items: types, constants, variables, operators, conversions, calls |
22+| `src/Item.raz` | 2,115 | 1,701 | Items: types, constants, variables, operators, conversions, calls |
2323 | `src/Parse.raz` | 4,218 | 3,648 | Declarations, statements, expressions, built-ins, recovery, warnings |
2424 | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
2525 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
@@ -1401,7 +1401,12 @@
14011401 a packed record's field, it puts in a temporary of the statement first
14021402 (`constTemp`). A `match` on something only read gives names that are
14031403 `isConst` too (`matchStatement`), where another module's variable is
1404-copied first and its names are the copy's. `return f(x);` passing on
1404+copied first and its names are the copy's.
1405+
1406+An address given where a `var` or `const` parameter of its type wants
1407+the variable, `f(ADR(b))` or `f(p)`, is the commonest slip in a program
1408+written before the library took `var`; `givenAddress` says what to
1409+write instead, `b` or `p^`, from the argument's own text. `return f(x);` passing on
14051410 f's several results, with `returnAll`, pushes them from the call's
14061411 temporary if they are two words, or else copies them to the place its
14071412 own hidden pointer names.

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

@@ -1572,6 +1572,28 @@
15721572 return nil;
15731573 end;
15741574
1575+(* An address given where a var or const parameter of its type wants the
1576+ variable itself, as ADR(b) for var b: Buf: an error that says what to
1577+ write instead, b for ADR(b) and p^ for a pointer p. Mostly met in
1578+ programs written before the library took var (report 2026-10.33). *)
1579+proc givenAddress(a: Item; p: ^Sym.Obj): ERROR;
1580+var s, kind: []U8;
1581+begin
1582+ if (a.typ^.form # Sym.fPtr) or (a.typ^.base = nil) or not Sym.same(a.typ^.base, p^.typ) then
1583+ return nil;
1584+ end;
1585+ if p^.isConst then kind := "const"; else kind := "var"; end;
1586+ s := Diag.source(a.pos);
1587+ if (LEN(s) > 5) and (s[0..4] = "ADR(") and (s[LEN(s) - 1] = ')') then
1588+ s := Diag.say("write {} for {}: the parameter is the variable itself, not its address",
1589+ [Fmt.str(s[4..LEN(s) - 1]), Fmt.str(s)]);
1590+ else
1591+ s := Diag.say("write {}^ for {}: the parameter is the variable itself, not a pointer to it",
1592+ [Fmt.str(s), Fmt.str(s)]);
1593+ end;
1594+ return Scan.markHelp(a.pos, say2("a {} parameter takes {}, found its address", kind, Sym.typeName(p^.typ)), s);
1595+end;
1596+
15751597 (* A const parameter's argument that isn't a variable, or is one with no
15761598 address of its own (a packed record's field, a view, through ^VOLATILE),
15771599 in a temporary of the statement, whose address then goes (report
@@ -1628,10 +1650,12 @@
16281650 value parameter, a record: copied to a temporary, its address goes
16291651 after, for const:
16301652 its own address goes; nothing is copied *)
1653+ try givenAddress(a, p);
16311654 try coerce(a, p^.typ);
16321655 if (a.mode # mVar) or a.odd or a.view or a.vol then try constTemp(a); end;
16331656 try address(a);
16341657 elsif p^.isVar then
1658+ try givenAddress(a, p);
16351659 try notReadOnly(a);
16361660 if (a.mode # mVar) or not a.isVar then return Scan.markAt(a.pos, "a var parameter needs a variable"); end;
16371661 if a.odd then return Scan.markAt(a.pos, "a field of a packed record can't be a var parameter"); end;

test/err/VarGivenAddress.err (history) added +13 -0

@@ -0,0 +1,13 @@
1+test/err/VarGivenAddress.raz:9:7: error: a var parameter takes B, found its address
2+ 9 | f(ADR(b));
3+ | ^^^^^^
4+help: write b for ADR(b): the parameter is the variable itself, not its address
5+test/err/VarGivenAddress.raz:10:7: error: a var parameter takes B, found its address
6+ 10 | f(p);
7+ | ^
8+help: write p^ for p: the parameter is the variable itself, not a pointer to it
9+test/err/VarGivenAddress.raz:11:12: error: a const parameter takes B, found its address
10+ 11 | _ := g(ADR(b));
11+ | ^^^^^^
12+help: write b for ADR(b): the parameter is the variable itself, not its address
13+raz: 3 errors

test/err/VarGivenAddress.raz (history) added +13 -0

@@ -0,0 +1,13 @@
1+module VarGivenAddress;
2+type B = record n: INT; end;
3+proc f(var b: B); begin b.n := 1; end;
4+proc g(const b: B): INT; begin return b.n; end;
5+proc main*;
6+var b: B; p: ^B;
7+begin
8+ p := ADR(b);
9+ f(ADR(b));
10+ f(p);
11+ _ := g(ADR(b));
12+end;
13+end VarGivenAddress.