From e078ca5dda64c37723ea30f0b69587b6b7e51969 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sat, 15 Aug 2026 09:49:16 +0200 Subject: [PATCH] fix(parser): def !~ operator method, and class << with an assignment target MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two def/class forms MRI accepts were rejected: * def !~(other) — the not-match operator method. NMATCH lexes as one token and is a valid binary operator, but parseDefName listed MATCH (=~) and not NMATCH, so def !~ failed with "expected method name after def". != was also untested though NEQ was already handled. * class << @x = Object.new — the singleton-class target may be a full assignment expression (assign, then open the result's singleton), so it is parsed with parseExprOrAssign rather than parseTernary, which stopped at the '='. Both are exercised by ruby/spec (Kernel#!~, Kernel#public_send). Tests added to TestOperatorMethodDef (!~, !=) and a new TestSingletonClassAssignTarget; go vet, the full suite and the 100% merged-coverage gate stay green. Co-Authored-By: Claude Opus 4.8 --- operator_def_xstr_test.go | 2 ++ parser.go | 6 ++++-- singleton_class_test.go | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/operator_def_xstr_test.go b/operator_def_xstr_test.go index cb9f782..d7c5f63 100644 --- a/operator_def_xstr_test.go +++ b/operator_def_xstr_test.go @@ -71,6 +71,8 @@ func TestOperatorMethodDef(t *testing.T) { {"def |(o); end", "|"}, {"def ^(o); end", "^"}, {"def =~(o); end", "=~"}, + {"def !~(o); end", "!~"}, + {"def !=(o); end", "!="}, {"def [](i); end", "[]"}, {"def []=(i, v); end", "[]="}, {"def +(o); end", "+"}, diff --git a/parser.go b/parser.go index 5d44f7d..72ac20c 100644 --- a/parser.go +++ b/parser.go @@ -454,7 +454,9 @@ func (p *Parser) parseClass() ast.Node { // `class << target` opens target's singleton (metaclass). A SHOVEL here is // the singleton-class form, not a constant path. if p.accept(token.SHOVEL) { - target := p.parseTernary() + // The target is a full expression, including an assignment — MRI allows + // `class << @x = Object.new` (assign, then open the result's singleton). + target := p.parseExprOrAssign() p.pushScope() // the singleton-class body has its own local scope body := p.parseBodyWithRescue() p.popScope() @@ -618,7 +620,7 @@ func (p *Parser) parseDefName() (string, bool) { return name, true case token.CONST, token.SPACESHIP, token.LT, token.GT, token.LE, token.GE, - token.EQ, token.EQQ, token.NEQ, token.MATCH, token.SHOVEL, token.RSHIFT, + token.EQ, token.EQQ, token.NEQ, token.MATCH, token.NMATCH, token.SHOVEL, token.RSHIFT, token.STAR, token.POW, token.SLASH, token.PERCENT, token.AMPER, token.PIPE, token.CARET: return p.advance().Lit, true diff --git a/singleton_class_test.go b/singleton_class_test.go index 2ec5cf2..ef58182 100644 --- a/singleton_class_test.go +++ b/singleton_class_test.go @@ -90,6 +90,20 @@ func TestSingletonClassExprTarget(t *testing.T) { } } +// TestSingletonClassAssignTarget covers `class << @x = expr` — MRI allows the +// singleton target to be an assignment (assign, then open the result's singleton +// class), which needs a full expression, not just a ternary, after the `<<`. +func TestSingletonClassAssignTarget(t *testing.T) { + sc := topSingleton(t, "class << @receiver = Object.new\n def m; end\nend") + asn, ok := sc.Target.(*ast.IvarAssign) + if !ok || asn.Name != "@receiver" { + t.Fatalf("Target = %T (%+v), want *ast.IvarAssign @receiver", sc.Target, sc.Target) + } + if len(sc.Body) != 1 { + t.Fatalf("Body has %d nodes, want 1", len(sc.Body)) + } +} + // TestSingletonClassEmptyBody covers a singleton class with no body statements. func TestSingletonClassEmptyBody(t *testing.T) { sc := topSingleton(t, "class << self\nend")