crystal icon indicating copy to clipboard operation
crystal copied to clipboard

Report line number in: "Error: instance variable 'x' of 'Y' must be ..."

Open docelic opened this issue 4 years ago • 3 comments
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)

docelic avatar Dec 13 '20 09:12 docelic

Could you include code to reproduce this? Thank you!

asterite avatar Dec 13 '20 11:12 asterite

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

docelic avatar Dec 13 '20 12:12 docelic

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

docelic avatar Jul 28 '24 07:07 docelic