Project

General

Profile

Actions

Bug #10653

closed

do-end block in ternary operator is syntax error

Added by yancya (Shinta Koyanagi) almost 10 years ago. Updated almost 9 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
[ruby-dev:48790]

Description

  • For example
ruby -v -e 'true ? 1.tap do |n| p n end : 0'
  • Result by 2.1.5
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]
1
  • Result by 2.2.0
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
-e:1: syntax error, unexpected keyword_do_cond, expecting ':'
true ? 1.tap do |n| p n end : 0
               ^
-e:1: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '('
true ? 1.tap do |n| p n end : 0
                       ^

Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #4276: Allow use of quotes in symbol syntactic sugar for hashesClosedmatz (Yukihiro Matsumoto)01/13/2011Actions
Related to Ruby master - Bug #11456: Hash リテラル中で rescue をもちいると SyntaxError になるClosedActions

Updated by nobu (Nobuyoshi Nakada) almost 10 years ago

  • Related to Feature #4276: Allow use of quotes in symbol syntactic sugar for hashes added
Actions #2

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

  • Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN to 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: REQUIRED
Actions #3

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

  • Status changed from Open to Closed

Applied in changeset r51617.


parse.y: fix block after conditional

  • parse.y: fix syntax error at do-block after a conditional
    operator. separate label-allowed and after-a-label states from
    others as bit flags. [ruby-dev:48790] [Bug #10653]
Actions #4

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

  • Related to Bug #11456: Hash リテラル中で rescue をもちいると SyntaxError になる added
Actions #5

Updated by nagachika (Tomoyuki Chikanaga) about 9 years ago

After backporting r50409 and r51617, TestSyntax#test_block_after_cond failed.

Actions #6

Updated by nagachika (Tomoyuki Chikanaga) about 9 years ago

r51616 should be backported too?

Actions #7

Updated by nagachika (Tomoyuki Chikanaga) about 9 years ago

  • Backport changed from 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: REQUIRED to 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: DONE

r50409, r51616, r51617 and r51624 were backported into ruby_2_2 branch at r52046.
With r50409 a minor incompatibility is introduced ({ %w"key": 0 } cause SyntaxError).
I'm not confident the incompatibility is essential part of this fixes, but the accurate surgery is required to eliminate the incompatibility. I gave up.

Updated by whitequark (whitequark *) almost 9 years ago

I was implementing this in my Ruby parser and I found a much simpler solution than what you've implemented here: https://github.com/whitequark/parser/commit/113442eff1f9c6f7e7205ec0dac8cb858b3b0fc7

In short, instead of explicitly enumerating all contexts where a label is valid, I only disallow quoted label at EXPR_VALUE using an additional flag. It works just as well. It also would not exhibit the bug with %w during backport.

Might I suggest a simplification to parse.y?

Updated by nobu (Nobuyoshi Nakada) almost 9 years ago

Currently it's a bit flag in parse.y too.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0