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.
| @@ -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` | 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 | | |
| 23 | 23 | | `src/Parse.raz` | 4,218 | 3,648 | 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` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | |
| @@ -1401,7 +1401,12 @@ | ||
| 1401 | 1401 | a packed record's field, it puts in a temporary of the statement first |
| 1402 | 1402 | (`constTemp`). A `match` on something only read gives names that are |
| 1403 | 1403 | `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 | |
| 1405 | 1410 | f's several results, with `returnAll`, pushes them from the call's |
| 1406 | 1411 | temporary if they are two words, or else copies them to the place its |
| 1407 | 1412 | own hidden pointer names. |
| @@ -1572,6 +1572,28 @@ | ||
| 1572 | 1572 | return nil; |
| 1573 | 1573 | end; |
| 1574 | 1574 | |
| 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 | + | |
| 1575 | 1597 | (* A const parameter's argument that isn't a variable, or is one with no |
| 1576 | 1598 | address of its own (a packed record's field, a view, through ^VOLATILE), |
| 1577 | 1599 | in a temporary of the statement, whose address then goes (report |
| @@ -1628,10 +1650,12 @@ | ||
| 1628 | 1650 | value parameter, a record: copied to a temporary, its address goes |
| 1629 | 1651 | after, for const: |
| 1630 | 1652 | its own address goes; nothing is copied *) |
| 1653 | + try givenAddress(a, p); | |
| 1631 | 1654 | try coerce(a, p^.typ); |
| 1632 | 1655 | if (a.mode # mVar) or a.odd or a.view or a.vol then try constTemp(a); end; |
| 1633 | 1656 | try address(a); |
| 1634 | 1657 | elsif p^.isVar then |
| 1658 | + try givenAddress(a, p); | |
| 1635 | 1659 | try notReadOnly(a); |
| 1636 | 1660 | if (a.mode # mVar) or not a.isVar then return Scan.markAt(a.pos, "a var parameter needs a variable"); end; |
| 1637 | 1661 | if a.odd then return Scan.markAt(a.pos, "a field of a packed record can't be a var parameter"); end; |
| @@ -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 |
| @@ -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. |