Skip to content

Commit 8afa48e

Browse files
tannevaledclaude
andauthored
parser: refuse a modifier-rescue and a not/nested-! command in an arg position, and require every clause terminator (#38)
* parser: refuse a modifier-`rescue` in an `arg` position, and a command as a nested `!` operand `arg` has no `modifier_rescue` alternative in parse.y v3_4_0 — only `stmt` (3184) and the assignment right-hand sides `arg_rhs` (4162), `command_rhs` and `endless_arg` do. So `f(x rescue nil)`, `[x rescue nil]`, `a[x rescue nil]`, `{k: x rescue nil}`, `case x; when y rescue nil` and, because `expr` has no such arm either, `if x rescue nil; end` are all SyntaxErrors in MRI, while `f((x rescue nil))` and `f(a = 1 rescue nil)` are not. `expr: '!' command_call` (3350) admits a paren-less command as the operand of `!`, but the operand is a `command_call`, not an `expr`, so the privilege does not nest: `!foo bar` parses and `!!foo bar` does not. `opt_rescue` (5926) and `case_body` (5397) both REQUIRE the `then` nonterminal. Without it the stray token of an `arg_value` we had already correctly refused — the `bar` of `rescue foo bar` — was read as the clause's first statement and the clause parsed anyway. Mechanism: rescueModOK inverts noRescueMod so the zero value is the refusing one, permitRescueMod is its only writer and withRescueModifier its only reader, mirroring argOnly/permitCommand/commandArgsFollow. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * parser: three more productions with no `command`/`rescue`/`not` arm, and the clause terminator - `defined?` unparenthesised is `arg: keyword_defined '\n'? begin_defined arg` (parse.y v3_4_0 4021) — an ARG production, so it must NOT go through commandArgsFollow, whose first act is to refuse everything in an `arg` position. `[defined? x]`, `{k: defined? x}`, `1 + defined? x` and `def m(a = defined? x); end` are legal MRI and v0.6.0 refused them. - `arg` has no `keyword_not` arm (only `expr: keyword_not expr`, 3345), so `[not x]`, `{k: not x}`, `f(1, not x)`, `[*not x]`, `f(&not x)`, `def m(a = not x); end` and `case y; when not x; end` are SyntaxErrors in both of MRI's parsers. - `then`/`do` — `term | KEYWORD | term KEYWORD` — is REQUIRED by every clause head (`opt_rescue` 5926, `case_body` 5397, `k_if`, `k_unless`, `expr_value_do`, `p_case_body`), so `if x end`, `while x end`, `for i in a end`, `case x; when 1 end` and `begin; x; rescue Foo end` are SyntaxErrors. Requiring it is also what makes a refused `arg_value` OBSERVABLE: the stray `bar` of `rescue foo bar` was being read as the clause's first statement. - modifier-`rescue` refused in the remaining `arg_value` positions: hash literal entries, a `when`/`rescue` list, a parameter default, a keyword-argument value. Tables: 319 enumerated shapes, each judged by BOTH `ruby -c` and `ruby --parser=parse.y -c` on ruby 4.0.5. 115 accepted -> REFUSED, 7 REFUSED -> accepted (the `defined?` family), 0 regressions, 13 shapes where MRI's two parsers disagree and which are therefore asserted in neither direction. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent 4476f42 commit 8afa48e

3 files changed

Lines changed: 558 additions & 74 deletions

File tree

‎arg_position_command_test.go‎

Lines changed: 328 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,165 @@ func TestParenLessCommandCallRefusedInAnArgPosition(t *testing.T) {
118118
`{k: foo bar}`,
119119
`{k: foo.bar baz}`,
120120
`~foo bar`,
121+
// --- v0.6.1 additions. Three further productions that have no
122+
// `command`/`modifier_rescue`/`keyword_not` alternative, plus the clause
123+
// terminator that makes the refusal observable. Every shape judged on ruby
124+
// 4.0.5 by BOTH `ruby -c` and `ruby --parser=parse.y -c`, which agree on all
125+
// of them.
126+
//
127+
// `arg: '!' arg` (4009) vs `expr: '!' command_call` (3350): the operand of
128+
// `!` may be a paren-less command ONLY at the `expr` level, and a
129+
// `command_call` is not itself a unary expression — hence `!foo bar` legal,
130+
// `!!foo bar` not.
131+
//
132+
// `arg` has no `modifier_rescue` arm — only `stmt` (3184) and the assignment
133+
// right-hand sides `arg_rhs` (4162) / `command_rhs` / `endless_arg` do — and
134+
// `expr` has none either, which covers the `expr_value` headers.
135+
//
136+
// `arg` has no `keyword_not` arm: `not` is `expr: keyword_not '\n'? expr`
137+
// (3345) only.
138+
//
139+
// And `then`/`do` — `term | KEYWORD | term KEYWORD` — is REQUIRED by every
140+
// clause head (`opt_rescue` 5926, `case_body` 5397, `k_if`/`k_unless`,
141+
// `expr_value_do`, `p_case_body`). Without it the stray token of an
142+
// `arg_value` already refused upstream was read as the clause's first
143+
// statement and the clause parsed anyway.
144+
"begin\nx\nrescue foo bar\ny\nend",
145+
"case x\nwhen foo bar\n1\nend",
146+
`!! foo bar`,
147+
`!!!!foo bar`,
148+
`!!!foo bar`,
149+
`!!Foo 1`,
150+
`!![foo bar]`,
151+
`!!a.b foo bar`,
152+
`!!defined? foo bar`,
153+
`!!foo bar, baz`,
154+
`!!foo bar`,
155+
`!!foo.bar baz`,
156+
`!!not foo bar`,
157+
`!!super 1`,
158+
`!!x = foo bar`,
159+
`!-foo bar`,
160+
`!a = foo bar`,
161+
`!~foo bar`,
162+
`+!foo bar`,
163+
`-!foo bar`,
164+
`->() { x; rescue foo bar }`,
165+
`-not x`,
166+
`1 + [x rescue nil]`,
167+
`1 + not x`,
168+
`Foo(x rescue nil)`,
169+
`[!!foo bar]`,
170+
`[*not x]`,
171+
`[*x rescue nil]`,
172+
`[1, 2 rescue nil]`,
173+
`[1, not x]`,
174+
`[1, x rescue nil]`,
175+
`[1].each do; x; rescue foo bar; end`,
176+
`[1].each { x; rescue foo bar; }`,
177+
`[[x rescue nil]]`,
178+
`[not x]`,
179+
`[x rescue nil]`,
180+
`[{k: x rescue nil}]`,
181+
`a = !!foo bar`,
182+
`a ? not x : 1`,
183+
`a ? x rescue nil : 1`,
184+
`a&.b(x rescue nil)`,
185+
`a.b(1, x rescue nil)`,
186+
`a.b(x rescue nil)`,
187+
`a::b(x rescue nil)`,
188+
`a[1, not x]`,
189+
`a[1, x rescue nil]`,
190+
`a[x rescue nil]`,
191+
`begin; x; rescue !foo bar; y; end`,
192+
`begin; x; rescue *foo bar; y; end`,
193+
`begin; x; rescue Foo => e end`,
194+
`begin; x; rescue Foo bar; y; end`,
195+
`begin; x; rescue Foo end`,
196+
`begin; x; rescue Foo if c; y; end`,
197+
`begin; x; rescue Foo rescue Bar; y; end`,
198+
`begin; x; rescue Foo rescue nil; y; end`,
199+
`begin; x; rescue Foo, Bar end`,
200+
`begin; x; rescue Foo, bar baz => e; y; end`,
201+
`begin; x; rescue Foo, bar baz; y; end`,
202+
`begin; x; rescue a = foo bar; y; end`,
203+
`begin; x; rescue end`,
204+
`begin; x; rescue foo bar => e; y; end`,
205+
`begin; x; rescue foo bar baz qux; y; end`,
206+
`begin; x; rescue foo bar then y; end`,
207+
`begin; x; rescue foo bar; y; end`,
208+
`begin; y; rescue not x; z; end`,
209+
`case x rescue nil; when 1; end`,
210+
`case x; in 1 end`,
211+
`case x; when 1 end`,
212+
`case x; when 1, 2 end`,
213+
`case x; when Foo, bar baz; 1; end`,
214+
`case x; when foo bar; 1; end`,
215+
`case x; when foo bar; end`,
216+
`case x; when y rescue nil then 1; end`,
217+
`case x; when y rescue nil; end`,
218+
`case y; when not x; end`,
219+
`class C; x; rescue foo bar; end`,
220+
`def m(*a rescue nil); end`,
221+
`def m(a = not x); end`,
222+
`def m(a = x rescue nil); end`,
223+
`def m(a: not x); end`,
224+
`def m(a: x rescue nil); end`,
225+
`def m; super(x rescue nil); end`,
226+
`def m; x; rescue *foo bar; end`,
227+
`def m; x; rescue Foo, bar baz; end`,
228+
`def m; x; rescue foo bar; end`,
229+
`def m; yield(x rescue nil); end`,
230+
`elsif_check = 1; if a; elsif b end`,
231+
`f(!x rescue nil)`,
232+
`f(&not x)`,
233+
`f(&x rescue nil)`,
234+
`f(**not x)`,
235+
`f(**x rescue nil)`,
236+
`f(*a, x rescue nil)`,
237+
`f(*x rescue nil)`,
238+
`f(-x rescue nil)`,
239+
`f(1, 2 rescue nil)`,
240+
`f(1, not x)`,
241+
`f(1, x rescue nil)`,
242+
`f(a: 1, b: x rescue nil)`,
243+
`f(defined?(x) rescue nil)`,
244+
`f(k: x rescue nil)`,
245+
`f(x + 1 rescue nil)`,
246+
`f(x ? 1 : 2 rescue nil)`,
247+
`f(x rescue nil)`,
248+
`f(x rescue nil, 1)`,
249+
`f(x, y rescue nil)`,
250+
`f(y, x rescue nil, 1)`,
251+
`foo(bar rescue baz)`,
252+
`for i in a end`,
253+
`for i in x rescue nil; end`,
254+
`h = {k: v rescue nil}`,
255+
`if !!foo bar then end`,
256+
`if !!foo bar; end`,
257+
`if x 1 end`,
258+
`if x end`,
259+
`if x rescue nil; end`,
260+
`module M; x; rescue foo bar; end`,
261+
`not !!foo bar`,
262+
`p !!foo bar`,
263+
`p(x rescue nil)`,
264+
`super(x rescue nil)`,
265+
`unless x end`,
266+
`unless x rescue nil; end`,
267+
`until x end`,
268+
`until x rescue nil; end`,
269+
`while !!foo bar; end`,
270+
`while [x rescue nil]; end`,
271+
`while x end`,
272+
`while x rescue nil; end`,
273+
`x = [x rescue nil]`,
274+
`{1 => not x}`,
275+
`{1 => x rescue nil}`,
276+
`{k: [x rescue nil]}`,
277+
`{k: not x}`,
278+
`{k: x rescue nil}`,
279+
`~!foo bar`,
121280
} {
122281
_, err := Parse(src)
123282
if err == nil {
@@ -234,6 +393,175 @@ func TestParenLessCommandCallStillAcceptedWhereMRIAdmitsIt(t *testing.T) {
234393
`{k: a[foo bar]}`,
235394
`{k: defined?(foo bar)}`,
236395
`{k: foo(bar baz)}`,
396+
// --- v0.6.1 additions: the other direction. The contrasts that name each
397+
// rule sit beside their refused twin above — `f((x rescue nil))` vs
398+
// `f(x rescue nil)`, `f(a = 1 rescue nil)` vs `f(x rescue nil)` (the
399+
// `arg_rhs` carve-out), `!!(foo bar)` vs `!!foo bar`, `when (y rescue nil)`
400+
// vs `when y rescue nil`. `defined? x` in an `arg` position is here because
401+
// `arg: keyword_defined '\n'? begin_defined arg` (4021) IS an `arg`
402+
// production: `[defined? x]` is legal MRI and v0.6.0 refused it.
403+
"begin\nx\nrescue Foo\ny\nend",
404+
"case x\nwhen Foo\n1\nend",
405+
`!!!defined? x`,
406+
`!!!x`,
407+
`!!(begin; foo bar; end)`,
408+
`!!(foo bar)`,
409+
`!!a[foo bar]`,
410+
`!!begin; foo bar; end`,
411+
`!!defined? x`,
412+
`!!defined?(foo bar)`,
413+
`!!foo(bar baz)`,
414+
`!!foo(bar)`,
415+
`!!if foo bar then 1 end`,
416+
`!!lambda { foo bar }`,
417+
`!!while foo bar; end`,
418+
`!!x.y`,
419+
`!!x[0]`,
420+
`!!x`,
421+
`!(foo bar)`,
422+
`!Foo 1`,
423+
`!a.b foo bar`,
424+
`!foo bar rescue 1`,
425+
`!foo bar, baz`,
426+
`!foo.bar baz`,
427+
`(not x)`,
428+
`(x rescue nil)`,
429+
`-(x rescue nil)`,
430+
`-> { x rescue nil }`,
431+
`-defined? x`,
432+
`-x rescue nil`,
433+
`1 + (x rescue nil)`,
434+
`1 + defined? x`,
435+
`1 + x rescue nil`,
436+
`@x = y rescue nil`,
437+
`X = y rescue nil`,
438+
`[!x]`,
439+
`[(x rescue nil)]`,
440+
`[1].map { |x| x rescue nil }`,
441+
`[a += x rescue nil]`,
442+
`[a = 1 rescue nil, 2]`,
443+
`[a = b = 1 rescue nil]`,
444+
`[a = x rescue nil]`,
445+
`[defined? foo]`,
446+
`[defined? x]`,
447+
`[defined?(x)]`,
448+
`a += x rescue nil`,
449+
`a = [1, 2] rescue nil`,
450+
`a = b = x rescue nil`,
451+
`a = x rescue nil`,
452+
`a ? 1 : x rescue nil`,
453+
`a, b = x rescue nil`,
454+
`a.b = y rescue nil`,
455+
`a[(x rescue nil)]`,
456+
`a[0] = y rescue nil`,
457+
`a[a = 1 rescue nil]`,
458+
`a[defined? x]`,
459+
`begin; x rescue nil; end`,
460+
`begin; x; ensure; foo bar; end`,
461+
`begin; x; rescue (Foo rescue nil); y; end`,
462+
`begin; x; rescue (foo bar); y; end`,
463+
`begin; x; rescue *A, B; y; end`,
464+
`begin; x; rescue *Foo; y; end`,
465+
`begin; x; rescue ::Foo; y; end`,
466+
`begin; x; rescue => e; y; end`,
467+
`begin; x; rescue Foo => e then y; end`,
468+
`begin; x; rescue Foo => e; y; end`,
469+
`begin; x; rescue Foo then y; end`,
470+
`begin; x; rescue Foo then; y; end`,
471+
`begin; x; rescue Foo, ::Bar; y; end`,
472+
`begin; x; rescue Foo, Bar => e; y; end`,
473+
`begin; x; rescue Foo, Bar then y; end`,
474+
`begin; x; rescue Foo, Bar; y; end`,
475+
`begin; x; rescue Foo, foo(bar baz); y; end`,
476+
`begin; x; rescue Foo::Bar; y; end`,
477+
`begin; x; rescue Foo; else; y; end`,
478+
`begin; x; rescue Foo; end`,
479+
`begin; x; rescue Foo; rescue Bar; end`,
480+
`begin; x; rescue Foo; y; end rescue nil`,
481+
`begin; x; rescue Foo; y; end`,
482+
`begin; x; rescue [Foo, Bar].first; y; end`,
483+
`begin; x; rescue a.b; y; end`,
484+
`begin; x; rescue a::B; y; end`,
485+
`begin; x; rescue a[foo bar]; y; end`,
486+
`begin; x; rescue foo(bar); y; end`,
487+
`begin; x; rescue; foo bar; end`,
488+
`begin; x; rescue; y; end`,
489+
`begin; y; rescue defined? x; z; end`,
490+
`case (x rescue nil); when 1; end`,
491+
`case x; in 1 then 2; end`,
492+
`case x; in 1; 2; end`,
493+
`case x; when (y rescue nil); end`,
494+
`case x; when *a; 3; end`,
495+
`case x; when 1 then 2; end`,
496+
`case x; when 1 then; 2; end`,
497+
`case x; when 1, 2; 3; end`,
498+
`case x; when 1; 2; end`,
499+
`case x; when 1; end`,
500+
`case x; when Foo; end`,
501+
`case x; when a.b; end`,
502+
`case x; when a[0]; end`,
503+
`case y; when defined? x; end`,
504+
`def m = x rescue nil`,
505+
`def m(a = (x rescue nil)); end`,
506+
`def m(a = defined? x); end`,
507+
`def m(a: (x rescue nil)); end`,
508+
`def m; return x rescue nil; end`,
509+
`def m; super x rescue nil; end`,
510+
`def m; x rescue nil; end`,
511+
`def m; x; ensure; y; end`,
512+
`def m; yield x rescue nil; end`,
513+
`f { x rescue nil }`,
514+
`f($x = 1 rescue nil)`,
515+
`f((x rescue nil))`,
516+
`f(1) { x rescue nil }`,
517+
`f(1, (x rescue nil))`,
518+
`f(1, a = 2 rescue nil)`,
519+
`f(1, defined? x)`,
520+
`f(::A = 1 rescue nil)`,
521+
`f(@x = 1 rescue nil)`,
522+
`f(A = 1 rescue nil)`,
523+
`f(a &&= 1 rescue nil)`,
524+
`f(a = 1 rescue nil)`,
525+
`f(a = b = 1 rescue nil)`,
526+
`f(a ||= 1 rescue nil)`,
527+
`f(a.b = 1 rescue nil)`,
528+
`f(a::B = 1 rescue nil)`,
529+
`f(a[0] = 1 rescue nil)`,
530+
`f(x) rescue nil`,
531+
`foo a = 1 rescue nil`,
532+
`foo x rescue nil`,
533+
`for i in (x rescue nil); end`,
534+
`for i in a do end`,
535+
`if (x rescue nil); end`,
536+
`if a; elsif b; end`,
537+
`if foo bar then 1 end`,
538+
`if not x; end`,
539+
`if x then 1 end`,
540+
`if x; 1; end`,
541+
`if x; end`,
542+
`lambda { x rescue nil }`,
543+
`loop do x rescue nil end`,
544+
`not !foo bar`,
545+
`not not foo bar`,
546+
`not not x`,
547+
`not x`,
548+
`p x rescue nil`,
549+
`return x rescue nil`,
550+
`unless (x rescue nil); end`,
551+
`unless not done; end`,
552+
`until (x rescue nil); end`,
553+
`while (x rescue nil); end`,
554+
`while not x; end`,
555+
`while x do end`,
556+
`while x; end`,
557+
`x = (not y)`,
558+
`x = begin; y; rescue; 1; end rescue nil`,
559+
`x = y rescue nil`,
560+
`x rescue nil`,
561+
`x ||= y rescue nil`,
562+
`{k: (x rescue nil)}`,
563+
`{k: a = x rescue nil}`,
564+
`{k: defined? x}`,
237565
} {
238566
if _, err := Parse(src); err != nil {
239567
t.Errorf("Parse(%q) = %v, want no error (MRI 4.0.5 accepts it)", src, err)

‎modifier_rescue_command_test.go‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package parser_test
33
import (
44
"testing"
55

6+
"github.com/go-ruby-parser/parser"
67
"github.com/go-ruby-parser/parser/ast"
78
)
89

@@ -156,9 +157,11 @@ func TestModifierRescueUnchangedNonCommand(t *testing.T) {
156157
})
157158
}
158159

159-
// TestModifierRescueInsideDelimitedArg checks that a modifier rescue inside a
160-
// nested delimited context (an explicit paren group, or a parenthesised argument
161-
// list) is still consumed there — the command-call suppression must not leak in.
160+
// TestModifierRescueInsideDelimitedArg checks where a modifier rescue nested
161+
// inside a delimited context is consumed. A `(…)` GROUP is MRI's `compstmt`, so
162+
// the rescue is consumed there; a `(…)` ARGUMENT LIST is not — its contents are
163+
// `call_args`, which has no `modifier_rescue` alternative. Both directions
164+
// measured on ruby 4.0.5 with `ruby -c` and `ruby --parser=parse.y -c`.
162165
func TestModifierRescueInsideDelimitedArg(t *testing.T) {
163166
// Explicit paren group as a command argument: the rescue binds inside the
164167
// group, so the group becomes a Begin argument of the command.
@@ -171,10 +174,20 @@ func TestModifierRescueInsideDelimitedArg(t *testing.T) {
171174
}
172175
})
173176

174-
// Parenthesised argument list nested inside a command call: the inner rescue
175-
// belongs to that argument (`foo(bar rescue baz)`), while `outer` keeps both.
176-
t.Run("paren arglist inside command", func(t *testing.T) {
177-
const src = `outer foo(bar rescue baz)`
177+
// A parenthesised ARGUMENT LIST is not a statement context: `f(bar rescue
178+
// baz)` is a SyntaxError in MRI, which wants `f((bar rescue baz))`. Both of
179+
// MRI's parsers refuse it — prism says "unexpected 'rescue' modifier; expected
180+
// a `)` to close the arguments" — because `call_args`/`args`/`arg_value` offer
181+
// no `modifier_rescue` arm; only `stmt` (3184) and `arg_rhs` (4162) do. This
182+
// subtest previously asserted the opposite and pinned our own divergence.
183+
t.Run("paren arglist inside command refuses it", func(t *testing.T) {
184+
for _, src := range []string{`outer foo(bar rescue baz)`, `foo(bar rescue baz)`} {
185+
if _, err := parser.Parse(src); err == nil {
186+
t.Errorf("Parse(%q) = nil error, want a parse error (MRI 4.0.5 refuses it)", src)
187+
}
188+
}
189+
// The shape MRI does accept: the argument's own parentheses make it a group.
190+
const src = `outer foo((bar rescue baz))`
178191
outer := wantCall(t, src, parseOne(t, src), "outer", 1)
179192
foo := wantCall(t, src, outer.Args[0], "foo", 1)
180193
body, _ := beginWrapping(t, src, foo.Args[0])

0 commit comments

Comments
 (0)