raz / commit 74519995

commit
7451999571f5874132a05a8593c827957ddec2a9
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
73c1f3ac
conform: a const parameter's slice returned, of the caller's array and of a temporary used within the statement, as report 2026-10.36's 7.3 has it

patch · log

3 files changed, +41 -1

conform/run/ConstParamSlice.expect (history) added +3 -0

@@ -0,0 +1,3 @@
1+jello
2+temp
3+2

conform/run/ConstParamSlice.raz (history) added +35 -0

@@ -0,0 +1,35 @@
1+(* A slice of a const parameter may be returned: it is the caller's
2+ array, not the procedure's frame. Given a temporary, the slice lives
3+ to the end of the caller's statement (report sections 7.3 and 8). *)
4+module ConstParamSlice;
5+import Out;
6+
7+type Line = record text: [8]U8; len: INT; end;
8+
9+var line: Line;
10+
11+proc used(const l: Line): []U8;
12+begin
13+ return l.text[0..l.len];
14+end;
15+
16+proc make(s: []U8): Line;
17+var l: Line;
18+begin
19+ COPY(l.text, s);
20+ l.len := LEN(s);
21+ return l;
22+end;
23+
24+proc main*;
25+var s: []U8;
26+begin
27+ line := make("hello");
28+ s := used(line);
29+ line.text[0] := 'j';
30+ Out.str(s); Out.ln; (* the caller's array: jello *)
31+ Out.str(used(make("temp"))); Out.ln; (* a temporary, read within the statement *)
32+ Out.int(LEN(used({text: "abc", len: 2}))); Out.ln;
33+end;
34+
35+end ConstParamSlice.

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

@@ -1425,7 +1425,9 @@
14251425
14261426 The frame: an item's `frame` says, for an array or record, that it lies
14271427 in the procedure's own frame (a local, a value parameter, which is a
1428-copy even when passed by address, or a temporary), and for a slice,
1428+copy even when passed by address, or a temporary; not a `var` or
1429+`const` parameter, the caller's, even when that is a temporary of the
1430+caller's statement), and for a slice,
14291431 that it points there. `makeItem` and `tempItem` set it; `field` and
14301432 `index` keep it for a part of the array, and drop it for an element of
14311433 a slice or a slice kept in a field, which may point anywhere; `deref`