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).
| @@ -380,6 +380,17 @@ | ||
| 380 | 380 | (`chooseRegs`). A pointer that isn't a variable, `ADR(x)`, is still |
| 381 | 381 | taken as the old form until v0.24.0; a pointer variable is the new |
| 382 | 382 | 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`. | |
| 383 | 394 | - `ROL(x, n)` and `ROR(x, n)` (report 2026-10.35) are done at `x`'s |
| 384 | 395 | own width, `rol al, cl` for a `U8`, with no mask of the count: x86 |
| 385 | 396 | takes it modulo 32 or 64, and a byte's or a word's rotation goes |
| @@ -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. |
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +2120=m9227 |
| @@ -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. |
| @@ -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. |
| @@ -1665,7 +1665,8 @@ | ||
| 1665 | 1665 | |
| 1666 | 1666 | Assignment and conversion: `coerce` makes a value fit the type it's |
| 1667 | 1667 | 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` | |
| 1669 | 1670 | an `I64` (`widens`), wherever it goes: a `for` loop's bounds, `INC`'s |
| 1670 | 1671 | step and several results pass through `coerce` too. An `INT` meeting |
| 1671 | 1672 | an `I64` in an operator is `op2`'s, which gives the result the `I64`; |
| @@ -184,6 +184,7 @@ | ||
| 184 | 184 | .It "ERROR" Ta "8" Ta "nil, or a constant's error" |
| 185 | 185 | .It "^T, proc ..." Ta "8" Ta "an address, or nil" |
| 186 | 186 | .It "[]T" Ta "16" Ta "a pointer and a length" |
| 187 | +.It "[]const T" Ta "16" Ta "a slice, its elements only read" | |
| 187 | 188 | .It "[n]T" Ta "n * SIZE(T)" Ta "n elements, copied whole" |
| 188 | 189 | .It "(a, b, c)" Ta "1" Ta "an enumeration, 256 names at most" |
| 189 | 190 | .It "set of E" Ta "1, 2, 4, 8" Ta "bit i for E's name i, 64 at most" |
| @@ -248,9 +249,15 @@ | ||
| 248 | 249 | .Cm "^VOLATILE T" , |
| 249 | 250 | .Cm [n]T |
| 250 | 251 | into |
| 251 | -.Cm []T , | |
| 252 | +.Cm []T | |
| 253 | +or | |
| 254 | +.Cm []const T , | |
| 255 | +a | |
| 256 | +.Cm []T | |
| 257 | +into | |
| 258 | +.Cm []const T , | |
| 252 | 259 | a string into |
| 253 | -.Cm []U8 , ^U8 | |
| 260 | +.Cm "[]const U8" , []U8 , ^U8 | |
| 254 | 261 | (with a zero byte after it) or |
| 255 | 262 | .Cm [n]U8 , |
| 256 | 263 | a procedure into its type, |
| @@ -68,7 +68,12 @@ | ||
| 68 | 68 | `CAS`, `XCHG` and `XADD` take the variable they work on, not a pointer |
| 69 | 69 | to it (section 6.8). It breaks a program that gives them a pointer; |
| 70 | 70 | 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. | |
| 72 | 77 | |
| 73 | 78 | The language is frozen, softly, from this version on: it changes only |
| 74 | 79 | to mend a defect, and its library grows only by what breaks no program |
| @@ -233,16 +238,18 @@ | ||
| 233 | 238 | not a variable: nothing that could write it, or let something else |
| 234 | 239 | write it, may name it or a field or element of it. So it can't be |
| 235 | 240 | 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' | |
| 240 | 246 | names stand for the fields of a copy. It can be read, copied and |
| 241 | 247 | compared, and a copy is the importer's own. What a pointer or a slice |
| 242 | 248 | in it points at is another variable, and isn't held to this: with |
| 243 | 249 | `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. | |
| 246 | 253 | |
| 247 | 254 | **Imports.** `import Out;` makes `Out`'s exports available as `Out.x`. |
| 248 | 255 | `import O := Out;` uses the name `O` instead. A module can't import |
| @@ -420,19 +427,19 @@ | ||
| 420 | 427 | literal (section 6.5) of constants, that of a variant one a case built |
| 421 | 428 | from constants, as a program builds one (section 5.6). Its elements |
| 422 | 429 | 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 | |
| 426 | 433 | constant stands for its literal wherever an array, record or variant is |
| 427 | 434 | wanted. A variant constant must have a |
| 428 | 435 | type: `const D = dot;` is an error, `const D: Shape = dot;` isn't. |
| 429 | 436 | |
| 430 | 437 | ``` |
| 431 | 438 | 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; | |
| 433 | 440 | const Rules: [2]Rule = [{class: "firefox", tag: 2}, {class: "mpv", float: true}]; |
| 434 | 441 | 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 *) | |
| 436 | 443 | ... |
| 437 | 444 | p := Primes[i]; |
| 438 | 445 | ``` |
| @@ -440,9 +447,9 @@ | ||
| 440 | 447 | It is used as a variable of that type is, but can't be changed: an |
| 441 | 448 | array constant can be indexed and sliced and its `LEN` is `n`, a |
| 442 | 449 | 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. | |
| 446 | 453 | |
| 447 | 454 | An array constant may be typed `[]T`, with no length: its length is |
| 448 | 455 | then its literal's, as a string constant's is the string's, and it is |
| @@ -470,7 +477,7 @@ | ||
| 470 | 477 | ``` |
| 471 | 478 | Type = QualIdent |
| 472 | 479 | | "[" Expr "]" Type (* array *) |
| 473 | - | "[" "]" Type (* slice *) | |
| 480 | + | "[" "]" [ "const" ] Type (* slice *) | |
| 474 | 481 | | "^" [ "VOLATILE" ] Type (* pointer *) |
| 475 | 482 | | [ "packed" ] "record" { IdentList ":" Type ";" } "end" |
| 476 | 483 | | "variant" { Case ";" } "end" |
| @@ -533,6 +540,25 @@ | ||
| 533 | 540 | doesn't own what it points at. `LEN(s)` is its count, and `PTR(s)` its |
| 534 | 541 | pointer (section 6.8). |
| 535 | 542 | |
| 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 | + | |
| 536 | 562 | ### 5.4 Pointers |
| 537 | 563 | |
| 538 | 564 | `^T` is the address of a `T`, as big as an `INT`. `nil` is the pointer |
| @@ -661,7 +687,7 @@ | ||
| 661 | 687 | holds. |
| 662 | 688 | |
| 663 | 689 | 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)` | |
| 665 | 691 | is `""`. |
| 666 | 692 | |
| 667 | 693 | ``` |
| @@ -719,8 +745,9 @@ | ||
| 719 | 745 | - `A` is an integer constant whose value fits in the integer type `B`; |
| 720 | 746 | - `A` is `nil` and `B` is a pointer, procedure or `ERROR` type; |
| 721 | 747 | - `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 | |
| 724 | 751 | bytes followed by a zero byte, as C wants), or `[n]U8` with `n` at |
| 725 | 752 | least the string's length (the rest is zero); |
| 726 | 753 | - `A` is a procedure and `B` a procedure type with the same signature; |
| @@ -731,11 +758,11 @@ | ||
| 731 | 758 | so on every target; unless the value is what an operator gives |
| 732 | 759 | (below). |
| 733 | 760 | |
| 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). | |
| 739 | 766 | |
| 740 | 767 | Nothing else converts silently. In particular, there is no other |
| 741 | 768 | conversion between integer types, between integers and reals, or |
| @@ -950,7 +977,7 @@ | ||
| 950 | 977 | a `[n]T`, of the same element type `T`, are `=` when they have the |
| 951 | 978 | same length and the same elements, and `#` otherwise, where `T` is an |
| 952 | 979 | 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 | |
| 954 | 981 | it stands, a check of the lengths and then of the bytes, not a call. |
| 955 | 982 | |
| 956 | 983 | Arrays of reals don't compare this way, since `-0.0 = 0.0` and a NaN |
| @@ -1013,9 +1040,9 @@ | ||
| 1013 | 1040 | | `ADR(x)` | the address of variable `x`, a `^T` | |
| 1014 | 1041 | | `LEN(a)` | the number of elements of an array or a string constant (an untyped constant) or of a slice (an `INT`) | |
| 1015 | 1042 | | `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 | | |
| 1017 | 1044 | | `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` | | |
| 1019 | 1046 | | `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)` | |
| 1020 | 1047 | | `FILL(d, x)` | sets every element of `d`, an array or a slice of elements of type `T`, to `x`, a value of `T` | |
| 1021 | 1048 | | `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 @@ | ||
| 1063 | 1090 | or array holding one, is an error. The slice is made, and checked, as |
| 1064 | 1091 | any slice of the variable is, so its bytes follow the rules for it: the |
| 1065 | 1092 | 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 | |
| 1067 | 1095 | other variable they are its own, as `ADR` gives them: a `const` |
| 1068 | 1096 | parameter, another module's variable, a view and a field of a packed |
| 1069 | 1097 | record are errors, as they are for `ADR`, and so is a volatile variable. |
| @@ -1082,7 +1110,7 @@ | ||
| 1082 | 1110 | ``` |
| 1083 | 1111 | type Value = record n: INT; p: ^U8; end; |
| 1084 | 1112 | |
| 1085 | -proc valueOf(s: []U8): Value; | |
| 1113 | +proc valueOf(s: []const U8): Value; | |
| 1086 | 1114 | begin |
| 1087 | 1115 | return {n: LEN(s), p: PTR(s)}; (* "" too *) |
| 1088 | 1116 | end; |
| @@ -1341,7 +1369,7 @@ | ||
| 1341 | 1369 | ``` |
| 1342 | 1370 | |
| 1343 | 1371 | `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 | |
| 1345 | 1373 | fields, in order, all of them; the names stand for those fields of `s` |
| 1346 | 1374 | itself, so assigning to one changes `s`; if `s` is a constant or a |
| 1347 | 1375 | `const` parameter, they can't be assigned; if `s` isn't a variable, |
| @@ -1386,7 +1414,7 @@ | ||
| 1386 | 1414 | two labels. An integer |
| 1387 | 1415 | `match` must have an `| else:` arm, which may be empty. |
| 1388 | 1416 | |
| 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), | |
| 1390 | 1418 | runs the arm one of whose labels it equals, as `=` compares strings |
| 1391 | 1419 | (section 6.5): |
| 1392 | 1420 | |
| @@ -1607,10 +1635,10 @@ | ||
| 1607 | 1635 | either, write it through a view (section 6.9), or assign the names a |
| 1608 | 1636 | `match` arm gives its fields. What a slice or a pointer in it points at |
| 1609 | 1637 | 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 | |
| 1614 | 1642 | parameter's type: a variable or a constant is passed in place, anything |
| 1615 | 1643 | else as a temporary, of the procedure if it holds an array, as an |
| 1616 | 1644 | array a call returns is (below). If the variable changes while the procedure runs, through a |
| @@ -1971,7 +1999,7 @@ | ||
| 1971 | 1999 | |
| 1972 | 2000 | Type = QualIdent |
| 1973 | 2001 | | "[" Expr "]" Type |
| 1974 | - | "[" "]" Type | |
| 2002 | + | "[" "]" [ "const" ] Type | |
| 1975 | 2003 | | "^" [ "VOLATILE" ] Type |
| 1976 | 2004 | | [ "packed" ] "record" { IdentList ":" Type ";" } "end" |
| 1977 | 2005 | | "variant" { Case ";" } "end" |
| @@ -269,3 +269,11 @@ | ||
| 269 | 269 | does, not a pointer to it (section 6.8): `XADD(count, 1)`, |
| 270 | 270 | `XADD(p^, 1)`. It breaks a program that gives them a pointer; raz |
| 271 | 271 | 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. |
| @@ -52,6 +52,8 @@ | ||
| 52 | 52 | misaligned, and with no address *) |
| 53 | 53 | ro*: BOOL; (* mVar: another module's exported variable, or part |
| 54 | 54 | 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 *) | |
| 55 | 57 | deref*: BOOL; (* mVar: at an address from a pointer, which may be |
| 56 | 58 | nil and isn't tested yet (nilTest) *) |
| 57 | 59 | obj*: ^Sym.Obj; (* mProc, mBuiltin, mCase; mVar: the variable named *) |
| @@ -553,6 +555,10 @@ | ||
| 553 | 555 | what is only read, above. *) |
| 554 | 556 | proc notReadOnly*(const x: Item): ERROR; |
| 555 | 557 | 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; | |
| 556 | 562 | if onlyRead(x) then |
| 557 | 563 | return Scan.markHelp(x.pos, say1("{} is only read here, and can't be changed", Diag.source(x.pos)), |
| 558 | 564 | "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 @@ | ||
| 589 | 595 | return Scan.markHelp(x.pos, text, "a view has no address, since it may be misaligned: copy it to a variable"); |
| 590 | 596 | end; |
| 591 | 597 | |
| 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 | + | |
| 592 | 608 | proc coerce*(var x: Item; t: ^Sym.Type): ERROR; |
| 593 | 609 | var s: ^Sym.Type; tmp: I64; inFrame: BOOL; hint: []U8; |
| 594 | 610 | begin |
| @@ -626,10 +642,21 @@ | ||
| 626 | 642 | elsif (s^.form = Sym.fPtr) and (t^.form = Sym.fPtr) and t^.vol and not s^.vol |
| 627 | 643 | and Sym.same(s^.base, t^.base) then |
| 628 | 644 | 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"); | |
| 629 | 650 | elsif (s^.form = Sym.fArray) and (t^.form = Sym.fSlice) and Sym.same(s^.base, t^.base) then |
| 630 | 651 | (* 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; | |
| 633 | 660 | if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end; |
| 634 | 661 | if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end; |
| 635 | 662 | if x.view then return noAddress(x, "can't make a slice of a view"); end; |
| @@ -714,7 +741,7 @@ | ||
| 714 | 741 | end; |
| 715 | 742 | try value(x); |
| 716 | 743 | 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; | |
| 718 | 745 | x.view := false; x.frame := false; |
| 719 | 746 | return nil; |
| 720 | 747 | end; |
| @@ -775,6 +802,7 @@ | ||
| 775 | 802 | else Gen.indexPtr(s); end; |
| 776 | 803 | x.mode := mVar; x.base := Gen.bStack; x.off := 0; |
| 777 | 804 | x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; x.view := false; |
| 805 | + x.elemRo := false; | |
| 778 | 806 | elsif (t^.form = Sym.fArray) and (y.mode = mConst) then |
| 779 | 807 | if (y.val < 0) or (y.val >= t^.len) then |
| 780 | 808 | 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 @@ | ||
| 790 | 818 | try value(y); |
| 791 | 819 | Gen.indexSlice(adrOf(x), s, 0, checks, y.pos); |
| 792 | 820 | x.base := Gen.bStack; x.off := 0; x.isVar := true; x.vol := false; x.ro := false; |
| 821 | + x.elemRo := t^.rdonly; | |
| 793 | 822 | end; |
| 794 | 823 | x.typ := t^.base; |
| 795 | 824 | (* an element of an array lies where the array does; one of a slice or |
| @@ -807,7 +836,7 @@ | ||
| 807 | 836 | end; |
| 808 | 837 | |
| 809 | 838 | 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; | |
| 811 | 840 | begin |
| 812 | 841 | t := x.typ; |
| 813 | 842 | if not Sym.isInt(hi.typ) then |
| @@ -816,7 +845,10 @@ | ||
| 816 | 845 | if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end; |
| 817 | 846 | if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end; |
| 818 | 847 | 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; | |
| 820 | 852 | try value(hi); |
| 821 | 853 | inFrame := x.frame and (t^.form # Sym.fPtr); |
| 822 | 854 | tmp := Gen.allocTemp(16, 8); |
| @@ -824,6 +856,7 @@ | ||
| 824 | 856 | else Gen.slice(adrOf(x), t^.form = Sym.fArray, t^.len, t^.base^.size, tmp, checks, lo.pos); end; |
| 825 | 857 | st := Sym.newType(Sym.fSlice, 16, 8); |
| 826 | 858 | st^.base := t^.base; |
| 859 | + st^.rdonly := roSlice; | |
| 827 | 860 | tempItem(x, st, tmp); |
| 828 | 861 | x.frame := inFrame; |
| 829 | 862 | return nil; |
| @@ -972,7 +1005,7 @@ | ||
| 972 | 1005 | both can be read: a call's two words, or a slice reached through an |
| 973 | 1006 | address on the stack, go to a temporary first. *) |
| 974 | 1007 | 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; | |
| 976 | 1009 | begin |
| 977 | 1010 | if x.mode = mStr then |
| 978 | 1011 | return Scan.markHelp(x.pos, "BYTES can't take a string constant", |
| @@ -993,7 +1026,15 @@ | ||
| 993 | 1026 | s := Gen.allocTemp(16, 8); |
| 994 | 1027 | Gen.popVar(frameAdr(s + 8), 8); |
| 995 | 1028 | 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; | |
| 997 | 1038 | x.frame := inFrame; |
| 998 | 1039 | return nil; |
| 999 | 1040 | end; |
| @@ -1082,6 +1123,7 @@ | ||
| 1082 | 1123 | try value(y); |
| 1083 | 1124 | Gen.indexSlice(adrOf(x), s, k - 1, checks, y.pos); |
| 1084 | 1125 | x.base := Gen.bStack; x.off := 0; x.isVar := true; x.vol := false; x.ro := false; |
| 1126 | + x.elemRo := at^.rdonly; | |
| 1085 | 1127 | x.typ := at^.base; |
| 1086 | 1128 | else |
| 1087 | 1129 | try index(x, y); |
| @@ -2375,6 +2417,10 @@ | ||
| 2375 | 2417 | proc destSlice(var d: Item; text: []U8): ERROR; |
| 2376 | 2418 | begin |
| 2377 | 2419 | 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; | |
| 2378 | 2424 | if not isSeq(d.typ) or (d.mode = mStr) or ((d.typ^.form = Sym.fArray) and ((d.mode # mVar) or not d.isVar)) then |
| 2379 | 2425 | return Scan.markAt(d.pos, say1(text, what(d))); |
| 2380 | 2426 | end; |
| @@ -928,6 +928,7 @@ | ||
| 928 | 928 | if Scan.tok = Scan.sRbrak then |
| 929 | 929 | Scan.next; |
| 930 | 930 | t := Sym.newType(Sym.fSlice, 16, 8); |
| 931 | + if Scan.tok = Scan.sConst then Scan.next; t^.rdonly := true; end; (* []const T *) | |
| 931 | 932 | t^.base := try parseType(); |
| 932 | 933 | else |
| 933 | 934 | start := Scan.tokPos(); |
| @@ -2521,7 +2522,7 @@ | ||
| 2521 | 2522 | try expression(x, nil); |
| 2522 | 2523 | if x.typ^.form = Sym.fConst then try Item.coerce(x, Sym.tI64); end; |
| 2523 | 2524 | (* 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)); | |
| 2525 | 2526 | p := try Item.matchBegin(x); |
| 2526 | 2527 | t := x.typ; |
| 2527 | 2528 | isStr := t^.form = Sym.fSlice; (* matchBegin made a string a slice *) |
| @@ -32,6 +32,7 @@ | ||
| 32 | 32 | size*, align*: I64; |
| 33 | 33 | signed*: BOOL; (* fInt *) |
| 34 | 34 | vol*: BOOL; (* fPtr: ^VOLATILE *) |
| 35 | + rdonly*: BOOL; (* fSlice: []const, its elements not written through it *) | |
| 35 | 36 | packd*: BOOL; (* fRecord *) |
| 36 | 37 | variadic*: BOOL; (* fProc: ends in ... *) |
| 37 | 38 | tuple*: BOOL; (* fRecord: the results of a procedure that has several *) |
| @@ -532,7 +533,7 @@ | ||
| 532 | 533 | match a^.form of |
| 533 | 534 | | fPtr: return (a^.vol = b^.vol) and same(a^.base, b^.base); |
| 534 | 535 | | 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); | |
| 536 | 537 | | fSet: return same(a^.base, b^.base); |
| 537 | 538 | | fProc: return alikeShape(a, b); |
| 538 | 539 | | fRecord: return alikeShape(a, b); |
| @@ -574,7 +575,7 @@ | ||
| 574 | 575 | Fmt.write(b, "[{}]", [Fmt.int(t^.len)]); |
| 575 | 576 | typeText(b, t^.base); |
| 576 | 577 | | fSlice: |
| 577 | - Buf.str(b, "[]"); | |
| 578 | + if t^.rdonly then Buf.str(b, "[]const "); else Buf.str(b, "[]"); end; | |
| 578 | 579 | typeText(b, t^.base); |
| 579 | 580 | | fRecord: |
| 580 | 581 | if not t^.tuple then |
| @@ -644,7 +645,7 @@ | ||
| 644 | 645 | return; |
| 645 | 646 | end; |
| 646 | 647 | 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)); | |
| 648 | 649 | mix(h, t^.len); |
| 649 | 650 | if t^.form in {fPtr, fArray, fSlice, fProc, fSet} then shape(h, t^.base, false); end; |
| 650 | 651 | f := t^.fields; |
| @@ -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 |
| @@ -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 |
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | conform/err/ExportAsSlice.raz:6:9: error: Owner.a is Owner's, and can't be changed outside it |
| 2 | 2 | 6 | sum(Owner.a); |
| 3 | 3 | | ^^^^^^^ |
| 4 | +help: it may be read as a []const INT | |
| 4 | 5 | raz: 1 error |
| @@ -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 | |
| 2 | 2 | 6 | _s := Owner.a[0..2]; |
| 3 | - | ^^^^^^^ | |
| 3 | + | ^^^^^^^^^^^^^ | |
| 4 | +help: its elements are only read, and a []T could write them | |
| 4 | 5 | raz: 1 error |