raz / commit b5e76eab

commit
b5e76eab93187b872d7ddbd67b2850be8f9ce991
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
4d6f1bca
Report 2026-10.37: []const T, a slice whose elements can't be written through it

Sym.Type's rdonly; a []T or an array goes into one with no code, never
back. An element read through one carries Item.elemRo, which
notReadOnly refuses, so every route of a write is caught in one place;
COPY and FILL refuse one as their destination; a slice of one, or of
another module's array, is one, so an exported array may now be read.
String literals, constants' slices, MSG and const parameters keep
their old types in raz 0.23, until the library can take []const (0.24).

patch · log

17 files changed, +265 -52

CLAUDE.md modified +11 -0

@@ -380,6 +380,17 @@
380380 (`chooseRegs`). A pointer that isn't a variable, `ADR(x)`, is still
381381 taken as the old form until v0.24.0; a pointer variable is the new
382382 form, so `XADD(p, 1)` is an error whose help says `p^`.
383+- `[]const T` (report 2026-10.37) is a slice type with `rdonly` set: not
384+ the same as `[]T`, which goes into it with no code (`coerce`), never
385+ back (help says why). An element read through one has the item's
386+ `elemRo`, which `notReadOnly` refuses, so every write route (`:=`,
387+ `INC`, a `var` argument, `ADR`, a view) is caught in one place; `COPY`
388+ and `FILL` refuse one as their destination (`destSlice`); a slice of
389+ one, or of an array read through one or of another module's array,
390+ is one (`slice`); `BYTES` of one is a `[]const U8`; `match` on an
391+ element gives names only read. Until v0.24.0 a string literal, a
392+ constant's slice, `MSG` and a `const` parameter's array keep their
393+ old types, `[]U8` and `[]T`.
383394 - `ROL(x, n)` and `ROR(x, n)` (report 2026-10.35) are done at `x`'s
384395 own width, `rol al, cl` for a `U8`, with no mask of the count: x86
385396 takes it modulo 32 or 64, and a byte's or a word's rotation goes

conform/err/ConstSliceToSlice.raz (history) added +16 -0

@@ -0,0 +1,16 @@
1+(* A []const T doesn't go where a []T is wanted, which could write
2+ its elements (report sections 5.3 and 5.10). *)
3+module ConstSliceToSlice;
4+
5+proc fill(s: []U8);
6+begin
7+ FILL(s, 0);
8+end;
9+
10+proc main*;
11+var r: []const U8;
12+begin
13+ fill(r);
14+end;
15+
16+end ConstSliceToSlice.

conform/err/ConstSliceWrite.raz (history) added +14 -0

@@ -0,0 +1,14 @@
1+(* An element of a []const slice is only read (report section 5.3). *)
2+module ConstSliceWrite;
3+
4+proc clear(s: []const U8);
5+begin
6+ s[0] := 0;
7+end;
8+
9+proc main*;
10+begin
11+ clear("x");
12+end;
13+
14+end ConstSliceWrite.

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

@@ -0,0 +1 @@
1+2120=m9227

conform/run/ConstSlice.raz (history) added +56 -0

@@ -0,0 +1,56 @@
1+(* []const T (report section 5.3): its elements are read and it is
2+ sliced; a []T, an array, a string or [] goes where one is wanted;
3+ another module's exported array is read as one (section 3); BYTES
4+ of one is a []const U8; and it compares and is matched as a []T. *)
5+module ConstSlice;
6+import Digits, Out;
7+
8+type Shape = variant dot; box(w: INT); end;
9+
10+(* How many times c is in s, which it only reads *)
11+proc count(s: []const U8; c: U8): INT;
12+var i, n: INT;
13+begin
14+ for i := 0 to LEN(s) - 1 do
15+ if s[i] = c then INC(n); end;
16+ end;
17+ return n;
18+end;
19+
20+(* The first two elements of s, as s is: a []const *)
21+proc head(s: []const U8): []const U8;
22+begin
23+ return s[0..2];
24+end;
25+
26+proc main*;
27+var a: [5]U8; w: []U8; r, b: []const U8; shapes: [2]Shape; cs: []const Shape;
28+begin
29+ a := "hello";
30+ w := a;
31+ Out.int(count(a, 'l'));
32+ Out.int(count(w, 'h'));
33+ Out.int(count("all", 'l'));
34+ Out.int(count([], 'l'));
35+ r := head(w);
36+ if (r = "he") and (r = w[0..2]) then Out.char('='); end;
37+ match r of
38+ | "he": Out.char('m');
39+ | else:
40+ end;
41+ r := Digits.digits;
42+ Out.int(INT(r[9]) - '0');
43+ r := Digits.digits[3..5];
44+ Out.int(LEN(r));
45+ b := BYTES(r);
46+ Out.int(LEN(b));
47+ shapes[1] := box(7);
48+ cs := shapes;
49+ match cs[1] of
50+ | box(width): Out.int(width);
51+ | dot:
52+ end;
53+ Out.char(10);
54+end;
55+
56+end ConstSlice.

