Commit ef28d62f
ef28d62f2039Merged
Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): is_a? / kind_of? / instance_of? on primitives
5.is_a?(Numeric) and friends were unsupported on primitive
receivers — the call fell through to the generic dispatch and
emitted 0. p then formatted the int 0 as 0 instead of the
boolean false (or true).
Two pieces:
- infer_call_type recognises kind_of? and instance_of? as
boolean-returning, joining the existing is_a? entry. Without
this, results showed up as
1/0frompinstead oftrue/false. - compile_int / float / string / symbol_method_expr each gain is_a?/kind_of?/instance_of? handlers that decide the answer at compile time from the constant arg name and Ruby's class hierarchy. is_a?/kind_of? walk the superclass chain (so Integer matches Integer, Numeric, Comparable, Object, Kernel, BasicObject); instance_of? only matches the exact class. Args that aren't a known class name return FALSE.
Test: primitive_is_a.rb covers Integer / Float / String / Symbol receivers across each ancestor, plus the instance_of? strictness distinction.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- ef28d62f2039
- Parent
- 8ff9aa915a30
spinel_codegen.rbmodifiedView file
@@ -3090,7 +3090,7 @@
30903090
if mname == "frozen?"30913091
return "bool"30923092
end3093-
if mname == "is_a?"3093+
if mname == "is_a?" || mname == "kind_of?" || mname == "instance_of?"30943094
return "bool"30953095
end30963096
if mname == "respond_to?"@@ -21963,6 +21963,38 @@
2196321963
end2196421964
2196521965
def compile_string_method_expr(nid, mname, rc)21966+
# is_a? / kind_of? / instance_of? for primitive String. Decide at21967+
# compile time based on Ruby's class hierarchy (String < Comparable21968+
# < Object). Anything outside that chain is FALSE.21969+
if mname == "is_a?" || mname == "kind_of?"21970+
args_id = @nd_arguments[nid]21971+
if args_id >= 021972+
a = get_args(args_id)21973+
if a.length > 021974+
arg_name = @nd_name[a[0]]21975+
if arg_name == "String" || arg_name == "Comparable" ||21976+
arg_name == "Object" || arg_name == "Kernel" || arg_name == "BasicObject"21977+
return "TRUE"21978+
end21979+
return "FALSE"21980+
end21981+
end21982+
return "FALSE"21983+
end21984+
if mname == "instance_of?"21985+
args_id = @nd_arguments[nid]21986+
if args_id >= 021987+
a = get_args(args_id)21988+
if a.length > 021989+
arg_name = @nd_name[a[0]]21990+
if arg_name == "String"21991+
return "TRUE"21992+
end21993+
return "FALSE"21994+
end21995+
end21996+
return "FALSE"21997+
end2196621998
# String#each_byte returns the receiver per CRuby. The statement-level2196721999
# handler at compile_block_iteration_stmt emits the loop for `do …2196822000
# end` / `{ … }` with no captured value; the expression-level form@@ -22490,6 +22522,35 @@
2249022522
2249122523
# Symbol methods. rc is a sp_sym expression.2249222524
def compile_symbol_method_expr(nid, mname, rc)22525+
if mname == "is_a?" || mname == "kind_of?"22526+
args_id = @nd_arguments[nid]22527+
if args_id >= 022528+
a = get_args(args_id)22529+
if a.length > 022530+
arg_name = @nd_name[a[0]]22531+
if arg_name == "Symbol" || arg_name == "Comparable" ||22532+
arg_name == "Object" || arg_name == "Kernel" || arg_name == "BasicObject"22533+
return "TRUE"22534+
end22535+
return "FALSE"22536+
end22537+
end22538+
return "FALSE"22539+
end22540+
if mname == "instance_of?"22541+
args_id = @nd_arguments[nid]22542+
if args_id >= 022543+
a = get_args(args_id)22544+
if a.length > 022545+
arg_name = @nd_name[a[0]]22546+
if arg_name == "Symbol"22547+
return "TRUE"22548+
end22549+
return "FALSE"22550+
end22551+
end22552+
return "FALSE"22553+
end2249322554
if mname == "to_s" || mname == "id2name" || mname == "name"2249422555
return "sp_sym_to_s(" + rc + ")"2249522556
end@@ -22561,6 +22622,41 @@
2256122622
end2256222623
2256322624
def compile_int_method_expr(nid, mname, rc)22625+
# is_a? / kind_of? on a primitive int: resolve at compile time. The22626+
# arg is a class constant (Integer / Numeric / Comparable / etc.);22627+
# answer based on Ruby's class hierarchy. Anything not in the int22628+
# ancestor chain is FALSE.22629+
if mname == "is_a?" || mname == "kind_of?"22630+
args_id = @nd_arguments[nid]22631+
if args_id >= 022632+
a = get_args(args_id)22633+
if a.length > 022634+
arg_name = @nd_name[a[0]]22635+
if arg_name == "Integer" || arg_name == "Numeric" ||22636+
arg_name == "Comparable" || arg_name == "Object" ||22637+
arg_name == "Kernel" || arg_name == "BasicObject"22638+
return "TRUE"22639+
end22640+
return "FALSE"22641+
end22642+
end22643+
return "FALSE"22644+
end22645+
# instance_of? is is_a? without superclass / mixin matching.22646+
if mname == "instance_of?"22647+
args_id = @nd_arguments[nid]22648+
if args_id >= 022649+
a = get_args(args_id)22650+
if a.length > 022651+
arg_name = @nd_name[a[0]]22652+
if arg_name == "Integer"22653+
return "TRUE"22654+
end22655+
return "FALSE"22656+
end22657+
end22658+
return "FALSE"22659+
end2256422660
if mname == "to_s"2256522661
if @nd_arguments[nid] >= 02256622662
aargs = get_args(@nd_arguments[nid])@@ -22688,6 +22784,36 @@
2268822784
end2268922785
2269022786
def compile_float_method_expr(nid, mname, rc)22787+
if mname == "is_a?" || mname == "kind_of?"22788+
args_id = @nd_arguments[nid]22789+
if args_id >= 022790+
a = get_args(args_id)22791+
if a.length > 022792+
arg_name = @nd_name[a[0]]22793+
if arg_name == "Float" || arg_name == "Numeric" ||22794+
arg_name == "Comparable" || arg_name == "Object" ||22795+
arg_name == "Kernel" || arg_name == "BasicObject"22796+
return "TRUE"22797+
end22798+
return "FALSE"22799+
end22800+
end22801+
return "FALSE"22802+
end22803+
if mname == "instance_of?"22804+
args_id = @nd_arguments[nid]22805+
if args_id >= 022806+
a = get_args(args_id)22807+
if a.length > 022808+
arg_name = @nd_name[a[0]]22809+
if arg_name == "Float"22810+
return "TRUE"22811+
end22812+
return "FALSE"22813+
end22814+
end22815+
return "FALSE"22816+
end2269122817
if mname == "itself"2269222818
return rc2269322819
endtest/primitive_is_a.rbaddedView file
Loading diff…
test/primitive_is_a.rb.expectedaddedView file
Loading diff…