Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to invoke macros on file private classes #3878

Closed
spalladino opened this issue Jan 12, 2017 · 3 comments
Closed

Unable to invoke macros on file private classes #3878

spalladino opened this issue Jan 12, 2017 · 3 comments

Comments

@spalladino
Copy link
Contributor

See #3858, by @makenowjust. This issue is here to keep track of the problem.

On Crystal 0.20.3, for instance:

private class Foo
end

Foo.new.dup

this code causes an error:

Error in foo.cr:4: instantiating 'Foo#dup()'

Foo.new.dup
        ^~~

in /.../src/reference.cr:40: expanding macro

    {% if @type.abstract? %}
    ^

in macro 'macro_XXX' /.../src/reference.cr:40, line 2:

   1.
>  2.       dup = Foo.allocate
   3.       dup.as(Void*).copy_from(self.as(Void*), instance_sizeof(Foo))
   4.       dup
   5.
@david50407
Copy link
Contributor

This is a simple patch to get Reference#dup work without changing macro: (But we still need to improve the macro system)

From 2b74a5a99eea3e7da6c62646ef609db5e7a6875e Mon Sep 17 00:00:00 2001
From: David Kuo <s50407s@gmail.com>
Date: Fri, 13 Jan 2017 21:42:00 +0800
Subject: [PATCH] Allow Reference#dup in private type

---
 src/reference.cr | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/reference.cr b/src/reference.cr
index 35e26d2..a09a8e5 100644
--- a/src/reference.cr
+++ b/src/reference.cr
@@ -44,8 +44,8 @@ class Reference
       # 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 = self.class.allocate
+      dup.as(Void*).copy_from(self.as(Void*), instance_sizeof(self))
       dup
     {% end %}
   end
--
2.7.4

tested with:

private class Foo
  property a = 1
end

f = Foo.new
p f.a

p f.a = 2
b = f.dup
p b.a

@asterite
Copy link
Member

@david50407 Doesn't work with:

class Foo
  property a = 1
end

class Bar < Foo
end

[Foo.new, Bar.new].map &.dup

This needs a compiler fix, there's no way to do it without changing the compiler.

@david50407
Copy link
Contributor

@asterite oops, I think I know why that fails

Should we create a macro system central-discussing issue like this #2665 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants