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

Inconsistent {{@type}} expansion in macros #9099

Open
bcardiff opened this issue Apr 16, 2020 · 2 comments
Open

Inconsistent {{@type}} expansion in macros #9099

bcardiff opened this issue Apr 16, 2020 · 2 comments

Comments

@bcardiff
Copy link
Member

If the macro is inside a module, the @type is referring to the container module instead of the type where the macro is expanded.

Running the docs tool with the following code exhibits the differences. But the issue affects also the generated code.

I was expecting to always be the type were the macro is executed (since the otherone can be explicitly mentioned)

module XXX
  macro m
    # This will be XXX (not ideal) `{{@type}}`
    def foo
      {{@type}}
    end
  end
end

macro m
  # This will be Foo (ideal) `{{@type}}`
  def bar
    {{@type}}
  end
end

class Foo
  XXX.m
  m
end

pp! Foo.new.foo # => XXX
pp! Foo.new.bar # => Foo

Extracted from #8994

@asterite
Copy link
Member

Right now you can do:

UInt8.slice(1, 2, 3)

That generates:

Slice(UInt8)...

The macro uses {{@type}} to get the UInt8 type.

Essentially, it seems there's no way to fix this unless we have a way to refer to where the macro is called, and where the macro is defined.

@asterite
Copy link
Member

Right now self inside macros gives an error because it has no meaning. We could make self work for this purpose: it's the type where the macro is defined. On the other hand, @type is the type where the macro is expanded.

When the macro happens inside a method, like:

class Foo
  def foo
    {{@type}}
  end
end

then @type and self will be the same. In the examples provided by @bcardiff @type will work as he expects it, which I think is what's expected, and self will return XXX inside XXX.m but will give an error saying "there's no self" inside the top-level macro m... just like what happens with regular methods.

However, this is a breaking change.

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

Successfully merging a pull request may close this issue.

2 participants