From 9a94d6127e5ca367eb6ce6ab0ae67475bd4125d9 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 08:42:00 +0000 Subject: [PATCH] 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. --- diff --git a/doc/compiler.md b/doc/compiler.md index 473419b..148a27b 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -19,7 +19,7 @@ | `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,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Item.raz` | 2,091 | 1,682 | Items: types, constants, variables, operators, conversions, calls | +| `src/Item.raz` | 2,115 | 1,701 | Items: types, constants, variables, operators, conversions, calls | | `src/Parse.raz` | 4,218 | 3,648 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | @@ -1401,7 +1401,12 @@ a packed record's field, it puts in a temporary of the statement first (`constTemp`). A `match` on something only read gives names that are `isConst` too (`matchStatement`), where another module's variable is -copied first and its names are the copy's. `return f(x);` passing on +copied first and its names are the copy's. + +An address given where a `var` or `const` parameter of its type wants +the variable, `f(ADR(b))` or `f(p)`, is the commonest slip in a program +written before the library took `var`; `givenAddress` says what to +write instead, `b` or `p^`, from the argument's own text. `return f(x);` passing on f's several results, with `returnAll`, pushes them from the call's temporary if they are two words, or else copies them to the place its own hidden pointer names. diff --git a/src/Item.raz b/src/Item.raz index 0c0a770..ee74e81 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -1572,6 +1572,28 @@ return nil; end; +(* An address given where a var or const parameter of its type wants the + variable itself, as ADR(b) for var b: Buf: an error that says what to + write instead, b for ADR(b) and p^ for a pointer p. Mostly met in + programs written before the library took var (report 2026-10.33). *) +proc givenAddress(a: Item; p: ^Sym.Obj): ERROR; +var s, kind: []U8; +begin + if (a.typ^.form # Sym.fPtr) or (a.typ^.base = nil) or not Sym.same(a.typ^.base, p^.typ) then + return nil; + end; + if p^.isConst then kind := "const"; else kind := "var"; end; + s := Diag.source(a.pos); + if (LEN(s) > 5) and (s[0..4] = "ADR(") and (s[LEN(s) - 1] = ')') then + s := Diag.say("write {} for {}: the parameter is the variable itself, not its address", + [Fmt.str(s[4..LEN(s) - 1]), Fmt.str(s)]); + else + s := Diag.say("write {}^ for {}: the parameter is the variable itself, not a pointer to it", + [Fmt.str(s), Fmt.str(s)]); + end; + return Scan.markHelp(a.pos, say2("a {} parameter takes {}, found its address", kind, Sym.typeName(p^.typ)), s); +end; + (* A const parameter's argument that isn't a variable, or is one with no address of its own (a packed record's field, a view, through ^VOLATILE), in a temporary of the statement, whose address then goes (report @@ -1628,10 +1650,12 @@ value parameter, a record: copied to a temporary, its address goes after, for const: its own address goes; nothing is copied *) + try givenAddress(a, p); try coerce(a, p^.typ); if (a.mode # mVar) or a.odd or a.view or a.vol then try constTemp(a); end; try address(a); elsif p^.isVar then + try givenAddress(a, p); try notReadOnly(a); if (a.mode # mVar) or not a.isVar then return Scan.markAt(a.pos, "a var parameter needs a variable"); end; if a.odd then return Scan.markAt(a.pos, "a field of a packed record can't be a var parameter"); end; diff --git a/test/err/VarGivenAddress.err b/test/err/VarGivenAddress.err new file mode 100644 index 0000000..e0c0918 --- /dev/null +++ b/test/err/VarGivenAddress.err @@ -0,0 +1,13 @@ +test/err/VarGivenAddress.raz:9:7: error: a var parameter takes B, found its address + 9 | f(ADR(b)); + | ^^^^^^ +help: write b for ADR(b): the parameter is the variable itself, not its address +test/err/VarGivenAddress.raz:10:7: error: a var parameter takes B, found its address + 10 | f(p); + | ^ +help: write p^ for p: the parameter is the variable itself, not a pointer to it +test/err/VarGivenAddress.raz:11:12: error: a const parameter takes B, found its address + 11 | _ := g(ADR(b)); + | ^^^^^^ +help: write b for ADR(b): the parameter is the variable itself, not its address +raz: 3 errors diff --git a/test/err/VarGivenAddress.raz b/test/err/VarGivenAddress.raz new file mode 100644 index 0000000..4fbea1b --- /dev/null +++ b/test/err/VarGivenAddress.raz @@ -0,0 +1,13 @@ +module VarGivenAddress; +type B = record n: INT; end; +proc f(var b: B); begin b.n := 1; end; +proc g(const b: B): INT; begin return b.n; end; +proc main*; +var b: B; p: ^B; +begin + p := ADR(b); + f(ADR(b)); + f(p); + _ := g(ADR(b)); +end; +end VarGivenAddress.