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

ruby-changes:61681

From: Jeremy <ko1@a...>
Date: Thu, 11 Jun 2020 23:31:09 +0900 (JST)
Subject: [ruby-changes:61681] 2188d6d160 (master): Warn when passing a non-literal block to Kernel#lambda

https://git.ruby-lang.org/ruby.git/commit/?id=2188d6d160

From 2188d6d160d3ba82432c87277310a4d417e136d5 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Wed, 10 Jun 2020 17:50:35 -0700
Subject: Warn when passing a non-literal block to Kernel#lambda

Implements [Feature #15973]

diff --git a/basictest/test.rb b/basictest/test.rb
index 25a4298..a2eb107 100755
--- a/basictest/test.rb
+++ b/basictest/test.rb
@@ -1425,9 +1425,6 @@ marity_test(:test_ok) https://github.com/ruby/ruby/blob/trunk/basictest/test.rb#L1425
 marity_test(:marity_test)
 marity_test(:p)
 
-lambda(&method(:test_ok)).call(true)
-lambda(&block_get{|a,n| test_ok(a,n)}).call(true, 2)
-
 class ITER_TEST1
    def a
      block_given?
diff --git a/proc.c b/proc.c
index ffc87b7..94722dd 100644
--- a/proc.c
+++ b/proc.c
@@ -855,6 +855,19 @@ rb_block_lambda(void) https://github.com/ruby/ruby/blob/trunk/proc.c#L855
 static VALUE
 f_lambda(VALUE _)
 {
+    VALUE block_handler = rb_vm_frame_block_handler(GET_EC()->cfp);
+
+    if (block_handler != VM_BLOCK_HANDLER_NONE) {
+        switch (vm_block_handler_type(block_handler)) {
+          case block_handler_type_proc:
+          case block_handler_type_symbol:
+          case block_handler_type_ifunc:
+            rb_warn_deprecated("lambda without a literal block", "the proc without lambda");
+          default:
+            break;
+        }
+    }
+
     return rb_block_lambda();
 }
 
diff --git a/test/ruby/test_iterator.rb b/test/ruby/test_iterator.rb
index 54c0953..820d559 100644
--- a/test/ruby/test_iterator.rb
+++ b/test/ruby/test_iterator.rb
@@ -339,8 +339,7 @@ class TestIterator < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_iterator.rb#L339
     marity_test(:marity_test)
     marity_test(:p)
 
-    lambda(&method(:assert)).call(true)
-    lambda(&get_block{|a,n| assert(a,n)}).call(true, "marity")
+    get_block{|a,n| assert(a,n)}.call(true, "marity")
   end
 
   def foo
diff --git a/test/ruby/test_lambda.rb b/test/ruby/test_lambda.rb
index 03b501a..75362e2 100644
--- a/test/ruby/test_lambda.rb
+++ b/test/ruby/test_lambda.rb
@@ -74,6 +74,12 @@ class TestLambdaParameters < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_lambda.rb#L74
     assert_raise(ArgumentError, bug9605) {proc(&plus).call [1,2]}
   end
 
+  def test_warning_for_non_literal_blocks
+    assert_warn(/lambda without a literal block/, '[ruby-core:93482] [Feature #15973]') do
+      lambda(&:symbol)
+    end
+  end
+
   def pass_along(&block)
     lambda(&block)
   end
-- 
cgit v0.10.2


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

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