Error: you've found a bug in the Crystal compiler -- Module validation failed

When I get this compilation error:

Module validation failed: Function return type does not match operand type of return inst!
  ret ptr %14, !dbg !42
 %"->" = type { ptr, ptr } (Exception)
  from /crystal/src/llvm/module.cr:79:9 in 'codegen'
  from /crystal/src/compiler/crystal/compiler.cr:220:15 in 'compile'
  from /crystal/src/compiler/crystal/command.cr:360:5 in 'run'
  from /crystal/src/compiler/crystal.cr:11:1 in '__crystal_main'
  from /crystal/src/crystal/main.cr:118:5 in 'main'
  from src/env/__libc_start_main.c:95:2 in 'libc_start_main_stage2'

Is there any way for me to figure out exactly which module/function is causing the error?

I’ve found several issue that mention this error, but none really provide much background on identifying the specific cause in a complex code base, root cause, or workarounds.

Thanks!

AFAIK your best bet is to just keep commenting out code until it stops, then assess what is still present, trying to reproduce it by moving everything into 1 file, then further reduce from there.

1 Like

Not the answer that I was hoping for. :slight_smile:

I know, in general terms, what code is involved. This error happens, in one codebase, when either of the following methods is called:

    def initialize(@name)
    end

    def initialize(@name = "", &blk : Event ->)
      yield self
    end

i.e.

event = OpenTelemetry::Event.new("Boom!")

So my assumption is that it’s not specifically that method call that is causing the error, but rather, evaluation of the rest of the OpenTelemetry::Event class.

Because of the way OpenTelemetry’s implementation specification works, there is a bit of complexity involved. Basically, a do-nothing reference API is implemented, and the SDK inherits from it to provide the actual functionality.

So while I can start commenting things out to do a manual binary search to try to determine when the compilation starts working, it is definitely a hassle that I was hoping to avoid. The code does nothing esoteric, which is why the occurrence of this error is interesting.

I guess that I will start the tedious process of commenting things out. Thanks much for responding!

I think we can make this question more general.

The error message shows that the Crystal compiler tried to check the code’s types during the code generation stage, but it failed. The line “ret ptr %14, !dbg !42” looks like LLVM IR (Intermediate Representation). If we could produce LLVM IR—for example, by using the --emit llvm-ir option—it would make our investigation easier. However, since the compiler fails at this point, it cannot output the IR. So, how can we debug this problem if we can’t see the generated IR directly?


At a quick glance, it seems that the omission of the block/ploc type specification may be causing some problems. Also, referencing self outside initialize often leads to trouble.

1 Like