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

ICE with untyped Array as instance var initializer #5383

Closed
xfbs opened this issue Dec 14, 2017 · 9 comments · Fixed by #6128
Closed

ICE with untyped Array as instance var initializer #5383

xfbs opened this issue Dec 14, 2017 · 9 comments · Fixed by #6128
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler

Comments

@xfbs
Copy link

xfbs commented Dec 14, 2017

I don't really know how to search if this bug has been posted before because I'm not sure what to search for, the error I'm getting is rather cryptic. This is the error I'm getting:

$ crystal build --verbose paths.cr 
cast from Nil to Crystal::GenericInstanceType+ failed, at /private/tmp/crystal-lang-20171012-68849-1f0n4y5/crystal-0.23.1/src/compiler/crystal/types.cr:1420:20:1420
0x10e81dc58: *raise<TypeCastError>:NoReturn at ??
0x10eee0cd4: *Crystal::TypeParameter#solve<Crystal::NonGenericClassType>:Crystal::ASTNode+ at ??
0x10eee1990: *Crystal::GenericInstanceType+@Crystal::GenericInstanceType#replace_type_parameters<Crystal::NonGenericClassType>:Crystal::Type+ at ??
0x10eedea0a: *Crystal::TypeDeclarationProcessor#process_owner_guessed_instance_var_declaration<Crystal::Type+, String, Crystal::TypeDeclarationProcessor::InstanceVarTypeInfo>:(Crystal::MetaTypeVar | Crystal::TypeDeclarationProcessor::Error | Nil) at ??
0x10eed86a3: *Crystal::TypeDeclarationProcessor#process_instance_vars_declarations:Nil at ??
0x10ee2adeb: *Crystal::Program#top_level_semantic<Crystal::ASTNode+>:Tuple(Crystal::ASTNode+, Crystal::TypeDeclarationProcessor) at ??
0x10ee24e09: *Crystal::Program#semantic<Crystal::ASTNode+, Bool>:Crystal::ASTNode+ at ??
0x10f2a35b5: *Crystal::Compiler#compile<Array(Crystal::Compiler::Source), String>:Crystal::Compiler::Result at ??
0x10e85bf8c: *Crystal::Command#run:(Bool | Crystal::Compiler::Result | IO::FileDescriptor | Nil) at ??
0x10e832ff6: main at ??

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 is the minimal file to trigger the bug:

class Paths
  def initialize(x, y)
    @count = Array.new(max_x){Array.new(max_y)}
  end
end

And this is the version of the compiler I'm using:

$ crystal -v
Crystal 0.23.1 (2017-10-12) LLVM 4.0.1

I'm running crystal on an OS X 10.11 El Capitan, and I installed crystal with homebrew if that makes any difference. Btw kudos to the team, I've really enjoyed playing with the language in the past few days!

@mig-hub
Copy link
Contributor

mig-hub commented Dec 14, 2017

@xfbs, Where do max_x and max_y come from? Is it a typo and you meant to put these names for the arguments of the method? or does the bug come from the fact that these variables don't exist?

@xfbs
Copy link
Author

xfbs commented Dec 14, 2017

Oh, my bad. I guess I simplified the code too much before posting. So the original code that triggers the bug was something like this:

class Paths
  def initialize(x, y)
    max_x = 0
    max_y = 0
    @count = Array.new(max_x){Array.new(max_y)}
  end
end

So, both max_x and max_y exist. I think what triggers the bug is because I didn't specify the type of the Array — this code works:

Array(Array(Int32)).new(max_x){Array(Int32).new(max_y)}

I'm wildly guessing here that the compiler doesn't like the fact that I've omitted the type specifiers, and that triggered some kind of internal bug?

@RX14 RX14 added topic:compiler kind:bug A bug in the code. Does not apply to documentation, specs, etc. labels Dec 14, 2017
@bew
Copy link
Contributor

bew commented Dec 14, 2017

Minimal:

Array.new(0) { Array.new(0) }

Fixed:

Array.new(0) { Array(Int32).new(0) }

@RX14
Copy link
Contributor

RX14 commented Dec 14, 2017

Can anyone confirm if this is fixed by #4974? (and by extension 0.24.1) I'm 98% sure it's a dupe.

@larubujo
Copy link
Contributor

same same but different. crashes in master

@RX14
Copy link
Contributor

RX14 commented Dec 14, 2017

Works for me on master:

$ bin/crystal /tmp/test.cr
Using compiled compiler at `.build/crystal'
Error in /tmp/test.cr:1: instantiating 'Array(T):Class#new(Int32)'

Array.new(0) { Array.new(0) }
                     ^~~

in src/array.cr:77: can't infer the type parameter T for the generic class Array(T). Please provide it explicitly

  def initialize(initial_capacity : Int)

Did you make sure to make crystal before checking @larubujo?

@RX14 RX14 closed this as completed Dec 14, 2017
@larubujo
Copy link
Contributor

@RX14 try this, which is original/similar code by op:

class Paths
  def initialize
    @count = Array.new(0) { }
  end
end

@larubujo
Copy link
Contributor

also for this, which is op code:

class Paths
  def initialize(x, y)
    max_x = 0
    max_y = 0
    @count = Array.new(max_x) { Array.new(max_y) }
  end
end

failure on both

@RX14
Copy link
Contributor

RX14 commented Dec 15, 2017

Thanks. Seems using any type of generic parameter inference is broken but only at the class level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants