Commit 9fdf82e3
9fdf82e37fe5Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): rescue + is_a? walk the class hierarchy
rescue ParentClass => e should catch a raised subclass instance,
and e.is_a?(AncestorClass) should walk the class chain. Pre-fix
sp_exc_is_a in lib/sp_runtime.h did a plain strcmp on the class
name, so raise C where C < B < A < StandardError skipped every
intermediate rescue arm and is_a? returned false for every
ancestor (including the exact class once dispatched).
The codegen now emits two name-keyed helpers when the program contains any RescueNode:
sp_exc_parent_of(name) -> NULL or parent name sp_exc_class_le(name, target) -> walks the parent chain
The parent table has two parts: a built-in chain that mirrors MRI's Exception hierarchy (StandardError -> Exception, RuntimeError -> StandardError, ArgumentError / TypeError / IndexError / etc. -> StandardError, KeyError -> IndexError, ...) plus the user-defined (cname, parent) pairs from @cls_names / @cls_parents. The walker is name-based (not cls_id-based) because the rescue path tracks sp_last_exc_cls as a string and the raised class might be a CRuby built-in that spinel doesn't carry in sp_class_names[].
compile_rescue_chain (both the dispatch and the
chain-into-tmp variant for method-level rescues) now lowers the
explicit-class arm to sp_exc_class_le. The exception-variable
dispatch in compile_call_expr picks up .is_a? / .kind_of?
alongside the existing .class / .message / .to_s /
.inspect / .full_message arms.
emit_class_runtime gains an emit_exc_class_hierarchy_helpers sub-call, hoisted out of the @needs_class_table early-return so small programs with rescue but no Class-value usage still pick up the helper. scan_for_class_hierarchy_usage flips @needs_exc_class_hierarchy on RescueNode discovery so the helper exists by emit time.
Fixes #627
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 9fdf82e37fe5
- Parent
- 89366c49afe9
spinel_codegen.rbmodifiedView file
# @needs_class_table covers the names table; these extend it. @needs_class_parents = 0 @needs_class_ancestors = 0 # Set when `rescue ExplicitClass` or `e.is_a?(C)` (where e was # bound by a rescue) needs to walk a class-name parent chain at # runtime. Gates sp_exc_parent_of + sp_exc_class_le emission. # Issue #627. @needs_exc_class_hierarchy = 0 @cls_ivar_names = "".split(",") @cls_ivar_types = "".split(",") # Per-ivar flag: was the ivar's first scanned write a definite # a definition. if @needs_class_table == 0 emit_raw("static const char *sp_class_to_s(sp_Class c){(void)c;return \"\";}") # Exception-class hierarchy helpers stand alone (don't need # sp_class_names[]), so emit them even when the rest of the # class-runtime cluster is skipped. Issue #627. emit_exc_class_hierarchy_helpers return end emit_raw("/* sp_Class names table */") emit_raw("static sp_PolyArray *sp_class_ancestors_arr(sp_Class c) __attribute__((unused));") emit_raw("static sp_PolyArray *sp_class_ancestors_arr(sp_Class c){sp_PolyArray*r=sp_PolyArray_new();if(c.cls_id<0||c.cls_id>=SP_CLASS_COUNT)return r;mrb_int s=sp_class_ancestors_off[c.cls_id];mrb_int e=sp_class_ancestors_off[c.cls_id+1];for(mrb_int k=s;k<e;k++){sp_Class el={sp_class_ancestors_flat[k]};sp_PolyArray_push(r,sp_box_class(el));}return r;}") end emit_exc_class_hierarchy_helpers if @needs_class_for_poly == 1 # Tier 4: recover the cls_id of a poly value. Each primitive # tag maps to the corresponding built-in cls_id (Integer=9, emit_raw("") end # Exception class-name parent walker. `rescue ParentClass` and # `e.is_a?(ParentClass)` need to know that `class B < A` makes # any `raise B` instance match a `rescue A`. The walker takes # class names (not cls_ids) because the rescue path tracks # sp_last_exc_cls as a string -- the raised class might not be # in the codegen-emitted sp_class_names[] table (e.g. when the # raised class is one of the built-in CRuby exception classes # like StandardError that spinel doesn't track as a user class). # Issue #627. Independent of @needs_class_table since the walker # uses inline strcmp / no sp_class_names lookup. def emit_exc_class_hierarchy_helpers if @needs_exc_class_hierarchy == 0 return end emit_raw("static const char *sp_exc_parent_of(const char *cls) __attribute__((unused));") ep_body = "static const char *sp_exc_parent_of(const char *cls){" # Built-in exception class hierarchy. Matches MRI's # Exception class chain that user code routinely names in # `rescue` / `raise` clauses without ever defining them. builtin_parents = [ ["StandardError", "Exception"], ["RuntimeError", "StandardError"], ["ArgumentError", "StandardError"], ["TypeError", "StandardError"], ["IndexError", "StandardError"], ["KeyError", "IndexError"], ["RangeError", "StandardError"], ["FloatDomainError", "RangeError"], ["NameError", "StandardError"], ["NoMethodError", "NameError"], ["ZeroDivisionError", "StandardError"], ["IOError", "StandardError"], ["EOFError", "IOError"], ["FrozenError", "RuntimeError"], ["NotImplementedError", "ScriptError"], ["ScriptError", "Exception"], ["LoadError", "ScriptError"], ["SyntaxError", "ScriptError"], ["StopIteration", "IndexError"], ["RegexpError", "StandardError"], ["EncodingError", "StandardError"], ["SystemCallError", "StandardError"], ["LocalJumpError", "StandardError"], ["FiberError", "StandardError"], ["Exception", ""], ] bp = 0 while bp < builtin_parents.length cname_bp = builtin_parents[bp][0] pname_bp = builtin_parents[bp][1] if pname_bp == "" ep_body = ep_body + "if(strcmp(cls,\"" + cname_bp + "\")==0)return NULL;" else ep_body = ep_body + "if(strcmp(cls,\"" + cname_bp + "\")==0)return \"" + pname_bp + "\";" end bp = bp + 1 end # User exception classes (and any user class -- the walker is # name-based so it works uniformly). Emit each (cname, parent) # pair from @cls_names + @cls_parents. uc = 0 while uc < @cls_names.length pname_uc = "" if uc < @cls_parents.length pname_uc = @cls_parents[uc] end if pname_uc != "" ep_body = ep_body + "if(strcmp(cls,\"" + @cls_names[uc] + "\")==0)return \"" + pname_uc + "\";" end uc = uc + 1 end ep_body = ep_body + "return NULL;}" emit_raw(ep_body) emit_raw("static mrb_bool sp_exc_class_le(const char *cls, const char *target) __attribute__((unused));") emit_raw("static mrb_bool sp_exc_class_le(const char *cls, const char *target){if(!cls||!target)return FALSE;if(strcmp(cls,target)==0)return TRUE;const char *cur=cls;int depth=0;while(cur&&depth<32){cur=sp_exc_parent_of(cur);if(!cur)return FALSE;if(strcmp(cur,target)==0)return TRUE;depth++;}return FALSE;}") end # unified cls_id space layout. # The emit-side cls_id is global: built-ins occupy # [0 .. builtin_class_count - 1], user classes if nid < 0 return end if @needs_class_parents == 1 && @needs_class_ancestors == 1 && @needs_class_for_poly == 1 t_ch = @nd_type[nid] # Any RescueNode means we'll need the exception-class hierarchy # walker -- the rescue dispatch uses it for `rescue ParentClass`, # and any `e.is_a?(X)` inside the body also needs it. Conservative # (bare `rescue` doesn't strictly need the helper, but the cost # is a couple-hundred bytes of dead code on programs without # is_a? on the bound var). Issue #627. if t_ch == "RescueNode" @needs_exc_class_hierarchy = 1 end if @needs_class_parents == 1 && @needs_class_ancestors == 1 && @needs_class_for_poly == 1 && @needs_exc_class_hierarchy == 1 return end t_ch = @nd_type[nid] if t_ch == "ConstantReadNode" cname_ch = @nd_name[nid] # ConstantReadNode resolving to a class / module / built-in if mname == "full_message" return "sp_sprintf(\"%s: %s\", " + exc_cls + ", lv_" + recv_name + ")" end # `e.is_a?(X)` / `.kind_of?(X)` on an exception binding walks # the class-name parent chain so a `rescue => e` catching a # subclass instance still answers true for the parent class. # Issue #627. The class-snapshot string `exc_cls` carries the # raised class name (set by sp_raise_cls); sp_exc_class_le # walks the parent table emitted from emit_class_runtime. if mname == "is_a?" || mname == "kind_of?" args_id_is = @nd_arguments[nid] if args_id_is >= 0 ais = get_args(args_id_is) if ais.length > 0 arg_name_is = resolve_introspection_arg_name(ais[0]) if arg_name_is != "" @needs_exc_class_hierarchy = 1 return "sp_exc_class_le(" + exc_cls + ", \"" + arg_name_is + "\")" end end end end end end has_type_check = 0 if exc_types.length > 0 has_type_check = 1 # Build condition: sp_exc_is_a(sp_last_exc_cls, "ClassName") # Build condition: sp_exc_class_le(sp_last_exc_cls, "ClassName") # walks the class-name parent chain so `rescue ParentClass` # catches a raised subclass. Issue #627. @needs_exc_class_hierarchy = 1 cond = "" k = 0 while k < exc_types.length if k > 0 cond = cond + " || " end cond = cond + "sp_exc_is_a((const char*)sp_last_exc_cls, \"" + @nd_name[exc_types[k]] + "\")" cond = cond + "sp_exc_class_le((const char*)sp_last_exc_cls, \"" + @nd_name[exc_types[k]] + "\")" k = k + 1 end emit(" if (" + cond + ") {") has_type_check = 0 if exc_types.length > 0 has_type_check = 1 # See compile_rescue_chain for sp_exc_class_le rationale (#627). @needs_exc_class_hierarchy = 1 cond = "" k = 0 while k < exc_types.length if k > 0 cond = cond + " || " end cond = cond + "sp_exc_is_a((const char*)sp_last_exc_cls, \"" + @nd_name[exc_types[k]] + "\")" cond = cond + "sp_exc_class_le((const char*)sp_last_exc_cls, \"" + @nd_name[exc_types[k]] + "\")" k = k + 1 end emit(" if (" + cond + ") {")test/rescue_walks_class_hierarchy.rbaddedView file
Loading diff…
test/rescue_walks_class_hierarchy.rb.expectedaddedView file
Loading diff…