crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.46k stars 1.62k forks source link

cast from Nil to Crystal::ProcInstanceType failed #11653

Open Blacksmoke16 opened 2 years ago

Blacksmoke16 commented 2 years ago
module Foo; end

struct CallbackProc
  def self.new(&block : Foo -> Nil)
    block
  end
end

callback = CallbackProc.new do |foo|
  foo.some_missing_method
end

https://play.crystal-lang.org/#/r/chwu

Produces:

cast from Nil to Crystal::ProcInstanceType failed, at /home/george/dev/git/crystal/src/compiler/crystal/semantic/bindings.cr:493:7:493 (TypeCastError)
from /home/george/dev/git/crystal/src/compiler/crystal/semantic/bindings.cr:492:5 in 'return_type'
from /home/george/dev/git/crystal/src/compiler/crystal/codegen/codegen.cr:593:27 in 'visit'
from /home/george/dev/git/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
from /home/george/dev/git/crystal/src/compiler/crystal/codegen/codegen.cr:2281:7 in 'accept'
from /home/george/dev/git/crystal/src/compiler/crystal/codegen/call.cr:331:5 in 'codegen_call_with_block_as_fun_literal'
from /home/george/dev/git/crystal/src/compiler/crystal/codegen/call.cr:30:9 in 'visit'
from /home/george/dev/git/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
from /home/george/dev/git/crystal/src/compiler/crystal/codegen/codegen.cr:2281:7 in 'accept'
from /home/george/dev/git/crystal/src/compiler/crystal/codegen/codegen.cr:1033:9 in 'codegen_assign'
from /home/george/dev/git/crystal/src/compiler/crystal/codegen/codegen.cr:997:7 in 'visit'
from /home/george/dev/git/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
from /home/george/dev/git/crystal/src/compiler/crystal/codegen/codegen.cr:2281:7 in 'accept'
from /home/george/dev/git/crystal/src/compiler/crystal/codegen/codegen.cr:667:9 in 'visit'
from /home/george/dev/git/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
from /home/george/dev/git/crystal/src/compiler/crystal/codegen/codegen.cr:2281:7 in 'accept'
from /home/george/dev/git/crystal/src/compiler/crystal/codegen/codegen.cr:69:7 in 'codegen'
from /home/george/dev/git/crystal/src/compiler/crystal/codegen/codegen.cr:67:5 in 'codegen:debug:single_module'
from /home/george/dev/git/crystal/src/compiler/crystal/progress_tracker.cr:22:7 in 'codegen'
from /home/george/dev/git/crystal/src/compiler/crystal/compiler.cr:173:16 in 'compile'
from /home/george/dev/git/crystal/src/compiler/crystal/command.cr:314:3 in 'compile'
from /home/george/dev/git/crystal/src/compiler/crystal/command.cr:218:14 in 'run_command'
from /home/george/dev/git/crystal/src/compiler/crystal/command.cr:127:7 in 'run'
from /home/george/dev/git/crystal/src/compiler/crystal/command.cr:51:5 in 'run'
from /home/george/dev/git/crystal/src/compiler/crystal/command.cr:50:3 in 'run'
from /home/george/dev/git/crystal/src/compiler/crystal.cr:11:1 in '__crystal_main'
from /home/george/dev/git/crystal/src/crystal/main.cr:115:5 in 'main_user_code'
from /home/george/dev/git/crystal/src/crystal/main.cr:101:7 in 'main'
from /home/george/dev/git/crystal/src/crystal/main.cr:127:3 in 'main'
from /usr/lib/libc.so.6 in '__libc_start_main'
from /home/george/dev/git/crystal/.build/crystal in '_start'
from ???

Happens on master as well as 1.2.2.

Seems to only happen if the argument to the proc is a module.

0x000def42 commented 2 years ago

Reproduction code looks similar to

module Foo; end
Proc(Foo, Nil).new do |foo|
  foo.some_missing_method
end

https://play.crystal-lang.org/#/r/cseq

It's produces:

Error: can't infer block return type, try to cast the block body with `as`. See: https://crystal-lang.org/reference/syntax_and_semantics/as.html#usage-for-when-the-compiler-cant-infer-the-type-of-a-block

I guess the same message would be suitable for the case above