crystal
crystal copied to clipboard
Report line number in: "Error: instance variable 'x' of 'Y' must be ..."
trafficstars
$ crystal test.cr
Showing last frame. Use --error-trace for full trace.
Error: instance variable '@x' of Test must be Int32, not Nil
Instance variable '@x' was used before it was initialized in one of the 'initialize' methods, rendering it nilable
Can this error message be improved to include the filename and line number? Similar to how other errors print it, like:
$ crystal test.cr
Showing last frame. Use --error-trace for full trace.
In src/test.cr:130:19
130 | @x[i] = n
^
Error: no overload matches 'Hash(String, Int32 | Nil)#[]=' with types String, (Int16 | Int32)
Could you include code to reproduce this? Thank you!
class X
def initialize
10.times do
if @x = "test" # =, not ==
end
end
end
end
X.new
https://carc.in/#/r/a3uu
Showing last frame. Use --error-trace for full trace.
Error: instance variable '@x' of X must be String, not Nil
Instance variable '@x' was used before it was initialized in one of the 'initialize' methods, rendering it nilable
If relevant, just mentioning that this issue is still present on 1.13.1.
Using the same example code:
crystal test.cr
Showing last frame. Use --error-trace for full trace.
Error: instance variable '@x' of X must be String, not Nil
Instance variable '@x' was used before it was initialized in one of the 'initialize' methods, rendering it nilable
A quick fix is to add --error-trace:
crystal run --error-trace test.cr
In test.cr:10:3
10 | X.new
^--
Error: instantiating 'X.new()'
Error: instance variable '@x' of X must be String, not Nil
Instance variable '@x' was used before it was initialized in one of the 'initialize' methods, rendering it nilable