Skip to content

v0.10.0 — unary plus dispatches #+@

Choose a tag to compare

@tannevaled tannevaled released this 30 Sep 10:57
· 9 commits to main since this release
994aedb

One behavioural fix, in the parser rather than in any consumer.

Unary plus dispatches #+@, except on a numeric literal (#45)

arg: tUPLUS arg calls call_uni_op(p, $2, idUPlus) (parse.y-ruby_4_0:3962), so unary plus is a method send. The parser elided it entirely.

MRI's rule is narrower than "always fold" or "always dispatch", and the exception is lexical — measured on ruby 4.0.5 with Integer#+@, Float#+@ and String#+@ each redefined:

+1, +1.5 the literal; nothing dispatched
+"lit", +n, +(1), +(1 + 1) dispatched

Only a plus directly before a numeric literal token is absorbed. A parenthesised literal is not, and neither is a variable holding a number.

Why it surfaced now. The elision was invisible while every string literal was mutable. Under # frozen_string_literal: true, +"..." is the ordinary way to ask a frozen literal for a mutable copy, and eliding the operator handed back the frozen literal:

# frozen_string_literal: true
buf = +"start"
buf << "-more"     # ruby: "start-more"   with the elision: FrozenError

For consumers

ast.UnaryExpr now carries Op: "+@" for the dispatching form — the method name to send, so it cannot be confused with binary +, the same convention ~ already uses. A consumer that treats an unknown unary Op as a method name needs no change; one that assumed unary plus never produced a node will now see one.

Measured through go-embedded-ruby/ruby with a local replace: all rows match ruby 4.0.5 byte for byte, precedence is unchanged (+2**3 is 8, +1.abs is 1), and its conformance sweep moves core/string/uplus_spec.rb 2 → 3 with core/string/uminus_spec.rb unmoved as the control. A consumer that freezes compiled bytecode will see its drift check fire, since the compiler now emits a send where it emitted nothing.