Commit 2578cf78
2578cf78555cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #325 from ryanseys/fix/class-threeq-primitives
fix(codegen): Class#=== for primitive type names (case-when)
Details
- Commit
- 2578cf78555c
- Parent
- 497d69700e72
- Merge parent
- bcc60b692718
spinel_codegen.rbmodifiedView file
@@ -3070,6 +3070,9 @@
30703070
if mname == "cover?"30713071
return "bool"30723072
end3073+
if mname == "==="3074+
return "bool"3075+
end30733076
if mname == "match?"30743077
return "bool"30753078
end@@ -20757,6 +20760,93 @@
2075720760
end2075820761
end2075920762
20763+
# `Class === expr` — case-when membership for primitive type names.20764+
# Resolve at compile time based on the arg's inferred type. Has to20765+
# run before compile_operator_expr / compile_eq (both of which would20766+
# mishandle the constant receiver as a regular C identifier and emit20767+
# `<undeclared identifier> == arg`).20768+
if mname == "===" && recv >= 0 && (@nd_type[recv] == "ConstantReadNode" || @nd_type[recv] == "ConstantPathNode")20769+
rcname_threeq = resolve_const_ref_name(recv)20770+
args_id_threeq = @nd_arguments[nid]20771+
if args_id_threeq >= 020772+
a_threeq = get_args(args_id_threeq)20773+
if a_threeq.length > 020774+
at_threeq = infer_type(a_threeq[0])20775+
if rcname_threeq == "Integer"20776+
return at_threeq == "int" ? "TRUE" : "FALSE"20777+
end20778+
if rcname_threeq == "Float"20779+
return at_threeq == "float" ? "TRUE" : "FALSE"20780+
end20781+
if rcname_threeq == "Numeric"20782+
return (at_threeq == "int" || at_threeq == "float") ? "TRUE" : "FALSE"20783+
end20784+
if rcname_threeq == "Comparable"20785+
# Comparable is included by Integer, Float, String, Symbol.20786+
return (at_threeq == "int" || at_threeq == "float" ||20787+
at_threeq == "string" || at_threeq == "mutable_str" ||20788+
at_threeq == "symbol") ? "TRUE" : "FALSE"20789+
end20790+
if rcname_threeq == "String"20791+
return at_threeq == "string" || at_threeq == "mutable_str" ? "TRUE" : "FALSE"20792+
end20793+
if rcname_threeq == "Symbol"20794+
return at_threeq == "symbol" ? "TRUE" : "FALSE"20795+
end20796+
if rcname_threeq == "Array"20797+
return is_array_type(at_threeq) == 1 ? "TRUE" : "FALSE"20798+
end20799+
if rcname_threeq == "Hash"20800+
# Hash type names all end in `_hash` (sym_int_hash, str_str_hash,20801+
# sym_poly_hash, etc.); poly_hash also matches the runtime form.20802+
is_hash = 020803+
if at_threeq == "poly_hash"20804+
is_hash = 120805+
elsif at_threeq.length > 5 && at_threeq[at_threeq.length - 5, 5] == "_hash"20806+
is_hash = 120807+
end20808+
return is_hash == 1 ? "TRUE" : "FALSE"20809+
end20810+
if rcname_threeq == "Range"20811+
return at_threeq == "range" ? "TRUE" : "FALSE"20812+
end20813+
if rcname_threeq == "TrueClass"20814+
# TrueClass === arg matches only the literal `true` value.20815+
# Compile-time decide for TrueNode/FalseNode literals; emit a20816+
# runtime check for other bool-typed values; FALSE for non-bool.20817+
if @nd_type[a_threeq[0]] == "TrueNode"20818+
return "TRUE"20819+
end20820+
if @nd_type[a_threeq[0]] == "FalseNode"20821+
return "FALSE"20822+
end20823+
if at_threeq == "bool"20824+
return "((" + compile_expr(a_threeq[0]) + ") == TRUE)"20825+
end20826+
return "FALSE"20827+
end20828+
if rcname_threeq == "FalseClass"20829+
if @nd_type[a_threeq[0]] == "FalseNode"20830+
return "TRUE"20831+
end20832+
if @nd_type[a_threeq[0]] == "TrueNode"20833+
return "FALSE"20834+
end20835+
if at_threeq == "bool"20836+
return "((" + compile_expr(a_threeq[0]) + ") == FALSE)"20837+
end20838+
return "FALSE"20839+
end20840+
if rcname_threeq == "NilClass"20841+
return at_threeq == "nil" ? "TRUE" : "FALSE"20842+
end20843+
if rcname_threeq == "Object" || rcname_threeq == "Kernel" || rcname_threeq == "BasicObject"20844+
return "TRUE"20845+
end20846+
end20847+
end20848+
end20849+
2076020850
# Operator on object with custom method2076120851
if is_operator_name(mname) == 12076220852
r = compile_obj_operator_expr(nid, mname, recv)test/class_threeq.rbaddedView file
Loading diff…
test/class_threeq.rb.expectedaddedView file
Loading diff…