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

ruby-changes:72628

From: Jeremy <ko1@a...>
Date: Fri, 22 Jul 2022 00:28:21 +0900 (JST)
Subject: [ruby-changes:72628] 12ac8971a3 (master): Do not have class/module keywords look up ancestors of Object

https://git.ruby-lang.org/ruby.git/commit/?id=12ac8971a3

From 12ac8971a394118a57640299f654e46e763093fa Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Mon, 20 Jun 2022 15:51:05 -0700
Subject: Do not have class/module keywords look up ancestors of Object

Fixes case where Object includes a module that defines a constant,
then using class/module keyword to define the same constant on
Object itself.

Implements [Feature #18832]
---
 spec/ruby/language/module_spec.rb | 15 ++++++++++++---
 test/ruby/test_module.rb          |  2 --
 test/ruby/test_syntax.rb          | 15 +++++++++++++++
 vm_insnhelper.c                   | 24 ++++--------------------
 4 files changed, 31 insertions(+), 25 deletions(-)

diff --git a/spec/ruby/language/module_spec.rb b/spec/ruby/language/module_spec.rb
index 1a5fcaf46f..e361bf58f5 100644
--- a/spec/ruby/language/module_spec.rb
+++ b/spec/ruby/language/module_spec.rb
@@ -28,9 +28,18 @@ describe "The module keyword" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/language/module_spec.rb#L28
     ModuleSpecs::Reopened.should be_true
   end
 
-  it "reopens a module included in Object" do
-    module IncludedModuleSpecs; Reopened = true; end
-    ModuleSpecs::IncludedInObject::IncludedModuleSpecs::Reopened.should be_true
+  ruby_version_is '3.2' do
+    it "does not reopen a module included in Object" do
+      module IncludedModuleSpecs; Reopened = true; end
+      ModuleSpecs::IncludedInObject::IncludedModuleSpecs.should_not == Object::IncludedModuleSpecs
+    end
+  end
+
+  ruby_version_is ''...'3.2' do
+    it "reopens a module included in Object" do
+      module IncludedModuleSpecs; Reopened = true; end
+      ModuleSpecs::IncludedInObject::IncludedModuleSpecs::Reopened.should be_true
+    end
   end
 
   it "raises a TypeError if the constant is a Class" do
diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb
index 3dae3916ff..137da09bc3 100644
--- a/test/ruby/test_module.rb
+++ b/test/ruby/test_module.rb
@@ -1328,8 +1328,6 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L1328
       end
     end
     include LangModuleSpecInObject
-    module LangModuleTop
-    end
     puts "ok" if LangModuleSpecInObject::LangModuleTop == LangModuleTop
     INPUT
 
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index 1d7b89de57..b0ad012131 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -1907,6 +1907,21 @@ eom https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L1907
     assert_equal 0...1, exp.call(a: 0)
   end
 
+  def test_class_module_Object_ancestors
+    assert_separately([], <<-RUBY)
+      m = Module.new
+      m::Bug18832 = 1
+      include m
+      class Bug18832; end
+    RUBY
+    assert_separately([], <<-RUBY)
+      m = Module.new
+      m::Bug18832 = 1
+      include m
+      module Bug18832; end
+    RUBY
+  end
+
   def test_cdhash
     assert_separately([], <<-RUBY)
       n = case 1 when 2r then false else true end
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index 5a77484b74..3c81544ba9 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -1065,20 +1065,6 @@ vm_get_cvar_base(const rb_cref_t *cref, const rb_control_frame_t *cfp, int top_l https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1065
     return klass;
 }
 
-static VALUE
-vm_search_const_defined_class(const VALUE cbase, ID id)
-{
-    if (rb_const_defined_at(cbase, id)) return cbase;
-    if (cbase == rb_cObject) {
-	VALUE tmp = RCLASS_SUPER(cbase);
-	while (tmp) {
-	    if (rb_const_defined_at(tmp, id)) return tmp;
-	    tmp = RCLASS_SUPER(tmp);
-	}
-    }
-    return 0;
-}
-
 static bool
 iv_index_tbl_lookup(struct st_table *iv_index_tbl, ID id, struct rb_iv_index_tbl_entry **ent)
 {
@@ -4461,16 +4447,14 @@ vm_dtrace(rb_event_flag_t flag, rb_execution_context_t *ec) https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L4447
 static VALUE
 vm_const_get_under(ID id, rb_num_t flags, VALUE cbase)
 {
-    VALUE ns;
-
-    if ((ns = vm_search_const_defined_class(cbase, id)) == 0) {
-	return ns;
+    if (!rb_const_defined_at(cbase, id)) {
+        return 0;
     }
     else if (VM_DEFINECLASS_SCOPED_P(flags)) {
-	return rb_public_const_get_at(ns, id);
+        return rb_public_const_get_at(cbase, id);
     }
     else {
-	return rb_const_get_at(ns, id);
+        return rb_const_get_at(cbase, id);
     }
 }
 
-- 
cgit v1.2.1


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

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