louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | import std/ref.{type Ref, get, set} |
| 2 | |
| 3 | // The region's value may not contain one of its references. |
| 4 | pub fn leak_result :: -> Int |
| 5 | { |
| 6 | let r = region { ref.new 1 } //~ ERROR E0701 would escape it |
| 7 | 0 |
| 8 | } |
| 9 | |
| 10 | // Nor may a reference leak through a variable defined outside. |
| 11 | fn leak_through_outer :: |
| 12 | { |
| 13 | let saved = [] |
| 14 | region |
| 15 | { |
| 16 | let r = ref.new 0 |
| 17 | let all = [r, ..saved] //~ ERROR E0701 would escape it |
| 18 | () |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // Using references outside a region is an unhandled effect. |
| 23 | pub fn outside :: -> Int |
| 24 | { |
| 25 | let r = ref.new 0 //~ ERROR E0502 performs `alloc |
| 26 | 0 |
| 27 | } |
| 28 | |
| 29 | // Contents can leave the region freely. |
| 30 | pub fn contents :: -> List<Int> |
| 31 | { |
| 32 | region |
| 33 | { |
| 34 | let r = ref.new [1, 2] |
| 35 | ref.get r |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // Allocation always happens in the innermost region. |
| 40 | pub fn allocate_outward :: -> Int |
| 41 | { |
| 42 | region |
| 43 | { |
| 44 | let outer = ref.new (ref.new 0) |
| 45 | region |
| 46 | { |
| 47 | ref.set outer (ref.new 1) //~ ERROR E0202 expected |
| 48 | 0 |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Only regions handle references. |
| 54 | effect noop |
| 55 | { |
| 56 | fn nothing :: -> () |
| 57 | } |
| 58 | pub fn mock :: -> Int |
| 59 | { |
| 60 | region |
| 61 | { |
| 62 | let r = ref.new 1 |
| 63 | handle |
| 64 | { //~ ERROR E0702 handled by `region { }` only |
| 65 | ref.get r |
| 66 | } |
| 67 | with |
| 68 | { get x -> 0 //~ ERROR E0202 expected |
| 69 | , set x v -> () |
| 70 | } |
| 71 | } |
| 72 | } |