@shd101wyy/yo 0.1.29 → 0.1.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. package/.github/skills/yo-async-effects/SKILL.md +3 -3
  2. package/.github/skills/yo-async-effects/async-effects-recipes.md +19 -11
  3. package/.github/skills/yo-core-patterns/core-patterns-cheatsheet.md +33 -13
  4. package/.github/skills/yo-project-workflow/workflow-cheatsheet.md +1 -1
  5. package/.github/skills/yo-syntax/syntax-cheatsheet.md +59 -21
  6. package/README.md +4 -3
  7. package/out/cjs/index.cjs +771 -676
  8. package/out/cjs/yo-cli.cjs +1003 -898
  9. package/out/cjs/yo-lsp.cjs +834 -739
  10. package/out/esm/index.mjs +716 -621
  11. package/out/types/src/codegen/exprs/async.d.ts +2 -0
  12. package/out/types/src/codegen/exprs/await.d.ts +1 -0
  13. package/out/types/src/codegen/exprs/closures.d.ts +4 -0
  14. package/out/types/src/codegen/functions/context.d.ts +6 -0
  15. package/out/types/src/env.d.ts +2 -0
  16. package/out/types/src/evaluator/builtins/pragma.d.ts +9 -0
  17. package/out/types/src/evaluator/builtins/unsafe.d.ts +8 -0
  18. package/out/types/src/evaluator/context.d.ts +2 -0
  19. package/out/types/src/evaluator/index.d.ts +1 -1
  20. package/out/types/src/evaluator/memory-safety.d.ts +14 -0
  21. package/out/types/src/evaluator/types/flowability.d.ts +6 -0
  22. package/out/types/src/expr-traversal.d.ts +1 -0
  23. package/out/types/src/expr.d.ts +4 -1
  24. package/out/types/src/public-safe-report.d.ts +19 -0
  25. package/out/types/src/tests/comptime-ref-gate.test.d.ts +1 -0
  26. package/out/types/src/tests/pragma-validation.test.d.ts +1 -0
  27. package/out/types/src/tests/public-safe-report.test.d.ts +1 -0
  28. package/out/types/src/tests/type-representation-pointer.test.d.ts +1 -0
  29. package/out/types/src/tests/unsafe-gate.test.d.ts +1 -0
  30. package/out/types/src/tests/unsafe-report-classify.test.d.ts +1 -0
  31. package/out/types/src/types/definitions.d.ts +2 -0
  32. package/out/types/src/types/utils.d.ts +4 -0
  33. package/out/types/src/unsafe-report.d.ts +29 -0
  34. package/out/types/src/value.d.ts +1 -0
  35. package/out/types/tsconfig.tsbuildinfo +1 -1
  36. package/package.json +1 -1
  37. package/scripts/add-pragma-for-pointer-decls.ts +134 -0
  38. package/scripts/add-pragma.ts +58 -0
  39. package/scripts/migrate-amp-method-calls.ts +186 -0
  40. package/scripts/migrate-clone-calls.ts +93 -0
  41. package/scripts/migrate-get-unwrap.ts +166 -0
  42. package/scripts/migrate-index-patterns.ts +210 -0
  43. package/scripts/migrate-index-trait.ts +142 -0
  44. package/scripts/migrate-iterator.ts +150 -0
  45. package/scripts/migrate-self-ptr.ts +220 -0
  46. package/scripts/migrate-skip-pragmas.ts +109 -0
  47. package/scripts/migrate-tostring.ts +134 -0
  48. package/scripts/trim-pragma.ts +130 -0
  49. package/scripts/wrap-extern-calls.ts +161 -0
  50. package/std/alg/hash.yo +3 -2
  51. package/std/allocator.yo +6 -5
  52. package/std/async.yo +2 -2
  53. package/std/collections/array_list.yo +59 -40
  54. package/std/collections/btree_map.yo +19 -18
  55. package/std/collections/deque.yo +9 -8
  56. package/std/collections/hash_map.yo +101 -13
  57. package/std/collections/hash_set.yo +5 -4
  58. package/std/collections/linked_list.yo +39 -4
  59. package/std/collections/ordered_map.yo +3 -3
  60. package/std/collections/priority_queue.yo +14 -13
  61. package/std/crypto/md5.yo +2 -1
  62. package/std/crypto/random.yo +16 -15
  63. package/std/crypto/sha256.yo +2 -1
  64. package/std/encoding/base64.yo +14 -14
  65. package/std/encoding/hex.yo +3 -3
  66. package/std/encoding/json.yo +59 -10
  67. package/std/encoding/punycode.yo +24 -23
  68. package/std/encoding/toml.yo +4 -3
  69. package/std/encoding/utf16.yo +2 -2
  70. package/std/env.yo +43 -28
  71. package/std/error.yo +6 -6
  72. package/std/fmt/display.yo +2 -2
  73. package/std/fmt/index.yo +6 -5
  74. package/std/fmt/to_string.yo +39 -38
  75. package/std/fmt/writer.yo +9 -8
  76. package/std/fs/dir.yo +34 -33
  77. package/std/fs/file.yo +52 -51
  78. package/std/fs/metadata.yo +10 -9
  79. package/std/fs/temp.yo +24 -13
  80. package/std/fs/walker.yo +10 -9
  81. package/std/gc.yo +1 -0
  82. package/std/glob.yo +7 -7
  83. package/std/http/client.yo +15 -14
  84. package/std/http/http.yo +6 -6
  85. package/std/http/index.yo +1 -1
  86. package/std/imm/list.yo +33 -0
  87. package/std/imm/map.yo +2 -1
  88. package/std/imm/set.yo +1 -0
  89. package/std/imm/sorted_map.yo +1 -0
  90. package/std/imm/sorted_set.yo +1 -0
  91. package/std/imm/string.yo +27 -23
  92. package/std/imm/vec.yo +18 -2
  93. package/std/io/reader.yo +2 -1
  94. package/std/io/writer.yo +3 -2
  95. package/std/libc/assert.yo +1 -0
  96. package/std/libc/ctype.yo +1 -0
  97. package/std/libc/dirent.yo +1 -0
  98. package/std/libc/errno.yo +1 -0
  99. package/std/libc/fcntl.yo +1 -0
  100. package/std/libc/float.yo +1 -0
  101. package/std/libc/limits.yo +1 -0
  102. package/std/libc/math.yo +1 -0
  103. package/std/libc/signal.yo +1 -0
  104. package/std/libc/stdatomic.yo +1 -0
  105. package/std/libc/stdint.yo +1 -0
  106. package/std/libc/stdio.yo +1 -0
  107. package/std/libc/stdlib.yo +1 -0
  108. package/std/libc/string.yo +1 -0
  109. package/std/libc/sys/stat.yo +1 -0
  110. package/std/libc/time.yo +1 -0
  111. package/std/libc/unistd.yo +1 -0
  112. package/std/libc/wctype.yo +1 -0
  113. package/std/libc/windows.yo +2 -0
  114. package/std/log.yo +7 -6
  115. package/std/net/addr.yo +5 -4
  116. package/std/net/dns.yo +7 -6
  117. package/std/net/errors.yo +8 -8
  118. package/std/net/tcp.yo +19 -18
  119. package/std/net/udp.yo +13 -12
  120. package/std/os/signal.yo +3 -3
  121. package/std/path.yo +1 -0
  122. package/std/prelude.yo +353 -182
  123. package/std/process/command.yo +40 -23
  124. package/std/process/index.yo +2 -1
  125. package/std/regex/compiler.yo +10 -9
  126. package/std/regex/index.yo +41 -41
  127. package/std/regex/match.yo +2 -2
  128. package/std/regex/parser.yo +21 -21
  129. package/std/regex/vm.yo +42 -41
  130. package/std/string/string.yo +95 -40
  131. package/std/string/string_builder.yo +9 -9
  132. package/std/string/unicode.yo +50 -49
  133. package/std/sync/channel.yo +2 -1
  134. package/std/sync/cond.yo +5 -4
  135. package/std/sync/mutex.yo +4 -3
  136. package/std/sys/advise.yo +1 -0
  137. package/std/sys/bufio/buf_reader.yo +17 -16
  138. package/std/sys/bufio/buf_writer.yo +10 -9
  139. package/std/sys/clock.yo +1 -0
  140. package/std/sys/copy.yo +1 -0
  141. package/std/sys/dir.yo +10 -9
  142. package/std/sys/dns.yo +6 -5
  143. package/std/sys/errors.yo +11 -11
  144. package/std/sys/events.yo +1 -0
  145. package/std/sys/externs.yo +38 -37
  146. package/std/sys/file.yo +17 -16
  147. package/std/sys/future.yo +4 -3
  148. package/std/sys/iov.yo +1 -0
  149. package/std/sys/mmap.yo +1 -0
  150. package/std/sys/path.yo +1 -0
  151. package/std/sys/perm.yo +2 -1
  152. package/std/sys/pipe.yo +1 -0
  153. package/std/sys/process.yo +5 -4
  154. package/std/sys/signal.yo +1 -0
  155. package/std/sys/socketpair.yo +1 -0
  156. package/std/sys/sockinfo.yo +1 -0
  157. package/std/sys/statfs.yo +2 -1
  158. package/std/sys/statx.yo +1 -0
  159. package/std/sys/sysinfo.yo +1 -0
  160. package/std/sys/tcp.yo +15 -14
  161. package/std/sys/temp.yo +1 -0
  162. package/std/sys/time.yo +2 -1
  163. package/std/sys/timer.yo +6 -6
  164. package/std/sys/tty.yo +2 -1
  165. package/std/sys/udp.yo +13 -12
  166. package/std/sys/unix.yo +12 -11
  167. package/std/testing/bench.yo +4 -3
  168. package/std/thread.yo +7 -6
  169. package/std/time/datetime.yo +18 -15
  170. package/std/time/duration.yo +11 -10
  171. package/std/time/instant.yo +4 -4
  172. package/std/time/sleep.yo +1 -0
  173. package/std/url/index.yo +3 -3
  174. package/std/worker.yo +4 -3
