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

Closure bug #3789

Closed
firejox opened this issue Dec 27, 2016 · 2 comments
Closed

Closure bug #3789

firejox opened this issue Dec 27, 2016 · 2 comments

Comments

@firejox
Copy link
Contributor

firejox commented Dec 27, 2016

require "c/pthread"

class FooThread
  @@current_thread_key = uninitialized LibC::PthreadKeyT
  @@key_once : LibC::PthreadOnceT = 0

  def start
    ret = 0
    LibC.pthread_once(pointerof(@@key_once), -> {
      ret = LibC.pthread_key_create(pointerof(@@current_thread_key), ->(data : Void*) {
        data.as(Thread)
      })
    })
  end
end

FooThread.new.start

lib LibC
  alias PthreadOnceT = Int
  alias PthreadKeyT = UInt

  fun pthread_once(key : PthreadOnceT*, init_routine : -> Void) : Int
  fun pthread_key_create(key : PthreadKeyT*, destructor : Void* -> Void) : Int
end

result in compiler error

Missing hash key: "data"
0x752ed7: *CallStack::unwind:Array(Pointer(Void)) at ??
0x752e6a: *CallStack#initialize:Array(Pointer(Void)) at ??
0x752e3a: *CallStack::new:CallStack at ??
0x7461fe: *raise<KeyError>:NoReturn at ??
0xfdf75a: *Hash(String, Crystal::MetaVar) at ??
0xfdf656: *Hash(String, Crystal::MetaVar) at ??
0x137a6b9: *Crystal::CleanupTransformer::ClosuredVarsCollector#visit<Crystal::Var+>:(Array(Crystal::ASTNode+) | Nil) at ??
0xbd2541: *Crystal::ASTNode+ at ??
0xcf5aaa: *Crystal::Cast#accept_children<Crystal::CleanupTransformer::ClosuredVarsCollector>:Nil at ??
0xbd2825: *Crystal::ASTNode+ at ??
0xd7ed62: *Crystal::Def+ at ??
0xd7eb3f: *Crystal::Def+ at ??
0xd536fa: *Crystal::ProcLiteral#accept_children<Crystal::CleanupTransformer::ClosuredVarsCollector>:Nil at ??
0xbd2ad6: *Crystal::ASTNode+ at ??
0xedd801: *Crystal::Call#accept_children<Crystal::CleanupTransformer::ClosuredVarsCollector>:Nil at ??
0xbd2ffb: *Crystal::ASTNode+ at ??
0xeb081d: *Crystal::Assign#accept_children<Crystal::CleanupTransformer::ClosuredVarsCollector>:Nil at ??
0xbd2f47: *Crystal::ASTNode+ at ??
0xd7ed62: *Crystal::Def+ at ??
0xd7eb3f: *Crystal::Def+ at ??
0x137a527: *Crystal::CleanupTransformer::ClosuredVarsCollector::collect<Crystal::Def+>:Array(Crystal::ASTNode+) at ??
0x1374f16: *Crystal::CleanupTransformer#check_args_are_not_closure<Crystal::Call, String>:Array(Crystal::ASTNode+) at ??
0x1373849: *Crystal::CleanupTransformer#transform<Crystal::Call>:Crystal::ASTNode+ at ??
0xbd0e96: *Crystal::ASTNode+ at ??
0x136f99d: *Crystal::CleanupTransformer#transform<Crystal::Expressions>:Crystal::Expressions at ??
0xbd0d9c: *Crystal::ASTNode+ at ??
0x1373f0d: *Crystal::CleanupTransformer#transform<Crystal::Call>:Crystal::ASTNode+ at ??
0xbd0e96: *Crystal::ASTNode+ at ??
0x136f99d: *Crystal::CleanupTransformer#transform<Crystal::Expressions>:Crystal::Expressions at ??
0xbd0d9c: *Crystal::ASTNode+ at ??
0x893406: *Crystal::Program#cleanup<Crystal::ASTNode+>:Crystal::ASTNode+ at ??
0x8930c5: *Crystal::Program#visit_main<Crystal::ASTNode+, Crystal::MainVisitor, Bool>:Crystal::ASTNode+ at ??
0x892fee: *Crystal::Program#visit_main:process_finished_hooks<Crystal::ASTNode+, Bool>:Crystal::ASTNode+ at ??
0x888219: *Crystal::Program#semantic<Crystal::ASTNode+, Bool>:Crystal::ASTNode+ at ??
0xfd37e4: *Crystal::Compiler#compile<Array(Crystal::Compiler::Source), String>:Crystal::Compiler::Result at ??
0xfda219: *Crystal::Command::CompilerConfig#compile<String>:Crystal::Compiler::Result at ??
0x86018d: *Crystal::Command#run_command<Bool>:Nil at ??
0x85c281: *Crystal::Command#run:(Array(Crystal::ImplementationTrace) | Array(Crystal::Init::View+:Class) | Array(String) | Bool | Crystal::Compiler::Result | Hash(String, String) | IO::FileDescriptor | Nil) at ??
0x85b97b: *Crystal::Command::run<Array(String)>:(Array(Crystal::ImplementationTrace) | Array(Crystal::Init::View+:Class) | Array(String) | Bool | Crystal::Compiler::Result | Hash(String, String) | IO::FileDescriptor | Nil) at ??
0x85b939: *Crystal::Command::run:(Array(Crystal::ImplementationTrace) | Array(Crystal::Init::View+:Class) | Array(String) | Bool | Crystal::Compiler::Result | Hash(String, String) | IO::FileDescriptor | Nil) at ??
0x72ff49: ??? at ??
0x73eae9: main at ??
0x7f7a8eba1291: __libc_start_main at ??
0x72f0ca: _start at ??
0x0: ??? 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

Crystal version

Crystal 0.20.3+2 [929a551] (2016-12-27)
Crystal 0.20.1 (2016-12-06)

@firejox firejox changed the title closure bug Closure bug Dec 27, 2016
@asterite
Copy link
Member

asterite commented Dec 27, 2016

Reduced:

lib LibFoo
  fun foo(init_routine : ->)
end

x = 0
LibFoo.foo(->{
  x = ->(data : Int32) {
    data
  }
})

@asterite asterite added this to the 0.20.4 milestone Dec 27, 2016
@asterite
Copy link
Member

@firejox Thank you for reporting! In the next version you will get a correct error:

Error in bar.cr:17: instantiating 'FooThread#start()'

FooThread.new.start
              ^~~~~

in bar.cr:9: can't send closure to C function (closured vars: ret)

    LibC.pthread_once(pointerof(@@key_once), ->{
                                             ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants