Skip to content
  • U.Nakamura's avatar
    a5272e64
    merge revision(s) ebb96fa8: [Backport #17321] · a5272e64
    U.Nakamura authored
    
    
    	Fix singleton class cloning
    
    	Before this commit, `clone` gave different results depending on whether the original object
    	had an attached singleton class or not.
    
    	Consider the following setup:
    	```
    	class Foo; end
    	Foo.singleton_class.define_method(:foo) {}
    
    	obj = Foo.new
    
    	obj.singleton_class if $call_singleton
    
    	clone = obj.clone
    	```
    
    	When `$call_singleton = false`, neither `obj.singleton_class.singleton_class` nor
    	`clone.singleton_class.singleton_class` own any methods.
    
    	However, when `$call_singleton = true`, `clone.singleton_class.singleton_class` would own a copy of
    	`foo` from `Foo.singleton_class`, even though `obj.singleton_class.singleton_class` does not.
    
    	The latter case is unexpected and results in a visibly different clone, depending on if the original object
    	had an attached class or not.
    
    Co-authored-by: default avatarUfuk Kayserilioglu <ufuk.kayserilioglu@shopify.com>
    	---
    	 class.c                 | 31 ++++++++++++++++++++++---------
    	 test/ruby/test_class.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++
    	 2 files changed, 69 insertions(+), 9 deletions(-)
    
    git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_6@67935 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
    a5272e64
    merge revision(s) ebb96fa8: [Backport #17321]
    U.Nakamura authored
    
    
    	Fix singleton class cloning
    
    	Before this commit, `clone` gave different results depending on whether the original object
    	had an attached singleton class or not.
    
    	Consider the following setup:
    	```
    	class Foo; end
    	Foo.singleton_class.define_method(:foo) {}
    
    	obj = Foo.new
    
    	obj.singleton_class if $call_singleton
    
    	clone = obj.clone
    	```
    
    	When `$call_singleton = false`, neither `obj.singleton_class.singleton_class` nor
    	`clone.singleton_class.singleton_class` own any methods.
    
    	However, when `$call_singleton = true`, `clone.singleton_class.singleton_class` would own a copy of
    	`foo` from `Foo.singleton_class`, even though `obj.singleton_class.singleton_class` does not.
    
    	The latter case is unexpected and results in a visibly different clone, depending on if the original object
    	had an attached class or not.
    
    Co-authored-by: default avatarUfuk Kayserilioglu <ufuk.kayserilioglu@shopify.com>
    	---
    	 class.c                 | 31 ++++++++++++++++++++++---------
    	 test/ruby/test_class.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++
    	 2 files changed, 69 insertions(+), 9 deletions(-)
    
    git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_6@67935 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Loading