Commit 56b09e11
56b09e11c8f9Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #476 from ryanseys/rs-is-a-descendant-runtime
fix(codegen): emit runtime cls_id check for is_a? / kind_of? / instance_of? on parent-typed recv
Details
- Commit
- 56b09e11c8f9
- Parent
- f7d874e26137
- Merge parent
- 28ccb0ee34ed
spinel_codegen.rbmodifiedView file
@@ -2871,6 +2871,31 @@
28712871
028722872
end28732873
2874+
# `is_descendant(child, ancestor)` — 1 iff `child` is a strict2875+
# (proper) descendant of `ancestor` in the class hierarchy; 02876+
# otherwise (including when they're identical). Wraps2877+
# is_class_or_ancestor, which is descendant-OR-EQUAL.2878+
def is_descendant(child, ancestor)2879+
if child == ancestor2880+
return 02881+
end2882+
is_class_or_ancestor(child, ancestor)2883+
end2884+
2885+
# `is_value_type_class(cname)` — 1 iff `cname` is a user class2886+
# marked value-type (stack-allocated, no subclasses by design,2887+
# no `cls_id` field on the struct). emit_class_fields gates the2888+
# cls_id emission on the inverse of this; runtime cls_id checks2889+
# must guard against value-type recvs to avoid referencing a2890+
# nonexistent struct member.2891+
def is_value_type_class(cname)2892+
ci = find_class_idx(cname)2893+
if ci >= 0 && @cls_is_value_type[ci] == 12894+
return 12895+
end2896+
02897+
end2898+
28742899
def is_operator_name(name)28752900
if name == "+"28762901
return 1@@ -17261,16 +17286,41 @@
1726117286
end1726217287
if is_class_or_ancestor(cname, arg0) == 11726317288
return "TRUE"17264-
else17265-
return "FALSE"1726617289
end17290+
# Descendant case: arg0 is a proper subclass of cname (recv's17291+
# static type). The recv's static type only bounds the runtime17292+
# class above; the instance can be cname or any descendant17293+
# including arg0. Consult the recv's actual cls_id slot.17294+
target_ci = find_class_idx(arg0)17295+
if target_ci >= 0 && is_descendant(arg0, cname) == 117296+
@needs_class_table = 117297+
@needs_class_ancestors = 117298+
recv_id = @nd_receiver[nid]17299+
if recv_id >= 017300+
return "sp_class_le((sp_Class){" + compile_expr(recv_id) + "->cls_id}, (sp_Class){" + cls_id_for_user_internal(target_ci).to_s + "LL})"17301+
end17302+
end17303+
# Unrelated classes — neither ancestor nor descendant. Always FALSE.17304+
return "FALSE"1726717305
end1726817306
return "FALSE"1726917307
end1727017308
17271-
# instance_of? — exact class identity, no ancestor walk. A parent17272-
# instance is never an exact-subclass match, so descendant resolution17273-
# is not relevant here; static comparison suffices.17309+
# instance_of? — exact class identity, no ancestor walk.17310+
# The recv's static type only bounds the runtime class above; the17311+
# instance can be the static type OR any descendant. The runtime17312+
# class can therefore equal arg0 only if arg0 is in that set —17313+
# i.e., arg0 is cname or a descendant of cname. Other shapes17314+
# (arg0 is an ancestor of cname, or unrelated) make the equality17315+
# statically impossible and collapse to FALSE.17316+
#17317+
# `cname == arg0` does NOT in general collapse to TRUE — a parent-17318+
# typed local holding a descendant returns FALSE for17319+
# `instance_of?(parent)`. The check is runtime via the recv's17320+
# cls_id slot. EXCEPTION: value-type classes have no subclasses17321+
# by design (no cls_id field on the struct, no polymorphism), so17322+
# for value-type cname the runtime is always exactly cname and17323+
# the cname == arg0 check collapses back to static TRUE.1727417324
if mname == "instance_of?"1727517325
if is_obj_type(recv_type) == 11727617326
cname = recv_type[4, recv_type.length - 4]@@ -17282,11 +17332,18 @@
1728217332
arg0 = resolve_introspection_arg_name(a[0])1728317333
end1728417334
end17285-
if cname == arg017286-
return "TRUE"17287-
else17288-
return "FALSE"17335+
target_ci = find_class_idx(arg0)17336+
if target_ci >= 0 && (cname == arg0 || is_descendant(arg0, cname) == 1)17337+
if is_value_type_class(cname) == 117338+
return "TRUE"17339+
end17340+
@needs_class_table = 117341+
recv_id = @nd_receiver[nid]17342+
if recv_id >= 017343+
return "(" + compile_expr(recv_id) + "->cls_id == " + cls_id_for_user_internal(target_ci).to_s + "LL)"17344+
end1728917345
end17346+
return "FALSE"1729017347
end1729117348
return "FALSE"1729217349
endtest/is_a_descendant_runtime.rbaddedView file
Loading diff…
test/is_a_descendant_runtime.rb.expectedaddedView file
Loading diff…