Bug #10653
closeddo-end block in ternary operator is syntax error
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
^
Updated by nobu (Nobuyoshi Nakada) almost 10 years ago
- Related to Feature #4276: Allow use of quotes in symbol syntactic sugar for hashes added
Updated by nobu (Nobuyoshi Nakada) over 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
Updated by nobu (Nobuyoshi Nakada) over 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]
Updated by nobu (Nobuyoshi Nakada) over 9 years ago
- Related to Bug #11456: Hash リテラル中で rescue をもちいると SyntaxError になる added
Updated by nagachika (Tomoyuki Chikanaga) about 9 years ago
After backporting r50409 and r51617, TestSyntax#test_block_after_cond failed.
Updated by nagachika (Tomoyuki Chikanaga) about 9 years ago
r51616 should be backported too?
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.