Commit e383ca60
e383ca609379Merged
Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
feat(codegen): Symbol#upcase and Symbol#downcase
Bundle the two siblings — same dispatch arm in compile_symbol_method_expr, same return-type entry in infer_method_call_return_type, same @needs_sym_intern feature flag. Splitting them into two commits would duplicate the entire commit body for a one-line code change each.
Both methods lower to: sp_sym_intern(sp_str_(sp_sym_to_s(rc)))
That is, look up the symbol's name string, upper- or lower-case it
through the existing sp_str_upcase / sp_str_downcase helpers, then
re-intern through sp_sym_intern's dynamic pool. The case-changed
string is freshly allocated by sp_str_, so sp_sym_intern's
strcmp-loop catches both pre-existing global-table symbols and any
prior dynamic-pool entry — round-trips like
:Hello.upcase == :HELLO therefore hold.
infer_method_call_return_type previously returned "string" for any upcase / downcase call; that was correct for String receivers but wrong for Symbol. Both arms now branch on receiver type — Symbol returns "symbol", everything else returns "string" as before.
@needs_sym_intern is set in scan_features when upcase / downcase is called on a Symbol receiver, mirroring the existing condition for to_sym / intern. Without this the dynamic-pool sp_sym_intern emission is omitted and the link fails on the call site.
Test exercises:
- upcase: lower input, already-upper, mixed-case, single char, underscore-only (case-insensitive char passthrough)
- downcase: same coverage as upcase
- chain round-trip both directions
- symbol-equality across re-intern (
:Hello.upcase == :HELLOetc.)
Out of scope: Symbol#swapcase, Symbol#capitalize. Both are direct mirrors via the corresponding sp_str_* helpers; ship as follow-ups if needed.
Bootstrap: gen2.c == gen3.c. Tests: 207 pass.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- e383ca609379
- Parent
- 02005f26deba
spinel_codegen.rbmodifiedView file
if mname == "round" return "int" end if mname == "upcase" return "string" end if mname == "downcase" if mname == "upcase" || mname == "downcase" if recv >= 0 && infer_type(recv) == "symbol" return "symbol" end return "string" end if mname == "swapcase" end end end # `:foo.upcase` / `:foo.downcase` lower to sp_str_upcase / # sp_str_downcase on the symbol's name string and re-intern via # sp_sym_intern. Mark the dynamic-pool path so it gets emitted. if mname == "upcase" || mname == "downcase" if @nd_receiver[nid] >= 0 if infer_type(@nd_receiver[nid]) == "symbol" @needs_sym_intern = 1 end end end # Methods that need string helpers only when receiver is string if mname == "+" || mname == "*" || mname == "reverse" if @nd_receiver[nid] >= 0 if mname == "to_sym" || mname == "intern" return rc end # Symbol#upcase / Symbol#downcase: lower-case via the str case helper # then re-intern. Naming convention `sp_str_<mname>` lets one branch # handle both — and slots in cleanly for capitalize/swapcase later. if mname == "upcase" || mname == "downcase" return "sp_sym_intern(sp_str_" + mname + "(sp_sym_to_s(" + rc + ")))" end if mname == "inspect" return "sp_str_concat(\":\", sp_sym_to_s(" + rc + "))" endtest/symbol_upcase_downcase.rbaddedView file
Loading diff…