| 1 | (* An array a call returns, or a record holding one, lives to the end of |
| 2 | the procedure the call is in (report section 8): a slice of it kept in |
| 3 | a variable still reads it after later statements, each call has its |
| 4 | own, and a const parameter's temporary holding one lives as long. *) |
| 5 | module CallArrayKept; |
| 6 | import Out; |
| 7 | |
| 8 | type R = record n: INT; a: [8]U8; end; |
| 9 | |
| 10 | (* "name-00" and c *) |
| 11 | proc name(c: U8): [8]U8; |
| 12 | var a: [8]U8; |
| 13 | begin |
| 14 | a := "name-000"; |
| 15 | a[7] := c; |
| 16 | return a; |
| 17 | end; |
| 18 | |
| 19 | (* A record holding name(c) *) |
| 20 | proc rec(c: U8): R; |
| 21 | begin |
| 22 | return {n: 1, a: name(c)}; |
| 23 | end; |
| 24 | |
| 25 | (* The array in r, as a slice *) |
| 26 | proc bytes(const r: R): []const U8; |
| 27 | begin |
| 28 | return r.a[0..8]; |
| 29 | end; |
| 30 | |
| 31 | proc main*; |
| 32 | var s, t, u, v: []const U8; i: INT; |
| 33 | begin |
| 34 | s := name('a')[0..8]; |
| 35 | t := name('b')[0..8]; |
| 36 | u := rec('c').a[0..8]; |
| 37 | v := bytes(rec('d')); |
| 38 | for i := 0 to 1 do |
| 39 | Out.str(name('x')[0..8]); |
| 40 | Out.char(' '); |
| 41 | end; |
| 42 | Out.str(s); |
| 43 | Out.char(' '); |
| 44 | Out.str(t); |
| 45 | Out.char(' '); |
| 46 | Out.str(u); |
| 47 | Out.char(' '); |
| 48 | Out.str(v); |
| 49 | Out.char(10); |
| 50 | end; |
| 51 | |
| 52 | end CallArrayKept. |