Commit 20239660

20239660ca1bMerged

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

Description

Emit sp_sym_intern whenever codegen lowers a call through it

Symbol#upcase / #downcase on a method-returned symbol used inside string interpolation lowered to sp_sym_to_s(sp_sym_intern(sp_str_upcase(sp_sym_to_s(...)))), but sp_sym_intern was never defined, so the build failed at link: undefined reference to 'sp_sym_intern'. Found via the spinelgems harness (nagiosplugin: "#{name.upcase} #{status.upcase}").

The sp_sym_intern definition is gated on @needs_sym_intern, which analyze sets as a memoized side effect of infer_call_type. For a symbol whose type only settles late in the fixpoint (a branch-poly method return), the .upcase node's type was memoized before the receiver resolved to symbol, so the flag stayed 0 in the IR -- yet codegen still emitted the call. (The other forms -- local symbol, literal, non-interpolated -- happen to infer in an order that sets the flag.)

Re-derive the flag in codegen from the now-stable types, via a generate_code pre-scan (scan_for_sym_intern_usage) run before the gated emit_sym_runtime, mirroring scan_for_method_usage / class-hierarchy pre-scans. Detects Symbol#upcase/#downcase and String#to_sym/ #intern, the two emit sites that lower through sp_sym_intern.

Closes #1355

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

Details

Commit
20239660ca1b
Parent
bc76f889a5c1

spinel_codegen.rbmodifiedView file

@@ -9197,6 +9197,17 @@
91979197 end
91989198 end
91999199 end
9200+ # @needs_sym_intern decides whether emit_sym_runtime defines
9201+ # sp_sym_intern, but analyze sets that flag as a memoized side effect
9202+ # of infer_call_type and misses cases where the type only settles
9203+ # late -- e.g. `Symbol#upcase` on a method-returned symbol inside
9204+ # string interpolation, where codegen still emits the sp_sym_intern
9205+ # call (compile_*_method_expr) and the link then fails on the missing
9206+ # definition. Re-derive the flag here from the now-stable codegen
9207+ # types, before the gated emit. Issue #1355.
9208+ if @needs_sym_intern == 0
9209+ scan_for_sym_intern_usage(@root_id)
9210+ end
92009211 emit_sym_runtime
92019212 emit_ffi_externs
92029213 # Emit program-specific regexp patterns
@@ -14121,6 +14132,43 @@
1412114132 # The codegen-side arms also flip the flag, but most of them
1412214133 # run after emit_class_methods -- the pre-scan is what makes
1412314134 # the gate effective.
14135+ # Pre-scan for constructs codegen lowers through sp_sym_intern:
14136+ # `Symbol#upcase` / `#downcase` (re-intern the case-folded name) and
14137+ # `String#to_sym` / `#intern` (runtime intern). Mirrors the emit-site
14138+ # conditions in the Symbol / String method compilers so the gated
14139+ # sp_sym_intern definition lands whenever a call to it is emitted,
14140+ # independent of analyze's @needs_sym_intern timing. Issue #1355.
14141+ def scan_for_sym_intern_usage(nid)
14142+ if nid < 0
14143+ return
14144+ end
14145+ if @needs_sym_intern == 1
14146+ return
14147+ end
14148+ if @nd_type[nid] == "CallNode"
14149+ msi = @nd_name[nid]
14150+ rsi = @nd_receiver[nid]
14151+ if rsi >= 0
14152+ rtsi = infer_type(rsi)
14153+ if (msi == "upcase" || msi == "downcase") && rtsi == "symbol"
14154+ @needs_sym_intern = 1
14155+ return
14156+ end
14157+ if (msi == "to_sym" || msi == "intern") && base_type(rtsi) == "string"
14158+ @needs_sym_intern = 1
14159+ return
14160+ end
14161+ end
14162+ end
14163+ cs = []
14164+ push_child_ids(nid, cs)
14165+ k = 0
14166+ while k < cs.length
14167+ scan_for_sym_intern_usage(cs[k])
14168+ k = k + 1
14169+ end
14170+ end
14171+
1412414172 def scan_for_method_usage(nid)
1412514173 if nid < 0
1412614174 return