I just wanted to do something and then I wrote some code and then this happened:
cast from Nil to Crystal::GenericInstanceType+ failed, at /crystal/src/compiler/crystal/types.cr:1428:20:1428
???
???
???
???
???
???
???
???
???
???
???
???
Error: you've found a bug in the Crystal compiler. Please open an issue, including source code that will allow us to reproduce the bug: https://github.com/crystal-lang/crystal/issues
This error happens with the following code:
class MyClass
def initialize
@array = Array.new(Int32) {Array.new(9, "")}
end
end
myClass = MyClass.new
My crystal -v output:
Crystal 0.24.1 (2017-12-22)
LLVM: 4.0.0
Default target: x86_64-unknown-linux-gnu
I'm running Crystal on the Linux Subsystem for Windows 10.
I also tried running it with --no-debug. But the error is still happening.
The Array.new(Type) {} is invalid and Crystal will report it, but if it's assigned to an instance variable the Semantic (new) phase crashes (with debug information):
cast from Nil to Crystal::GenericInstanceType+ failed, at /opt/crystal-0.24.0/src/compiler/crystal/types.cr:1428:20:1428
???
/opt/crystal-0.24.0/src/compiler/crystal/types.cr:1428:9 in 'solve'
/opt/crystal-0.24.0/src/compiler/crystal/types.cr:1637:27 in 'replace_type_parameters'
/opt/crystal-0.24.0/src/compiler/crystal/semantic/type_declaration_processor.cr:171:14 in '???'
/opt/crystal-0.24.0/src/compiler/crystal/semantic/type_declaration_processor.cr:232:7 in 'process_instance_vars_declarations'
/opt/crystal-0.24.0/src/compiler/crystal/semantic/type_declaration_processor.cr:150:5 in 'top_level_semantic'
/opt/crystal-0.24.0/src/compiler/crystal/semantic.cr:22:23 in 'semantic'
/opt/crystal-0.24.0/src/compiler/crystal/compiler.cr:141:14 in 'compile'
Oh okay!
Still a bug though, it should report a proper error message ;)
I hit this error while trying to get a globally-accessible game map variable. Looks like #5383 is the same issue, as well.
The code I used:
width = 16
height = 16
module Map
@main = Array.new(width){ Array(Int32|Nil).new(height) {nil}}
def self.main
@main
end
end
And crystal --version says Crystal 0.24.1 (2017-12-22).
Looks like this works @ethankaminski
Array(Array(Int32 | Nil)).new(16) { Array.new(16) { nil } }
Duplicate of #5383
Most helpful comment
Still a bug though, it should report a proper error message ;)