Commit 2b5da1b0

2b5da1b0fd56Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Bind super() params for user exception subclass messages (fixes #1048)

raise E.new(a, b) / e = E.new(a, b) for a user exception subclass whose #initialize does super("... #{a} ... #{b} ...") reported only the first construction arg as the message, dropping the interpolation (and any literal text the user wrote). The exception-construction paths took compile_expr(first_arg) directly instead of evaluating the super message expression.

Add exc_super_msg: locate the subclass's #initialize, find its super(<msg>) call, and emit <msg> with the init params bound to the construction args. Binding uses a fresh temp per param plus the global inline rename map (so compile_expr resolves param reads, including those inside interpolation, to the temps) and a scoped declare_var of the source names (so interpolation's infer_type picks %s vs %lld correctly). No class/method context switch: the helper is gated to super args that reference only params (no self/ivars/method calls), with the existing first-arg / default behavior as the fallback. Wired into both the raise E.new(...) and assigned-E.new(...) paths.

Only params actually referenced in the super message are bound, so a construction arg the message ignores is never compiled -- otherwise a non-first arg with an unresolvable call would surface a "cannot resolve" warning the old first-arg-only path hid.

The helper takes the already-extracted construction-arg list (an int array) rather than the construction node id: passing a bare node-id int let analyze's param-type fixpoint unify it with the callers' nid and flip nid (hence compile_expr/infer_type) to string, corrupting the self-hosted build. Plain super(msg) and default-valued params are covered as no-regression cases.

Fixes #1048

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

Details

Commit
2b5da1b0fd56
Parent
4a0d0e1104dd

spinel_codegen.rbmodifiedView file

