Commit 555a7f47
555a7f47b0bdMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
fix(codegen,runtime): built-in exception .new + raise (#Phase 1C/1D)
Pre-fix RuntimeError.new("msg") (and other built-in exception
classes) failed C compile: analyze inferred the local as
obj_RuntimeError but no sp_RuntimeError struct existed; only
user-defined classes get structs.
Strategy: model built-in exception objects as their message string
plus a side-channel class name -- the same convention spinel's
rescue => e binding uses (find_exc_var_cls). The constructor lowers
to just the message expression; the LV-write codegen pushes the LV
into the @exc_var_* side table. Subsequent e.class / e.message /
e.is_a? / e.inspect dispatch through the existing exc_var arm.
Changes:
analyze:
- New
is_builtin_exception_class_name(name)listing the built-in exception classes (Exception / StandardError / RuntimeError / ArgumentError / etc). - infer_call_type and infer_constructor_type ".new" arms return "string" for built-in exception classes (instead of "obj_"). The LV slot then lowers to const char *.
codegen:
- Mirror
is_builtin_exception_class_namehelper. - compile_constructor_expr arm: built-in exception
.new(msg)lowers to the message expression directly (empty string when no arg). - LV write codegen: when RHS is a built-in exception
.new(...), push the LV name + class name into @exc_var_*. No matching pop -- the binding's class is static for the rest of the program scope. - infer_type CallNode arm (before the cache lookup):
<lv>.class / .message / .to_s / .inspect / .full_messageon an exc-var LV returns "string" so call sites that consume the value (puts, sp_sprintf "%s") pick the string-format path. analyze cached this CallNode as int (default) without knowing about the side channel. - New
emit_raise_one_arg(arg_id)helper for the 1-argraiseshape: raise ClassName -> sp_raise("ClassName") raise BuiltinExc.new(m) -> sp_raise_cls("ExcName", m) (1D) raise lv -> sp_raise_cls(, lv) when lv is in the exc_var table (1D) raise "msg" -> sp_raise(msg) Shared by both stmt-form (compile_control_call_stmt) and expr-form (compile_call_expr) raise arms.
runtime (sp_bigint.c):
- Defensive:
sp_bigint_to_s(NULL)now returns "0" instead of segfaulting. Surfaces when a bigint-returning method's else-only return path leaves the slot at the NULL default; the caller's formatter then prints "0" rather than crashing. Same shape the existing sp_bigint_to_int(NULL) -> 0 defensive guard fixed.
Regression: test/exception_class_new.rb covers .new + .class / .message / .is_a?, raise <exc_object>, raise inline .new, multiple built-in exception classes.
Default tests 449/0/0, promote 448/0/0, optcarrot 59662 unchanged.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 555a7f47b0bd
- Parent
- 2547ef71d9a9
lib/sp_bigint.cmodifiedView file
}const char *sp_bigint_to_s(sp_Bigint *b) { if (!b) return "0"; /* defensive: NULL bigint surfaces as "0" rather than segfault */ sp_bigint_init_ctx(); mpz_t *z = &b->mpz; /* Small number fast path. Two limbs (DIG_SIZE=32) hold up to 64spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/exception_class_new.rbaddedView file
Loading diff…
test/exception_class_new.rb.expectedaddedView file
Loading diff…