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

@@ -12608,7 +12608,7 @@
1260812608 m == "slice" || m == "fetch" || m == "dig" ||
1260912609 m == "freeze" || m == "frozen?" || m == "dup" || m == "clone" ||
1261012610 m == "hash" || m == "class" || m == "tap" ||
12611- m == "==" || m == "!=" || m == "eql?" || m == "equal?" ||
12611+ m == "==" || m == "!=" || m == "eql?" || m == "equal?" || m == "<=>" ||
1261212612 m == "nil?" || m == "is_a?" || m == "kind_of?" || m == "respond_to?" ||
1261312613 # String / Hash / Integer-shared (kept in sync with the
1261412614 # extension to called_methods_only_on_container_builtins).
@@ -25945,6 +25945,24 @@
2594525945 if mname == "inspect"
2594625946 return "sp_int_to_s(" + rc + ")"
2594725947 end
25948+ # `int <=> other` -- standard 3-way compare. Without this arm
25949+ # the int-recv fallback at compile_call_expr's tail picks a
25950+ # user-defined `<=>` (issue #399) and recurses. Comparable's
25951+ # `<` / `>` / `<=` / `>=` already route to a numeric compare
25952+ # in compile_call_expr; `<=>` itself was missing.
25953+ if mname == "<=>"
25954+ args_id_cmp = @nd_arguments[nid]
25955+ if args_id_cmp >= 0
25956+ a_cmp = get_args(args_id_cmp)
25957+ if a_cmp.length >= 1
25958+ rt_cmp = infer_type(a_cmp[0])
25959+ if rt_cmp == "int" || rt_cmp == "float"
25960+ arg_e = compile_expr(a_cmp[0])
25961+ return "((" + rc + ") < (" + arg_e + ") ? (mrb_int)-1 : ((" + rc + ") > (" + arg_e + ") ? (mrb_int)1 : (mrb_int)0))"
25962+ end
25963+ end
25964+ end
25965+ end
2594825966 if mname == "digits"
2594925967 @needs_int_array = 1
2595025968 @needs_gc = 1