ruby-changes:72434
From: Nobuyoshi <ko1@a...>
Date: Wed, 6 Jul 2022 08:32:54 +0900 (JST)
Subject: [ruby-changes:72434] eaeb130b11 (master): [Bug #18890] newline should be insignificant after pattern label
https://git.ruby-lang.org/ruby.git/commit/?id=eaeb130b11 From eaeb130b11fefe91aaf61f294ea32af76dada74f Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Sun, 3 Jul 2022 14:45:24 +0900 Subject: [Bug #18890] newline should be insignificant after pattern label --- parse.y | 2 +- test/ruby/test_parse.rb | 33 +++++++++++++++++++++++++++++++++ test/ruby/test_pattern_matching.rb | 15 +++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/parse.y b/parse.y index 5fae5dc218..081fc1c868 100644 --- a/parse.y +++ b/parse.y @@ -7457,7 +7457,7 @@ parser_string_term(struct parser_params *p, int func) https://github.com/ruby/ruby/blob/trunk/parse.y#L7457 } if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) { nextc(p); - SET_LEX_STATE(EXPR_BEG|EXPR_LABEL); + SET_LEX_STATE(EXPR_ARG|EXPR_LABELED); return tLABEL_END; } SET_LEX_STATE(EXPR_END); diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb index 8b7c8f0e5b..16764a2146 100644 --- a/test/ruby/test_parse.rb +++ b/test/ruby/test_parse.rb @@ -1370,6 +1370,39 @@ x = __ENCODING__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L1370 assert_syntax_error('x = \ 42', /escaped space/) end + def test_label + expected = {:foo => 1} + + code = '{"foo": 1}' + assert_valid_syntax(code) + assert_equal(expected, eval(code)) + + code = '{foo: 1}' + assert_valid_syntax(code) + assert_equal(expected, eval(code)) + + class << (obj = Object.new) + attr_reader :arg + def set(arg) + @arg = arg + end + end + + assert_valid_syntax(code = "#{<<~"do;"}\n#{<<~'end;'}") + do; + obj.set foo: + 1 + end; + assert_equal(expected, eval(code)) + + assert_valid_syntax(code = "#{<<~"do;"}\n#{<<~'end;'}") + do; + obj.set "foo": + 1 + end; + assert_equal(expected, eval(code)) + end + =begin def test_past_scope_variable assert_warning(/past scope/) {catch {|tag| eval("BEGIN{throw tag}; tap {a = 1}; a")}} diff --git a/test/ruby/test_pattern_matching.rb b/test/ruby/test_pattern_matching.rb index 7531466f91..b8c445c806 100644 --- a/test/ruby/test_pattern_matching.rb +++ b/test/ruby/test_pattern_matching.rb @@ -1155,6 +1155,21 @@ END https://github.com/ruby/ruby/blob/trunk/test/ruby/test_pattern_matching.rb#L1155 end end + [{a: 42}, {b: 42}].each do |i| + assert_block('newline should be insignificant after pattern label') do + case i + in a: + 0 + true + in "b": + 0 + true + else + false + end + end + end + assert_syntax_error(%q{ case _ in a:, a: -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/