Commit d1105343

d1105343b4edMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 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
d1105343b4ed
Parent
cbbcef4706ef

spinel_analyze.rbmodifiedView file

@@ -1828,6 +1828,27 @@
18281828 # the cache once with correct context, infer_type stays
18291829 # uncached during emission.
18301830 t = @nd_type[nid]
1831+ if t == "SuperNode" || t == "ForwardingSuperNode"
1832+ # `super` returns whatever the parent's same-named method
1833+ # returns. Walk to the parent's `find_method_owner`-resolved
1834+ # method and read its return type.
1835+ if @current_class_idx >= 0 && @current_method_name != ""
1836+ parent_name_st = @cls_parents[@current_class_idx]
1837+ if parent_name_st != ""
1838+ parent_ci_st = find_class_idx(parent_name_st)
1839+ if parent_ci_st >= 0
1840+ owner_st = find_method_owner(parent_ci_st, @current_method_name)
1841+ if owner_st != ""
1842+ ret_st = cls_method_return(find_class_idx(owner_st), @current_method_name)
1843+ if ret_st != ""
1844+ return ret_st
1845+ end
1846+ end
1847+ end
1848+ end
1849+ end
1850+ return "int"
1851+ end
18311852 if t == "IntegerNode"
18321853 return "int"
18331854 end