The Crystal Programming Language Forum

Source line numbers within macros (`#<loc:...>`)

I found out that I can get the “right” line number (I mean, the source line from which the code is generated) within macro generated code using the #<loc:...> directive.
This is useful for error and failed tests messages, otherwise they point to the top of the macro itself.

Not wanting to write (and update) the file name and line numbers by hand, I came up with

#<loc:"{{ __FILE__.id }}",{{ __LINE__ }},0>

(it needs explicit quotes, the more intuitive #<loc:__FILE__,...> and #<loc:{{ __FILE__ }},...> do not work).

Out of curiosity, I thought I could generate that directive with a macro. I tried several variations of

macro fix_location
  #<loc:\{{ __FILE__ }},\{{ __LINE__ }},0>
end

and similar things, hoping delayed expansion, but no, I get those backslashes in the generated code, and the line of the macro anyways. What am I doing wrong? Or are directives a special case?
No big deal, just curiosity.

And should I do #<loc:push> / #<loc:pop> too?
It seems to automatically resume “normal” line numbers outside of the macro code though.

BTW, is #<loc:...> documented somewhere? I just found it by chance while spelunking.