conform/run/mods/Digits.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+(* An exported array, for ConstSlice to read as a []const U8 *)
2+module Digits;
3+
4+var digits*: [10]U8;
5+
6+begin
7+ digits := "0123456789";
8+end Digits.

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

@@ -1665,7 +1665,8 @@
16651665
16661666 Assignment and conversion: `coerce` makes a value fit the type it's
16671667 going to, or reports that it can't; it's where a string literal becomes
1668-a slice, an array a slice, an untyped constant a typed one, and an `INT`
1668+a slice, an array a slice, a `[]T` a `[]const T` (the same two words,
1669+its type changed), an untyped constant a typed one, and an `INT`
16691670 an `I64` (`widens`), wherever it goes: a `for` loop's bounds, `INC`'s
16701671 step and several results pass through `coerce` too. An `INT` meeting
16711672 an `I64` in an operator is `op2`'s, which gives the result the `I64`;

doc/raz.7 (history) modified +9 -2

@@ -184,6 +184,7 @@
184184 .It "ERROR" Ta "8" Ta "nil, or a constant's error"
185185 .It "^T, proc ..." Ta "8" Ta "an address, or nil"
186186 .It "[]T" Ta "16" Ta "a pointer and a length"
187+.It "[]const T" Ta "16" Ta "a slice, its elements only read"
187188 .It "[n]T" Ta "n * SIZE(T)" Ta "n elements, copied whole"
188189 .It "(a, b, c)" Ta "1" Ta "an enumeration, 256 names at most"
189190 .It "set of E" Ta "1, 2, 4, 8" Ta "bit i for E's name i, 64 at most"
@@ -248,9 +249,15 @@
248249 .Cm "^VOLATILE T" ,
249250 .Cm [n]T
250251 into
251-.Cm []T ,
252+.Cm []T
253+or
254+.Cm []const T ,
255+a
256+.Cm []T
257+into
258+.Cm []const T ,
252259 a string into
253-.Cm []U8 , ^U8
260+.Cm "[]const U8" , []U8 , ^U8
254261 (with a zero byte after it) or
255262 .Cm [n]U8 ,
256263 a procedure into its type,

doc/raz.md (history) modified +64 -36

