Skip to content

Commit

Permalink
Fixed: can't dup class inheriting abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Dec 18, 2016
1 parent b8334ae commit 02f5555
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions spec/std/reference_spec.cr
Expand Up @@ -25,6 +25,16 @@ module ReferenceSpec

def_clone
end

abstract class Abstract
end

class Concrete < Abstract
property x

def initialize(@x : Int32)
end
end
end

describe "Reference" do
Expand Down Expand Up @@ -79,6 +89,14 @@ describe "Reference" do
duplicate.y.should be(original.y)
end

it "can dup class that inherits abstract class" do
original = ReferenceSpec::Concrete.new(2).as(ReferenceSpec::Abstract)
duplicate = original.dup
duplicate.should be_a(ReferenceSpec::Concrete)
duplicate.should_not be(original)
duplicate.x.should eq(original.x)
end

it "clones with def_clone" do
original = ReferenceSpec::DupCloneClass.new
clone = original.clone
Expand Down
6 changes: 5 additions & 1 deletion src/reference.cr
Expand Up @@ -37,7 +37,11 @@ class Reference
# This allocates a new object and copies the contents of
# `self` into it.
def dup
{% begin %}
{% if @type.abstract? %}
# This shouldn't happen, as the type is abstract,
# but we need to avoid the allocate invocation below
raise "can't dup {{@type}}"
{% else %}
dup = {{@type}}.allocate
dup.as(Void*).copy_from(self.as(Void*), instance_sizeof({{@type}}))
dup
Expand Down

0 comments on commit 02f5555

Please sign in to comment.