From 73c1f3ac156c992c391302bcad1680a6aac5474d Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:17:43 +0100 Subject: [PATCH] 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 --- diff --git a/CLAUDE.md b/CLAUDE.md index dbda932..63f1109 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -280,7 +280,8 @@ `LEN` of it is no constant. A constant's slice part takes `[]`, names a constant whole, of any module, by its type, not its text, and takes an array literal (report 2026-10.36), an array constant of its own in - a chunk with no name, nested to any depth. + a chunk with no name, nested to any depth. An `ERROR` constant given + another takes its chunk: the same error. - An array a call returns is a temporary of the statement (report 2026-10.28, 8): a slice of it kept past the statement is undefined (10), and isn't caught. diff --git a/conform/err/ErrorConstNil.raz b/conform/err/ErrorConstNil.raz new file mode 100644 index 0000000..e09fbb1 --- /dev/null +++ b/conform/err/ErrorConstNil.raz @@ -0,0 +1,3 @@ +module ErrorConstNil; +const E: ERROR = nil; +end ErrorConstNil. diff --git a/conform/err/ErrorConstVar.raz b/conform/err/ErrorConstVar.raz new file mode 100644 index 0000000..5a38a3d --- /dev/null +++ b/conform/err/ErrorConstVar.raz @@ -0,0 +1,4 @@ +module ErrorConstVar; +var last: ERROR; +const E: ERROR = last; +end ErrorConstVar. diff --git a/conform/run/ErrorConstNamed.expect b/conform/run/ErrorConstNamed.expect new file mode 100644 index 0000000..fbb7d09 --- /dev/null +++ b/conform/run/ErrorConstNamed.expect @@ -0,0 +1 @@ +same same same same different different different too short diff --git a/conform/run/ErrorConstNamed.raz b/conform/run/ErrorConstNamed.raz new file mode 100644 index 0000000..dd19d53 --- /dev/null +++ b/conform/run/ErrorConstNamed.raz @@ -0,0 +1,27 @@ +(* An ERROR constant named by another is that same error; only a string + makes a new one (report section 5.8). *) +module ErrorConstNamed; +import Out, PictBase; + +const + Short*: ERROR = PictBase.Short; + Again: ERROR = Short; + Mine: ERROR = "too short"; + Errs: [2]ERROR = [Short, Mine]; + +proc main*; +var e: ERROR; +begin + e := PictBase.read(2); + if e = Short then Out.str("same "); end; + if Short = PictBase.Short then Out.str("same "); end; + if Again = PictBase.Short then Out.str("same "); end; + if Errs[0] = e then Out.str("same "); end; + if Mine # PictBase.Short then Out.str("different "); end; + if Errs[1] # e then Out.str("different "); end; + if Short # PictBase.Bad then Out.str("different "); end; + Out.str(MSG(Again)); + Out.ln; +end; + +end ErrorConstNamed. diff --git a/conform/run/mods/PictBase.raz b/conform/run/mods/PictBase.raz new file mode 100644 index 0000000..e30d0bf --- /dev/null +++ b/conform/run/mods/PictBase.raz @@ -0,0 +1,14 @@ +(* Errors that another module passes on, for ErrorConstNamed *) +module PictBase; + +const + Short*: ERROR = "too short"; + Bad*: ERROR = "bad header"; + +proc read*(n: INT): ERROR; +begin + if n < 4 then return Short; end; + return nil; +end; + +end PictBase. diff --git a/doc/compiler.md b/doc/compiler.md index f32be3f..c5d8460 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -22,8 +22,8 @@ | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,594 | 1,310 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,688 | 1,902 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Item.raz` | 2,378 | 1,909 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,513 | 3,887 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Item.raz` | 2,381 | 1,912 | Items: types, constants, variables, operators, conversions, calls | +| `src/Parse.raz` | 4,518 | 3,890 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 418 | 333 | The command line | @@ -1407,6 +1407,14 @@ const Rows: [][]INT = [[1, 2], [3]]; (* read as *) const R0: [2]INT = [1, 2]; R1: [1]INT = [3]; Rows: [][]INT = [R0, R1]; + +An `ERROR` constant is a chunk of read-only data, its message's length +and then its bytes, made by `errorChunk` (`Item.raz`) from a string; +the constant's `val` is that chunk plus one, so that `nil` is 0, and +errors compare by it. One whose value is another `ERROR` constant, +`const Short*: ERROR = PictBase.Short;`, takes that one's `val` in +`constDecl`, and no chunk: it is the same error, and `Short = +PictBase.Short` holds. Only a string makes a new one. `makeItem` makes it an `mVar` there that isn't `isVar`, so it is read, indexed, sliced and diff --git a/src/Item.raz b/src/Item.raz index bceeec1..8b773c6 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -2352,7 +2352,10 @@ proc errorChunk*(var x: Item; home, name: INT): (INT, ERROR); var c, xs, xl: INT; p: ^U8; q: ^I64; begin - if x.mode # mStr then return 0, Scan.markAt(x.pos, "an error is made from a string"); end; + if x.mode # mStr then + return 0, Scan.markHelp(x.pos, say1("an error is made from a string, found {}", what(x)), + "or from another ERROR constant, which is the same error"); + end; xl := INT(x.val); p := Scan.grab(xl + 8); q := ^I64(p); diff --git a/src/Parse.raz b/src/Parse.raz index 7c1baff..24bdb9a 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -3623,7 +3623,12 @@ o := Sym.newObj(Sym.oConst, n, x.typ); o^.pos := at; o^.exported := exp and top; - if t = Sym.tError then + if (t = Sym.tError) and (x.mode = Item.mConst) and (x.typ = Sym.tError) and (x.val > 0) then + (* another ERROR constant (report, 5.8): that same error, its + chunk, so that the two compare equal; only a string makes one *) + o^.typ := Sym.tError; + o^.val := x.val; + elsif t = Sym.tError then o^.typ := Sym.tError; c := try Item.errorChunk(x, Sym.curModule^.sname, n); o^.val := c + 1; diff --git a/test/msg/ErrorConstNil.err b/test/msg/ErrorConstNil.err new file mode 100644 index 0000000..24c82ab --- /dev/null +++ b/test/msg/ErrorConstNil.err @@ -0,0 +1,5 @@ +conform/err/ErrorConstNil.raz:2:18: error: an error is made from a string, found nil + 2 | const E: ERROR = nil; + | ^^^ +help: or from another ERROR constant, which is the same error +raz: 1 error diff --git a/test/msg/ErrorConstVar.err b/test/msg/ErrorConstVar.err new file mode 100644 index 0000000..d757916 --- /dev/null +++ b/test/msg/ErrorConstVar.err @@ -0,0 +1,5 @@ +conform/err/ErrorConstVar.raz:3:18: error: an error is made from a string, found ERROR + 3 | const E: ERROR = last; + | ^^^^ +help: or from another ERROR constant, which is the same error +raz: 1 error diff --git a/test/msg/GenErrorString.err b/test/msg/GenErrorString.err index 16fa8d6..f38ea88 100644 --- a/test/msg/GenErrorString.err +++ b/test/msg/GenErrorString.err @@ -1,4 +1,5 @@ -conform/err/GenErrorString.raz:2:18: error: an error is made from a string +conform/err/GenErrorString.raz:2:18: error: an error is made from a string, found 1 2 | const E: ERROR = 1; | ^ +help: or from another ERROR constant, which is the same error raz: 1 error