@@ -68,7 +68,12 @@
6868 `CAS`, `XCHG` and `XADD` take the variable they work on, not a pointer
6969 to it (section 6.8). It breaks a program that gives them a pointer;
7070 this compiler, raz 0.23, still takes one that isn't a variable,
71-`ADR(x)` say, as before.
71+`ADR(x)` say, as before. And `[]const T` is a slice whose elements
72+can't be written through it (section 5.3), which a string literal, a
73+slice of a constant, `MSG` and a slice of a `const` parameter's array
74+are (sections 4, 5.10, 6.8 and 8). It breaks a program that writes
75+through one of these; raz 0.23 still gives them their old types, as
76+`[]U8` and `[]T`, until 0.24.
7277
7378 The language is frozen, softly, from this version on: it changes only
7479 to mend a defect, and its library grows only by what breaks no program
@@ -233,16 +238,18 @@
233238 not a variable: nothing that could write it, or let something else
234239 write it, may name it or a field or element of it. So it can't be
235240 assigned, passed as a `var` argument, changed by `INC`, `DEC` or
236-`NEW`, or given to `ADR`; an array in it can't be sliced, or used
237-where a slice is expected (the silent conversion of an array, section
238-5.10), though a slice in it may be sliced, since that makes a new
239-slice and can't change the variable; in `match`, its arms'
241+`NEW`, or given to `ADR`; an array in it is sliced, or used where a
242+slice is expected (the silent conversion of an array, section 5.10),
243+only as a `[]const T`, whose elements can't be written (section 5.3);
244+a slice in it may be sliced, since that makes a new slice and can't
245+change the variable; in `match`, its arms'
240246 names stand for the fields of a copy. It can be read, copied and
241247 compared, and a copy is the importer's own. What a pointer or a slice
242248 in it points at is another variable, and isn't held to this: with
243249 `s: []U8` exported, `M.s[0] := 0` writes what `M.s` points at, not
244-`M`. A module that means to share an array exports a pointer to it,
245-or a procedure that returns a slice of it.
250+`M`. A module that means to share an array for reading exports it;
251+for writing, it exports a pointer to it, or a procedure that returns a
252+slice of it.
246253
247254 **Imports.** `import Out;` makes `Out`'s exports available as `Out.x`.
248255 `import O := Out;` uses the name `O` instead. A module can't import
@@ -420,19 +427,19 @@
420427 literal (section 6.5) of constants, that of a variant one a case built
421428 from constants, as a program builds one (section 5.6). Its elements
422429 and fields are integers, reals, `BOOL`s, enumerations, sets, `ERROR`s,
423-procedures, `nil`, a string for a `[]U8`, an array constant, an array
424-literal or the empty slice for a slice of its element type, and
425-arrays, records and variants of these; no other pointer. A named
430+procedures, `nil`, a string for a `[]const U8`, an array constant, an
431+array literal or the empty slice for a `[]const T` of its element type,
432+and arrays, records and variants of these; no other pointer. A named
426433 constant stands for its literal wherever an array, record or variant is
427434 wanted. A variant constant must have a
428435 type: `const D = dot;` is an error, `const D: Shape = dot;` isn't.
429436
430437 ```
431438 const Primes: [5]I64 = [2, 3, 5, 7, 11];
432-type Rule = record class: []U8; tag: INT; float: BOOL; end;
439+type Rule = record class: []const U8; tag: INT; float: BOOL; end;
433440 const Rules: [2]Rule = [{class: "firefox", tag: 2}, {class: "mpv", float: true}];
434441 const Shapes: []Shape = [circle(1.0), rect(2.0, 3.0), dot]; (* Shape of section 5.6 *)
435-const Rows: [][]INT = [[1, 2], [3]]; (* each row an array constant of its own *)
442+const Rows: [][]const INT = [[1, 2], [3]]; (* each row an array constant of its own *)
436443 ...
437444 p := Primes[i];
438445 ```
@@ -440,9 +447,9 @@
440447 It is used as a variable of that type is, but can't be changed: an
441448 array constant can be indexed and sliced and its `LEN` is `n`, a
442449 record constant's fields can be read, and either can be assigned,
443-compared as its type allows, and passed. Its parts are constant as a
444-string's bytes are (section 5.10). A part of it isn't a constant, even
445-at a constant index.
450+compared as its type allows, and passed. A slice of it, or of an array
451+in it, is a `[]const T` (section 5.3), as a string's is. A part of it
452+isn't a constant, even at a constant index.
446453
447454 An array constant may be typed `[]T`, with no length: its length is
448455 then its literal's, as a string constant's is the string's, and it is
@@ -470,7 +477,7 @@
470477 ```
471478 Type = QualIdent
472479 | "[" Expr "]" Type (* array *)
473- | "[" "]" Type (* slice *)
480+ | "[" "]" [ "const" ] Type (* slice *)
474481 | "^" [ "VOLATILE" ] Type (* pointer *)
475482 | [ "packed" ] "record" { IdentList ":" Type ";" } "end"
476483 | "variant" { Case ";" } "end"
@@ -533,6 +540,25 @@
533540 doesn't own what it points at. `LEN(s)` is its count, and `PTR(s)` its
534541 pointer (section 6.8).
535542
543+`[]const T` is a slice whose elements can't be written through it. It
544+is laid out as `[]T` is. Its elements are read, and it is sliced, and a
545+slice of it is a `[]const T` too; but an element of it isn't assigned,
546+given to `COPY` or `FILL` as the destination, changed by `INC` or `DEC`,
547+passed as a `var` argument, or written through a view, and `ADR` of one
548+is an error. A `[]T` and an array of `T` go silently where a
549+`[]const T` is wanted, never the other way (section 5.10). So a
550+procedure that only reads a slice takes a `[]const T`, and every
551+caller's slice or array fits it:
552+
553+```
554+proc count(s: []const U8; c: U8): INT; (* reads s, and says so *)
555+```
556+
557+A string literal and a slice of a constant are `[]const T` (sections 4
558+and 5.10). Another slice may still point at the same elements and
559+write them: `const` says what this slice may do, not what the elements
560+are.
561+
536562 ### 5.4 Pointers
537563
538564 `^T` is the address of a `T`, as big as an `INT`. `nil` is the pointer
@@ -661,7 +687,7 @@
661687 holds.
662688
663689 Errors compare with `=` and `#` only, by which declaration made them,
664-never by their message. `MSG(e)` is the message, a `[]U8`; `MSG(nil)`
690+never by their message. `MSG(e)` is the message, a `[]const U8`; `MSG(nil)`
665691 is `""`.
666692
667693 ```
@@ -719,8 +745,9 @@
719745 - `A` is an integer constant whose value fits in the integer type `B`;
720746 - `A` is `nil` and `B` is a pointer, procedure or `ERROR` type;
721747 - `A` is `^T` and `B` is `^VOLATILE T`;
722-- `A` is `[n]T` and `B` is `[]T`;
723-- `A` is a string literal and `B` is `[]U8`, `^U8` (a pointer to the
748+- `A` is `[n]T` and `B` is `[]T` or `[]const T`, or `A` is `[]T` and
749+ `B` is `[]const T`;
750+- `A` is a string literal and `B` is `[]const U8`, `^U8` (a pointer to the
724751 bytes followed by a zero byte, as C wants), or `[n]U8` with `n` at
725752 least the string's length (the rest is zero);
726753 - `A` is a procedure and `B` a procedure type with the same signature;
@@ -731,11 +758,11 @@
731758 so on every target; unless the value is what an operator gives
732759 (below).
733760
734-A string's bytes are constant. A `[]U8` or `^U8` made from a string
735-literal, or a slice of a string constant, points at them, and writing
736-through it isn't defined: on a target with read-only memory, it may
737-stop the program as a bad address (section 10). A `[n]U8` made from a
738-string is a copy, the program's to change.
761+A string's bytes are constant. A string literal, or a slice of a string
762+constant, is a `[]const U8`, so writing them is an error. A `[n]U8`
763+made from a string is a copy, the program's to change. A `^U8` made
764+from one, for C, points at the constant bytes, and writing through it
765+isn't defined (section 10).
739766
740767 Nothing else converts silently. In particular, there is no other
741768 conversion between integer types, between integers and reals, or
@@ -950,7 +977,7 @@
950977 a `[n]T`, of the same element type `T`, are `=` when they have the
951978 same length and the same elements, and `#` otherwise, where `T` is an
952979 integer type, `BOOL`, an enumeration, a set or a pointer type. A string
953-literal compares as a `[]U8`. The comparison is code of its own where
980+literal compares as a `[]const U8`. The comparison is code of its own where
954981 it stands, a check of the lengths and then of the bytes, not a call.
955982
956983 Arrays of reals don't compare this way, since `-0.0 = 0.0` and a NaN
@@ -1013,9 +1040,9 @@
10131040 | `ADR(x)` | the address of variable `x`, a `^T` |
10141041 | `LEN(a)` | the number of elements of an array or a string constant (an untyped constant) or of a slice (an `INT`) |
10151042 | `PTR(s)` | the address the slice `s` starts at, a `^T` for `s: []T`, unchecked; for an array `a`, that of `a[0..LEN(a)]` |
1016-| `BYTES(x)` | the bytes of `x` as a `[]U8`: for a slice `s: []T`, its `LEN(s) * SIZE(T)` bytes; for a variable of any other type `T`, its `SIZE(T)` bytes |
1043+| `BYTES(x)` | the bytes of `x` as a `[]U8`: for a slice `s: []T`, its `LEN(s) * SIZE(T)` bytes, a `[]const U8` for a `[]const T`; for a variable of any other type `T`, its `SIZE(T)` bytes |
10171044 | `NEW(p, a)` | sets `p: ^T` to a fresh, zeroed `T` from the allocator `a: ^Mem.Allocator`, or to `nil` if `a` has no room |
1018-| `MSG(e)` | the message of the error `e`, a `[]U8` |
1045+| `MSG(e)` | the message of the error `e`, a `[]const U8` |
10191046 | `COPY(d, s)` | copies the elements of `s` to the start of `d`, both arrays or slices of one type `T`; `LEN(s)` must be at most `LEN(d)` |
10201047 | `FILL(d, x)` | sets every element of `d`, an array or a slice of elements of type `T`, to `x`, a value of `T` |
10211048 | `SYSCALL(n, x1, ..., x6)` | Linux system call `n` with up to six arguments, each a pointer or an integer no wider than `INT`; the raw result, an `INT` (a negative number is `-errno`) |
@@ -1063,7 +1090,8 @@
10631090 or array holding one, is an error. The slice is made, and checked, as
10641091 any slice of the variable is, so its bytes follow the rules for it: the
10651092 bytes of a local can't be returned (section 7.3). Of a slice they are
1066-its elements, as `PTR(s)` gives them, whatever holds the slice. Of any
1093+its elements, as `PTR(s)` gives them, whatever holds the slice, and
1094+can be written only if the slice can. Of any
10671095 other variable they are its own, as `ADR` gives them: a `const`
10681096 parameter, another module's variable, a view and a field of a packed
10691097 record are errors, as they are for `ADR`, and so is a volatile variable.
@@ -1082,7 +1110,7 @@
10821110 ```
10831111 type Value = record n: INT; p: ^U8; end;
10841112
1085-proc valueOf(s: []U8): Value;
1113+proc valueOf(s: []const U8): Value;
10861114 begin
10871115 return {n: LEN(s), p: PTR(s)}; (* "" too *)
10881116 end;
@@ -1341,7 +1369,7 @@
13411369 ```
13421370
13431371 `match` takes a variant, an enumeration, an integer type (a `U8`
1344-included) or a string, a `[]U8`; other types are an error. It runs the arm of the case `s` holds. An arm may name the case's
1372+included) or a string, a `[]U8` or a `[]const U8`; other types are an error. It runs the arm of the case `s` holds. An arm may name the case's
13451373 fields, in order, all of them; the names stand for those fields of `s`
13461374 itself, so assigning to one changes `s`; if `s` is a constant or a
13471375 `const` parameter, they can't be assigned; if `s` isn't a variable,
@@ -1386,7 +1414,7 @@
13861414 two labels. An integer
13871415 `match` must have an `| else:` arm, which may be empty.
13881416
1389-`match` on a string, a `[]U8` or what converts to one (section 5.10),
1417+`match` on a string, a `[]U8`, a `[]const U8` or what converts to one (section 5.10),
13901418 runs the arm one of whose labels it equals, as `=` compares strings
13911419 (section 6.5):
13921420
@@ -1607,10 +1635,10 @@
16071635 either, write it through a view (section 6.9), or assign the names a
16081636 `match` arm gives its fields. What a slice or a pointer in it points at
16091637 isn't part of it: the procedure may write through them, as it may
1610-through a value parameter's; and a slice it makes of an array in it,
1611-to read, is a slice like any other, which writes the caller's array if
1612-written through. `const` keeps the procedure from changing the
1613-variable by its name, not every path to its bytes. Its argument is any expression of the
1638+through a value parameter's; but a slice it makes of an array in it is
1639+a `[]const T` (section 5.3), so it can't write the caller's array
1640+through it. `const` keeps the procedure from changing the variable,
1641+not what its slices and pointers point at. Its argument is any expression of the
16141642 parameter's type: a variable or a constant is passed in place, anything
16151643 else as a temporary, of the procedure if it holds an array, as an
16161644 array a call returns is (below). If the variable changes while the procedure runs, through a
@@ -1971,7 +1999,7 @@
19711999
19722000 Type = QualIdent
19732001 | "[" Expr "]" Type
1974- | "[" "]" Type
2002+ | "[" "]" [ "const" ] Type
19752003 | "^" [ "VOLATILE" ] Type
19762004 | [ "packed" ] "record" { IdentList ":" Type ";" } "end"
19772005 | "variant" { Case ";" } "end"

