An ERROR constant given another is that same error, its chunk, so const Short*: ERROR = PictBase.Short; makes Short = PictBase.Short hold, as report 2026-10.36 has it
| @@ -280,7 +280,8 @@ | ||
| 280 | 280 | `LEN` of it is no constant. A constant's slice part takes `[]`, names |
| 281 | 281 | a constant whole, of any module, by its type, not its text, and takes |
| 282 | 282 | an array literal (report 2026-10.36), an array constant of its own in |
| 283 | - a chunk with no name, nested to any depth. | |
| 283 | + a chunk with no name, nested to any depth. An `ERROR` constant given | |
| 284 | + another takes its chunk: the same error. | |
| 284 | 285 | - An array a call returns is a temporary of the statement (report |
| 285 | 286 | 2026-10.28, 8): a slice of it kept past the statement is undefined |
| 286 | 287 | (10), and isn't caught. |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module ErrorConstNil; | |
| 2 | +const E: ERROR = nil; | |
| 3 | +end ErrorConstNil. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +module ErrorConstVar; | |
| 2 | +var last: ERROR; | |
| 3 | +const E: ERROR = last; | |
| 4 | +end ErrorConstVar. |
| @@ -0,0 +1 @@ | ||
| 1 | +same same same same different different different too short |
| @@ -0,0 +1,27 @@ | ||
| 1 | +(* An ERROR constant named by another is that same error; only a string | |
| 2 | + makes a new one (report section 5.8). *) | |
| 3 | +module ErrorConstNamed; | |
| 4 | +import Out, PictBase; | |
| 5 | + | |
| 6 | +const | |
| 7 | + Short*: ERROR = PictBase.Short; | |
| 8 | + Again: ERROR = Short; | |
| 9 | + Mine: ERROR = "too short"; | |
| 10 | + Errs: [2]ERROR = [Short, Mine]; | |
| 11 | + | |
| 12 | +proc main*; | |
| 13 | +var e: ERROR; | |
| 14 | +begin | |
| 15 | + e := PictBase.read(2); | |
| 16 | + if e = Short then Out.str("same "); end; | |
| 17 | + if Short = PictBase.Short then Out.str("same "); end; | |
| 18 | + if Again = PictBase.Short then Out.str("same "); end; | |
| 19 | + if Errs[0] = e then Out.str("same "); end; | |
| 20 | + if Mine # PictBase.Short then Out.str("different "); end; | |
| 21 | + if Errs[1] # e then Out.str("different "); end; | |
| 22 | + if Short # PictBase.Bad then Out.str("different "); end; | |
| 23 | + Out.str(MSG(Again)); | |
| 24 | + Out.ln; | |
| 25 | +end; | |
| 26 | + | |
| 27 | +end ErrorConstNamed. |
| @@ -0,0 +1,14 @@ | ||
| 1 | +(* Errors that another module passes on, for ErrorConstNamed *) | |
| 2 | +module PictBase; | |
| 3 | + | |
| 4 | +const | |
| 5 | + Short*: ERROR = "too short"; | |
| 6 | + Bad*: ERROR = "bad header"; | |
| 7 | + | |
| 8 | +proc read*(n: INT): ERROR; | |
| 9 | +begin | |
| 10 | + if n < 4 then return Short; end; | |
| 11 | + return nil; | |
| 12 | +end; | |
| 13 | + | |
| 14 | +end PictBase. |
| @@ -22,8 +22,8 @@ | ||
| 22 | 22 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 23 | 23 | | `src/X64.raz` | 1,594 | 1,310 | The instruction encoder, jumps within a procedure, the assembler | |
| 24 | 24 | | `src/Gen.raz` | 2,688 | 1,902 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 25 | -| `src/Item.raz` | 2,378 | 1,909 | Items: types, constants, variables, operators, conversions, calls | | |
| 26 | -| `src/Parse.raz` | 4,513 | 3,887 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 25 | +| `src/Item.raz` | 2,381 | 1,912 | Items: types, constants, variables, operators, conversions, calls | | |
| 26 | +| `src/Parse.raz` | 4,518 | 3,890 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 27 | 27 | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | |
| 28 | 28 | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | |
| 29 | 29 | | `src/Raz.raz` | 418 | 333 | The command line | |
| @@ -1407,6 +1407,14 @@ | ||
| 1407 | 1407 | |
| 1408 | 1408 | const Rows: [][]INT = [[1, 2], [3]]; (* read as *) |
| 1409 | 1409 | const R0: [2]INT = [1, 2]; R1: [1]INT = [3]; Rows: [][]INT = [R0, R1]; |
| 1410 | + | |
| 1411 | +An `ERROR` constant is a chunk of read-only data, its message's length | |
| 1412 | +and then its bytes, made by `errorChunk` (`Item.raz`) from a string; | |
| 1413 | +the constant's `val` is that chunk plus one, so that `nil` is 0, and | |
| 1414 | +errors compare by it. One whose value is another `ERROR` constant, | |
| 1415 | +`const Short*: ERROR = PictBase.Short;`, takes that one's `val` in | |
| 1416 | +`constDecl`, and no chunk: it is the same error, and `Short = | |
| 1417 | +PictBase.Short` holds. Only a string makes a new one. | |
| 1410 | 1418 | |
| 1411 | 1419 | `makeItem` makes it an |
| 1412 | 1420 | `mVar` there that isn't `isVar`, so it is read, indexed, sliced and |
| @@ -2352,7 +2352,10 @@ | ||
| 2352 | 2352 | proc errorChunk*(var x: Item; home, name: INT): (INT, ERROR); |
| 2353 | 2353 | var c, xs, xl: INT; p: ^U8; q: ^I64; |
| 2354 | 2354 | begin |
| 2355 | - if x.mode # mStr then return 0, Scan.markAt(x.pos, "an error is made from a string"); end; | |
| 2355 | + if x.mode # mStr then | |
| 2356 | + return 0, Scan.markHelp(x.pos, say1("an error is made from a string, found {}", what(x)), | |
| 2357 | + "or from another ERROR constant, which is the same error"); | |
| 2358 | + end; | |
| 2356 | 2359 | xl := INT(x.val); |
| 2357 | 2360 | p := Scan.grab(xl + 8); |
| 2358 | 2361 | q := ^I64(p); |
| @@ -3623,7 +3623,12 @@ | ||
| 3623 | 3623 | o := Sym.newObj(Sym.oConst, n, x.typ); |
| 3624 | 3624 | o^.pos := at; |
| 3625 | 3625 | o^.exported := exp and top; |
| 3626 | - if t = Sym.tError then | |
| 3626 | + if (t = Sym.tError) and (x.mode = Item.mConst) and (x.typ = Sym.tError) and (x.val > 0) then | |
| 3627 | + (* another ERROR constant (report, 5.8): that same error, its | |
| 3628 | + chunk, so that the two compare equal; only a string makes one *) | |
| 3629 | + o^.typ := Sym.tError; | |
| 3630 | + o^.val := x.val; | |
| 3631 | + elsif t = Sym.tError then | |
| 3627 | 3632 | o^.typ := Sym.tError; |
| 3628 | 3633 | c := try Item.errorChunk(x, Sym.curModule^.sname, n); |
| 3629 | 3634 | o^.val := c + 1; |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ErrorConstNil.raz:2:18: error: an error is made from a string, found nil | |
| 2 | + 2 | const E: ERROR = nil; | |
| 3 | + | ^^^ | |
| 4 | +help: or from another ERROR constant, which is the same error | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ErrorConstVar.raz:3:18: error: an error is made from a string, found ERROR | |
| 2 | + 3 | const E: ERROR = last; | |
| 3 | + | ^^^^ | |
| 4 | +help: or from another ERROR constant, which is the same error | |
| 5 | +raz: 1 error |
| @@ -1,4 +1,5 @@ | ||
| 1 | -conform/err/GenErrorString.raz:2:18: error: an error is made from a string | |
| 1 | +conform/err/GenErrorString.raz:2:18: error: an error is made from a string, found 1 | |
| 2 | 2 | 2 | const E: ERROR = 1; |
| 3 | 3 | | ^ |
| 4 | +help: or from another ERROR constant, which is the same error | |
| 4 | 5 | raz: 1 error |