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

ruby-changes:58934

From: Nobuyoshi <ko1@a...>
Date: Thu, 28 Nov 2019 13:50:54 +0900 (JST)
Subject: [ruby-changes:58934] 8b4ee5d6ba (master): Raise `NoMatchingPatternError` when expr `in` pat doesn't match

https://git.ruby-lang.org/ruby.git/commit/?id=8b4ee5d6ba

From 8b4ee5d6ba92a385eedc9235ce0a2d5618deecf0 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 28 Nov 2019 13:44:25 +0900
Subject: Raise `NoMatchingPatternError` when expr `in` pat doesn't match

* `expr in pattern` should raise `NoMatchingError` when unmatched
* `expr in pattern` should return `nil`. (this is unspecified, but
  this feature is experimental, at all)

[Feature #16355]

diff --git a/parse.y b/parse.y
index 3db0691..c1df306 100644
--- a/parse.y
+++ b/parse.y
@@ -1571,7 +1571,7 @@ expr		: command_call https://github.com/ruby/ruby/blob/trunk/parse.y#L1571
 		    {
 			p->in_kwarg = !!$<num>3;
 		    /*%%%*/
-			$$ = NEW_CASE3($1, NEW_IN($5, NEW_TRUE(&@5), NEW_FALSE(&@5), &@5), &@$);
+			$$ = NEW_CASE3($1, NEW_IN($5, 0, 0, &@5), &@$);
 			rb_warn0L(nd_line($$), "Pattern matching is experimental, and the behavior may change in future versions of Ruby!");
 		    /*% %*/
 		    /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
diff --git a/test/ruby/test_pattern_matching.rb b/test/ruby/test_pattern_matching.rb
index 889f8dd..50bab51 100644
--- a/test/ruby/test_pattern_matching.rb
+++ b/test/ruby/test_pattern_matching.rb
@@ -1261,8 +1261,11 @@ END https://github.com/ruby/ruby/blob/trunk/test/ruby/test_pattern_matching.rb#L1261
   ################################################################
 
   def test_modifier_in
-    assert_equal true, (1 in a)
+    assert_nil (1 in a)
     assert_equal 1, a
+    assert_raise(NoMatchingPatternError) do
+      {a: 1} in {a: 0}
+    end
     assert_valid_syntax "p(({} in {a:}), a:\n 1)"
     assert_syntax_error(%q{
       1 in a, b
-- 
cgit v0.10.2


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

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