Skip to content

Commit 7c9f793

Browse files
committed
BasicObject.dup should be TypeError: can't copy the root class
1 parent 9d7fe49 commit 7c9f793

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

spec/ruby/core/class/dup_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,7 @@ def self.message
6161
CoreClassSpecs::RecordCopy.name.should == "CoreClassSpecs::RecordCopy"
6262
end
6363

64+
it "raises TypeError if called on BasicObject" do
65+
-> { BasicObject.dup }.should raise_error(TypeError, "can't copy the root class")
66+
end
6467
end

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,12 @@ protected RubyDynamicObject copyRubyDynamicObject(RubyDynamicObject self,
518518

519519
@Specialization
520520
protected RubyDynamicObject copyRubyClass(RubyClass self,
521-
@Cached @Exclusive CopyInstanceVariablesNode copyInstanceVariablesNode) {
521+
@Cached @Exclusive CopyInstanceVariablesNode copyInstanceVariablesNode,
522+
@Cached InlinedBranchProfile rootClassProfile) {
523+
if (self == coreLibrary().basicObjectClass) {
524+
rootClassProfile.enter(this);
525+
throw new RaiseException(getContext(), coreExceptions().typeError("can't copy the root class", this));
526+
}
522527
var newClass = new RubyClass(coreLibrary().classClass, getLanguage(), getEncapsulatingSourceSection(),
523528
null, null, false, null, self.superclass);
524529
copyInstanceVariablesNode.execute(newClass, self);

0 commit comments

Comments
 (0)