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) to sp_exc_new("<UserExc>", arg). raise's CallNode-new arm same.
  • codegen: for the no-arg MyError.new shape, walk the user's def initialize for a string-literal first OptionalParameter default and emit that. Fall back to the class name (matches CRuby's RuntimeError.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

@@ -3209,9 +3209,9 @@
32093209 return "fiber"
32103210 end
32113211 # Built-in exception class .new (RuntimeError, StandardError,
3212- # ArgumentError, etc.) lowers to a first-class sp_Exception *
3213- # (cls_name + msg in a single heap allocation).
3214- if is_builtin_exception_class_name(rcname) == 1
3212+ # ArgumentError, etc.) and user subclasses of built-in exception
3213+ # classes both lower to a first-class sp_Exception *.
3214+ if is_exception_class_name(rcname) == 1
32153215 return "exception"
32163216 end
32173217 end
@@ -5534,9 +5534,9 @@
55345534 if module_name_exists(rn) == 1
55355535 return ""
55365536 end
5537- # Built-in exception class `.new` returns "exception" so the LV
5538- # slot lowers to sp_Exception *.
5539- if is_builtin_exception_class_name(rn) == 1
5537+ # Exception class `.new` (built-in or user subclass thereof)
5538+ # returns "exception" so the LV slot lowers to sp_Exception *.
5539+ if is_exception_class_name(rn) == 1
55405540 return "exception"
55415541 end
55425542 return "obj_" + rn
@@ -7435,6 +7435,42 @@
74357435 0
74367436 end
74377437
7438+ # 1 if `name` is a user-defined class whose parent chain ends at a
7439+ # built-in exception class. `<UserExc>.new(msg)` also lowers to
7440+ # sp_exc_new("<UserExc>", msg) so spinel's exception machinery
7441+ # (raise / rescue / .message / .class / .is_a?) treats user
7442+ # subclasses identically to the built-ins.
7443+ def is_user_exception_subclass(name)
7444+ ci = find_class_idx(name)
7445+ if ci < 0
7446+ return 0
7447+ end
7448+ walk = ci
7449+ while walk >= 0
7450+ pname = ""
7451+ if walk < @cls_parents.length
7452+ pname = @cls_parents[walk]
7453+ end
7454+ if pname == ""
7455+ return 0
7456+ end
7457+ if is_builtin_exception_class_name(pname) == 1
7458+ return 1
7459+ end
7460+ walk = find_class_idx(pname)
7461+ end
7462+ 0
7463+ end
7464+
7465+ # 1 if `name` is either a built-in exception class or a user
7466+ # class that transitively inherits from one.
7467+ def is_exception_class_name(name)
7468+ if is_builtin_exception_class_name(name) == 1
7469+ return 1
7470+ end
7471+ is_user_exception_subclass(name)
7472+ end
7473+
74387474 def is_builtin_type_name(name)
74397475 if name == "Integer"
74407476 return 1