Crystal: `typeof(rand() < 0.5 && nil || 2)` crashes playground hard

Created on 6 Aug 2016  Â·  6Comments  Â·  Source: crystal-lang/crystal

How to reproduce

Put the following expression into the playground as started with crystal play:

typeof(rand() < 0.5 && nil || 2)

This is what's visible in the shell that ran the playground.

➜  hello git:(master) ✗ crystal play
Listening on http://localhost:8080
Invalid memory access (signal 11) at address 0x0
[4335323760] __crystal_sigfault_handler +64
[140735760454954] _sigtramp +26
[4349732084] _ZN4llvm9IRBuilderILb1ENS_14ConstantFolderENS_24IRBuilderDefaultInserterILb1EEEE10CreateLoadEPNS_5ValueEPKc +52
[4345409006] *Crystal::CodeGenVisitor#to_rhs<LLVM::Value, Crystal::Type+>:LLVM::Value +830
[4345601800] *Crystal::CodeGenVisitor#codegen_return<Crystal::Type+>:(LLVM::Value | Nil) +264
[4345753008] *Crystal::CodeGenVisitor#finish:Hash(String, LLVM::Module) +64
[4345322698] *Crystal::Compiler#codegen<Crystal::Program, Crystal::Expressions, Array(Crystal::Compiler::Source), String>:(Array(String) | Nil) +5722
[4336634208] *Crystal::Compiler#compile<Array(Crystal::Compiler::Source), String>:Crystal::Compiler::Result +5472
[4345865067] *Crystal::Playground::Session#run<String, Int32>:(HTTP::Server::Response::Output | IO::FileDescriptor+ | Int32 | Nil) +4539
[4346123943] *HTTP::WebSocket#run:Nil +7991
[4346107566] *Crystal::Playground::PathWebSocketHandler#call<HTTP::Server::Context>:(Bool | HTTP::Server::Response | HTTP::Server::Response::Output | IO::FileDescriptor+ | Int32 | Nil) +1774
[4345907982] ~procProc(Nil)@./src/http/server.cr:146 +2030
[4335249062] *Fiber#run:(Int64 | Nil) +54

Meta

➜  hello git:(master) ✗ crystal --version && sw_vers
Crystal 0.18.7 (2016-07-27)
ProductName:    Mac OS X
ProductVersion: 10.11.6
BuildVersion:   15G31
➜  hello git:(master) ✗
bug compiler

Most helpful comment

I finally managed to upload a recording of the presentation I gave recently:

thumb

All 6 comments

I was able to reduce it to this:

def foo
  yield
end

x = typeof(nil && 1)
foo do
  if x
  end
end
x

Still can't figure out what's causing this.

I am impressed by the pace you are working at ! Crystal to me hits the sweet spot between ergonomics and performance, and seems truly _next-gen_ in everything it already has to offer.

I actually try to squash bugs as soon as possible, specially if they are easy to fix, otherwise the repo will be filled with these.

In this specific case the problem was bigger: when we designed the type-inference algorithm we set a couple of invariants that need to hold (basically, a node's type can only grow, it can go from Foo to Foo | Bar, but it can never shrink from Foo | Bar to Foo) , otherwise the inferred type wasn't correct or wasn't correctly propagated to other AST nodes. This invariant didn't hold in a few cases because of some incorrect optimizations I introduced, and the commit that fixed this brought that invariant back. I wanted to do this many times before but failed, but after a few similar bug reports I decided to give it one more chance, doing things slowly and with patience :-)

@Byron and thank you for your kind words :)

@asterite Anytime :) ! To be honest I feel as excited about Crystal as I did for Rust nearly two years ago (it was still 0.3 back then, but had a date for 1.0 set already). This basically means I will stay around, even though I have no immediate Project I would want to do in it.

This Friday I will also hold a brownbag @ThoughtWorks Germany about this _gem_. Good things ought to be shared. Something that will be noted is with which ease it was possible to compile the compiler directly from the repository. This basically means that a simple .crystal.version file will allow you to get reproducible builds even in an otherwise volatile, quickly changing environment. To my mind, this makes it usable even without stability guarantees. Awesome !
The first impression people get (i.e. _I got_) from Crystal is close to perfect, and this means that someone did a very, very good job ! Kudos to the team !

I finally managed to upload a recording of the presentation I gave recently:

thumb

Was this page helpful?
0 / 5 - 0 ratings