doc/report-history.md (history) modified +8 -0

@@ -269,3 +269,11 @@
269269 does, not a pointer to it (section 6.8): `XADD(count, 1)`,
270270 `XADD(p^, 1)`. It breaks a program that gives them a pointer; raz
271271 0.23 still takes one that isn't a variable, `ADR(x)`, until 0.24.
272+ And `[]const T`, a slice whose elements can't be written through it
273+ (section 5.3): a `[]T` or an array goes where one is wanted, never
274+ the other way. A string literal and a slice of a constant are
275+ `[]const`, as `MSG` and a slice of a `const` parameter's array are,
276+ so writing them is an error, not undefined; another module's exported
277+ array may be read as one (sections 3, 4, 5.10, 6.8 and 8). It breaks
278+ a program that writes through these; raz 0.23 still gives them their
279+ old types until 0.24.

src/Item.raz (history) modified +53 -7

@@ -52,6 +52,8 @@
5252 misaligned, and with no address *)
5353 ro*: BOOL; (* mVar: another module's exported variable, or part
5454 of one, which only that module changes *)
55+ elemRo*: BOOL; (* mVar: an element of a []const slice, or part of
56+ one, read through it and not written *)
5557 deref*: BOOL; (* mVar: at an address from a pointer, which may be
5658 nil and isn't tested yet (nilTest) *)
5759 obj*: ^Sym.Obj; (* mProc, mBuiltin, mCase; mVar: the variable named *)
@@ -553,6 +555,10 @@
553555 what is only read, above. *)
554556 proc notReadOnly*(const x: Item): ERROR;
555557 begin
558+ if x.elemRo then
559+ return Scan.markHelp(x.pos, say1("{} is read through a []const slice, and can't be changed", Diag.source(x.pos)),
560+ "a []const slice's elements are only read: write them through a []T");
561+ end;
556562 if onlyRead(x) then
557563 return Scan.markHelp(x.pos, say1("{} is only read here, and can't be changed", Diag.source(x.pos)),
558564 "a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it");
@@ -589,6 +595,16 @@
589595 return Scan.markHelp(x.pos, text, "a view has no address, since it may be misaligned: copy it to a variable");
590596 end;
591597
598+(* The []const T of the slice type t, for a message *)
599+proc constOf(t: ^Sym.Type): ^Sym.Type;
600+var c: ^Sym.Type;
601+begin
602+ c := Sym.newType(Sym.fSlice, 16, 8);
603+ c^.base := t^.base;
604+ c^.rdonly := true;
605+ return c;
606+end;
607+
592608 proc coerce*(var x: Item; t: ^Sym.Type): ERROR;
593609 var s: ^Sym.Type; tmp: I64; inFrame: BOOL; hint: []U8;
594610 begin
@@ -626,10 +642,21 @@
626642 elsif (s^.form = Sym.fPtr) and (t^.form = Sym.fPtr) and t^.vol and not s^.vol
627643 and Sym.same(s^.base, t^.base) then
628644 x.typ := t;
645+ elsif (s^.form = Sym.fSlice) and (t^.form = Sym.fSlice) and t^.rdonly and Sym.same(s^.base, t^.base) then
646+ x.typ := t; (* a []T is a []const T as it is: its elements only read *)
647+ elsif (s^.form = Sym.fSlice) and (t^.form = Sym.fSlice) and s^.rdonly and Sym.same(s^.base, t^.base) then
648+ return mismatchHelp(x, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)),
649+ "its elements are only read, and a []T could write them");
629650 elsif (s^.form = Sym.fArray) and (t^.form = Sym.fSlice) and Sym.same(s^.base, t^.base) then
630651 (* a slice of what is only read may be made, to read, as of a
631- constant; another module's array can't be lent to be written *)
632- if not onlyRead(x) then try notReadOnly(x); end;
652+ constant; another module's array, or one read through a
653+ []const, only as a []const *)
654+ if x.ro and not onlyRead(x) and not t^.rdonly then
655+ return Scan.markHelp(x.pos, Diag.say("{} is {}'s, and can't be changed outside it",
656+ [Fmt.str(Diag.source(x.pos)), Fmt.str(Scan.nameOf(x.obj^.home^.name))]),
657+ say1("it may be read as a {}", Sym.typeName(constOf(t))));
658+ end;
659+ if not onlyRead(x) and not t^.rdonly then try notReadOnly(x); end;
633660 if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end;
634661 if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end;
635662 if x.view then return noAddress(x, "can't make a slice of a view"); end;
@@ -714,7 +741,7 @@
714741 end;
715742 try value(x);
716743 x.mode := mVar; x.base := Gen.bStack; x.off := 0; x.deref := true;
717- x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false;
744+ x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; x.elemRo := false;
718745 x.view := false; x.frame := false;
719746 return nil;
720747 end;
@@ -775,6 +802,7 @@
775802 else Gen.indexPtr(s); end;
776803 x.mode := mVar; x.base := Gen.bStack; x.off := 0;
777804 x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; x.view := false;
805+ x.elemRo := false;
778806 elsif (t^.form = Sym.fArray) and (y.mode = mConst) then
779807 if (y.val < 0) or (y.val >= t^.len) then
780808 return Scan.markAt(y.pos, Diag.say("index {} is out of range for a length of {}", [Fmt.int(y.val), Fmt.int(t^.len)]));
@@ -790,6 +818,7 @@
790818 try value(y);
791819 Gen.indexSlice(adrOf(x), s, 0, checks, y.pos);
792820 x.base := Gen.bStack; x.off := 0; x.isVar := true; x.vol := false; x.ro := false;
821+ x.elemRo := t^.rdonly;
793822 end;
794823 x.typ := t^.base;
795824 (* an element of an array lies where the array does; one of a slice or
@@ -807,7 +836,7 @@
807836 end;
808837
809838 proc slice*(var x, lo, hi: Item): ERROR;
810-var t, st: ^Sym.Type; tmp: I64; inFrame: BOOL;
839+var t, st: ^Sym.Type; tmp: I64; inFrame, roSlice: BOOL;
811840 begin
812841 t := x.typ;
813842 if not Sym.isInt(hi.typ) then
@@ -816,7 +845,10 @@
816845 if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end;
817846 if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end;
818847 if x.view then return noAddress(x, "can't make a slice of a view"); end;
819- if (t^.form = Sym.fArray) and not onlyRead(x) then try notReadOnly(x); end; (* as in coerce *)
848+ (* a slice of another module's array, or of one read through a
849+ []const, is a []const; of a const parameter's, a slice to read *)
850+ roSlice := (t^.form = Sym.fSlice) and t^.rdonly;
851+ if t^.form = Sym.fArray then roSlice := (x.ro and not onlyRead(x)) or x.elemRo; end;
820852 try value(hi);
821853 inFrame := x.frame and (t^.form # Sym.fPtr);
822854 tmp := Gen.allocTemp(16, 8);
@@ -824,6 +856,7 @@
824856 else Gen.slice(adrOf(x), t^.form = Sym.fArray, t^.len, t^.base^.size, tmp, checks, lo.pos); end;
825857 st := Sym.newType(Sym.fSlice, 16, 8);
826858 st^.base := t^.base;
859+ st^.rdonly := roSlice;
827860 tempItem(x, st, tmp);
828861 x.frame := inFrame;
829862 return nil;
@@ -972,7 +1005,7 @@
9721005 both can be read: a call's two words, or a slice reached through an
9731006 address on the stack, go to a temporary first. *)
9741007 proc bytesOf*(var x: Item): ERROR;
975-var t: ^Sym.Type; inFrame: BOOL; y: Item; s: I64;
1008+var t, bt: ^Sym.Type; inFrame: BOOL; y: Item; s: I64;
9761009 begin
9771010 if x.mode = mStr then
9781011 return Scan.markHelp(x.pos, "BYTES can't take a string constant",
@@ -993,7 +1026,15 @@
9931026 s := Gen.allocTemp(16, 8);
9941027 Gen.popVar(frameAdr(s + 8), 8);
9951028 Gen.popVar(frameAdr(s), 8);
996- tempItem(x, Sym.tBytes, s);
1029+ if t^.rdonly then
1030+ (* the bytes of a []const T are only read too: a []const U8 *)
1031+ bt := Sym.newType(Sym.fSlice, 16, 8);
1032+ bt^.base := Sym.tU8;
1033+ bt^.rdonly := true;
1034+ tempItem(x, bt, s);
1035+ else
1036+ tempItem(x, Sym.tBytes, s);
1037+ end;
9971038 x.frame := inFrame;
9981039 return nil;
9991040 end;
@@ -1082,6 +1123,7 @@
10821123 try value(y);
10831124 Gen.indexSlice(adrOf(x), s, k - 1, checks, y.pos);
10841125 x.base := Gen.bStack; x.off := 0; x.isVar := true; x.vol := false; x.ro := false;
1126+ x.elemRo := at^.rdonly;
10851127 x.typ := at^.base;
10861128 else
10871129 try index(x, y);
@@ -2375,6 +2417,10 @@
23752417 proc destSlice(var d: Item; text: []U8): ERROR;
23762418 begin
23772419 if (d.typ^.form = Sym.fArray) and (d.mode = mVar) then try notReadOnly(d); end;
2420+ if (d.typ^.form = Sym.fSlice) and d.typ^.rdonly then
2421+ return Scan.markHelp(d.pos, say1("{} is a []const slice, whose elements can't be changed", Diag.source(d.pos)),
2422+ "write them through a []T");
2423+ end;
23782424 if not isSeq(d.typ) or (d.mode = mStr) or ((d.typ^.form = Sym.fArray) and ((d.mode # mVar) or not d.isVar)) then
23792425 return Scan.markAt(d.pos, say1(text, what(d)));
23802426 end;

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

@@ -928,6 +928,7 @@
928928 if Scan.tok = Scan.sRbrak then
929929 Scan.next;
930930 t := Sym.newType(Sym.fSlice, 16, 8);
931+ if Scan.tok = Scan.sConst then Scan.next; t^.rdonly := true; end; (* []const T *)
931932 t^.base := try parseType();
932933 else
933934 start := Scan.tokPos();
@@ -2521,7 +2522,7 @@
25212522 try expression(x, nil);
25222523 if x.typ^.form = Sym.fConst then try Item.coerce(x, Sym.tI64); end;
25232524 (* a const parameter or a constant: then so are the names its arms give *)
2524- onlyRead := Item.onlyRead(x) or ((x.mode = Item.mVar) and (x.obj # nil) and (x.obj^.cls = Sym.oConst));
2525+ onlyRead := Item.onlyRead(x) or x.elemRo or ((x.mode = Item.mVar) and (x.obj # nil) and (x.obj^.cls = Sym.oConst));
25252526 p := try Item.matchBegin(x);
25262527 t := x.typ;
25272528 isStr := t^.form = Sym.fSlice; (* matchBegin made a string a slice *)

src/Sym.raz (history) modified +4 -3

@@ -32,6 +32,7 @@
3232 size*, align*: I64;
3333 signed*: BOOL; (* fInt *)
3434 vol*: BOOL; (* fPtr: ^VOLATILE *)
35+ rdonly*: BOOL; (* fSlice: []const, its elements not written through it *)
3536 packd*: BOOL; (* fRecord *)
3637 variadic*: BOOL; (* fProc: ends in ... *)
3738 tuple*: BOOL; (* fRecord: the results of a procedure that has several *)
@@ -532,7 +533,7 @@
532533 match a^.form of
533534 | fPtr: return (a^.vol = b^.vol) and same(a^.base, b^.base);
534535 | fArray: return (a^.len = b^.len) and same(a^.base, b^.base);
535- | fSlice: return same(a^.base, b^.base);
536+ | fSlice: return (a^.rdonly = b^.rdonly) and same(a^.base, b^.base);
536537 | fSet: return same(a^.base, b^.base);
537538 | fProc: return alikeShape(a, b);
538539 | fRecord: return alikeShape(a, b);
@@ -574,7 +575,7 @@
574575 Fmt.write(b, "[{}]", [Fmt.int(t^.len)]);
575576 typeText(b, t^.base);
576577 | fSlice:
577- Buf.str(b, "[]");
578+ if t^.rdonly then Buf.str(b, "[]const "); else Buf.str(b, "[]"); end;
578579 typeText(b, t^.base);
579580 | fRecord:
580581 if not t^.tuple then
@@ -644,7 +645,7 @@
644645 return;
645646 end;
646647 mix(h, INT(t^.form));
647- mix(h, bit(t^.vol) + 2 * bit(t^.variadic));
648+ mix(h, bit(t^.vol) + 2 * bit(t^.variadic) + 4 * bit(t^.rdonly));
648649 mix(h, t^.len);
649650 if t^.form in {fPtr, fArray, fSlice, fProc, fSet} then shape(h, t^.base, false); end;
650651 f := t^.fields;

test/msg/ConstSliceToSlice.err (history) added +8 -0

@@ -0,0 +1,8 @@
1+conform/err/ConstSliceToSlice.raz:13:10: error: can't use []const U8 as []U8
2+ 13 | fill(r);
3+ | ^
4+help: its elements are only read, and a []T could write them
5+conform/err/ConstSliceToSlice.raz:5:11: note: it goes to s, declared here
6+ 5 | proc fill(s: []U8);
7+ | ^
8+raz: 1 error

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

@@ -0,0 +1,5 @@
1+conform/err/ConstSliceWrite.raz:6:5: error: s[0] is read through a []const slice, and can't be changed
2+ 6 | s[0] := 0;
3+ | ^^^^
4+help: a []const slice's elements are only read: write them through a []T
5+raz: 1 error

test/msg/ExportAsSlice.err (history) modified +1 -0

@@ -1,4 +1,5 @@
11 conform/err/ExportAsSlice.raz:6:9: error: Owner.a is Owner's, and can't be changed outside it
22 6 | sum(Owner.a);
33 | ^^^^^^^
4+help: it may be read as a []const INT
45 raz: 1 error

test/msg/ExportSlice.err (history) modified +3 -2

@@ -1,4 +1,5 @@
1-conform/err/ExportSlice.raz:6:11: error: Owner.a is Owner's, and can't be changed outside it
1+conform/err/ExportSlice.raz:6:11: error: can't use []const INT as []INT
22 6 | _s := Owner.a[0..2];
3- | ^^^^^^^
3+ | ^^^^^^^^^^^^^
4+help: its elements are only read, and a []T could write them
45 raz: 1 error