[前][次][番号順一覧][スレッド一覧]

ruby-changes:71786

From: Nobuyoshi <ko1@a...>
Date: Thu, 21 Apr 2022 16:10:48 +0900 (JST)
Subject: [ruby-changes:71786] 3ddf6ad4d2 (master): Private local variables should shadow outer variables [Bug #18629]

https://git.ruby-lang.org/ruby.git/commit/?id=3ddf6ad4d2

From 3ddf6ad4d2f6dae4caa00b8c407768c7062099a0 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 21 Apr 2022 16:04:36 +0900
Subject: Private local variables should shadow outer variables [Bug #18629]

---
 parse.y                 | 3 ++-
 test/ruby/test_parse.rb | 4 ++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/parse.y b/parse.y
index c7ed2edea0..0bf717aa95 100644
--- a/parse.y
+++ b/parse.y
@@ -10994,9 +10994,9 @@ is_private_local_id(ID name) https://github.com/ruby/ruby/blob/trunk/parse.y#L10994
 static int
 shadowing_lvar_0(struct parser_params *p, ID name)
 {
-    if (is_private_local_id(name)) return 1;
     if (dyna_in_block(p)) {
 	if (dvar_curr(p, name)) {
+	    if (is_private_local_id(name)) return 1;
 	    yyerror0("duplicated argument name");
 	}
 	else if (dvar_defined(p, name) || local_id(p, name)) {
@@ -11009,6 +11009,7 @@ shadowing_lvar_0(struct parser_params *p, ID name) https://github.com/ruby/ruby/blob/trunk/parse.y#L11009
     }
     else {
 	if (local_id(p, name)) {
+	    if (is_private_local_id(name)) return 1;
 	    yyerror0("duplicated argument name");
 	}
     }
diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb
index d697a29c1c..2841e20f6d 100644
--- a/test/ruby/test_parse.rb
+++ b/test/ruby/test_parse.rb
@@ -930,6 +930,10 @@ x = __ENCODING__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L930
     assert_no_warning(/shadowing outer local variable/) {eval("a=1; tap {|a|}")}
   end
 
+  def test_shadowing_private_local_variable
+    assert_equal 1, eval("_ = 1; [[2]].each{ |(_)| }; _")
+  end
+
   def test_unused_variable
     o = Object.new
     assert_warning(/assigned but unused variable/) {o.instance_eval("def foo; a=1; nil; end")}
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]