package/std/prelude.yo CHANGED
@@ -1,14 +1,16 @@
1
- // @skip_prelude
2
1
  //! Prelude — automatically imported into every Yo source file.
3
2
  //!
4
3
  //! Provides core language primitives: traits (`Comptime`, `Runtime`, `Clone`,
5
4
  //! `Eq`, `Ord`, `Hash`, …), fundamental types (`Option`, `Result`, `Box`,
6
5
  //! `Slice`, `Range`, `String`, …), operator traits (`Add`, `Sub`, …), type
7
6
  //! reflection (`TypeInfo`), conversion (`Into`, `From`), the async runtime
8
- //! (`IO`, `Future`, `JoinHandle`), and derive rules.
7
+ //! (`Io`, `Future`, `JoinHandle`), and derive rules.
9
8
  //!
10
9
  //! **Do NOT import `std/prelude`** — it is loaded automatically and an
11
10
  //! explicit import will produce a compile error.
11
+ // === Foundational traits ===
12
+ // `Comptime` and `Runtime` are defined first because every other
13
+ // type (including `Pragma` below) is type-checked against them.
12
14
  /// Comptime trait — indicates a type that can be used at compile-time.
13
15
  /// Examples: `i32`, `bool`, `Type`, `comptime_int`, `comptime_float`, `comptime_string`.
14
16
  /// Non-examples: `int`, `ushort` (runtime-only types).
@@ -23,6 +25,49 @@ Runtime :: trait(
23
25
  id := "Runtime"
24
26
  );
25
27
  export(Runtime);
28
+
29
+ // === Pragma — file-level privilege flags ===
30
+ //
31
+ // `Pragma` is defined here (right after the foundational traits and
32
+ // BEFORE any extern/asm/pointer-op site) so the prelude can declare
33
+ // its own `pragma(Pragma.AllowUnsafe);` via ordinary evaluation. The
34
+ // prelude is the foundation of Yo's unsafe-capable machinery
35
+ // (raw-pointer impls, FFI declarations, the async runtime, …); the
36
+ // pragma must be registered before the first extern/asm/pointer-op
37
+ // declaration fires the Phase C gates.
38
+ //
39
+ // `SkipPrelude` is recognized via a separate AST-level pre-scan that
40
+ // runs before the prelude loads (chicken-and-egg: a file with
41
+ // `pragma(Pragma.SkipPrelude);` doesn't have the prelude in scope).
42
+ // All other pragma variants are validated by ordinary evaluation
43
+ // against this enum. See plans/MEMORY_SAFETY.md.
44
+ /// File-level privilege flag for the `pragma(...)` builtin. Declared
45
+ /// at the top of a Yo file (e.g. `pragma(Pragma.AllowUnsafe);`) to
46
+ /// opt the file into compiler-recognized behaviors. See
47
+ /// plans/MEMORY_SAFETY.md.
48
+ Pragma :: enum(
49
+ /// File is permitted to use raw pointer ops, `unsafe(...)`,
50
+ /// `asm(...)`, and `extern(...)`. Without this, those constructs
51
+ /// produce compile errors.
52
+ AllowUnsafe,
53
+ /// Disable the auto-import of `std/prelude`. Used by the prelude
54
+ /// itself, and by any tool/test that wants to bootstrap manually.
55
+ /// Recognized at the AST level before the prelude loads.
56
+ SkipPrelude,
57
+ /// Test-runner directive: skip this test file on ALL WASM targets.
58
+ SkipWasm,
59
+ /// Test-runner directive: skip when target is wasm32-emscripten.
60
+ SkipWasm32Emscripten,
61
+ /// Test-runner directive: skip when target is wasm32-wasi.
62
+ SkipWasm32Wasi
63
+ );
64
+ export(Pragma);
65
+
66
+ // Prelude opts itself out of the implicit self-prelude import and
67
+ // into unsafe-capable mode. Both calls evaluate normally against the
68
+ // `Pragma` enum defined just above.
69
+ pragma(Pragma.SkipPrelude);
70
+ pragma(Pragma.AllowUnsafe);
26
71
  // === Builtin Yo functions ===