@@ -1043,6 +1043,211 @@
10431043 "(&(\"\\xff\" \"" + cls_name_to_ruby(cname) + "\")[1])"
10441044 end
10451045
1046+ # `raise E.new(a, b)` / `E.new(a, b)` for a user exception subclass
1047+ # whose `#initialize` calls `super(<msg>)`: the user's message is
1048+ # `<msg>` evaluated with the init params bound to the construction
1049+ # args, NOT just the first construction arg. Returns the C expr for
1050+ # that message, or "" to fall back to first-arg behavior.
1051+ #
1052+ # Binding strategy (see fiber_var_ref): declare a fresh temp per init
1053+ # param, set the global @inline_rename_map so compile_expr resolves
1054+ # param reads (including those inside string interpolation) to those
1055+ # temps, compile the super arg, materialize into a const-char temp.
1056+ # No class/method context switch -- gated to super args that touch
1057+ # only params (no self/ivars/method calls), so self never matters.
1058+ def find_super_node(nid)
1059+ if nid < 0
1060+ return -1
1061+ end
1062+ t = @nd_type[nid]
1063+ if t == "SuperNode"
1064+ return nid
1065+ end
1066+ # Don't descend into nested defs/classes -- a `super` there belongs
1067+ # to a different method.
1068+ if t == "DefNode" || t == "ClassNode" || t == "ModuleNode" || t == "SingletonClassNode"
1069+ return -1
1070+ end
1071+ cs = []
1072+ push_child_ids(nid, cs)
1073+ k = 0
1074+ while k < cs.length
1075+ r = find_super_node(cs[k])
1076+ if r >= 0
1077+ return r
1078+ end
1079+ k = k + 1
1080+ end
1081+ -1
1082+ end
1083+
1084+ def super_arg_refs_only_params(nid, pnames)
1085+ if nid < 0
1086+ return 1
1087+ end
1088+ t = @nd_type[nid]
1089+ if t == "CallNode" || t == "SelfNode" || t == "YieldNode"
1090+ return 0
1091+ end
1092+ if t == "InstanceVariableReadNode" || t == "InstanceVariableWriteNode" || t == "GlobalVariableReadNode"
1093+ return 0
1094+ end
1095+ if t == "ConstantReadNode" || t == "ConstantPathNode"
1096+ return 0
1097+ end
1098+ if t == "LocalVariableReadNode"
1099+ if not_in(@nd_name[nid], pnames) == 1
1100+ return 0
1101+ end
1102+ end
1103+ cs = []
1104+ push_child_ids(nid, cs)
1105+ k = 0
1106+ while k < cs.length
1107+ if super_arg_refs_only_params(cs[k], pnames) == 0
1108+ return 0
1109+ end
1110+ k = k + 1
1111+ end
1112+ 1
1113+ end
1114+
1115+ # Collect the init-param names actually referenced in the super
1116+ # message expression. Only these need binding, so we never compile a
1117+ # construction arg the message doesn't use -- avoiding a spurious
1118+ # "cannot resolve" on, say, a 2nd arg that the old first-arg-only path
1119+ # silently dropped.
1120+ def collect_super_arg_param_refs(nid, pnames, acc)
1121+ if nid < 0
1122+ return
1123+ end
1124+ if @nd_type[nid] == "LocalVariableReadNode"
1125+ nm = @nd_name[nid]
1126+ if not_in(nm, pnames) == 0 && not_in(nm, acc) == 1
1127+ acc.push(nm)
1128+ end
1129+ end
1130+ cs = []
1131+ push_child_ids(nid, cs)
1132+ k = 0
1133+ while k < cs.length
1134+ collect_super_arg_param_refs(cs[k], pnames, acc)
1135+ k = k + 1
1136+ end
1137+ end
1138+
1139+ def exc_param_default_val(def_id, name)
1140+ params_id = @nd_parameters[def_id]
1141+ if params_id >= 0
1142+ opts = parse_id_list(@nd_optionals[params_id])
1143+ oi = 0
1144+ while oi < opts.length
1145+ if @nd_name[opts[oi]] == name
1146+ dv = @nd_expression[opts[oi]]
1147+ if dv >= 0
1148+ return compile_expr(dv)
1149+ end
1150+ end
1151+ oi = oi + 1
1152+ end
1153+ end
1154+ ""
1155+ end
1156+
1157+ def exc_super_msg(cname, ctor_args)
1158+ def_id = find_initialize_def_for_class(0, cname)
1159+ if def_id < 0
1160+ return ""
1161+ end
1162+ body = @nd_body[def_id]
1163+ if body < 0
1164+ return ""
1165+ end
1166+ sup = find_super_node(body)
1167+ if sup < 0
1168+ return ""
1169+ end
1170+ sargs_id = @nd_arguments[sup]
1171+ if sargs_id < 0
1172+ return ""
1173+ end
1174+ sargs = get_args(sargs_id)
1175+ if sargs.length < 1
1176+ return ""
1177+ end
1178+ super_arg = sargs[0]
1179+ ci = find_class_idx(cname)
1180+ if ci < 0
1181+ return ""
1182+ end
1183+ midx = cls_find_method_direct(ci, "initialize")
1184+ if midx < 0
1185+ return ""
1186+ end
1187+ pnames = cls_meth_pnames_get(ci, midx)
1188+ ptypes = cls_meth_ptypes_get(ci, midx)
1189+ if super_arg_refs_only_params(super_arg, pnames) == 0
1190+ return ""
1191+ end
1192+ # ctor_args is the already-extracted construction-arg node-id list
1193+ # (an int array). Passing the extracted list rather than the bare
1194+ # construction node id keeps analyze from unifying a node-id param
1195+ # with the caller's `nid` and corrupting its type.
1196+ cargs = ctor_args
1197+ refset = "".split(",", -1)
1198+ collect_super_arg_param_refs(super_arg, pnames, refset)
1199+ suffix = new_temp
1200+ mfrom = "".split(",", -1)
1201+ mto = "".split(",", -1)
1202+ # Isolate the binding's scope entries so the param names/types we
1203+ # register for find_var_type (used by interpolation's infer_type)
1204+ # don't leak into the construction site's scope.
1205+ push_scope
1206+ pk = 0
1207+ while pk < pnames.length
1208+ # Bind only params the super message actually references, so a
1209+ # construction arg the message ignores is never compiled (it would
1210+ # otherwise surface a "cannot resolve" the old first-arg path hid).
1211+ if not_in(pnames[pk], refset) == 0
1212+ pt = "string"
1213+ if pk < ptypes.length && ptypes[pk] != ""
1214+ pt = ptypes[pk]
1215+ end
1216+ tnm = pnames[pk] + suffix
1217+ val = ""
1218+ if pk < cargs.length && cargs[pk] >= 0
1219+ val = compile_expr(cargs[pk])
1220+ else
1221+ val = exc_param_default_val(def_id, pnames[pk])
1222+ end
1223+ if val == ""
1224+ pop_scope
1225+ return ""
1226+ end
1227+ emit(" " + c_type(pt) + " lv_" + tnm + " = " + val + ";")
1228+ # tnm: declared type of the actual C temp (compile_expr unbox path).
1229+ # pname: the source name, so infer_type's find_var_type resolves the
1230+ # param type when picking interpolation's %s vs %lld.
1231+ declare_var(tnm, pt)
1232+ declare_var(pnames[pk], pt)
1233+ mfrom.push(pnames[pk])
1234+ mto.push(tnm)
1235+ end
1236+ pk = pk + 1
1237+ end
1238+ saved_f = @inline_rename_map_from
1239+ saved_t = @inline_rename_map_to
1240+ @inline_rename_map_from = mfrom
1241+ @inline_rename_map_to = mto
1242+ msg = compile_expr(super_arg)
1243+ @inline_rename_map_from = saved_f
1244+ @inline_rename_map_to = saved_t
1245+ pop_scope
1246+ em = new_temp
1247+ emit(" const char *" + em + " = " + msg + ";")
1248+ em
1249+ end
1250+
10461251 # Reverse the `_`-as-module-separator mangling applied by
10471252 # collect_class_with_prefix. Walks the longest prefix that matches
10481253 # a registered module (recursively, so a nested `Outer_Inner`
@@ -1387,6 +1592,11 @@
13871592 cname_inner = @nd_name[r_inner]
13881593 if is_exception_class_name(cname_inner) == 1
13891594 @needs_exc_class_hierarchy = 1
1595+ super_msg_inner = exc_super_msg(cname_inner, get_args(@nd_arguments[arg_id]))
1596+ if super_msg_inner != ""
1597+ emit(" sp_raise_cls(\"" + cname_inner + "\", " + super_msg_inner + ");")
1598+ return
1599+ end
13901600 args_id_inner = @nd_arguments[arg_id]
13911601 if args_id_inner >= 0
13921602 aa_inner = get_args(args_id_inner)
@@ -20013,12 +20223,14 @@
2001320223 if is_exception_class_name(cname) == 1
2001420224 @needs_exc_class_hierarchy = 1
2001520225 @needs_setjmp = 1 # pulls in sp_exc_* runtime helpers
20016- msg_expr_be = ""
20017- args_id_be = @nd_arguments[nid]
20018- if args_id_be >= 0
20019- aa_be = get_args(args_id_be)
20020- if aa_be.length >= 1
20021- msg_expr_be = compile_expr(aa_be[0])
20226+ msg_expr_be = exc_super_msg(cname, get_args(@nd_arguments[nid]))
20227+ if msg_expr_be == ""
20228+ args_id_be = @nd_arguments[nid]
20229+ if args_id_be >= 0
20230+ aa_be = get_args(args_id_be)
20231+ if aa_be.length >= 1
20232+ msg_expr_be = compile_expr(aa_be[0])
20233+ end
2002220234 end
2002320235 end
2002420236 if msg_expr_be == ""