Commit bc76f889
bc76f889a5c1Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Mangle and dispatch unary operator methods (def -@ / def +@)
def -@ / def +@ emitted the literal -@ / +@ into the generated C
identifier (sp_Money_-@), producing "stray '@' in program". Any class
with unary minus (money / measure / vector negation) failed to compile.
Found via the spinelgems harness (latinum, as-duration).
Two codegen-only changes:
- sanitize_name:
-@->_uminus,+@->_uplus, so the method definition and its call site mangle to a valid C name. - is_operator_name: recognize
-@/+@so a-obj/+objwhose receiver class defines them routes through compile_obj_operator_expr (which already dispatches any class-defined operator to sp__(recv)) instead of falling through to the numeric negation arm in compile_operator_expr.
Numeric -int / -float and the existing binary operator dispatch are
unchanged (compile_obj_operator_expr is a no-op for non-obj receivers).
Analyze already infers -obj / +obj as the receiver's class via the
obj-receiver user-method deferral, so no analyze change is needed.
Closes #1357
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- bc76f889a5c1
- Parent
- df35063c58b7
spinel_codegen.rbmodifiedView file
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 # they fall through to the numeric negation arm in # compile_operator_expr and the user method is never called. if name == "-@" return 1 end if name == "+@" return 1 end 0 end if name == "[]=" return "_aset" end # Unary operator methods. The bare `-@` / `+@` would otherwise emit # the literal `@` (and `-`) into the C identifier (`sp_X_-@`). if name == "-@" return "_uminus" end if name == "+@" return "_uplus" end result = "" i = 0 while i < name.lengthtest/unary_operator_methods.rbaddedView file
Loading diff…
test/unary_operator_methods.rb.expectedaddedView file
Loading diff…