27
72
  extern(
28
73
  "Yo",
@@ -180,15 +225,30 @@ export(Dispose);
180
225
  Index :: (fn(comptime(Idx) : Type) -> comptime(Trait))(
181
226
  trait(
182
227
  Output : Type,
183
- index : (fn(self : *(Self), idx : Idx) -> *(Self.Output))
228
+ index : (fn(ref(self) : Self, idx : Idx) -> *(Self.Output))
184
229
  )
185
230
  );
186
231
  export(Index);
232
+ /// Indexable trait — projection-style indexing yielding a borrow.
233
+ /// Companion to `Index`, but instead of returning a raw `*(T)` the
234
+ /// `project` method returns a `ref(Element)` — a second-class
235
+ /// reference that flows through Yo's flowability rule (R1–R4) and
236
+ /// can be received by a `ref(name) := coll.project(pos);` local
237
+ /// binding. The for-loop macro lowers iteration through this trait
238
+ /// so user code never sees a raw pointer. See
239
+ /// plans/ITERATOR_REDESIGN.md.
240
+ Indexable :: (fn(comptime(Idx) : Type) -> comptime(Trait))(
241
+ trait(
242
+ Element : Type,
243
+ project : (fn(ref(self) : Self, pos : Idx) -> ref(Self.Element))
244
+ )
245
+ );
246
+ export(Indexable);
187
247
  /// ComptimeIndex — compile-time indexing trait for constant expressions.
188
248
  ComptimeIndex :: (fn(comptime(Idx) : Type, where(Idx <: Comptime)) -> comptime(Trait))(
189
249
  trait(
190
250
  Output : Type,
191
- index : (fn(comptime(self) : *(Self), comptime(idx) : Idx) -> comptime(*(Self.Output))),
251
+ index : (fn(comptime(ref(self)) : Self, comptime(idx) : Idx) -> comptime(*(Self.Output))),
192
252
  where(Self <: Comptime, Self.Output <: Comptime)
193
253
  )
194
254
  );
@@ -652,14 +712,14 @@ Hash :: trait(
652
712
  * Compute the hash of a value
653
713
  * Returns a u64 hash value
654
714
  */
655
- (hash) : (fn(self : *(Self)) -> u64)
715
+ (hash) : (fn(ref(self) : Self) -> u64)
656
716
  );
657
717
  export(Hash);
658
718
  /// Clone trait — deep-copy a value.
659
719
  Clone :: trait(
660
720
  /// Create an independent clone of `self`.
661
721
  clone :
662
- fn(self : *(Self)) -> Self
722
+ fn(ref(self) : Self) -> Self
663
723
  );
664
724
  export(Clone);
665
725
  /// Isolation trait — runtime check whether a value can be safely transferred.
@@ -985,7 +1045,7 @@ impl(
985
1045
  Clone(
986
1046
  clone : (
987
1047
  (self) ->
988
- __yo_return_self(self.*)
1048
+ __yo_return_self(self)
989
1049
  )
990
1050
  )
991
1051
  );
@@ -995,7 +1055,7 @@ impl(
995
1055
  (hash) : (
996
1056
  (self) ->
997
1057
  cond(
998
- self.* => u64(1),
1058
+ self => u64(1),
999
1059
  true => u64(0)
1000
1060
  )
1001
1061
  )
@@ -1141,13 +1201,13 @@ impl(
1141
1201
  impl(
1142
1202
  i8,
1143
1203
  Hash(
1144
- (hash) : ((self) -> u64(self.*))
1204
+ (hash) : ((self) -> u64(self))
1145
1205
  )
1146
1206
  );
1147
1207
  impl(
1148
1208
  i8,
1149
1209
  Clone(
1150
- clone : ((self) -> __yo_return_self(self.*))
1210
+ clone : ((self) -> __yo_return_self(self))
1151
1211
  )
1152
1212
  );
1153
1213
  impl(
@@ -1431,13 +1491,13 @@ impl(
1431
1491
  impl(
1432
1492
  i16,
1433
1493
  Hash(
1434
- (hash) : ((self) -> u64(self.*))
1494
+ (hash) : ((self) -> u64(self))
1435
1495
  )
1436
1496
  );
1437
1497
  impl(
1438
1498
  i16,
1439
1499
  Clone(
1440
- clone : ((self) -> __yo_return_self(self.*))
1500
+ clone : ((self) -> __yo_return_self(self))
1441
1501
  )
1442
1502
  );
1443
1503
  impl(
@@ -1783,7 +1843,7 @@ impl(
1783
1843
  Hash(
1784
1844
  (hash) : (
1785
1845
  (self) -> {
1786
- u32_value := u32(self.*);
1846
+ u32_value := u32(self);
1787
1847
  u64(u32_value)
1788
1848
  }
1789
1849
  )
@@ -1794,7 +1854,7 @@ impl(
1794
1854
  Clone(
1795
1855
  clone : (
1796
1856
  (self) ->
1797
- __yo_return_self(self.*)
1857
+ __yo_return_self(self)
1798
1858
  )
1799
1859
  )
1800
1860
  );
@@ -2019,13 +2079,13 @@ impl(
2019
2079
  impl(
2020
2080
  i64,
2021
2081
  Hash(
2022
- (hash) : ((self) -> u64(self.*))
2082
+ (hash) : ((self) -> u64(self))
2023
2083
  )
2024
2084
  );
2025
2085
  impl(
2026
2086
  i64,
2027
2087
  Clone(
2028
- clone : ((self) -> __yo_return_self(self.*))
2088
+ clone : ((self) -> __yo_return_self(self))
2029
2089
  )
2030
2090
  );
2031
2091
  impl(
@@ -2309,13 +2369,13 @@ impl(
2309
2369
  impl(
2310
2370
  u8,
2311
2371
  Hash(
2312
- (hash) : ((self) -> u64(self.*))
2372
+ (hash) : ((self) -> u64(self))
2313
2373
  )
2314
2374
  );
2315
2375
  impl(
2316
2376
  u8,
2317
2377
  Clone(
2318
- clone : ((self) -> __yo_return_self(self.*))
2378
+ clone : ((self) -> __yo_return_self(self))
2319
2379
  )
2320
2380
  );
2321
2381
  impl(
@@ -2589,13 +2649,13 @@ impl(
2589
2649
  impl(
2590
2650
  u16,
2591
2651
  Hash(
2592
- (hash) : ((self) -> u64(self.*))
2652
+ (hash) : ((self) -> u64(self))
2593
2653
  )
2594
2654
  );
2595
2655
  impl(
2596
2656
  u16,
2597
2657
  Clone(
2598
- clone : ((self) -> __yo_return_self(self.*))
2658
+ clone : ((self) -> __yo_return_self(self))
2599
2659
  )
2600
2660
  );
2601
2661
  impl(
@@ -2869,13 +2929,13 @@ impl(
2869
2929
  impl(
2870
2930
  u32,
2871
2931
  Hash(
2872
- (hash) : ((self) -> u64(self.*))
2932
+ (hash) : ((self) -> u64(self))
2873
2933
  )
2874
2934
  );
2875
2935
  impl(
2876
2936
  u32,
2877
2937
  Clone(
2878
- clone : ((self) -> __yo_return_self(self.*))
2938
+ clone : ((self) -> __yo_return_self(self))
2879
2939
  )
2880
2940
  );
2881
2941
  impl(
@@ -3149,13 +3209,13 @@ impl(
3149
3209
  impl(
3150
3210
  u64,
3151
3211
  Hash(
3152
- (hash) : ((self) -> self.*)
3212
+ (hash) : ((self) -> self)
3153
3213
  )
3154
3214
  );
3155
3215
  impl(
3156
3216
  u64,
3157
3217
  Clone(
3158
- clone : ((self) -> __yo_return_self(self.*))
3218
+ clone : ((self) -> __yo_return_self(self))
3159
3219
  )
3160
3220
  );
3161
3221
  impl(
@@ -3409,13 +3469,13 @@ impl(
3409
3469
  impl(
3410
3470
  f32,
3411
3471
  Hash(
3412
- (hash) : ((self) -> u64(i32(self.*)))
3472
+ (hash) : ((self) -> u64(i32(self)))
3413
3473
  )
3414
3474
  );
3415
3475
  impl(
3416
3476
  f32,
3417
3477
  Clone(
3418
- clone : ((self) -> __yo_return_self(self.*))
3478
+ clone : ((self) -> __yo_return_self(self))
3419
3479
  )
3420
3480
  );
3421
3481
  impl(
@@ -3610,13 +3670,13 @@ impl(
3610
3670
  impl(
3611
3671
  f64,
3612
3672
  Hash(
3613
- (hash) : ((self) -> u64(i64(self.*)))
3673
+ (hash) : ((self) -> u64(i64(self)))
3614
3674
  )
3615
3675
  );
3616
3676
  impl(
3617
3677
  f64,
3618
3678
  Clone(
3619
- clone : ((self) -> __yo_return_self(self.*))
3679
+ clone : ((self) -> __yo_return_self(self))
3620
3680
  )
3621
3681
  );
3622
3682
  impl(
@@ -3836,13 +3896,13 @@ impl(
3836
3896
  impl(
3837
3897
  isize,
3838
3898
  Hash(
3839
- (hash) : ((self) -> u64(self.*))
3899
+ (hash) : ((self) -> u64(self))
3840
3900
  )
3841
3901
  );
3842
3902
  impl(
3843
3903
  isize,
3844
3904
  Clone(
3845
- clone : ((self) -> __yo_return_self(self.*))
3905
+ clone : ((self) -> __yo_return_self(self))
3846
3906
  )
3847
3907
  );
3848
3908
  impl(
@@ -4129,13 +4189,13 @@ impl(
4129
4189
  impl(
4130
4190
  usize,
4131
4191
  Hash(
4132
- (hash) : ((self) -> u64(self.*))
4192
+ (hash) : ((self) -> u64(self))
4133
4193
  )
4134
4194
  );
4135
4195
  impl(
4136
4196
  usize,
4137
4197
  Clone(
4138
- clone : ((self) -> __yo_return_self(self.*))
4198
+ clone : ((self) -> __yo_return_self(self))
4139
4199
  )
4140
4200
  );
4141
4201
  impl(
@@ -4413,13 +4473,13 @@ impl(
4413
4473
  impl(
4414
4474
  char,
4415
4475
  Hash(
4416
- (hash) : ((self) -> u64(self.*))
4476
+ (hash) : ((self) -> u64(self))
4417
4477
  )
4418
4478
  );
4419
4479
  impl(
4420
4480
  char,
4421
4481
  Clone(
4422
- clone : ((self) -> __yo_return_self(self.*))
4482
+ clone : ((self) -> __yo_return_self(self))
4423
4483
  )
4424
4484
  );
4425
4485
  _char :: char;
@@ -4523,13 +4583,13 @@ impl(
4523
4583
  impl(
4524
4584
  int,
4525
4585
  Hash(
4526
- (hash) : ((self) -> u64(self.*))
4586
+ (hash) : ((self) -> u64(self))
4527
4587
  )
4528
4588
  );
4529
4589
  impl(
4530
4590
  int,
4531
4591
  Clone(
4532
- clone : ((self) -> __yo_return_self(self.*))
4592
+ clone : ((self) -> __yo_return_self(self))
4533
4593
  )
4534
4594
  );
4535
4595
  _int :: int;
@@ -4633,13 +4693,13 @@ impl(
4633
4693
  impl(
4634
4694
  uint,
4635
4695
  Hash(
4636
- (hash) : ((self) -> u64(self.*))
4696
+ (hash) : ((self) -> u64(self))
4637
4697
  )
4638
4698
  );
4639
4699
  impl(
4640
4700
  uint,
4641
4701
  Clone(
4642
- clone : ((self) -> __yo_return_self(self.*))
4702
+ clone : ((self) -> __yo_return_self(self))
4643
4703
  )
4644
4704
  );
4645
4705
  _uint :: uint;
@@ -4743,13 +4803,13 @@ impl(
4743
4803
  impl(
4744
4804
  short,
4745
4805
  Hash(
4746
- (hash) : ((self) -> u64(self.*))
4806
+ (hash) : ((self) -> u64(self))
4747
4807
  )
4748
4808
  );
4749
4809
  impl(
4750
4810
  short,
4751
4811
  Clone(
4752
- clone : ((self) -> __yo_return_self(self.*))
4812
+ clone : ((self) -> __yo_return_self(self))
4753
4813
  )
4754
4814
  );
4755
4815
  _short :: short;
@@ -4853,13 +4913,13 @@ impl(
4853
4913
  impl(
4854
4914
  ushort,
4855
4915
  Hash(
4856
- (hash) : ((self) -> u64(self.*))
4916
+ (hash) : ((self) -> u64(self))
4857
4917
  )
4858
4918
  );
4859
4919
  impl(
4860
4920
  ushort,
4861
4921
  Clone(
4862
- clone : ((self) -> __yo_return_self(self.*))
4922
+ clone : ((self) -> __yo_return_self(self))
4863
4923
  )
4864
4924
  );
4865
4925
  _ushort :: ushort;
@@ -4963,13 +5023,13 @@ impl(
4963
5023
  impl(
4964
5024
  long,
4965
5025
  Hash(
4966
- (hash) : ((self) -> u64(self.*))
5026
+ (hash) : ((self) -> u64(self))
4967
5027
  )
4968
5028
  );
4969
5029
  impl(
4970
5030
  long,
4971
5031
  Clone(
4972
- clone : ((self) -> __yo_return_self(self.*))
5032
+ clone : ((self) -> __yo_return_self(self))
4973
5033
  )
4974
5034
  );
4975
5035
  _long :: long;
@@ -5073,13 +5133,13 @@ impl(
5073
5133
  impl(
5074
5134
  ulong,
5075
5135
  Hash(
5076
- (hash) : ((self) -> u64(self.*))
5136
+ (hash) : ((self) -> u64(self))
5077
5137
  )
5078
5138
  );
5079
5139
  impl(
5080
5140
  ulong,
5081
5141
  Clone(
5082
- clone : ((self) -> __yo_return_self(self.*))
5142
+ clone : ((self) -> __yo_return_self(self))
5083
5143
  )
5084
5144
  );
5085
5145
  _ulong :: ulong;
@@ -5183,13 +5243,13 @@ impl(
5183
5243
  impl(
5184
5244
  longlong,
5185
5245
  Hash(
5186
- (hash) : ((self) -> u64(self.*))
5246
+ (hash) : ((self) -> u64(self))
5187
5247
  )
5188
5248
  );
5189
5249
  impl(
5190
5250
  longlong,
5191
5251
  Clone(
5192
- clone : ((self) -> __yo_return_self(self.*))
5252
+ clone : ((self) -> __yo_return_self(self))
5193
5253
  )
5194
5254
  );
5195
5255
  _longlong :: longlong;
@@ -5293,13 +5353,13 @@ impl(
5293
5353
  impl(
5294
5354
  ulonglong,
5295
5355
  Hash(
5296
- (hash) : ((self) -> u64(self.*))
5356
+ (hash) : ((self) -> u64(self))
5297
5357
  )
5298
5358
  );
5299
5359
  impl(
5300
5360
  ulonglong,
5301
5361
  Clone(
5302
- clone : ((self) -> __yo_return_self(self.*))
5362
+ clone : ((self) -> __yo_return_self(self))
5303
5363
  )
5304
5364
  );
5305
5365
  _ulonglong :: ulonglong;
@@ -5403,13 +5463,13 @@ impl(
5403
5463
  impl(
5404
5464
  longdouble,
5405
5465
  Hash(
5406
- (hash) : ((self) -> u64(i64(self.*)))
5466
+ (hash) : ((self) -> u64(i64(self)))
5407
5467
  )
5408
5468
  );
5409
5469
  impl(
5410
5470
  longdouble,
5411
5471
  Clone(
5412
- clone : ((self) -> __yo_return_self(self.*))
5472
+ clone : ((self) -> __yo_return_self(self))
5413
5473
  )
5414
5474
  );
5415
5475
  _longdouble :: longdouble;
@@ -5483,7 +5543,7 @@ impl(forall(T : Type, U : usize), where(T <: Runtime), Array(T, U), Runtime());
5483
5543
  impl(
5484
5544
  forall(T : Type, U : usize),
5485
5545
  Array(T, U),
5486
- len : (fn(self : *(Self)) -> usize)({
5546
+ len : (fn(ref(self) : Self) -> usize)({
5487
5547
  return(U);
5488
5548
  })
5489
5549
  /*
@@ -5505,13 +5565,17 @@ impl(
5505
5565
  })
5506
5566
  );
5507
5567
  /// Array Index impls
5568
+ /// Bodies take `&(self)` so the `*(Array(T, N))`-expecting builtin
5569
+ /// receives the right C-ABI shape — `ref(self) : Self` gives the body
5570
+ /// a value-typed view of `self` even though the runtime passes a
5571
+ /// pointer. See plans/ITERATOR_REDESIGN.md.
5508
5572
  impl(
5509
5573
  forall(T : Type, N : usize),
5510
5574
  Array(T, N),
5511
5575
  Index(usize)(
5512
5576
  Output : T,
5513
- index : (fn(self : *(Self), idx : usize) -> *(Self.Output))(
5514
- __yo_array_index(self, idx)
5577
+ index : (fn(ref(self) : Self, idx : usize) -> *(Self.Output))(
5578
+ __yo_array_index(&(self), idx)
5515
5579
  )
5516
5580
  )
5517
5581
  );
@@ -5520,8 +5584,8 @@ impl(
5520
5584
  Array(T, N),
5521
5585
  Index(Range(usize))(
5522
5586
  Output : Slice(T),
5523
- index : (fn(self : *(Self), idx : Range(usize)) -> *(Self.Output))(
5524
- __yo_array_index_range(self, idx)
5587
+ index : (fn(ref(self) : Self, idx : Range(usize)) -> *(Self.Output))(
5588
+ __yo_array_index_range(&(self), idx)
5525
5589
  )
5526
5590
  )
5527
5591
  );
@@ -5530,8 +5594,27 @@ impl(
5530
5594
  Array(T, N),
5531
5595
  Index(RangeInclusive(usize))(
5532
5596
  Output : Slice(T),
5533
- index : (fn(self : *(Self), idx : RangeInclusive(usize)) -> *(Self.Output))(
5534
- __yo_array_index_range_inclusive(self, idx)
5597
+ index : (fn(ref(self) : Self, idx : RangeInclusive(usize)) -> *(Self.Output))(
5598
+ __yo_array_index_range_inclusive(&(self), idx)
5599
+ )
5600
+ )
5601
+ );
5602
+ /// Array Indexable impl — projection-style indexing yielding `ref(T)`.
5603
+ /// Body takes `&(self)` so `__yo_array_index`'s `self : *(Array(T, N))`
5604
+ /// parameter is satisfied — the `ref(self) : Self` binding gives the
5605
+ /// body a value-typed view of `self` even though the C ABI passes a
5606
+ /// pointer. The `unsafe` wrap is the trusted escape from the
5607
+ /// flowability rule (privileged code only). See plans/ITERATOR_REDESIGN.md.
5608
+ impl(
5609
+ forall(T : Type, N : usize),
5610
+ Array(T, N),
5611
+ Indexable(usize)(
5612
+ Element : T,
5613
+ project : (fn(ref(self) : Self, pos : usize) -> ref(T))(
5614
+ cond(
5615
+ (pos >= N) => panic("Array: project out of bounds"),
5616
+ true => unsafe(__yo_array_index(&(self), pos))
5617
+ )
5535
5618
  )
5536
5619
  )
5537
5620
  );
@@ -5558,14 +5641,14 @@ impl(
5558
5641
  __yo_slice_ptr(self)
5559
5642
  )
5560
5643
  );
5561
- /// Slice Index impls
5644
+ /// Slice Index impls (same `&(self)` rationale as Array above).
5562
5645
  impl(
5563
5646
  forall(T : Type),
5564
5647
  Slice(T),
5565
5648
  Index(usize)(
5566
5649
  Output : T,
5567
- index : (fn(self : *(Self), idx : usize) -> *(Self.Output))(
5568
- __yo_slice_index(self, idx)
5650
+ index : (fn(ref(self) : Self, idx : usize) -> *(Self.Output))(
5651
+ __yo_slice_index(&(self), idx)
5569
5652
  )
5570
5653
  )
5571
5654
  );
@@ -5574,8 +5657,8 @@ impl(
5574
5657
  Slice(T),
5575
5658
  Index(Range(usize))(
5576
5659
  Output : Slice(T),
5577
- index : (fn(self : *(Self), idx : Range(usize)) -> *(Self.Output))(
5578
- __yo_slice_index_range(self, idx)
5660
+ index : (fn(ref(self) : Self, idx : Range(usize)) -> *(Self.Output))(
5661
+ __yo_slice_index_range(&(self), idx)
5579
5662
  )
5580
5663
  )
5581
5664
  );
@@ -5584,8 +5667,22 @@ impl(
5584
5667
  Slice(T),
5585
5668
  Index(RangeInclusive(usize))(
5586
5669
  Output : Slice(T),
5587
- index : (fn(self : *(Self), idx : RangeInclusive(usize)) -> *(Self.Output))(
5588
- __yo_slice_index_range_inclusive(self, idx)
5670
+ index : (fn(ref(self) : Self, idx : RangeInclusive(usize)) -> *(Self.Output))(
5671
+ __yo_slice_index_range_inclusive(&(self), idx)
5672
+ )
5673
+ )
5674
+ );
5675
+ /// Slice Indexable impl — see Array Indexable above.
5676
+ impl(
5677
+ forall(T : Type),
5678
+ Slice(T),
5679
+ Indexable(usize)(
5680
+ Element : T,
5681
+ project : (fn(ref(self) : Self, pos : usize) -> ref(T))(
5682
+ cond(
5683
+ (pos >= self.len()) => panic("Slice: project out of bounds"),
5684
+ true => unsafe(__yo_slice_index(&(self), pos))
5685
+ )
5589
5686
  )
5590
5687
  )
5591
5688
  );
@@ -5596,7 +5693,7 @@ impl(
5596
5693
  Array(T, N),
5597
5694
  ComptimeIndex(usize)(
5598
5695
  Output : T,
5599
- index : (fn(comptime(self) : *(Self), comptime(idx) : usize) -> comptime(*(Self.Output)))(
5696
+ index : (fn(comptime(ref(self)) : Self, comptime(idx) : usize) -> comptime(*(Self.Output)))(
5600
5697
  __yo_comptime_array_index(self, idx)
5601
5698
  )
5602
5699
  )
@@ -5607,7 +5704,7 @@ impl(
5607
5704
  Array(T, N),
5608
5705
  ComptimeIndex(Range(usize))(
5609
5706
  Output : Slice(T),
5610
- index : (fn(comptime(self) : *(Self), comptime(idx) : Range(usize)) -> comptime(*(Self.Output)))(
5707
+ index : (fn(comptime(ref(self)) : Self, comptime(idx) : Range(usize)) -> comptime(*(Self.Output)))(
5611
5708
  __yo_comptime_array_index_range(self, idx)
5612
5709
  )
5613
5710
  )
@@ -5618,7 +5715,7 @@ impl(
5618
5715
  Array(T, N),
5619
5716
  ComptimeIndex(RangeInclusive(usize))(
5620
5717
  Output : Slice(T),
5621
- index : (fn(comptime(self) : *(Self), comptime(idx) : RangeInclusive(usize)) -> comptime(*(Self.Output)))(
5718
+ index : (fn(comptime(ref(self)) : Self, comptime(idx) : RangeInclusive(usize)) -> comptime(*(Self.Output)))(
5622
5719
  __yo_comptime_array_index_range_inclusive(self, idx)
5623
5720
  )
5624
5721
  )
@@ -5630,7 +5727,7 @@ impl(
5630
5727
  Slice(T),
5631
5728
  ComptimeIndex(usize)(
5632
5729
  Output : T,
5633
- index : (fn(comptime(self) : *(Self), comptime(idx) : usize) -> comptime(*(Self.Output)))(
5730
+ index : (fn(comptime(ref(self)) : Self, comptime(idx) : usize) -> comptime(*(Self.Output)))(
5634
5731
  __yo_comptime_slice_index(self, idx)
5635
5732
  )
5636
5733
  )
@@ -5641,7 +5738,7 @@ impl(
5641
5738
  Slice(T),
5642
5739
  ComptimeIndex(Range(usize))(
5643
5740
  Output : Slice(T),
5644
- index : (fn(comptime(self) : *(Self), comptime(idx) : Range(usize)) -> comptime(*(Self.Output)))(
5741
+ index : (fn(comptime(ref(self)) : Self, comptime(idx) : Range(usize)) -> comptime(*(Self.Output)))(
5645
5742
  __yo_comptime_slice_index_range(self, idx)
5646
5743
  )
5647
5744
  )
@@ -5652,7 +5749,7 @@ impl(
5652
5749
  Slice(T),
5653
5750
  ComptimeIndex(RangeInclusive(usize))(
5654
5751
  Output : Slice(T),
5655
- index : (fn(comptime(self) : *(Self), comptime(idx) : RangeInclusive(usize)) -> comptime(*(Self.Output)))(
5752
+ index : (fn(comptime(ref(self)) : Self, comptime(idx) : RangeInclusive(usize)) -> comptime(*(Self.Output)))(
5656
5753
  __yo_comptime_slice_index_range_inclusive(self, idx)
5657
5754
  )
5658
5755
  )
@@ -5808,7 +5905,7 @@ impl(
5808
5905
  ComptimeList(T),
5809
5906
  ComptimeIndex(usize)(
5810
5907
  Output : T,
5811
- index : (fn(comptime(self) : *(Self), comptime(idx) : usize) -> comptime(*(Self.Output)))(
5908
+ index : (fn(comptime(ref(self)) : Self, comptime(idx) : usize) -> comptime(*(Self.Output)))(
5812
5909
  __yo_comptime_list_index(self, idx)
5813
5910
  )
5814
5911
  )
@@ -5819,7 +5916,7 @@ impl(
5819
5916
  ComptimeList(T),
5820
5917
  ComptimeIndex(Range(usize))(
5821
5918
  Output : ComptimeList(T),
5822
- index : (fn(comptime(self) : *(Self), comptime(idx) : Range(usize)) -> comptime(*(Self.Output)))(
5919
+ index : (fn(comptime(ref(self)) : Self, comptime(idx) : Range(usize)) -> comptime(*(Self.Output)))(
5823
5920
  __yo_comptime_list_index_range(self, idx)
5824
5921
  )
5825
5922
  )
@@ -5830,7 +5927,7 @@ impl(
5830
5927
  ComptimeList(T),
5831
5928
  ComptimeIndex(RangeInclusive(usize))(
5832
5929
  Output : ComptimeList(T),
5833
- index : (fn(comptime(self) : *(Self), comptime(idx) : RangeInclusive(usize)) -> comptime(*(Self.Output)))(
5930
+ index : (fn(comptime(ref(self)) : Self, comptime(idx) : RangeInclusive(usize)) -> comptime(*(Self.Output)))(
5834
5931
  __yo_comptime_list_index_range_inclusive(self, idx)
5835
5932
  )
5836
5933
  )
@@ -6697,7 +6794,7 @@ __derive_clone :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comptim
6697
6794
  "",
6698
6795
  (fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))({
6699
6796
  fname :: fields.get(fi).name;
6700
- part :: __s3("(&(self.*.", fname, ")).clone()");
6797
+ part :: __s3("self.", fname, ".clone()");
6701
6798
  cond(
6702
6799
  (a == "") => part,
6703
6800
  true => __s3(a, ", ", part)
@@ -6741,8 +6838,8 @@ __derive_clone :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comptim
6741
6838
  (fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))({
6742
6839
  fname :: v.fields.get(fi).name;
6743
6840
  cond(
6744
- (a == "") => __s3("(&(__v_", fname, ")).clone()"),
6745
- true => __s4(a, ", (&(__v_", fname, ")).clone()")
6841
+ (a == "") => __s3("__v_", fname, ".clone()"),
6842
+ true => __s4(a, ", __v_", fname, ".clone()")
6746
6843
  )
6747
6844
  })
6748
6845
  );
@@ -6761,7 +6858,7 @@ __derive_clone :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comptim
6761
6858
  )
6762
6859
  })
6763
6860
  );
6764
- match_body :: __s3("match(self.*,\n ", clone_branches, "\n )");
6861
+ match_body :: __s3("match(self,\n ", clone_branches, "\n )");
6765
6862
  ctx.make_impl(
6766
6863
  quote(
6767
6864
  Clone(
@@ -6774,7 +6871,7 @@ __derive_clone :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comptim
6774
6871
  ctx.make_impl(
6775
6872
  quote(
6776
6873
  Clone(
6777
- clone : ((self) -> self.*)
6874
+ clone : ((self) -> self)
6778
6875
  )
6779
6876
  )
6780
6877
  )
@@ -6804,8 +6901,8 @@ __derive_hash :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comptime
6804
6901
  (fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))({
6805
6902
  fname :: fields.get(fi).name;
6806
6903
  cond(
6807
- (fi == 0) => __s3("(h : u64) = (&(self.*.", fname, ")).hash()"),
6808
- true => __s4(a, ";\n h = ((h * u64(31)) + (&(self.*.", fname, ")).hash())")
6904
+ (fi == 0) => __s3("(h : u64) = self.", fname, ".hash()"),
6905
+ true => __s4(a, ";\n h = ((h * u64(31)) + self.", fname, ".hash())")
6809
6906
  )
6810
6907
  })
6811
6908
  );
@@ -6848,7 +6945,7 @@ __derive_hash :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comptime
6848
6945
  __s3("(h : u64) = u64(", vi_str, ")"),
6849
6946
  (fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))({
6850
6947
  fname :: v.fields.get(fi).name;
6851
- __s4(a, "; h = ((h * u64(31)) + (&(__v_", fname, ")).hash())")
6948
+ __s4(a, "; h = ((h * u64(31)) + __v_", fname, ".hash())")
6852
6949
  })
6853
6950
  );
6854
6951
  __s5(
@@ -6866,7 +6963,7 @@ __derive_hash :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comptime
6866
6963
  )
6867
6964
  })
6868
6965
  );
6869
- match_body :: __s3("match(self.*,\n ", hash_branches, "\n )");
6966
+ match_body :: __s3("match(self,\n ", hash_branches, "\n )");
6870
6967
  ctx.make_impl(
6871
6968
  quote(
6872
6969
  Hash(
@@ -7279,9 +7376,9 @@ impl(
7279
7376
  clone : (
7280
7377
  (self) ->
7281
7378
  match(
7282
- self.*,
7379
+ self,
7283
7380
  .None => Option(T).None,
7284
- .Some(v) => Option(T).Some((&(v)).clone())
7381
+ .Some(v) => Option(T).Some(v.clone())
7285
7382
  )
7286
7383
  )
7287
7384
  )
@@ -7315,9 +7412,9 @@ impl(
7315
7412
  clone : (
7316
7413
  (self) ->
7317
7414
  match(
7318
- self.*,
7319
- .Ok(v) => Result(T, E).Ok((&(v)).clone()),
7320
- .Err(e) => Result(T, E).Err((&(e)).clone())
7415
+ self,
7416
+ .Ok(v) => Result(T, E).Ok(v.clone()),
7417
+ .Err(e) => Result(T, E).Err(e.clone())
7321
7418
  )
7322
7419
  )
7323
7420
  )
@@ -7375,7 +7472,7 @@ impl(
7375
7472
  where(T <: Hash),
7376
7473
  Box(T),
7377
7474
  Hash(
7378
- (hash) : ((self) -> (self.*).*.hash())
7475
+ (hash) : ((self) -> self.*.hash())
7379
7476
  )
7380
7477
  );
7381
7478
  impl(
@@ -7383,7 +7480,10 @@ impl(
7383
7480
  where(T <: Eq(T)),
7384
7481
  Box(T),
7385
7482
  Eq(Box(T))(
7386
- (==) : ((lhs, rhs) -> (lhs.* == rhs.*))
7483
+ // SAFETY: Box always holds a non-null pointer to Rc-managed
7484
+ // storage; the deref is sound for as long as either operand
7485
+ // is alive, which covers the duration of this comparison.
7486
+ (==) : ((lhs, rhs) -> unsafe(lhs.* == rhs.*))
7387
7487
  )
7388
7488
  );
7389
7489
  impl(
@@ -7391,7 +7491,7 @@ impl(
7391
7491
  where(T <: Clone),
7392
7492
  Box(T),
7393
7493
  Clone(
7394
- clone : ((self) -> box((&(self.*.*)).clone()))
7494
+ clone : ((self) -> box(self.*.clone()))
7395
7495
  )
7396
7496
  );
7397
7497
  export(
@@ -7521,8 +7621,8 @@ impl(
7521
7621
  /// Create a new uninitialized value.
7522
7622
  new : (fn() -> Self)(__yo_maybe_uninit_new(Self)),
7523
7623
  /// Get a mutable pointer to the underlying value.
7524
- as_ptr : (fn(self : *(Self)) -> *(BaseType))(
7525
- __yo_maybe_uninit_as_ptr(Self, BaseType, self)
7624
+ as_ptr : (fn(ref(self) : Self) -> *(BaseType))(
7625
+ __yo_maybe_uninit_as_ptr(Self, BaseType, &(self))
7526
7626
  ),
7527
7627
  /// Assume the value is initialized and extract it. **Undefined behavior if not initialized.**
7528
7628
  assume_init : (fn(own(self) : Self) -> BaseType)(
@@ -7536,7 +7636,7 @@ Iterator :: trait(
7536
7636
  Item : Type,
7537
7637
  /// Advance the iterator and return the next value, or `None` when exhausted.
7538
7638
  next :
7539
- fn(self : *(Self)) -> Option(Self.Item)
7639
+ fn(ref(self) : Self) -> Option(Self.Item)
7540
7640
  );
7541
7641
  export(Iterator);
7542
7642
  /// IntoIterator trait — convert a collection into an iterator.
@@ -7549,29 +7649,25 @@ IntoIterator :: trait(
7549
7649
  );
7550
7650
  export(IntoIterator);
7551
7651
  /// === Array Iterator ===
7552
- /**
7553
- * Pointer iterator for Array(T, N) - yields *(T) pointers to each element
7554
- * Used by Array.iter()
7555
- */
7556
- ArrayIterPtr :: (fn(comptime(T) : Type) -> comptime(Type))(
7557
- struct(
7558
- _ptr : *(T),
7559
- _index : usize,
7560
- _len : usize
7561
- )
7652
+ /// Position iterator for Array(T, N) — yields `usize` indices.
7653
+ /// Used by `Array.iter()`; the for-macro pairs each yielded position
7654
+ /// with `Array.project(pos)` (Indexable trait) to hand the body a
7655
+ /// `ref(T)`-binding into the element. See plans/ITERATOR_REDESIGN.md.
7656
+ _ArrayPosIter :: struct(
7657
+ _index : usize,
7658
+ _len : usize
7562
7659
  );
7563
7660
  impl(
7564
- forall(T : Type),
7565
- ArrayIterPtr(T),
7661
+ _ArrayPosIter,
7566
7662
  Iterator(
7567
- Item : *(T),
7568
- next : (fn(self : *(Self)) -> Option(*(T)))(
7663
+ Item : usize,
7664
+ next : (fn(ref(self) : Self) -> Option(usize))(
7569
7665
  cond(
7570
7666
  (self._index >= self._len) =>.None,
7571
7667
  true => {
7572
- element_ptr := (self._ptr &+ self._index);
7668
+ out := self._index;
7573
7669
  self._index = (self._index + usize(1));
7574
- .Some(element_ptr)
7670
+ .Some(out)
7575
7671
  }
7576
7672
  )
7577
7673
  )
@@ -7580,34 +7676,30 @@ impl(
7580
7676
  impl(
7581
7677
  forall(T : Type, N : usize),
7582
7678
  Array(T, N),
7583
- iter : (fn(self : *(Self)) -> ArrayIterPtr(T))(
7584
- ArrayIterPtr(T)(_ptr : &((self.*)(usize(0))), _index : usize(0), _len : N)
7679
+ iter : (fn(ref(self) : Self) -> _ArrayPosIter)(
7680
+ _ArrayPosIter(_index : usize(0), _len : N)
7585
7681
  )
7586
7682
  );
7587
- export(ArrayIterPtr);
7588
7683
  /// === Slice Iterator ===
7589
- /**
7590
- * Pointer iterator for Slice(T) - yields *(T) pointers to each element
7591
- * Used by Slice.iter()
7592
- */
7593
- SliceIterPtr :: (fn(comptime(T) : Type) -> comptime(Type))(
7594
- struct(
7595
- _slice : Slice(T),
7596
- _index : usize
7597
- )
7684
+ /// Position iterator for Slice(T) — yields `usize` indices into the
7685
+ /// slice. Used by `Slice.iter()`; the for-macro pairs each yielded
7686
+ /// position with `Slice.project(pos)` (Indexable) to bind the body's
7687
+ /// `ref`-name. See plans/ITERATOR_REDESIGN.md.
7688
+ _SlicePosIter :: struct(
7689
+ _index : usize,
7690
+ _len : usize
7598
7691
  );
7599
7692
  impl(
7600
- forall(T : Type),
7601
- SliceIterPtr(T),
7693
+ _SlicePosIter,
7602
7694
  Iterator(
7603
- Item : *(T),
7604
- next : (fn(self : *(Self)) -> Option(*(T)))(
7695
+ Item : usize,
7696
+ next : (fn(ref(self) : Self) -> Option(usize))(
7605
7697
  cond(
7606
- (self._index >= self._slice.len()) =>.None,
7698
+ (self._index >= self._len) =>.None,
7607
7699
  true => {
7608
- ptr := &((self._slice)(self._index));
7700
+ out := self._index;
7609
7701
  self._index = (self._index + usize(1));
7610
- .Some(ptr)
7702
+ .Some(out)
7611
7703
  }
7612
7704
  )
7613
7705
  )
@@ -7616,11 +7708,10 @@ impl(
7616
7708
  impl(
7617
7709
  forall(T : Type),
7618
7710
  Slice(T),
7619
- iter : (fn(self : *(Self)) -> SliceIterPtr(T))(
7620
- SliceIterPtr(T)(_slice : self.*, _index : usize(0))
7711
+ iter : (fn(ref(self) : Self) -> _SlicePosIter)(
7712
+ _SlicePosIter(_index : usize(0), _len : self.len())
7621
7713
  )
7622
7714
  );
7623
- export(SliceIterPtr);
7624
7715
  /// TryFrom trait — fallible conversion from one type to another.
7625
7716
  TryFrom :: (fn(comptime(From) : Type) -> comptime(Trait))(
7626
7717
  trait(
@@ -7684,17 +7775,38 @@ try :: (fn(quote(expr_to_try) : Expr) -> unquote(Expr))({
7684
7775
  })
7685
7776
  });
7686
7777
  export(try);
7687
- /// `for` macro — iterate over a collection using the `Iterator` trait.
7778
+ /// `for` macro — iterate over a collection. The lambda's binding shape
7779
+ /// dictates whether the iteration borrows or consumes:
7688
7780
  ///
7689
- /// # Example
7781
+ /// `for(coll, ref(x) => body)` — borrow form. Calls `coll.iter()`
7782
+ /// (must yield positions) + `coll.project(pos)` for each. `x` is
7783
+ /// `ref`-bound to the element; reads auto-deref, writes propagate
7784
+ /// back to the collection; `coll` survives the loop.
7785
+ ///
7786
+ /// `for(coll, (x) => body)` — value form. Calls `coll.into_iter()`
7787
+ /// and binds `x` to each yielded value. The collection is moved
7788
+ /// into the iterator and consumed.
7789
+ ///
7790
+ /// Combinator chains (`coll.iter().map(f)`, etc.) only support the
7791
+ /// value form — they yield computed values, not borrows. A blanket
7792
+ /// `into_iter` impl on `Iterator` (below) makes the value form work
7793
+ /// transparently on iterator chains.
7794
+ ///
7795
+ /// See plans/ITERATOR_REDESIGN.md.
7796
+ ///
7797
+ /// # Examples
7690
7798
  /// ```rust
7691
- /// for arr.iter(), (value) => {
7692
- /// printf("value %d\n", value.*);
7693
- /// };
7799
+ /// // Borrow form mutate elements in place.
7800
+ /// for(arr, ref(x) => {
7801
+ /// x = (x + i32(1));
7802
+ /// });
7803
+ ///
7804
+ /// // Value form — consume the collection.
7805
+ /// for(list.into_iter(), (x) => print(x));
7694
7806
  /// ```
7695
7807
  for :: (
7696
7808
  fn(
7697
- quote(iter) : Expr,
7809
+ quote(coll) : Expr,
7698
7810
  quote(handle) : Expr
7699
7811
  ) -> unquote(Expr)
7700
7812
  )({
@@ -7709,21 +7821,68 @@ for :: (
7709
7821
  comptime_assert(args.len() == 2, "'for' macro requires 2 arguments in the function handle");
7710
7822
  variable :: args.car();
7711
7823
  body :: args.cdr().car();
7712
- iter_var :: gensym("for_iter_var");
7713
- temp_var :: gensym("for_temp_var");
7714
- quote({
7715
- unquote(iter_var) := unquote(iter);
7716
- while(runtime(true), {
7717
- unquote(temp_var) := &(unquote(iter_var)).next();
7718
- match(
7719
- unquote(temp_var),
7720
- .Some(unquote(variable)) => unquote(body),
7721
- .None => {
7722
- break;
7723
- }
7724
- )
7725
- });
7726
- })
7824
+ // Detect `ref(name) => body` — borrow form. Otherwise
7825
+ // it's the value form `(name) => body`.
7826
+ is_ref_form :: cond(
7827
+ variable.is_fn_call() => {
7828
+ v_callee :: variable.get_callee();
7829
+ v_callee.is_atom() && (v_callee == quote(ref))
7830
+ },
7831
+ true => false
7832
+ );
7833
+ cond(
7834
+ is_ref_form => {
7835
+ v_args :: variable.get_args();
7836
+ comptime_assert(v_args.len() == 1, "'for' macro: ref(name) requires exactly one name");
7837
+ name :: v_args.car();
7838
+ iter_var :: gensym("for_iter_var");
7839
+ pos_var :: gensym("for_pos_var");
7840
+ // Note: the borrow form does NOT materialize a local
7841
+ // copy of `coll`. Writes through `ref(x) := …
7842
+ // coll.project(pos)` need to propagate to the caller's
7843
+ // storage; a value-typed local copy would receive the
7844
+ // writes silently. The for-macro consumes `coll` as an
7845
+ // expression directly so the `&coll` pointer it
7846
+ // implicitly forms reaches the caller's collection.
7847
+ // (This is correct for `coll : Array(T, N)` /
7848
+ // `Slice(T)` / `String` value-typed collections and
7849
+ // for `ref(coll)`-bound parameter inputs; for owning
7850
+ // collections like `ArrayList` whose internal storage
7851
+ // is heap, the receiver-by-ref semantics already work.)
7852
+ quote({
7853
+ unquote(iter_var) := unquote(coll).iter();
7854
+ while(runtime(true), {
7855
+ match(
7856
+ unquote(iter_var).next(),
7857
+ .Some(unquote(pos_var)) => {
7858
+ ref(unquote(name)) := unquote(coll).project(unquote(pos_var));
7859
+ unquote(body);
7860
+ },
7861
+ .None => {
7862
+ break;
7863
+ }
7864
+ );
7865
+ });
7866
+ })
7867
+ },
7868
+ true => {
7869
+ iter_var :: gensym("for_iter_var");
7870
+ temp_var :: gensym("for_temp_var");
7871
+ quote({
7872
+ unquote(iter_var) := unquote(coll).into_iter();
7873
+ while(runtime(true), {
7874
+ unquote(temp_var) := unquote(iter_var).next();
7875
+ match(
7876
+ unquote(temp_var),
7877
+ .Some(unquote(variable)) => unquote(body),
7878
+ .None => {
7879
+ break;
7880
+ }
7881
+ )
7882
+ });
7883
+ })
7884
+ }
7885
+ )
7727
7886
  },
7728
7887
  true => {
7729
7888
  comptime_assert(false, "for macro requires a function handle with `=>`");
@@ -7764,9 +7923,9 @@ impl(
7764
7923
  IterMap(I, B, F),
7765
7924
  Iterator(
7766
7925
  Item : B,
7767
- next : (fn(self : *(Self)) -> Option(B))(
7926
+ next : (fn(ref(self) : Self) -> Option(B))(
7768
7927
  match(
7769
- &(self._inner).next(),
7928
+ self._inner.next(),
7770
7929
  .None =>.None,
7771
7930
  .Some(item) =>.Some((self._f)(item))
7772
7931
  )
@@ -7791,10 +7950,10 @@ impl(
7791
7950
  IterFilter(I, F),
7792
7951
  Iterator(
7793
7952
  Item : A,
7794
- next : (fn(self : *(Self)) -> Option(A))({
7953
+ next : (fn(ref(self) : Self) -> Option(A))({
7795
7954
  (loop_result : Option(A)) =.None;
7796
7955
  while(runtime(true), {
7797
- candidate := &(self._inner).next();
7956
+ candidate := self._inner.next();
7798
7957
  match(
7799
7958
  candidate,
7800
7959
  .None => {
@@ -7831,12 +7990,12 @@ impl(
7831
7990
  IterTake(I),
7832
7991
  Iterator(
7833
7992
  Item : A,
7834
- next : (fn(self : *(Self)) -> Option(A))(
7993
+ next : (fn(ref(self) : Self) -> Option(A))(
7835
7994
  cond(
7836
7995
  (self._remaining == usize(0)) =>.None,
7837
7996
  true => {
7838
7997
  self._remaining = (self._remaining - usize(1));
7839
- &(self._inner).next()
7998
+ self._inner.next()
7840
7999
  }
7841
8000
  )
7842
8001
  )
@@ -7859,9 +8018,9 @@ impl(
7859
8018
  IterSkip(I),
7860
8019
  Iterator(
7861
8020
  Item : A,
7862
- next : (fn(self : *(Self)) -> Option(A))({
8021
+ next : (fn(ref(self) : Self) -> Option(A))({
7863
8022
  while(self._to_skip > usize(0), {
7864
- result := &(self._inner).next();
8023
+ result := self._inner.next();
7865
8024
  match(
7866
8025
  result,
7867
8026
  .None => {
@@ -7873,7 +8032,7 @@ impl(
7873
8032
  }
7874
8033
  );
7875
8034
  });
7876
- &(self._inner).next()
8035
+ self._inner.next()
7877
8036
  })
7878
8037
  )
7879
8038
  );
@@ -7895,9 +8054,9 @@ impl(
7895
8054
  IterEnumerate(I),
7896
8055
  Iterator(
7897
8056
  Item : IterPair(usize, A),
7898
- next : (fn(self : *(Self)) -> Option(IterPair(usize, A)))(
8057
+ next : (fn(ref(self) : Self) -> Option(IterPair(usize, A)))(
7899
8058
  match(
7900
- &(self._inner).next(),
8059
+ self._inner.next(),
7901
8060
  .None =>.None,
7902
8061
  .Some(item) => {
7903
8062
  idx := self._index;
@@ -7926,12 +8085,12 @@ impl(
7926
8085
  IterZip(I, J),
7927
8086
  Iterator(
7928
8087
  Item : IterPair(A, B),
7929
- next : (fn(self : *(Self)) -> Option(IterPair(A, B)))(
8088
+ next : (fn(ref(self) : Self) -> Option(IterPair(A, B)))(
7930
8089
  match(
7931
- &(self._left).next(),
8090
+ self._left.next(),
7932
8091
  .None =>.None,
7933
8092
  .Some(a) => match(
7934
- &(self._right).next(),
8093
+ self._right.next(),
7935
8094
  .None =>.None,
7936
8095
  .Some(b) =>.Some(IterPair(A, B)(_0 : a, _1 : b))
7937
8096
  )
@@ -7943,6 +8102,18 @@ export(IterZip);
7943
8102
  // ---------------------------------------------------------------------------
7944
8103
  // Blanket methods — added to all types implementing Iterator
7945
8104
  // ---------------------------------------------------------------------------
8105
+ /// Blanket `into_iter` — every `Iterator` is its own `IntoIterator`,
8106
+ /// returning `self`. This lets `for(combinator_chain, (x) => body)`
8107
+ /// expand to `combinator_chain.into_iter().next()` uniformly with
8108
+ /// `for(coll, (x) => body)` — the macro always calls `.into_iter()`
8109
+ /// for the value-iteration form, and for an iterator we hand back
8110
+ /// self. See plans/ITERATOR_REDESIGN.md.
8111
+ impl(
8112
+ forall(I : Type),
8113
+ where(I <: Iterator),
8114
+ I,
8115
+ into_iter : (fn(self : Self) -> Self)(self)
8116
+ );
7946
8117
  impl(
7947
8118
  forall(I : Type),
7948
8119
  where(I <: Iterator),
@@ -8026,7 +8197,7 @@ impl(
8026
8197
  iter := self;
8027
8198
  while(runtime(true), {
8028
8199
  match(
8029
- (&(iter)).next(),
8200
+ iter.next(),
8030
8201
  .None => {
8031
8202
  break;
8032
8203
  },
@@ -8049,7 +8220,7 @@ impl(
8049
8220
  iter := self;
8050
8221
  while(runtime(true), {
8051
8222
  match(
8052
- (&(iter)).next(),
8223
+ iter.next(),
8053
8224
  .None => {
8054
8225
  break;
8055
8226
  },
@@ -8071,7 +8242,7 @@ impl(
8071
8242
  iter := self;
8072
8243
  while(runtime(true), {
8073
8244
  match(
8074
- (&(iter)).next(),
8245
+ iter.next(),
8075
8246
  .None => {
8076
8247
  break;
8077
8248
  },
@@ -8095,7 +8266,7 @@ impl(
8095
8266
  iter := self;
8096
8267
  while(runtime(true), {
8097
8268
  match(
8098
- (&(iter)).next(),
8269
+ iter.next(),
8099
8270
  .None => {
8100
8271
  break;
8101
8272
  },
@@ -8125,7 +8296,7 @@ impl(
8125
8296
  iter := self;
8126
8297
  while(runtime(true), {
8127
8298
  match(
8128
- (&(iter)).next(),
8299
+ iter.next(),
8129
8300
  .None => {
8130
8301
  break;
8131
8302
  },
@@ -8175,11 +8346,11 @@ JoinHandle :: (fn(comptime(T) : Type) -> comptime(Type))(
8175
8346
  )
8176
8347
  );
8177
8348
  export(JoinHandle);
8178
- /// IO module — the async runtime effect.
8349
+ /// Io module — the async runtime effect.
8179
8350
  ///
8180
8351
  /// Provides `io.async`, `io.await`, `io.spawn`, and `io.state` operations.
8181
- /// Automatically injected into `main` when declared with `io : IO`.
8182
- IO :: struct(
8352
+ /// Automatically injected into `main` when declared with `io : Io`.
8353
+ Io :: struct(
8183
8354
  /// Create a new `Future` from an async closure.
8184
8355
  async : (fn(forall(T : Type, E : Type.Struct), action : Impl(Fn(e : E) -> T)) -> Impl(Future(T, E))),
8185
8356
  /// Await a `Future`, suspending until its result is ready.
@@ -8189,7 +8360,7 @@ IO :: struct(
8189
8360
  /// Spawn a `Future` as an independent task, returning a `JoinHandle`.
8190
8361
  spawn : (fn(forall(T : Type, E : Type.Struct), fut : Impl(Future(T, E)), e : E) -> JoinHandle(T))
8191
8362
  );
8192
- export(IO);
8363
+ export(Io);
8193
8364
  extern(
8194
8365
  "Yo",
8195
8366
  __yo_io_async : (fn(forall(T : Type, E : Type.Struct), action : Impl(Fn(e : E) -> T)) -> Impl(Future(T, E))),
@@ -8197,19 +8368,19 @@ extern(
8197
8368
  __yo_io_state : (fn(forall(T : Type, E : Type), fut : Impl(Future(T, E))) -> FutureState),
8198
8369
  __yo_io_spawn : (fn(forall(T : Type, E : Type.Struct), fut : Impl(Future(T, E)), e : E) -> JoinHandle(T))
8199
8370
  );
8200
- /// Built-in IO module instance. Automatically injected for `main` functions
8201
- /// with `io : IO`.
8202
- __yo_builtin_io :: IO(
8371
+ /// Built-in Io module instance. Automatically injected for `main` functions
8372
+ /// with `io : Io`.
8373
+ __yo_builtin_io :: Io(
8203
8374
  async : __yo_io_async,
8204
8375
  await : __yo_io_await,
8205
8376
  state : __yo_io_state,
8206
8377
  spawn : __yo_io_spawn
8207
8378
  );
8208
8379
  export(__yo_builtin_io);
8209
- /// JoinHandle methods — defined after IO so `io : IO` resolves.
8380
+ /// JoinHandle methods — defined after Io so `io : Io` resolves.
8210
8381
  extern(
8211
8382
  "Yo",
8212
- __yo_join_handle_await : (fn(forall(T : Type), self : JoinHandle(T), io : IO) -> Option(T))
8383
+ __yo_join_handle_await : (fn(forall(T : Type), self : JoinHandle(T), io : Io) -> Option(T))
8213
8384
  );
8214
8385
  impl(
8215
8386
  forall(T : Type),