Commit e304b98e

e304b98e78d5Merged

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

Description

Declare rescue-bound locals inside Fiber bodies

rescue => e inside a Fiber.new body binds e as a local of the generated _fiber_body_N function, but the binding was attributed to the enclosing scope rather than the fiber body's own local set, so the body referenced an undeclared lv_e and failed to compile.

Collect the names bound by rescue => e clauses lexically inside the fiber body (without descending into nested closures, which declare their own) and force them to true fiber-body locals typed exception, dropping them from the capture set so a same-named outer variable is not captured in their place. compile_rescue_chain already emits lv_e = sp_exc_new(...); it just needed the local declared and typed.

The lambda/proc variant of the same construct compiles after this but still mis-types the local in infer_type (analyze attributes the binding to the enclosing scope, which infer_type reads from its cache); that deeper layer is tracked separately.

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

Details

Commit
e304b98e78d5
Parent
20239660ca1b

spinel_codegen.rbmodifiedView file

@@ -17934,6 +17934,37 @@
1793417934 scan_fiber_free_vars(body, all_plist, all_names, free_vars, free_var_types)
1793517935 end
1793617936
17937+ # `rescue => e` inside the fiber body binds `e` as a fiber-body local,
17938+ # not a capture of a same-named outer var. scan_fiber_free_vars would
17939+ # otherwise capture it while compile_rescue_chain emits a bare `lv_e`,
17940+ # leaving it undeclared in `_fiber_body_N`. Force such names to true
17941+ # locals (typed `exception`) and drop them from the capture set.
17942+ rb_names_f = "".split(",", -1)
17943+ if body >= 0
17944+ collect_rescue_bound_names(body, rb_names_f)
17945+ end
17946+ rbi = 0
17947+ while rbi < rb_names_f.length
17948+ rbn = rb_names_f[rbi]
17949+ new_fv = "".split(",", -1)
17950+ new_fvt = "".split(",", -1)
17951+ fvj = 0
17952+ while fvj < free_vars.length
17953+ if free_vars[fvj] != rbn
17954+ new_fv.push(free_vars[fvj])
17955+ new_fvt.push(free_var_types[fvj])
17956+ end
17957+ fvj = fvj + 1
17958+ end
17959+ free_vars = new_fv
17960+ free_var_types = new_fvt
17961+ if not_in(rbn, local_names) == 1 && not_in(rbn, all_plist) == 1
17962+ local_names.push(rbn)
17963+ local_types.push("exception")
17964+ end
17965+ rbi = rbi + 1
17966+ end
17967+
1793717968 # Capture `self` when the body references the enclosing instance
1793817969 # (explicit `self`, ivar reads/writes, or no-receiver method calls
1793917970 # that resolve to `self.<m>`). Without this, `_fiber_body_N`'s
@@ -42033,6 +42064,47 @@
4203342064 end
4203442065 end
4203542066
42067+ # Collect the names bound by `rescue => e` clauses lexically inside a
42068+ # proc/lambda/fiber body, without descending into nested closure bodies
42069+ # (those declare their own). compile_rescue_chain emits `lv_<name> = ...`
42070+ # assuming the binding is a declared local of the function it lowers
42071+ # into; but a rescue-bound var is attributed to the *enclosing* scope,
42072+ # so a closure body never declares it and the body function references
42073+ # an undeclared `lv_<name>`. Callers declare these as `exception` locals.
42074+ def collect_rescue_bound_names(nid, acc)
42075+ if nid < 0
42076+ return
42077+ end
42078+ t = @nd_type[nid]
42079+ if t == "LambdaNode"
42080+ return
42081+ end
42082+ if t == "CallNode" && @nd_name[nid] == "new"
42083+ rv = @nd_receiver[nid]
42084+ if rv >= 0 && constructor_class_name(rv) == "Fiber"
42085+ return
42086+ end
42087+ end
42088+ rc = @nd_rescue_clause[nid]
42089+ while rc >= 0
42090+ ref = @nd_reference[rc]
42091+ if ref >= 0
42092+ en = @nd_name[ref]
42093+ if not_in(en, acc) == 1
42094+ acc.push(en)
42095+ end
42096+ end
42097+ rc = @nd_subsequent[rc]
42098+ end
42099+ cs_rb = []
42100+ push_child_ids(nid, cs_rb)
42101+ k = 0
42102+ while k < cs_rb.length
42103+ collect_rescue_bound_names(cs_rb[k], acc)
42104+ k = k + 1
42105+ end
42106+ end
42107+
4203642108 def scan_lambda_ret_types(stmts)
4203742109 stmts.each { |sid|
4203842110 scan_lambda_ret_types_node(sid)