Commit 447f4690

447f469047b7Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): container-op == nil checks lower to CRuby-equivalent runtime predicates

Adam Doppelt's #562. Nine <container_op> == nil predicates that CRuby evaluates as true silently emit false because spinel's typed-container returns can't represent nil at the value level: sp_IntArray_get returns 0 on out-of-range, sp_StrIntHash_get returns 0 on missing key, etc. The existing int == nil short-circuit in compile_eq folded the predicate to FALSE.

New compile_container_op_nil_check recognizes specific call shapes on the lhs of == nil and emits the CRuby-equivalent runtime predicate before the int-vs-nil short-circuit fires:

  • arr[i] / arr.delete_at(i) -> (i < 0 || i >= len)
  • arr.first / arr.last / arr.pop -> (len == 0)
  • arr.find_index(x) -> !arr.include?(x)
  • hash[k] -> !hash.has_key?(k)
  • regex.match(s) -> !regex.match?(s)

Plus Array#instance_of?(Array) / Hash#instance_of?(Hash) fold to TRUE at compile time: every Array variant (int_array / str_array / float_array / sym_array / ptr_array / poly_array) is conceptually Array, and every Hash variant is Hash. Without this static fold, the existing user-class instance_of? path returned FALSE because is_obj_type rejected typed-container recv shapes.

Closes #562

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

Details

Commit
447f469047b7
Parent
e9b30d6719d5

spinel_codegen.rbmodifiedView file

