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 / +obj whose 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

@@ -4613,6 +4613,17 @@
46134613 if name == "<=>"
46144614 return 1
46154615 end
4616+ # Unary operator methods `def -@` / `def +@`. A `-obj` / `+obj`
4617+ # whose receiver class defines them dispatches through
4618+ # compile_obj_operator_expr like the binary operators; without this
4619+ # they fall through to the numeric negation arm in
4620+ # compile_operator_expr and the user method is never called.
4621+ if name == "-@"
4622+ return 1
4623+ end
4624+ if name == "+@"
4625+ return 1
4626+ end
46164627 0
46174628 end
46184629
@@ -5550,6 +5561,14 @@
55505561 if name == "[]="
55515562 return "_aset"
55525563 end
5564+ # Unary operator methods. The bare `-@` / `+@` would otherwise emit
5565+ # the literal `@` (and `-`) into the C identifier (`sp_X_-@`).
5566+ if name == "-@"
5567+ return "_uminus"
5568+ end
5569+ if name == "+@"
5570+ return "_uplus"
5571+ end
55535572 result = ""
55545573 i = 0
55555574 while i < name.length