ruby-changes:64122
From: Kazuki <ko1@a...>
Date: Sun, 13 Dec 2020 11:52:23 +0900 (JST)
Subject: [ruby-changes:64122] a8cf526ae9 (master): Don't emit warning when the pattern of one-line pattern matching is just a variable pattern
https://git.ruby-lang.org/ruby.git/commit/?id=a8cf526ae9 From a8cf526ae99c9e0823cd8c7c5ac96e43061fafa6 Mon Sep 17 00:00:00 2001 From: Kazuki Tsujimoto <kazuki@c...> Date: Sun, 13 Dec 2020 11:47:02 +0900 Subject: 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 diff --git a/parse.y b/parse.y index 9f7d1cd..bc33c6a 100644 --- a/parse.y +++ b/parse.y @@ -502,6 +502,7 @@ static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fnd https://github.com/ruby/ruby/blob/trunk/parse.y#L502 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 https://github.com/ruby/ruby/blob/trunk/parse.y#L1662 p->ctxt.in_kwarg = $<ctxt>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 https://github.com/ruby/ruby/blob/trunk/parse.y#L11688 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 62c617b..b705638 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 https://github.com/ruby/ruby/blob/trunk/spec/ruby/command_line/dash_upper_w_spec.rb#L33 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 ecf75bf..b21436d 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 https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/warning/element_set_spec.rb#L10 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 1596b2b..edc3826 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 https://github.com/ruby/ruby/blob/trunk/spec/ruby/language/pattern_matching_spec.rb#L43 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 -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/