Commit 97b9723b

97b9723bc4c5Merged

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

Description

Add exceptions: begin/rescue, raise, e.message (setjmp model)

  • TY_EXCEPTION for the rescue variable (sp_Exception *); message/to_s/ inspect -> sp_exc_message.
  • begin/rescue lowers to the sp_exc_top/setjmp(sp_exc_stack) frame: try body in the ==0 branch (sp_exc_top--), handler in the else. emit_rescue walks the rescue chain: catch-all when no types or a StandardError-ish type, else exact class-name match (sp_str_eq), binding => e via sp_exc_new and re-raising unmatched.
  • raise: string -> sp_raise; ConstantClass[, msg] -> sp_raise_cls; bare raise re-raises the active rescue's class/msg.
  • locals in a scope containing begin/rescue are declared volatile (value types take a leading qualifier, pointers T * volatile) so values survive longjmp. declare_local rewritten uniformly for this.

Typed/catch-all rescue, division-by-zero, raise with class+message, and a local set in the body then read in the handler all match CRuby. make test: 43 (begin-as-value / ensure / subclass matching still pending).

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
97b9723bc4c5
Parent
ed3efe4f6bef

src/analyze.cmodifiedView file

@@ -142,6 +142,13 @@
142142 if (!strcmp(name, "[]=")) return ty_array_elem(rt);
143143 }
144144
145+ /* exception receiver methods */
146+ if (recv >= 0 && rt == TY_EXCEPTION) {
147+ if (!strcmp(name, "message") || !strcmp(name, "to_s") ||
148+ !strcmp(name, "to_str") || !strcmp(name, "inspect") ||
149+ !strcmp(name, "full_message")) return TY_STRING;
150+ }
151+
145152 /* range receiver methods */
146153 if (recv >= 0 && rt == TY_RANGE) {
147154 if (!strcmp(name, "to_a")) return TY_INT_ARRAY;
@@ -1075,6 +1082,20 @@
10751082 register_locals(c);
10761083 register_attrs(c);
10771084 register_globals_consts(c);
1085+
1086+ /* rescue variables (`rescue => e`) are typed as exception objects */
1087+ for (int id = 0; id < c->nt->count; id++) {
1088+ const char *ty = nt_type(c->nt, id);
1089+ if (!ty || strcmp(ty, "RescueNode")) continue;
1090+ int ref = nt_ref(c->nt, id, "reference");
1091+ if (ref < 0 || strcmp(nt_type(c->nt, ref) ? nt_type(c->nt, ref) : "", "LocalVariableTargetNode")) continue;
1092+ const char *nm = nt_str(c->nt, ref, "name");
1093+ if (!nm) continue;
1094+ LocalVar *lv = scope_local_intern(comp_scope_of(c, ref), nm);
1095+ lv->type = TY_EXCEPTION;
1096+ lv->is_block_param = 1; /* set externally; don't reset in the fixpoint */
1097+ }
1098+
10781099 resolve_parents(c);
10791100 inherit_members(c);
10801101