Crystal: Using with ... yield twice

Created on 1 Nov 2017  路  7Comments  路  Source: crystal-lang/crystal

The following prints almost two pages of weird characters on my screen. Also, the output changes from run to run. The code is insane, I no longer remember what I was trying to accomplish with it and it should not actually compile.

def insane_method(value)
  with value yield
  with self yield
end

puts insane_method(:abc) { itself }

I am using Crystal 0.23.1 [e2a1389] (2017-07-13) LLVM 3.8.1 on ubuntu 16.04 LTS.

My guess is that the problem here has to do with how only one of the two receivers responds to itself, but somehow the compiler does not catch that.

Moreover, trying to build with --release results in a failure during the codegen phase with the following error:

crystal: /var/cache/omnibus/src/llvm/llvm-3.8.1.src/lib/CodeGen/LexicalScopes.cpp:160: llvm::LexicalScope* llvm::LexicalScopes::getOrCreateRegularScope(const llvm::DILocalScope*): Assertion `cast<DISubprogram>(Scope)->describes(MF->getFunction())' failed.
/usr/bin/crystal: line 102: 433222 Aborted                 "$INSTALL_DIR/embedded/bin/crystal" "$@"
bug compiler

Most helpful comment

No, it's a bug in the compiler

  1. There's no self there
  2. Even if it's in a class it breaks. with ... yield can't receive two different types (or maybe yes, but it should be the union, I don't know)

All 7 comments

This is a bug in LLVM 3.8. Updating to LLVM 3.9 fixes this.

See https://github.com/crystal-lang/crystal/issues/4719

No, it's a bug in the compiler

  1. There's no self there
  2. Even if it's in a class it breaks. with ... yield can't receive two different types (or maybe yes, but it should be the union, I don't know)

I now tried with the crystal version at https://github.com/crystal-lang/crystal/releases/download/0.24.0/crystal-0.24.0-2-linux-x86_64.tar.gz, which reports itself as

Crystal 0.23.0+313 [ea4187c57] (2017-10-27)

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

This time I get the following error when running it:

Invalid memory access (signal 11) at address 0x4
[0x44b335] *CallStack::print_backtrace:Int32 +117
[0x4402f6] __crystal_sigfault_handler +70
[0x7f5869e1b390] ???
[0x44d075] *String#to_slice:Slice(UInt8) +21
[0x44dce8] *String#to_s<IO::FileDescriptor>:Nil +24
[0x45b6a1] *IO::FileDescriptor +17
[0x485ddd] *Symbol#to_s<IO::FileDescriptor>:IO::FileDescriptor +45
[0x45c7b5] *IO::FileDescriptor +21
[0x45c77e] *IO::FileDescriptor +14
[0x440335] *puts<Symbol>:Nil +37
[0x4337ee] __crystal_main +1598
[0x4404b6] *_crystal_main<Int32, Pointer(Pointer(UInt8))>:Nil +6
[0x486306] *Crystal::main_user_code<Int32, Pointer(Pointer(UInt8))>:Nil +6
[0x4861d2] *Crystal::main<Int32, Pointer(Pointer(UInt8))>:Int32 +50
[0x43e6f6] main +6
[0x7f586943e830] __libc_start_main +240
[0x4330d9] _start +41
[0x0] ???

Compilation with --release still fails with:

crystal: /var/cache/omnibus/src/llvm/llvm-3.9.1.src/lib/CodeGen/LexicalScopes.cpp:159: llvm::LexicalScope* llvm::LexicalScopes::getOrCreateRegularScope(const llvm::DILocalScope*): Assertion `cast<DISubprogram>(Scope)->describes(MF->getFun
ction())' failed.
/m/home/home8/82/jbomanso/data/Downloads/crystal-0.24.0-2/bin/crystal: line 102: 437332 Aborted                 "$INSTALL_DIR/embedded/bin/crystal" "$@"

I agree with "with ... yield can't receive two different types" and I implement such a feature at this branch as an experiment.

Also happens with this:

def foo
  with 1 yield
  with 1.5 yield
end

foo do
  p abs
end

So it has nothing to do with self.

That's a bit more difficult to fix.

These days we get a validation error for this:

Module validation failed: Call parameter type does not match function signature!

Checked in

$ crystal --version
Crystal 0.31.1 (2019-10-02)

LLVM: 8.0.1
Default target: x86_64-apple-macosx

Original example seem to be fixed now

$ crystal test.cr
Showing last frame. Use --error-trace for full trace.

In test.cr:3:8

 3 | with self yield
          ^---
Error: there's no self in this scope

Ary's example still fails with

Module validation failed: Call parameter type does not match function signature!
Was this page helpful?
0 / 5 - 0 ratings

Related issues

farleyknight picture farleyknight  路  64Comments

stugol picture stugol  路  70Comments

akzhan picture akzhan  路  67Comments

asterite picture asterite  路  139Comments

xtagon picture xtagon  路  132Comments