Skip to content

Commit f17405b

Browse files
Earlopainmatzbot
authored andcommitted
[ruby/prism] Reject some cases with return and command calls
The same also applies to `break`/`next`. https://bugs.ruby-lang.org/issues/21540 ruby/prism@3a38b192e3
1 parent e39fd45 commit f17405b

6 files changed

Lines changed: 37 additions & 1 deletion

File tree

lib/prism/translation/parser/lexer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class Lexer
203203
# The following token types are listed as those classified as `tLPAREN`.
204204
LPAREN_CONVERSION_TOKEN_TYPES = Set.new([
205205
:kBREAK, :tCARET, :kCASE, :tDIVIDE, :kFOR, :kIF, :kNEXT, :kRETURN, :kUNTIL, :kWHILE, :tAMPER, :tANDOP, :tBANG, :tCOMMA, :tDOT2, :tDOT3,
206-
:tEQL, :tLPAREN, :tLPAREN2, :tLPAREN_ARG, :tLSHFT, :tNL, :tOP_ASGN, :tOROP, :tPIPE, :tSEMI, :tSTRING_DBEG, :tUMINUS, :tUPLUS
206+
:tEQL, :tLPAREN, :tLPAREN2, :tLPAREN_ARG, :tLSHFT, :tNL, :tOP_ASGN, :tOROP, :tPIPE, :tSEMI, :tSTRING_DBEG, :tUMINUS, :tUPLUS, :tLCURLY
207207
])
208208

209209
# Types of tokens that are allowed to continue a method call with comments in-between.

prism/prism.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19102,7 +19102,13 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1910219102
pm_binding_power_t binding_power = pm_binding_powers[parser->current.type].left;
1910319103

1910419104
if (binding_power == PM_BINDING_POWER_UNSET || binding_power >= PM_BINDING_POWER_RANGE) {
19105+
pm_token_t next = parser->current;
1910519106
parse_arguments(parser, &arguments, false, PM_TOKEN_EOF, (uint16_t) (depth + 1));
19107+
19108+
// Reject `foo && return bar`.
19109+
if (!accepts_command_call && arguments.arguments != NULL) {
19110+
PM_PARSER_ERR_TOKEN_FORMAT(parser, next, PM_ERR_EXPECT_EOL_AFTER_STATEMENT, pm_token_type_human(next.type));
19111+
}
1910619112
}
1910719113
}
1910819114

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
foo && return bar
2+
^~~ unexpected local variable or method, expecting end-of-input
3+
4+
tap { foo && break bar }
5+
^~~ unexpected local variable or method, expecting end-of-input
6+
7+
tap { foo && next bar }
8+
^~~ unexpected local variable or method, expecting end-of-input
9+
10+
foo && return()
11+
^ unexpected '(', expecting end-of-input
12+
13+
foo && return(bar)
14+
^ unexpected '(', expecting end-of-input
15+
16+
foo && return(bar, baz)
17+
^~~~~~~~~~ unexpected write target
18+
^ unexpected '(', expecting end-of-input
19+

test/prism/fixtures/break.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ tap { break() }
2020

2121
tap { break(1) }
2222

23+
tap { (break 1) }
24+
25+
tap { foo && (break 1) }
26+
2327
foo { break 42 } == 42
2428

2529
foo { |a| break } == 42

test/prism/fixtures/next.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ tap { next
2222
tap { next() }
2323

2424
tap { next(1) }
25+
26+
tap { (next 1) }
27+
28+
tap { foo && (next 1) }

test/prism/fixtures/return.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ return()
2222

2323
return(1)
2424

25+
(return 1)
26+
27+
foo && (return 1)

0 commit comments

Comments
 (0)