Commit 9c62cd3d

9c62cd3d05a0Merged

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

Description

feat(codegen,analyze): built-in exception .new returns sp_Exception * (Phase 2.2)

Promote built-in exception .new from the Phase 1 (const char* msg

  • side-channel cls name) lowering to a first-class sp_Exception *.
  • analyze: infer_call_type / infer_constructor_type emit "exception" for <BuiltinExc>.new(...). infer_call_type also returns the right shape for .class / .message / .to_s / .inspect / .full_message (string), .backtrace (nil), and the type-checking predicates .is_a? / .kind_of? / .instance_of? (bool) when the receiver types as exception. The recv-type check is gated by the method name first so non-exception calls don't pay the recursion.

  • codegen: new c_type/is_nullable_pointer_type/c_default_val arms lower "exception" to sp_Exception * with NULL default. compile_constructor_expr emits sp_exc_new("<Cls>", <msg>). A new recv_type == "exception" dispatch arm lowers .class/.message/etc. emit_raise_one_arg detects sp_Exception * LVs and CallNode results typing as exception, emitting sp_raise_exc directly.

  • runtime: sp_exc_class_name / sp_exc_message / sp_raise_exc accept volatile sp_Exception * so callers can pass volatile LV slots (required across setjmp) without -Wdiscarded-qualifiers.

Tests: default 450/0/0, promote 450/0/0, optcarrot checksum 59662.

The rescue => e side-channel (@exc_var_names / find_exc_var_cls) is still in place; Phase 2.3 will migrate it onto sp_Exception *.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
9c62cd3d05a0
Parent
31bcc60f50e7

lib/sp_runtime.hmodifiedView file

@@ -2199,9 +2199,21 @@
21992199 e->msg = msg ? msg : sp_str_empty;
22002200 return e;
22012201 }
2202-static const char *sp_exc_class_name(sp_Exception *e) { return e ? e->cls_name : "RuntimeError"; }
2203-static const char *sp_exc_message(sp_Exception *e) { return e ? e->msg : sp_str_empty; }
2204-static void sp_raise_exc(sp_Exception *e) {
2202+/* Accept `volatile` pointers: LV slots holding sp_Exception * are
2203+ declared volatile when they live across setjmp, so callers may
2204+ pass volatile-qualified pointers in. The pointee itself isn't
2205+ volatile (cls_name/msg are stable post-construction), so we
2206+ strip volatile internally for one access. */
2207+static const char *sp_exc_class_name(volatile sp_Exception *ve) {
2208+ sp_Exception *e = (sp_Exception *)ve;
2209+ return e ? e->cls_name : "RuntimeError";
2210+}
2211+static const char *sp_exc_message(volatile sp_Exception *ve) {
2212+ sp_Exception *e = (sp_Exception *)ve;
2213+ return e ? e->msg : sp_str_empty;
2214+}
2215+static void sp_raise_exc(volatile sp_Exception *ve) {
2216+ sp_Exception *e = (sp_Exception *)ve;
22052217 if (!e) sp_raise("(nil exception)");
22062218 sp_raise_cls(e->cls_name, e->msg);
22072219 }