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
if name == "<=>" return 1 end # Bitwise / shift operator methods (`def <<`, `def | / & / ^`). A # `recv OP x` whose receiver class defines them must dispatch through # compile_obj_operator_expr; without this they fall through to # compile_operator_expr and emit a raw C `<<` / `|` on the struct # pointer (invalid operands). `%` is covered by the arithmetic arm # above. Builtin receivers (int / string / array) aren't obj-typed so # compile_obj_operator_expr returns "" and the raw-operator path still # handles them. if name == "<<" return 1 end if name == ">>" return 1 end if name == "|" return 1 end if name == "&" return 1 end if name == "^" return 1 end # Unary operator methods `def -@` / `def +@`. A `-obj` / `+obj` # whose receiver class defines them dispatches through # compile_obj_operator_expr like the binary operators; without this if name == "[]=" return "_aset" end # Bitwise operator methods. The char-loop fallback below would copy # `|` / `&` / `^` / `%` verbatim into the C identifier (`sp_X_|`), # which isn't a legal C function name. if name == "|" return "_or" end if name == "&" return "_and" end if name == "^" return "_xor" end if name == "%" return "_mod" end # Unary operator methods. The bare `-@` / `+@` would otherwise emit # the literal `@` (and `-`) into the C identifier (`sp_X_-@`). if name == "-@"test/op_method_bitwise.rbaddedView file
Loading diff…
test/op_method_bitwise.rb.expectedaddedView file
Loading diff…
test/op_method_shift.rbaddedView file
Loading diff…
test/op_method_shift.rb.expectedaddedView file
Loading diff…