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

Make #is_a? in macros respect the AST node hierarchy #11062

Merged
merged 1 commit into from
Aug 12, 2021

Conversation

HertzDevil
Copy link
Contributor

Currently, is_a? in macros compares the class names directly:

{{ [1, 2, 3].is_a?(ASTNode) }} # => false

# the above is akin to the following in normal code:
[1, 2, 3].class.name == Object.class.name # => false

Since Crystal::ASTNode is an abstract class, it follows that is_a?(ASTNode) always fails, even though every AST node obviously inherits from that class, both in the macro language and internally. This PR makes the internal AST node hierarchy available here, so is_a?(ASTNode) becomes true.

More useful is the ability to match abstract AST node types that aren't ASTNode:

macro foo(x)
  {% puts x.exp if x.is_a?(UnaryExpression) %}
end

foo !true         # => true
foo sizeof(Int32) # => Int32
foo *[1, 2, 3]    # => [1, 2, 3]
foo 4 + 5
foo /bar/

@beta-ziliani beta-ziliani added this to the 1.2.0 milestone Aug 10, 2021
Copy link
Member

@beta-ziliani beta-ziliani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks once again 🙏

@straight-shoota straight-shoota merged commit 5410318 into crystal-lang:master Aug 12, 2021
@HertzDevil HertzDevil deleted the feature/macro-is-a branch August 12, 2021 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants