Commit 6b78933b

6b78933bba10Merged

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

Description

fix(codegen): case <poly> when <lit> lowers via tag-check

Fixes #387.

case <poly_value> when :sym_literal previously lowered to mrb_int _t = lv_poly; (sp_RbVal struct→int copy that doesn't compile) followed by _t == SPS_<sym> (which would compare the union's first field to the sym id even if assignment had compiled). Same gap for any other when-literal kind on a poly predicate.

Three changes in compile_case_stmt / compile_case_return / compile_when_conds:

  1. When pred type is poly, hold the case temp as sp_RbVal (was always mrb_int).
  2. compile_case_return also gets the poly arm + an obj_ pointer arm (compile_case_stmt already had the pointer arm).
  3. compile_when_conds emits per-when-literal:
    • SymbolNode -> (t.tag == SP_TAG_SYM && t.v.i == SPS_<n>)
    • StringNode -> (t.tag == SP_TAG_STR && strcmp(t.v.s, ...) == 0)
    • IntegerNode -> (t.tag == SP_TAG_INT && t.v.i == ...)
    • FloatNode -> (t.tag == SP_TAG_FLT && t.v.f == ...)
    • NilNode -> (t.tag == SP_TAG_NIL)
    • True/FalseNode -> (t.tag == SP_TAG_BOOL && [!]t.v.b)
    • else fallback -> sp_poly_eq(t, box(rhs))

Class-const when Foo is left for the future Class-object work (falls into the fallback arm; reasonable runtime behaviour for the rare cases that hit it).

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

Details

Commit
6b78933bba10
Parent
9543c3ec6d60

spinel_codegen.rbmodifiedView file

@@ -2350,6 +2350,27 @@
23502350 return "void"
23512351 end
23522352 t = @nd_type[nid]
2353+ if t == "SuperNode" || t == "ForwardingSuperNode"
2354+ # `super` returns whatever the parent's same-named method
2355+ # returns. Walk to the parent's `find_method_owner`-resolved
2356+ # method and read its return type.
2357+ if @current_class_idx >= 0 && @current_method_name != ""
2358+ parent_name_st = @cls_parents[@current_class_idx]
2359+ if parent_name_st != ""
2360+ parent_ci_st = find_class_idx(parent_name_st)
2361+ if parent_ci_st >= 0
2362+ owner_st = find_method_owner(parent_ci_st, @current_method_name)
2363+ if owner_st != ""
2364+ ret_st = cls_method_return(find_class_idx(owner_st), @current_method_name)
2365+ if ret_st != ""
2366+ return ret_st
2367+ end
2368+ end
2369+ end
2370+ end
2371+ end
2372+ return "int"
2373+ end
23532374 if t == "IntegerNode"
23542375 return "int"
23552376 end
@@ -22163,9 +22184,64 @@
2216322184 end
2216422185 return tmp
2216522186 end
22187+ if t == "SuperNode" || t == "ForwardingSuperNode"
22188+ return compile_super_expr(nid, t)
22189+ end
2216622190 "0"
2216722191 end
2216822192
22193+ # `super` / `super(...)` in a non-`initialize` instance method.
22194+ # Issue #401 — without this the call fell through to `0`.
22195+ # `super` inside `initialize` is handled in the constructor synth
22196+ # path (lines ~19041) and doesn't reach compile_expr.
22197+ def compile_super_expr(nid, t)
22198+ if @current_class_idx < 0 || @current_method_name == ""
22199+ return "0"
22200+ end
22201+ parent_name = @cls_parents[@current_class_idx]
22202+ if parent_name == ""
22203+ return "0"
22204+ end
22205+ parent_ci = find_class_idx(parent_name)
22206+ if parent_ci < 0
22207+ return "0"
22208+ end
22209+ mname_su = @current_method_name
22210+ owner_su = find_method_owner(parent_ci, mname_su)
22211+ if owner_su == ""
22212+ return "0"
22213+ end
22214+ cast_self = "(sp_" + owner_su + " *)self"
22215+ args_c = ""
22216+ if t == "ForwardingSuperNode"
22217+ # Forward every formal param of the current method by name.
22218+ cur_params = @cls_meth_params[@current_class_idx].split("|")
22219+ midx_cur = cls_find_method_direct(@current_class_idx, mname_su)
22220+ if midx_cur >= 0 && midx_cur < cur_params.length
22221+ pn_list = cur_params[midx_cur].split(",")
22222+ pi_su = 0
22223+ while pi_su < pn_list.length
22224+ if pn_list[pi_su] != ""
22225+ args_c = args_c + ", lv_" + pn_list[pi_su]
22226+ end
22227+ pi_su = pi_su + 1
22228+ end
22229+ end
22230+ else
22231+ # SuperNode: explicit `super(arg1, arg2, ...)` — use the arg list.
22232+ args_id_su = @nd_arguments[nid]
22233+ if args_id_su >= 0
22234+ a_ids = get_args(args_id_su)
22235+ ai_su = 0
22236+ while ai_su < a_ids.length
22237+ args_c = args_c + ", " + compile_expr(a_ids[ai_su])
22238+ ai_su = ai_su + 1
22239+ end
22240+ end
22241+ end
22242+ "sp_" + owner_su + "_" + sanitize_name(mname_su) + "(" + cast_self + args_c + ")"
22243+ end
22244+
2216922245 def c_string_literal(s)
2217022246 # Input `s` is the runtime Ruby string content (already-cooked: any
2217122247 # backslash in `s` is a literal backslash, NOT an escape introducer).
@@ -40455,6 +40531,14 @@
4045540531 tmp = new_temp
4045640532 if pred_type == "string"
4045740533 emit(" const char *" + tmp + " = " + pred_val + ";")
40534+ elsif pred_type == "poly"
40535+ # `case <poly> when ...` -- mirror compile_case_stmt's
40536+ # poly arm. Issue #387.
40537+ emit(" sp_RbVal " + tmp + " = " + pred_val + ";")
40538+ elsif is_obj_type(pred_type) == 1
40539+ bt_cr = base_type(pred_type)
40540+ cn_cr = bt_cr[4, bt_cr.length - 4]
40541+ emit(" sp_" + cn_cr + " *" + tmp + " = " + pred_val + ";")
4045840542 else
4045940543 emit(" mrb_int " + tmp + " = " + pred_val + ";")
4046040544 end