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 results of Crystal::ASTNode::Call#id #14500

Open
HertzDevil opened this issue Apr 16, 2024 · 6 comments
Open

Inconsistent results of Crystal::ASTNode::Call#id #14500

HertzDevil opened this issue Apr 16, 2024 · 6 comments

Comments

@HertzDevil
Copy link
Contributor

After #14490, I checked if any other #to_macro_id implementation is also problematic. This is the one for Call:

def to_macro_id
if !obj && !block && args.empty?
@name
else
to_s
end
end

It looks like the @name branch is for Call nodes that have no arguments at all, but this misses a few cases:

macro foo(x)
  {{ x.id }}
end

foo(bar(&a))   # => bar
foo(bar(a: 1)) # => bar

It also most certainly suffers from the same issue as #14489 (comment) regarding global Calls:

foo(::bar)       # => bar
foo(::bar(1))    # => ::bar(1)
foo(::bar(a: 1)) # => bar
@straight-shoota
Copy link
Member

The implementation should probably just delegate to #to_s.

I'm even wondering why we even have a separate #to_macro_id. Shouldn't #to_s always be correct?

@HertzDevil
Copy link
Contributor Author

HertzDevil commented Apr 16, 2024

#to_s would preserve e.g. the single or double quotation marks of a CharLiteral or StringLiteral, which we do not want.

@straight-shoota
Copy link
Member

Yeah right. Besides these exceptions?

@HertzDevil
Copy link
Contributor Author

But those exceptions are precisely why #to_s isn't "always" correct?

@straight-shoota
Copy link
Member

I mean for every other type than CharLiteral and StringLiteral, #to_macro_id could be identical to #to_s.

@HertzDevil
Copy link
Contributor Author

There is also SymbolLiteral#to_macro_id which drops the leading :. But I think the point is that the defined overloads avoid having to create a fresh Crystal::ToSVisitor every time ASTNode#id is called.

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

2 participants