Commit 28ccb0ee

28ccb0ee34edMerged

Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): emit runtime cls_id check for is_a? / kind_of? / instance_of? on parent-typed recv

For recv.M?(target) where recv has static type R and target is class T, the class lattice has three cases:

R ⊆ T (R is T or descendant of T) is_a?: TRUE, instance_of?: depends R and T unrelated both: FALSE T ⊊ R (T is proper descendant of R) both: depend on runtime cls_id

The recv's static type only bounds the runtime class above; the actual instance can be R or any descendant. Cases that depend on the runtime class were previously collapsing to literal TRUE or FALSE, breaking CRuby parity:

  • is_a? / kind_of? case 3 (target T proper-descendant of static R): emit sp_class_le(recv->cls_id, T) so a parent-typed local holding a descendant matches when appropriate.

  • instance_of? case 3 (same): emit recv->cls_id == T for exact equality (no ancestor walk; instance_of? is stricter than is_a?).

  • instance_of? case 1 (cname == arg0): the prior code returned literal TRUE, which is wrong when a parent-typed local holds a descendant — animal_typed.instance_of?(Animal) should be FALSE if the runtime class is Dog. Emit the same runtime cls_id == arg0 check. The single exception is value-type classes (marked in @cls_is_value_type): these have no subclasses by design and no cls_id field on the struct, so the runtime is always exactly cname and TRUE collapses back to a static value.

Refactor: extract is_descendant(child, ancestor) from the existing is_class_or_ancestor (which is descendant-OR-equal) for clearer call sites. Add is_value_type_class(cname) to gate the instance_of? case-1 runtime-check carve-out.

Test: extend test/is_a_descendant_runtime.rb with instance_of? cases — Parent → Child → GrandChild hierarchy. Covers exact-child match returning TRUE, deeper-grandchild returning FALSE (the bug the literal-FALSE collapse would have hidden), and parent-typed local holding a Child returning FALSE for instance_of?(Parent) (the bug the literal-TRUE collapse would have hidden).

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
28ccb0ee34ed
Parent
1d5291554707

spinel_codegen.rbmodifiedView file

@@ -2989,6 +2989,31 @@
29892989 0
29902990 end
29912991
2992+ # `is_descendant(child, ancestor)` — 1 iff `child` is a strict
2993+ # (proper) descendant of `ancestor` in the class hierarchy; 0
2994+ # otherwise (including when they're identical). Wraps
2995+ # is_class_or_ancestor, which is descendant-OR-EQUAL.
2996+ def is_descendant(child, ancestor)
2997+ if child == ancestor
2998+ return 0
2999+ end
3000+ is_class_or_ancestor(child, ancestor)
3001+ end
3002+
3003+ # `is_value_type_class(cname)` — 1 iff `cname` is a user class
3004+ # marked value-type (stack-allocated, no subclasses by design,
3005+ # no `cls_id` field on the struct). emit_class_fields gates the
3006+ # cls_id emission on the inverse of this; runtime cls_id checks
3007+ # must guard against value-type recvs to avoid referencing a
3008+ # nonexistent struct member.
3009+ def is_value_type_class(cname)
3010+ ci = find_class_idx(cname)
3011+ if ci >= 0 && @cls_is_value_type[ci] == 1
3012+ return 1
3013+ end
3014+ 0
3015+ end
3016+
29923017 def is_operator_name(name)
29933018 if name == "+"
29943019 return 1
@@ -17315,16 +17340,41 @@
1731517340 end
1731617341 if is_class_or_ancestor(cname, arg0) == 1
1731717342 return "TRUE"
17318- else
17319- return "FALSE"
1732017343 end
17344+ # Descendant case: arg0 is a proper subclass of cname (recv's
17345+ # static type). The recv's static type only bounds the runtime
17346+ # class above; the instance can be cname or any descendant
17347+ # including arg0. Consult the recv's actual cls_id slot.
17348+ target_ci = find_class_idx(arg0)
17349+ if target_ci >= 0 && is_descendant(arg0, cname) == 1
17350+ @needs_class_table = 1
17351+ @needs_class_ancestors = 1
17352+ recv_id = @nd_receiver[nid]
17353+ if recv_id >= 0
17354+ return "sp_class_le((sp_Class){" + compile_expr(recv_id) + "->cls_id}, (sp_Class){" + cls_id_for_user_internal(target_ci).to_s + "LL})"
17355+ end
17356+ end
17357+ # Unrelated classes — neither ancestor nor descendant. Always FALSE.
17358+ return "FALSE"
1732117359 end
1732217360 return "FALSE"
1732317361 end
1732417362
17325- # instance_of? — exact class identity, no ancestor walk. A parent
17326- # instance is never an exact-subclass match, so descendant resolution
17327- # is not relevant here; static comparison suffices.
17363+ # instance_of? — exact class identity, no ancestor walk.
17364+ # The recv's static type only bounds the runtime class above; the
17365+ # instance can be the static type OR any descendant. The runtime
17366+ # class can therefore equal arg0 only if arg0 is in that set —
17367+ # i.e., arg0 is cname or a descendant of cname. Other shapes
17368+ # (arg0 is an ancestor of cname, or unrelated) make the equality
17369+ # statically impossible and collapse to FALSE.
17370+ #
17371+ # `cname == arg0` does NOT in general collapse to TRUE — a parent-
17372+ # typed local holding a descendant returns FALSE for
17373+ # `instance_of?(parent)`. The check is runtime via the recv's
17374+ # cls_id slot. EXCEPTION: value-type classes have no subclasses
17375+ # by design (no cls_id field on the struct, no polymorphism), so
17376+ # for value-type cname the runtime is always exactly cname and
17377+ # the cname == arg0 check collapses back to static TRUE.
1732817378 if mname == "instance_of?"
1732917379 if is_obj_type(recv_type) == 1
1733017380 cname = recv_type[4, recv_type.length - 4]
@@ -17336,11 +17386,18 @@
1733617386 arg0 = resolve_introspection_arg_name(a[0])
1733717387 end
1733817388 end
17339- if cname == arg0
17340- return "TRUE"
17341- else
17342- return "FALSE"
17389+ target_ci = find_class_idx(arg0)
17390+ if target_ci >= 0 && (cname == arg0 || is_descendant(arg0, cname) == 1)
17391+ if is_value_type_class(cname) == 1
17392+ return "TRUE"
17393+ end
17394+ @needs_class_table = 1
17395+ recv_id = @nd_receiver[nid]
17396+ if recv_id >= 0
17397+ return "(" + compile_expr(recv_id) + "->cls_id == " + cls_id_for_user_internal(target_ci).to_s + "LL)"
17398+ end
1734317399 end
17400+ return "FALSE"
1734417401 end
1734517402 return "FALSE"
1734617403 end