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

Explicitly returning a lib struct or tuple inside a fun results in a codegen error #14392

Open
BlobCodes opened this issue Mar 22, 2024 · 0 comments

Comments

@BlobCodes
Copy link
Contributor

Bug Report

Any code explicitly returning a lib struct or a tuple inside a fun results in a codegen error.

Here's an example (also works with empty prelude):

lib LibFoo
  struct Bar
    value : Int32
  end
end

fun foo : LibFoo::Bar
  return LibFoo::Bar.new
end

This results in the following compilation error:

Module validation failed: Function return type does not match operand type of return inst!
  ret %"struct.LibFoo::Bar" %2, !dbg !668
 { i64 } (Exception)
  from /crystal/src/string.cr:5488:31 in 'codegen'
  from /crystal/src/compiler/crystal/compiler.cr:195:16 in 'compile:combine_rpath'
  from /crystal/src/compiler/crystal/compiler.cr:188:56 in 'compile:combine_rpath'
  from /crystal/src/compiler/crystal/command/eval.cr:30:5 in 'eval'
  from /crystal/src/compiler/crystal/command.cr:125:12 in 'run'
  from /crystal/src/compiler/crystal.cr:11:1 in '__crystal_main'
  from /crystal/src/crystal/main.cr:129:5 in 'main'
  from src/env/__libc_start_main.c:95:2 in 'libc_start_main_stage2'

Another example:

fun foo : {Int32}
  return {0}
end

..which results in this error:

Module validation failed: Function return type does not match operand type of return inst!
  ret %"Tuple(Int32)" %2, !dbg !668
 { i64 } (Exception)
  from /crystal/src/string.cr:5488:31 in 'codegen'
  from /crystal/src/compiler/crystal/compiler.cr:195:16 in 'compile:combine_rpath'
  from /crystal/src/compiler/crystal/compiler.cr:188:56 in 'compile:combine_rpath'
  from /crystal/src/compiler/crystal/command/eval.cr:30:5 in 'eval'
  from /crystal/src/compiler/crystal/command.cr:125:12 in 'run'
  from /crystal/src/compiler/crystal.cr:11:1 in '__crystal_main'
  from /crystal/src/crystal/main.cr:129:5 in 'main'
  from src/env/__libc_start_main.c:95:2 in 'libc_start_main_stage2'

System information

$ crystal -v
Crystal 1.11.2 [fda656c71] (2024-01-18)

LLVM: 15.0.7
Default target: x86_64-unknown-linux-gnu
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