Commit 0b1f8d7d

0b1f8d7d93c4Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

docs(hash-nullable): defer Phase 4, document cascade findings

Phase 4 (*IntHash _get returns SP_INT_NIL) was attempted today 2026-05-25 and reverted after hitting three coordinated cascades that need a different design than the int? sentinel approach:

  1. Self-host bootstrap breaks via type inference of "int?" through spinel_analyze.rb's own internal LVs (unify_call_types treats ("string", "int?") as incompatible -> poly), widening not_in's param and downstream not_in callers.
  2. &&= / ||= codegen uses raw C truthy on the _get result, treating SP_INT_NIL (= INT64_MIN, non-zero) as truthy -- contrary to Ruby's nil-is-falsy semantics. Needs parallel arms in compile_index_and_assign / compile_index_or_assign.
  3. Existing tests like sym_int_hash_merge expect puts h[missing_k] to output 0; updating to empty line requires the int? puts arm PLUS proper analyze propagation.

Re-plan: use the poly-wrapper route (mirroring Array#index from e46ec54). sp_StrIntHash_get_poly / sp_SymIntHash_get_poly return sp_RbVal directly, leveraging existing poly infrastructure (sp_poly_truthy, sp_poly_inspect, etc.) and bypassing the int? cascade. Cost: boxing on every [] lookup; profile before committing for frameworks heavy in typed-int hash use.

Until Phase 4 lands properly, *IntHash[missing] still returns the value-type zero -- a known Ruby semantic violation, but stable.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
0b1f8d7d93c4
Parent
5e916b66b8e4

docs/HASH-NULLABLE.mdmodifiedView file

@@ -235,22 +235,84 @@
235235 (no real change since `string` is already nullable at the C level —
236236 mostly tightens up call-site emit where it assumed non-NULL).
237237
238-## Phase 4 Scope
238+## Phase 4 Scope — DEFERRED (2026-05-25)
239+
240+Implementing `*IntHash` → SP_INT_NIL is significantly more invasive
241+than initially scoped. First attempt (16:00-16:40 JST) hit three
242+cascading issues, all reverted before commit:
243+
244+### Cascade 1: Self-host bootstrap breaks via inferred-LV typing
245+
246+Changing `analyze`'s `[]` return for `str_int_hash` from `"int"` to
247+`"int?"` propagates "int?" through spinel_analyze.rb's OWN type
248+inference. Specifically `unify_return_type` and `unify_call_types`
249+treat `"int?"` as a distinct type from `"int"`, widening many
250+internal calls (like `not_in(name, arr)`) to poly. Mitigations
251+attempted:
252+
253+- `unify_hash_value_types`: treat `int?` as `int` (added)
254+- `unify_return_type`: treat `int?` as `int` for fallback (added)
255+
256+These alone aren't enough — `unify_call_types`'s
257+`base_type(old_pt) == base_type(at)` check at line 11568 still
258+diverges for `("string", "int?")` pairs, which then fall through
259+to the trailing "incompatible → poly" branch. The bootstrap fails
260+with `lv_kt = sp_box_nil()` typing (poly) on locals that should
261+be `const char *` (the result of `infer_type(...)`).
262+
263+### Cascade 2: `&&=` / `||=` codegen treats `SP_INT_NIL` as truthy
264+
265+The compound-assignment codegen lowers `counts[k] &&= v` to:
266+
267+```c
268+if (sp_StrIntHash_get(counts, k)) {
269+ sp_StrIntHash_set(counts, k, v);
270+}
271+```
272+
273+The `if (...)` is C-truthy on the raw mrb_int. After Phase 4,
274+missing-key returns `SP_INT_NIL = INT64_MIN`, which is **non-zero**
275+and therefore C-truthy. Ruby says nil is falsy. The fix requires
276+the codegen to emit `if (!sp_int_is_nil(...))` for typed-int-hash
277+sources — a parallel arm in `compile_index_and_assign` /
278+`compile_index_or_assign`.
279+
280+### Cascade 3: Existing test assumptions
281+
282+`test/sym_int_hash_merge.rb` line 10 expects `puts h3[:q]` after
283+`h3.delete(:q)` to output `0`. Updating to empty line (Ruby's
284+`puts nil`) requires the int? puts arm AND propagation through
285+the analyze type so the call site dispatches to it. Without all
286+cascades fixed, the test produces the raw `-9223372036854775808`
287+output.
288+
289+### Re-planned Phase 4 (alternative: poly wrapper)
290+
291+The cleaner approach, mirroring `Array#index` (commit e46ec54):
292+
293+1. Add `sp_StrIntHash_get_poly(h, k)` runtime helper that returns
294+ `sp_RbVal` (sp_box_nil() for missing, sp_box_int(v) for present).
295+2. Same for `sp_SymIntHash_get_poly`.
296+3. Codegen `h[k]` direct dispatch for `str_int_hash` / `sym_int_hash`
297+ routes to `_get_poly` (returns sp_RbVal).
298+4. Analyze `[]` returns `"poly"` for these variants.
299+5. Compound writes (`&&=`/`||=`) read via `_get_poly`, write via
300+ `_set` after unboxing.
301+
302+The poly route reuses existing infrastructure (sp_poly_truthy,
303+sp_poly_inspect, etc.) and avoids the int? cascade. Cost: boxing
304+on every `[]` lookup. For frameworks heavy in typed-int hash use,
305+profile before committing.
239306
240-Implement `*IntHash` → SP_INT_NIL:
307+### Status
241308
242-1. `lib/sp_runtime.h` `sp_StrIntHash_get`: `return h->default_v;` →
243- `return SP_INT_NIL;` (drop `h->default_v` for the `[]` path —
244- `fetch(k, def)` already handles user default)
245-2. Similar for `SymIntHash`
246-3. Analyze: `str_int_hash[]` returns `int?` instead of `int`
247-4. Codegen: widen LV slot to `int?` when assigned from typed-int
248- hash `[]`
309+Phase 4 deferred until either:
310+- A focused session implements the poly-wrapper route end-to-end, or
311+- The int? cascade is fixed at all sites (3+ places in
312+ spinel_analyze.rb plus the compound-assignment codegen)
249313
250-Caveat: `default_v` is currently used by both `[]` AND `fetch(k)`
251-(no-arg `fetch`). Need to distinguish. Likely: keep `default_v`
252-field, but `_get` (which lowers `[]`) returns SP_INT_NIL always;
253-add `_fetch_default` or pass-through. Determined in Phase 4.
314+Until then, `*IntHash[missing]` returns the value-type zero (`0`)
315+— a known Ruby semantic violation, but stable.
254316
255317 ## Phase 5 Scope
256318