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:
- 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. &&=/||=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.- Existing tests like sym_int_hash_merge expect
puts h[missing_k]to output0; 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
(no real change since `string` is already nullable at the C level —mostly tightens up call-site emit where it assumed non-NULL).## Phase 4 Scope## Phase 4 Scope — DEFERRED (2026-05-25)Implementing `*IntHash` → SP_INT_NIL is significantly more invasivethan initially scoped. First attempt (16:00-16:40 JST) hit threecascading issues, all reverted before commit:### Cascade 1: Self-host bootstrap breaks via inferred-LV typingChanging `analyze`'s `[]` return for `str_int_hash` from `"int"` to`"int?"` propagates "int?" through spinel_analyze.rb's OWN typeinference. Specifically `unify_return_type` and `unify_call_types`treat `"int?"` as a distinct type from `"int"`, widening manyinternal calls (like `not_in(name, arr)`) to poly. Mitigationsattempted:- `unify_hash_value_types`: treat `int?` as `int` (added)- `unify_return_type`: treat `int?` as `int` for fallback (added)These alone aren't enough — `unify_call_types`'s`base_type(old_pt) == base_type(at)` check at line 11568 stilldiverges for `("string", "int?")` pairs, which then fall throughto the trailing "incompatible → poly" branch. The bootstrap failswith `lv_kt = sp_box_nil()` typing (poly) on locals that shouldbe `const char *` (the result of `infer_type(...)`).### Cascade 2: `&&=` / `||=` codegen treats `SP_INT_NIL` as truthyThe compound-assignment codegen lowers `counts[k] &&= v` to:```cif (sp_StrIntHash_get(counts, k)) { sp_StrIntHash_set(counts, k, v);}```The `if (...)` is C-truthy on the raw mrb_int. After Phase 4,missing-key returns `SP_INT_NIL = INT64_MIN`, which is **non-zero**and therefore C-truthy. Ruby says nil is falsy. The fix requiresthe codegen to emit `if (!sp_int_is_nil(...))` for typed-int-hashsources — a parallel arm in `compile_index_and_assign` /`compile_index_or_assign`.### Cascade 3: Existing test assumptions`test/sym_int_hash_merge.rb` line 10 expects `puts h3[:q]` after`h3.delete(:q)` to output `0`. Updating to empty line (Ruby's`puts nil`) requires the int? puts arm AND propagation throughthe analyze type so the call site dispatches to it. Without allcascades fixed, the test produces the raw `-9223372036854775808`output.### Re-planned Phase 4 (alternative: poly wrapper)The cleaner approach, mirroring `Array#index` (commit e46ec54):1. Add `sp_StrIntHash_get_poly(h, k)` runtime helper that returns `sp_RbVal` (sp_box_nil() for missing, sp_box_int(v) for present).2. Same for `sp_SymIntHash_get_poly`.3. Codegen `h[k]` direct dispatch for `str_int_hash` / `sym_int_hash` routes to `_get_poly` (returns sp_RbVal).4. Analyze `[]` returns `"poly"` for these variants.5. Compound writes (`&&=`/`||=`) read via `_get_poly`, write via `_set` after unboxing.The poly route reuses existing infrastructure (sp_poly_truthy,sp_poly_inspect, etc.) and avoids the int? cascade. Cost: boxingon every `[]` lookup. For frameworks heavy in typed-int hash use,profile before committing.Implement `*IntHash` → SP_INT_NIL:### Status1. `lib/sp_runtime.h` `sp_StrIntHash_get`: `return h->default_v;` → `return SP_INT_NIL;` (drop `h->default_v` for the `[]` path — `fetch(k, def)` already handles user default)2. Similar for `SymIntHash`3. Analyze: `str_int_hash[]` returns `int?` instead of `int`4. Codegen: widen LV slot to `int?` when assigned from typed-int hash `[]`Phase 4 deferred until either:- A focused session implements the poly-wrapper route end-to-end, or- The int? cascade is fixed at all sites (3+ places in spinel_analyze.rb plus the compound-assignment codegen)Caveat: `default_v` is currently used by both `[]` AND `fetch(k)`(no-arg `fetch`). Need to distinguish. Likely: keep `default_v`field, but `_get` (which lowers `[]`) returns SP_INT_NIL always;add `_fetch_default` or pass-through. Determined in Phase 4.Until then, `*IntHash[missing]` returns the value-type zero (`0`)— a known Ruby semantic violation, but stable.## Phase 5 Scope