Project

General

Profile

Actions

Bug #22447

open

parse.y `-> do ... end` COND state

Bug #22447: parse.y `-> do ... end` COND state

Added by kddnewton (Kevin Newton) 26 days ago. Updated 20 days ago.


Description

With parse.y, do-blocks inside a -> do ... end lambda body are rejected when the lambda appears in a while/until/for condition:

while -> do foo do end end; end
# => syntax error, unexpected 'do' for condition, expecting 'end' or dummy end

The same body is accepted outside a condition, and with brace delimiters inside one:

-> do foo do end end                  # OK
while -> { foo do end }; end          # OK
while (foo do end); end               # OK
while def m; foo do end; end; end     # OK
while -> do foo do end end; end       # SyntaxError
until -> do foo do end end; end       # SyntaxError
for x in -> do foo do end end do end  # SyntaxError

Nesting a -> do lambda in an unparenthesized lambda parameter default also triggers it:

while -> a = -> do foo do end end do end; end  # SyntaxError

Pretty much everywhere this kind of thing happens there is a pair of CMDARG_PUSH(0) and COND_PUSH(0), but lambda is missing the latter. I think that's really all that needs to change to make this work:

--- a/parse.y
+++ b/parse.y
@@ lambda           : tLAMBDA[lpar]
                   f_larglist[args]
                     {
                         CMDARG_PUSH(0);
+                        COND_PUSH(0);
                     }
                   lambda_body[body]
                     {
@@
                         restore_block_exit(p, $al
+                        COND_POP();
                         CMDARG_POP();

It looks like this was fixed in other places (test_methoddef_in_cond and test_classdef_in_cond both test this kind of thing).

Updated by nobu (Nobuyoshi Nakada) 20 days ago Actions #1 [ruby-core:127203]

  • Assignee set to jhawthorn (John Hawthorn)
  • Backport changed from 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN to 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED

Thanks.
Could you apply the fix?

Actions

Also available in: PDF Atom