Commit 9dc84040

9dc840407ba1Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~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
9dc840407ba1
Parent
d1105343b4ed

spinel_codegen.rbmodifiedView file

@@ -5081,7 +5081,7 @@
50815081 m == "slice" || m == "fetch" || m == "dig" ||
50825082 m == "freeze" || m == "frozen?" || m == "dup" || m == "clone" ||
50835083 m == "hash" || m == "class" || m == "tap" ||
5084- m == "==" || m == "!=" || m == "eql?" || m == "equal?" ||
5084+ m == "==" || m == "!=" || m == "eql?" || m == "equal?" || m == "<=>" ||
50855085 m == "nil?" || m == "is_a?" || m == "kind_of?" || m == "respond_to?" ||
50865086 # String / Hash / Integer-shared (kept in sync with the
50875087 # extension to called_methods_only_on_container_builtins).
@@ -12766,6 +12766,24 @@
1276612766 if mname == "inspect"
1276712767 return "sp_int_to_s(" + rc + ")"
1276812768 end
12769+ # `int <=> other` -- standard 3-way compare. Without this arm
12770+ # the int-recv fallback at compile_call_expr's tail picks a
12771+ # user-defined `<=>` (issue #399) and recurses. Comparable's
12772+ # `<` / `>` / `<=` / `>=` already route to a numeric compare
12773+ # in compile_call_expr; `<=>` itself was missing.
12774+ if mname == "<=>"
12775+ args_id_cmp = @nd_arguments[nid]
12776+ if args_id_cmp >= 0
12777+ a_cmp = get_args(args_id_cmp)
12778+ if a_cmp.length >= 1
12779+ rt_cmp = infer_type(a_cmp[0])
12780+ if rt_cmp == "int" || rt_cmp == "float"
12781+ arg_e = compile_expr(a_cmp[0])
12782+ return "((" + rc + ") < (" + arg_e + ") ? (mrb_int)-1 : ((" + rc + ") > (" + arg_e + ") ? (mrb_int)1 : (mrb_int)0))"
12783+ end
12784+ end
12785+ end
12786+ end
1276912787 if mname == "digits"
1277012788 @needs_int_array = 1
1277112789 @needs_gc = 1