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

[BUG] "cast from Nil to Crystal::GenericInstanceType+ " #6390

Closed
TachoMex opened this issue Jul 15, 2018 · 8 comments · Fixed by #7743
Closed

[BUG] "cast from Nil to Crystal::GenericInstanceType+ " #6390

TachoMex opened this issue Jul 15, 2018 · 8 comments · Fixed by #7743
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:semantic

Comments

@TachoMex
Copy link

Hello, I found a bug while solving this

Here are some details:

cast from Nil to Crystal::GenericInstanceType+ failed, at /crystal/src/compiler/crystal/types.cr:1543:20:1543 (TypeCastError)
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
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
$ crystal -v
Crystal 0.25.1 [b782738ff] (2018-06-27)

LLVM: 4.0.0
Default target: x86_64-unknown-linux-gnu

$ uname -a
Linux ubuntu 4.15.0-23-generic #25-Ubuntu SMP Wed May 23 18:02:16 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

The source is:

# @matrix = File.read_lines("../081.in")
#              .map do |row|
#                row.split(',').map { |x| x.to_i }
#              end
matrix = [
  [131, 673, 234, 103, 18],
  [201, 96, 342, 965, 150],
  [630, 803, 746, 422, 111],
  [537, 699, 497, 121, 956],
  [805, 732, 524, 37, 331]
]

class P082
  Steps = [{ 1, 0 }, { -1, 0 }, { 0, 1 }]

  getter :best

  def initialize(matrix)
    @best = 10**9
    @n = matrix.size
    @matrix = matrix
    @visited = Array.new(N) { Array.new(N, false) }
  end

  def find(i, j, current)
    return if current > @best
    if j == @n - 1
      @best = [current, @best].min
      return
    end
    @visited[i][j] = true
    current += @matrix[i][j]
    Steps.each do |di, dj|
      new_i = di + i
      new_j = dj + j
      next if new_i < 0 || new_i >= @n
      next if new_j < 0 || new_j >= @n
      next if @visited[new_i][new_j]
      find(new_i, new_j, @visited, current)
    end

    current -= @matrix[i][j]
    @visited[i][j] = false
  end

  def solve
    (0...@n).each do |i|
      find(i, 0, 0)
    end
  end
end

solution = P082.new(matrix)
solution.solve
puts solution.best

I'm happy of helping at least with a bug reported.
If I can help don't hesitate on contact me.

Thanks guys and keep up the good job!

@asterite
Copy link
Member

Reduced:

class Foo
  def initialize
    @x = Array.new(10) { Array.new(10, false) }
  end
end

Foo.new

@Daniel-Worrall
Copy link
Contributor

Daniel-Worrall commented Jul 15, 2018

Minimal code is

class Foo
  def initialize
    @x= Array.new(1) { }
  end
end

Foo.new

It seems it fails due to it being an instance assign as, without it, it compiles as normal.

If N was still there, it would compile complaining about not inferring N.

E: Astersniped

@asterite
Copy link
Member

Reduced with --prelude=empty:

class Gen(T)
  def self.new(&block)
    Gen(T).build
  end

  def self.build : self
  end
end

class Foo
  def initialize
    @x = Gen.new { }
  end
end

Foo.new

You can workaround it by manually specifying the type of the instance variables:

class P082
  @visited : Array(Array(...))
  # etc.
end

@Daniel-Worrall
Copy link
Contributor

Daniel-Worrall commented Jul 15, 2018

In fact, this was addressed in #5383 (comment) but perhaps the merged fix addressed the title code and did not aim to solve the code in the comments which was why it was reopened.

@asterite
Copy link
Member

I think it's a slightly different scenario.

In any case you'll need to write those type declarations even after the bug is fixed.

Out of curiosity, what are the other 81 problems? :-)

@Daniel-Worrall
Copy link
Contributor

This comment specifically is the reduction that fails in this issue. #5383 (comment)

@TachoMex
Copy link
Author

I'm uploading them soon 😛 as starting a polyglot path and my first step is "Solve the first 100".
I'm very happy using crystal. I hope I can contribute to the shards stack soon, but first I have to code a little more.

I have done that as the work around.

@icy-arctic-fox
Copy link
Contributor

icy-arctic-fox commented Dec 13, 2018

Just encountered this with the following code:

class Example
  def initialize(@args = ARGV)
  end
end

https://play.crystal-lang.org/#/r/5s9l

cast from Nil to Crystal::GenericInstanceType+ failed, at /crystal/src/compiler/crystal/types.cr:1632:20:1632 (TypeCastError)

I think it's related to this issue because it's failing to figure out the type is Array(String) from ARGV.
A workaround is to use the following (explicitly defined types):

class Example
  def initialize(@args : Array(String) = ARGV)
  end
end

Not as elegant, but it works.

@asterite asterite added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:semantic labels May 3, 2019
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:semantic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants