raz / commit 73c1f3ac

commit
73c1f3ac156c992c391302bcad1680a6aac5474d
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
3fb27192
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

patch · log

12 files changed, +83 -6

CLAUDE.md modified +2 -1

@@ -280,7 +280,8 @@
280280 `LEN` of it is no constant. A constant's slice part takes `[]`, names
281281 a constant whole, of any module, by its type, not its text, and takes
282282 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.
284285 - An array a call returns is a temporary of the statement (report
285286 2026-10.28, 8): a slice of it kept past the statement is undefined
286287 (10), and isn't caught.

conform/err/ErrorConstNil.raz (history) added +3 -0

@@ -0,0 +1,3 @@
1+module ErrorConstNil;
2+const E: ERROR = nil;
3+end ErrorConstNil.

conform/err/ErrorConstVar.raz (history) added +4 -0

@@ -0,0 +1,4 @@
1+module ErrorConstVar;
2+var last: ERROR;
3+const E: ERROR = last;
4+end ErrorConstVar.

conform/run/ErrorConstNamed.expect (history) added +1 -0

@@ -0,0 +1 @@
1+same same same same different different different too short

conform/run/ErrorConstNamed.raz (history) added +27 -0

@@ -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.

conform/run/mods/PictBase.raz (history) added +14 -0

@@ -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.

doc/compiler.md (history) modified +10 -2

@@ -22,8 +22,8 @@
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2323 | `src/X64.raz` | 1,594 | 1,310 | The instruction encoder, jumps within a procedure, the assembler |
2424 | `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 |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
2929 | `src/Raz.raz` | 418 | 333 | The command line |
@@ -1407,6 +1407,14 @@
14071407
14081408 const Rows: [][]INT = [[1, 2], [3]]; (* read as *)
14091409 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.
14101418
14111419 `makeItem` makes it an
14121420 `mVar` there that isn't `isVar`, so it is read, indexed, sliced and

src/Item.raz (history) modified +4 -1

@@ -2352,7 +2352,10 @@
23522352 proc errorChunk*(var x: Item; home, name: INT): (INT, ERROR);
23532353 var c, xs, xl: INT; p: ^U8; q: ^I64;
23542354 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;
23562359 xl := INT(x.val);
23572360 p := Scan.grab(xl + 8);
23582361 q := ^I64(p);

src/Parse.raz (history) modified +6 -1

@@ -3623,7 +3623,12 @@
36233623 o := Sym.newObj(Sym.oConst, n, x.typ);
36243624 o^.pos := at;
36253625 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
36273632 o^.typ := Sym.tError;
36283633 c := try Item.errorChunk(x, Sym.curModule^.sname, n);
36293634 o^.val := c + 1;

test/msg/ErrorConstNil.err (history) added +5 -0

@@ -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

test/msg/ErrorConstVar.err (history) added +5 -0

@@ -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

test/msg/GenErrorString.err (history) modified +2 -1

@@ -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
22 2 | const E: ERROR = 1;
33 | ^
4+help: or from another ERROR constant, which is the same error
45 raz: 1 error