Commit 9421518d

9421518d0204Merged

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

Description

Route and mangle user-defined bitwise/shift operator methods

def << / def >> / def | / def & / def ^ / def % on a user class were never dispatched as method calls: the binary-operator call site only diverts to compile_obj_operator_expr when is_operator_name returns 1, and these were absent, so recv << x / recv | x fell through to compile_operator_expr and emitted a raw C << / | on the struct pointer (invalid operands). For | & ^ % the method definition itself also mangled wrong: sanitize_name's char-loop copied the operator char verbatim into the C identifier (sp_T_|), not a legal C name.

Add << >> | & ^ to is_operator_name (so the call site routes to the generic compile_obj_operator_expr, which already emits sp__<sanitize_name(op)>), and add | & ^ % mangle entries to sanitize_name (_or / _and / _xor / _mod). Builtin receivers (int / string / array) aren't obj-typed, so compile_obj_operator_expr returns "" and the raw-operator path still handles 6 | 1, arr << x, etc.

Closes #1358 Closes #1368

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

Details

Commit
9421518d0204
Parent
2374f028338d

spinel_codegen.rbmodifiedView file

@@ -4708,6 +4708,29 @@
47084708 if name == "<=>"
47094709 return 1
47104710 end
4711+ # Bitwise / shift operator methods (`def <<`, `def | / & / ^`). A
4712+ # `recv OP x` whose receiver class defines them must dispatch through
4713+ # compile_obj_operator_expr; without this they fall through to
4714+ # compile_operator_expr and emit a raw C `<<` / `|` on the struct
4715+ # pointer (invalid operands). `%` is covered by the arithmetic arm
4716+ # above. Builtin receivers (int / string / array) aren't obj-typed so
4717+ # compile_obj_operator_expr returns "" and the raw-operator path still
4718+ # handles them.
4719+ if name == "<<"
4720+ return 1
4721+ end
4722+ if name == ">>"
4723+ return 1
4724+ end
4725+ if name == "|"
4726+ return 1
4727+ end
4728+ if name == "&"
4729+ return 1
4730+ end
4731+ if name == "^"
4732+ return 1
4733+ end
47114734 # Unary operator methods `def -@` / `def +@`. A `-obj` / `+obj`
47124735 # whose receiver class defines them dispatches through
47134736 # compile_obj_operator_expr like the binary operators; without this
@@ -5656,6 +5679,21 @@
56565679 if name == "[]="
56575680 return "_aset"
56585681 end
5682+ # Bitwise operator methods. The char-loop fallback below would copy
5683+ # `|` / `&` / `^` / `%` verbatim into the C identifier (`sp_X_|`),
5684+ # which isn't a legal C function name.
5685+ if name == "|"
5686+ return "_or"
5687+ end
5688+ if name == "&"
5689+ return "_and"
5690+ end
5691+ if name == "^"
5692+ return "_xor"
5693+ end
5694+ if name == "%"
5695+ return "_mod"
5696+ end
56595697 # Unary operator methods. The bare `-@` / `+@` would otherwise emit
56605698 # the literal `@` (and `-`) into the C identifier (`sp_X_-@`).
56615699 if name == "-@"