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 == :HELLO etc.)

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

@@ -2174,10 +2174,10 @@
21742174 if mname == "round"
21752175 return "int"
21762176 end
2177- if mname == "upcase"
2178- return "string"
2179- end
2180- if mname == "downcase"
2177+ if mname == "upcase" || mname == "downcase"
2178+ if recv >= 0 && infer_type(recv) == "symbol"
2179+ return "symbol"
2180+ end
21812181 return "string"
21822182 end
21832183 if mname == "swapcase"
@@ -8392,6 +8392,16 @@
83928392 end
83938393 end
83948394 end
8395+ # `:foo.upcase` / `:foo.downcase` lower to sp_str_upcase /
8396+ # sp_str_downcase on the symbol's name string and re-intern via
8397+ # sp_sym_intern. Mark the dynamic-pool path so it gets emitted.
8398+ if mname == "upcase" || mname == "downcase"
8399+ if @nd_receiver[nid] >= 0
8400+ if infer_type(@nd_receiver[nid]) == "symbol"
8401+ @needs_sym_intern = 1
8402+ end
8403+ end
8404+ end
83958405 # Methods that need string helpers only when receiver is string
83968406 if mname == "+" || mname == "*" || mname == "reverse"
83978407 if @nd_receiver[nid] >= 0
@@ -16443,6 +16453,12 @@
1644316453 if mname == "to_sym" || mname == "intern"
1644416454 return rc
1644516455 end
16456+ # Symbol#upcase / Symbol#downcase: lower-case via the str case helper
16457+ # then re-intern. Naming convention `sp_str_<mname>` lets one branch
16458+ # handle both — and slots in cleanly for capitalize/swapcase later.
16459+ if mname == "upcase" || mname == "downcase"
16460+ return "sp_sym_intern(sp_str_" + mname + "(sp_sym_to_s(" + rc + ")))"
16461+ end
1644616462 if mname == "inspect"
1644716463 return "sp_str_concat(\":\", sp_sym_to_s(" + rc + "))"
1644816464 end