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

Consider shortened debug output when there is a failure in a macro #4700

Closed
paulcsmith opened this issue Jul 10, 2017 · 1 comment
Closed

Comments

@paulcsmith
Copy link
Contributor

paulcsmith commented Jul 10, 2017

Let's say I have something like this that generates quite a bit of code

macro gen_tag_helpers
  {% for tag in [:a, :img, :hr, :div, :many_more_tags] %}
    def {{ tag.id }}(text)
      puts text.upcase
    end
  {% end %}
end

gen_tag_helpers

not_string = 1
# This will blow up because `1` does not implement `upcase`
img(not_string)

The output is this:

Error in macro_errors.cr:12: instantiating 'img(Int32)'

img(not_string)
^~~

in macro_errors.cr:9: expanding macro

gen_tag_helpers
^

in macro 'gen_tag_helpers' /Users/paulcsmith/learning/crystal-playground/macro_errors.cr:1, line 7:

   1.
   2.     def a(text)
   3.       puts text.upcase
   4.     end
   5.
   6.     def img(text)
>  7.       puts text.upcase
   8.     end
   9.
  10.     def hr(text)
  11.       puts text.upcase
  12.     end
  13.
  14.     def div(text)
  15.       puts text.upcase
  16.     end
  17.
+ many many more lines!

undefined method 'upcase' for Int32
Rerun with --error-trace to show a complete error trace.

The problem here is that I would expect this to be last

img(not_string)
^~~

It is super helpful that the compiler prints the macro so you can see what's wrong, but sometimes macros generate a bunch of code so finding the line that's the problem can be quite difficult. I wonder if it could print just some of the code. Maybe the 10 lines around the failure area with a compiler flag for showing everything if it's not enough. In my experience only a few lines of context are needed to see what the problem is.

It may also be helpful to colorize the line as :red so it draws attention, but that may be a separate issue.

Thanks for taking a look. Crystal rocks!

@asterite
Copy link
Member

The last line in the error output is:

undefined method 'upcase' for Int32

The compiler always shows the relevant error last, because that's what you immediately see above the prompt. I don't think we'll change this.

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

No branches or pull requests

2 participants