From a8cf526ae99c9e0823cd8c7c5ac96e43061fafa6 Mon Sep 17 00:00:00 2001 From: Kazuki Tsujimoto Date: Sun, 13 Dec 2020 11:47:02 +0900 Subject: [PATCH] Don't emit warning when the pattern of one-line pattern matching is just a variable pattern https://github.com/ruby/dev-meeting-log/blob/master/DevelopersMeeting20201210Japan.md#feature-17371-reintroduce-expr-in-pat-ktsj --- parse.y | 17 +++++++++++++---- spec/ruby/command_line/dash_upper_w_spec.rb | 4 ++-- spec/ruby/core/warning/element_set_spec.rb | 4 ++-- spec/ruby/language/pattern_matching_spec.rb | 4 ++-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/parse.y b/parse.y index 9f7d1cd3b7..bc33c6a23f 100644 --- a/parse.y +++ b/parse.y @@ -502,6 +502,7 @@ static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fnd static NODE *new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc); static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc); static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc); +static void warn_one_line_pattern_matching(struct parser_params *p, NODE *node, NODE *pattern); static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc); static NODE *args_with_numbered(struct parser_params*,NODE*,int); @@ -1661,10 +1662,7 @@ expr : command_call p->ctxt.in_kwarg = $3.in_kwarg; /*%%%*/ $$ = NEW_CASE3($1, NEW_IN($5, 0, 0, &@5), &@$); - - if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL)) - rb_warn0L(nd_line($$), "One-line pattern matching is experimental, and the behavior may change in future versions of Ruby!"); - + warn_one_line_pattern_matching(p, $$, $5); /*% %*/ /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/ } @@ -11690,6 +11688,17 @@ new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, co return node; } +static void +warn_one_line_pattern_matching(struct parser_params *p, NODE *node, NODE *pattern) +{ + enum node_type type; + type = nd_type(pattern); + + if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL) && + !(type == NODE_LASGN || type == NODE_DASGN || type == NODE_DASGN_CURR)) + rb_warn0L(nd_line(node), "One-line pattern matching is experimental, and the behavior may change in future versions of Ruby!"); +} + static NODE* dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc) { diff --git a/spec/ruby/command_line/dash_upper_w_spec.rb b/spec/ruby/command_line/dash_upper_w_spec.rb index 62c617b892..b705638606 100644 --- a/spec/ruby/command_line/dash_upper_w_spec.rb +++ b/spec/ruby/command_line/dash_upper_w_spec.rb @@ -33,11 +33,11 @@ ruby_version_is "2.7" do describe "The -W command line option with :no-experimental" do before do ruby_version_is ""..."3.0" do - @src = 'case 0; in a; end' + @src = 'case [0, 1]; in [a, b]; end' end ruby_version_is "3.0" do - @src = '1 => a' + @src = '[0, 1] => [a, b]' end end diff --git a/spec/ruby/core/warning/element_set_spec.rb b/spec/ruby/core/warning/element_set_spec.rb index ecf75bf85c..b21436d3c7 100644 --- a/spec/ruby/core/warning/element_set_spec.rb +++ b/spec/ruby/core/warning/element_set_spec.rb @@ -10,11 +10,11 @@ ruby_version_is '2.7' do describe ":experimental" do before do ruby_version_is ""..."3.0" do - @src = 'case 0; in a; end' + @src = 'case [0, 1]; in [a, b]; end' end ruby_version_is "3.0" do - @src = '1 => a' + @src = '[0, 1] => [a, b]' end end diff --git a/spec/ruby/language/pattern_matching_spec.rb b/spec/ruby/language/pattern_matching_spec.rb index 1596b2b8d2..edc3826448 100644 --- a/spec/ruby/language/pattern_matching_spec.rb +++ b/spec/ruby/language/pattern_matching_spec.rb @@ -43,11 +43,11 @@ ruby_version_is "2.7" do describe "warning" do before do ruby_version_is ""..."3.0" do - @src = 'case 0; in a; end' + @src = 'case [0, 1]; in [a, b]; end' end ruby_version_is "3.0" do - @src = '1 => a' + @src = '[0, 1] => [a, b]' end end