Commit 1d5adcad
1d5adcad1a59Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(codegen,analyze): user subclasses of Exception lower to sp_Exception
class MyError < StandardError followed by MyError.new("msg") /
raise MyError, "msg" / rescue => e; e.message previously left
the subclass on the regular obj_MyError path. The constructor call
didn't touch the exception machinery, so e.message warned
"cannot resolve call to 'message' on obj_MyError (emitting 0)"
and the program got 0 instead of the message.
Treat any class that transitively inherits from a built-in exception class the same way as the built-in itself:
- analyze: is_exception_class_name(name) wraps is_builtin_* with a parent-chain walk via @cls_parents. infer_call_type / infer_constructor_type return "exception" for those.
- codegen: compile_constructor_expr lowers
<UserExc>.new(arg)tosp_exc_new("<UserExc>", arg). raise's CallNode-new arm same. - codegen: for the no-arg
MyError.newshape, walk the user'sdef initializefor a string-literal first OptionalParameter default and emit that. Fall back to the class name (matches CRuby'sRuntimeError.new.message).
Limitation noted: the user-defined initialize body is bypassed
entirely. The supported pattern is the forwarder
def initialize(msg = "..."); super(msg); end. Bodies that set
unrelated ivars / do real work in initialize are out of scope.
Tests: default 454/0/0, promote 454/0/0, optcarrot checksum 59662.
Closes #683
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 1d5adcad1a59
- Parent
- 1175512df5dd
spinel_analyze.rbmodifiedView file
return "fiber" end # Built-in exception class .new (RuntimeError, StandardError, # ArgumentError, etc.) lowers to a first-class sp_Exception * # (cls_name + msg in a single heap allocation). if is_builtin_exception_class_name(rcname) == 1 # ArgumentError, etc.) and user subclasses of built-in exception # classes both lower to a first-class sp_Exception *. if is_exception_class_name(rcname) == 1 return "exception" end end if module_name_exists(rn) == 1 return "" end # Built-in exception class `.new` returns "exception" so the LV # slot lowers to sp_Exception *. if is_builtin_exception_class_name(rn) == 1 # Exception class `.new` (built-in or user subclass thereof) # returns "exception" so the LV slot lowers to sp_Exception *. if is_exception_class_name(rn) == 1 return "exception" end return "obj_" + rn 0 end # 1 if `name` is a user-defined class whose parent chain ends at a # built-in exception class. `<UserExc>.new(msg)` also lowers to # sp_exc_new("<UserExc>", msg) so spinel's exception machinery # (raise / rescue / .message / .class / .is_a?) treats user # subclasses identically to the built-ins. def is_user_exception_subclass(name) ci = find_class_idx(name) if ci < 0 return 0 end walk = ci while walk >= 0 pname = "" if walk < @cls_parents.length pname = @cls_parents[walk] end if pname == "" return 0 end if is_builtin_exception_class_name(pname) == 1 return 1 end walk = find_class_idx(pname) end 0 end # 1 if `name` is either a built-in exception class or a user # class that transitively inherits from one. def is_exception_class_name(name) if is_builtin_exception_class_name(name) == 1 return 1 end is_user_exception_subclass(name) end def is_builtin_type_name(name) if name == "Integer" return 1spinel_codegen.rbmodifiedView file
Loading diff…
test/exception_user_subclass.rbaddedView file
Loading diff…
test/exception_user_subclass.rb.expectedaddedView file
Loading diff…