Commit f5f0efa6
f5f0efa6cd70Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified
Description
fix(codegen): super outside initialize + int <=> int recursion
Two issues batched because both surface in the same kind of code (operator dispatch on user classes whose body delegates to a primitive operation).
Fixes #401 — super in a non-initialize instance method emitted
literal 0 instead of a parent-method call. Compiled cleanly when
the method's return type was mrb_int (silent wrong result), failed
C-compile when types diverged. compile_expr now has a SuperNode /
ForwardingSuperNode arm that resolves the parent method via
find_method_owner and emits sp_<owner>_<mname>((sp_<owner> *)self, <args>). ForwardingSuperNode forwards every formal param of the
current method by name; SuperNode uses the explicit arg list.
infer_type also returns the parent method's return type so the
result temp is typed correctly. The constructor-side super (lines
~19062) was already handled and is unchanged.
Fixes #399 — a user-defined def <=>(other) whose body called
<=> on Integer ivars (@n <=> other.n) lowered to a recursive
call into the user's <=> method, casting the int receivers to
sp_V *. Type error + would-be infinite recursion. compile_int_method_expr
now has a <=> arm that emits the standard 3-way compare for
int/int and int/float. <=> is also added to
is_primitive_shared_method so the int-recv fallback at the tail of
compile_call_expr stops picking the user class.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- f5f0efa6cd70
- Parent
- 6b78933bba10
spinel_codegen.rbmodifiedView file
m == "slice" || m == "fetch" || m == "dig" || m == "freeze" || m == "frozen?" || m == "dup" || m == "clone" || m == "hash" || m == "class" || m == "tap" || m == "==" || m == "!=" || m == "eql?" || m == "equal?" || m == "==" || m == "!=" || m == "eql?" || m == "equal?" || m == "<=>" || m == "nil?" || m == "is_a?" || m == "kind_of?" || m == "respond_to?" || # String / Hash / Integer-shared (kept in sync with the # extension to called_methods_only_on_container_builtins). if mname == "inspect" return "sp_int_to_s(" + rc + ")" end # `int <=> other` -- standard 3-way compare. Without this arm # the int-recv fallback at compile_call_expr's tail picks a # user-defined `<=>` (issue #399) and recurses. Comparable's # `<` / `>` / `<=` / `>=` already route to a numeric compare # in compile_call_expr; `<=>` itself was missing. if mname == "<=>" args_id_cmp = @nd_arguments[nid] if args_id_cmp >= 0 a_cmp = get_args(args_id_cmp) if a_cmp.length >= 1 rt_cmp = infer_type(a_cmp[0]) if rt_cmp == "int" || rt_cmp == "float" arg_e = compile_expr(a_cmp[0]) return "((" + rc + ") < (" + arg_e + ") ? (mrb_int)-1 : ((" + rc + ") > (" + arg_e + ") ? (mrb_int)1 : (mrb_int)0))" end end end end if mname == "digits" @needs_int_array = 1 @needs_gc = 1test/super_in_non_initialize.rbaddedView file
Loading diff…
test/super_in_non_initialize.rb.expectedaddedView file
Loading diff…
test/user_cmp_int_ivars.rbaddedView file
Loading diff…
test/user_cmp_int_ivars.rb.expectedaddedView file
Loading diff…