@@ -18621,16 +18621,26 @@
1862118621 # for value-type cname the runtime is always exactly cname and
1862218622 # the cname == arg0 check collapses back to static TRUE.
1862318623 if mname == "instance_of?"
18624+ arg0 = ""
18625+ args_id = @nd_arguments[nid]
18626+ if args_id >= 0
18627+ a = get_args(args_id)
18628+ if a.length > 0
18629+ arg0 = resolve_introspection_arg_name(a[0])
18630+ end
18631+ end
18632+ # Built-in container classes: every Array variant
18633+ # (int_array / str_array / etc.) is conceptually `Array`
18634+ # in CRuby; every Hash variant is `Hash`. Static recv-type
18635+ # knowledge folds the check at compile time. Issue #562.
18636+ if arg0 == "Array" && is_array_type(recv_type) == 1
18637+ return "TRUE"
18638+ end
18639+ if arg0 == "Hash" && is_hash_type(recv_type) == 1
18640+ return "TRUE"
18641+ end
1862418642 if is_obj_type(recv_type) == 1
1862518643 cname = recv_type[4, recv_type.length - 4]
18626- arg0 = ""
18627- args_id = @nd_arguments[nid]
18628- if args_id >= 0
18629- a = get_args(args_id)
18630- if a.length > 0
18631- arg0 = resolve_introspection_arg_name(a[0])
18632- end
18633- end
1863418644 target_ci = find_class_idx(arg0)
1863518645 if target_ci >= 0 && (cname == arg0 || is_descendant(arg0, cname) == 1)
1863618646 if is_value_type_class(cname) == 1
@@ -20357,6 +20367,171 @@
2035720367 "(" + str_c + "[(mrb_int)" + idx_c + "] " + c_op + " '" + ch + "')"
2035820368 end
2035920369
20370+ # Helper for compile_eq's `<container_op> == nil` shape.
20371+ # Returns the C truthy expression for the nil-on-miss check
20372+ # (negated when op is "!="), or "" when the shape isn't
20373+ # recognized so compile_eq falls through to its existing
20374+ # type-strict short-circuits. Issue #562.
20375+ def compile_container_op_nil_check(recv_nid, op)
20376+ mname = @nd_name[recv_nid]
20377+ inner_recv = @nd_receiver[recv_nid]
20378+ if inner_recv < 0
20379+ return ""
20380+ end
20381+ inner_t = infer_type(inner_recv)
20382+ inner_c = compile_expr(inner_recv)
20383+ args_id_c = @nd_arguments[recv_nid]
20384+ # arr[i] == nil -> (i < 0 || i >= len).
20385+ if mname == "[]" && is_array_type(inner_t) == 1 && args_id_c >= 0
20386+ a_c = get_args(args_id_c)
20387+ if a_c.length >= 1
20388+ idx_c = compile_expr(a_c[0])
20389+ len_fn = array_length_fn(inner_t)
20390+ if len_fn != ""
20391+ chk = "(" + idx_c + " < 0 || " + idx_c + " >= " + len_fn + "(" + inner_c + "))"
20392+ return op == "==" ? chk : "(!" + chk + ")"
20393+ end
20394+ end
20395+ end
20396+ # hash[k] == nil -> !has_key?(k). For int-leaf hashes the
20397+ # stored value 0 IS distinguishable from missing key via
20398+ # has_key, so the check is sound (Hash#[] returns nil on
20399+ # miss in CRuby).
20400+ if mname == "[]" && is_hash_type(inner_t) == 1 && args_id_c >= 0
20401+ a_c = get_args(args_id_c)
20402+ if a_c.length >= 1
20403+ has_key_fn = hash_has_key_fn(inner_t)
20404+ if has_key_fn != ""
20405+ key_c = hash_key_arg_for(inner_t, a_c[0])
20406+ chk = "(!" + has_key_fn + "(" + inner_c + ", " + key_c + "))"
20407+ return op == "==" ? chk : "(!" + chk + ")"
20408+ end
20409+ end
20410+ end
20411+ # arr.first / arr.last / arr.pop == nil -> (len == 0).
20412+ # pop on an empty array is a no-op in CRuby (returns nil),
20413+ # so the equality check pre-mutation answer is correct.
20414+ if (mname == "first" || mname == "last" || mname == "pop") && is_array_type(inner_t) == 1
20415+ len_fn = array_length_fn(inner_t)
20416+ if len_fn != "" && (args_id_c < 0 || get_args(args_id_c).length == 0)
20417+ chk = "(" + len_fn + "(" + inner_c + ") == 0)"
20418+ return op == "==" ? chk : "(!" + chk + ")"
20419+ end
20420+ end
20421+ # arr.delete_at(n) == nil -> (n < 0 || n >= len). Checked
20422+ # BEFORE the mutation; CRuby's delete_at returns nil on
20423+ # out-of-range without mutating.
20424+ if mname == "delete_at" && is_array_type(inner_t) == 1 && args_id_c >= 0
20425+ a_c = get_args(args_id_c)
20426+ if a_c.length >= 1
20427+ idx_c = compile_expr(a_c[0])
20428+ len_fn = array_length_fn(inner_t)
20429+ if len_fn != ""
20430+ chk = "(" + idx_c + " < 0 || " + idx_c + " >= " + len_fn + "(" + inner_c + "))"
20431+ return op == "==" ? chk : "(!" + chk + ")"
20432+ end
20433+ end
20434+ end
20435+ # arr.find_index(x) == nil -> !arr.include?(x).
20436+ if mname == "find_index" && is_array_type(inner_t) == 1 && args_id_c >= 0
20437+ a_c = get_args(args_id_c)
20438+ if a_c.length >= 1
20439+ inc_fn = array_include_fn(inner_t)
20440+ if inc_fn != ""
20441+ val_c = compile_expr(a_c[0])
20442+ chk = "(!" + inc_fn + "(" + inner_c + ", " + val_c + "))"
20443+ return op == "==" ? chk : "(!" + chk + ")"
20444+ end
20445+ end
20446+ end
20447+ # regex.match(str) == nil -> !regex.match?(str).
20448+ if mname == "match" && args_id_c >= 0
20449+ rpat = regex_pat_c_expr(inner_recv)
20450+ if rpat != ""
20451+ a_c = get_args(args_id_c)
20452+ if a_c.length >= 1
20453+ chk = "(!sp_re_match_p(" + rpat + ", " + compile_expr(a_c[0]) + "))"
20454+ return op == "==" ? chk : "(!" + chk + ")"
20455+ end
20456+ end
20457+ end
20458+ ""
20459+ end
20460+
20461+ # Helper: array-type -> length helper fn name.
20462+ def array_length_fn(t)
20463+ if t == "int_array" || t == "sym_array"
20464+ return "sp_IntArray_length"
20465+ end
20466+ if t == "float_array"
20467+ return "sp_FloatArray_length"
20468+ end
20469+ if t == "str_array"
20470+ return "sp_StrArray_length"
20471+ end
20472+ if t == "poly_array"
20473+ return "sp_PolyArray_length"
20474+ end
20475+ if t.end_with?("_ptr_array")
20476+ return "sp_PtrArray_length"
20477+ end
20478+ ""
20479+ end
20480+
20481+ def array_include_fn(t)
20482+ if t == "int_array" || t == "sym_array"
20483+ return "sp_IntArray_include"
20484+ end
20485+ if t == "float_array"
20486+ return "sp_FloatArray_include"
20487+ end
20488+ if t == "str_array"
20489+ return "sp_StrArray_include"
20490+ end
20491+ if t == "poly_array"
20492+ return "sp_PolyArray_include"
20493+ end
20494+ ""
20495+ end
20496+
20497+ def hash_has_key_fn(t)
20498+ if t == "str_int_hash"
20499+ return "sp_StrIntHash_has_key"
20500+ end
20501+ if t == "str_str_hash"
20502+ return "sp_StrStrHash_has_key"
20503+ end
20504+ if t == "str_poly_hash"
20505+ return "sp_StrPolyHash_has_key"
20506+ end
20507+ if t == "sym_int_hash"
20508+ return "sp_SymIntHash_has_key"
20509+ end
20510+ if t == "sym_str_hash"
20511+ return "sp_SymStrHash_has_key"
20512+ end
20513+ if t == "sym_poly_hash"
20514+ return "sp_SymPolyHash_has_key"
20515+ end
20516+ if t == "int_str_hash"
20517+ return "sp_IntStrHash_has_key"
20518+ end
20519+ if t == "poly_poly_hash"
20520+ return "sp_PolyPolyHash_has_key"
20521+ end
20522+ ""
20523+ end
20524+
20525+ def hash_key_arg_for(t, arg_id)
20526+ if t == "poly_poly_hash"
20527+ return box_expr_to_poly(arg_id)
20528+ end
20529+ if t.start_with?("str_")
20530+ return compile_expr_as_string(arg_id)
20531+ end
20532+ compile_expr(arg_id)
20533+ end
20534+
2036020535 def compile_eq(nid, op)
2036120536 recv = @nd_receiver[nid]
2036220537 lt = infer_type(recv)
@@ -20372,6 +20547,17 @@
2037220547 if arg_id >= 0
2037320548 at = infer_type(arg_id)
2037420549 end
20550+ # Container-op `== nil` / `!= nil` shapes that the typed
20551+ # returns can't represent at the value level (sp_IntArray_get
20552+ # returns 0 on out-of-range, not nil). Recognize the call
20553+ # shape on the lhs and emit a length / has_key check
20554+ # equivalent to CRuby's nil-on-miss semantics. Issue #562.
20555+ if at == "nil" && recv >= 0 && @nd_type[recv] == "CallNode"
20556+ r2 = compile_container_op_nil_check(recv, op)
20557+ if r2 != ""
20558+ return r2
20559+ end
20560+ end
2037520561 # sp_Class equality via field compare.
2037620562 # Class is a value-type struct so plain `==` on a struct
2037720563 # isn't valid C; route through the sp_class_eq runtime helper.