Crystal: Invalid memory access when using generics

Created on 14 Nov 2017  路  2Comments  路  Source: crystal-lang/crystal

class Unimportant
end

abstract class Node
  abstract def call # replace abstract declaration with the definition below and it works
  # def call
  #   puts "YYY"
  #   [] of Unimportant
  # end
end

class InnerNode(T) < Node
  getter receiver : Node

  def initialize(value : T, @receiver : Node)
  end

  def call
    puts "InnerNode"
    receiver.call
  end
end

class EmptyNode < Node
  def call
    puts "EmptyNode"
  end
end

# A
c = InnerNode.new(true, EmptyNode.new)
p c.call

# B
c = InnerNode.new(5, EmptyNode.new)

# C
InnerNode.new("string", EmptyNode.new)

# ERROR
v = 1.to_i64 # remove to_i64 and it works

c = InnerNode.new(true, InnerNode.new(v, EmptyNode.new))
c.call

Crashes with:

InnerNode
EmptyNode
nil
InnerNode
Invalid memory access (signal 11) at address 0x1a7bd60
[0x4509e5] *CallStack::print_backtrace:Int32 +117
[0x4450f6] __crystal_sigfault_handler +70
[0x7f56785fd390] ???
[0x1a7bd60] ???

Note:

  • does not crash when replacing the abstract declaration with a default implementation
  • does not crash when not converting 1 to Int64
  • does not crash when A, B or C are left out (i guess it has to do with the possible types for the generic or so)
  • The value of type T is not stored in the generic, its just a parameter
  • Does not seem to be an infinite recursion since i have puts statements in every implemented method

Happens in:

  • Crystal 0.23.1 [e2a1389e8] (2017-07-13) LLVM 3.8.1
  • Crystal 0.23.0+313 [ea4187c57] (2017-10-27)
  • ???

On linux mint.

Might be related to #2665. Feel free to close this if this is not a separate issue.

bug compiler topicgenerics

Most helpful comment

Does not crashes anymore

$ crystal test.cr
InnerNode
EmptyNode
nil
InnerNode
InnerNode
EmptyNode
$ crystal --version
Crystal 0.31.1 (2019-10-02)

LLVM: 8.0.1
Default target: x86_64-apple-macosx

All 2 comments

Looks a lot like #3990, though you've managed to reduce it farther than I did. I'd love to know what's going on with this one.

Does not crashes anymore

$ crystal test.cr
InnerNode
EmptyNode
nil
InnerNode
InnerNode
EmptyNode
$ crystal --version
Crystal 0.31.1 (2019-10-02)

LLVM: 8.0.1
Default target: x86_64-apple-macosx
Was this page helpful?
0 / 5 - 0 ratings

Related issues

asterite picture asterite  路  3Comments

jhass picture jhass  路  3Comments

relonger picture relonger  路  3Comments

pbrusco picture pbrusco  路  3Comments

Sija picture Sija  路  3Comments