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

Codegen bug: Call parameter type does not match function signature #13209

Open
wolfgang371 opened this issue Mar 21, 2023 · 5 comments
Open

Codegen bug: Call parameter type does not match function signature #13209

wolfgang371 opened this issue Mar 21, 2023 · 5 comments

Comments

@wolfgang371
Copy link

wolfgang371 commented Mar 21, 2023

macro assert(invariant)
    raise("runtime_assert") if !({{invariant}}) # if you omit the () on the conditional, it compliles fine
end

def a(value : String)
    assert(value.is_a?(Int)) # original code had this method in a generic class, with value of type T
    value = [1,value].max # if you put "1" here, you get "BUG: trying to downcast NoReturn (Crystal::NoReturnType) <- String (Crystal::NonGenericClassType) (Exception)"
end

a("42")

# `crystal build fail.cr` yields:
# Invalid memory access (signal 11) at address 0x32356fb
# [0xd3d196] ???
# [0xd3d15a] ???
# [0x35cacc8] ???

I'm using Crystal 1.7.3 on Ubuntu 18.04.

@wolfgang371
Copy link
Author

The code is obviously nonsense, it's the result of stripping down a quite big chunk of code.
It seems to be very much related to the parenthesis I needed to introduce in my assert macro since I realized that assert(a != b) is susceptible to the very same missing macro parenthesis problem as in old C.

@wolfgang371 wolfgang371 changed the title Compiler crash related to macros and Compiler crash related to macros Mar 21, 2023
@straight-shoota
Copy link
Member

straight-shoota commented Mar 21, 2023

This is actually a codegen error, unrelated to any macros.
If you expand the macro, you get the same error:

def a(value : String)
    raise("runtime_assert") if !(value.is_a?(Int))
    value = [1,value].max
end

a("42")

Full stack trace with LLVM 15 (Crystal version 1.8.0-dev [3a360b1]):

Module validation failed: Call parameter type does not match function signature!
  %12 = load i8, ptr %11, align 1, !dbg !657
 ptr  %13 = call i32 @"*Array(Int32)@Enumerable(T)#max:Int32"(i8 %12), !dbg !657
 (Exception)
  from src/llvm/module.cr:72:9 in 'verify'
  from src/compiler/crystal/codegen/codegen.cr:378:9 in 'finish'
  from src/compiler/crystal/codegen/codegen.cr:76:7 in 'codegen'
  from src/compiler/crystal/codegen/codegen.cr:72:5 in 'codegen:debug:single_module'
  from src/compiler/crystal/progress_tracker.cr:22:7 in 'codegen'
  from src/compiler/crystal/compiler.cr:162:16 in 'compile'
  from src/compiler/crystal/command/eval.cr:29:5 in 'eval'
  from src/compiler/crystal/command.cr:97:7 in 'run'
  from src/compiler/crystal/command.cr:51:5 in 'run'
  from src/compiler/crystal/command.cr:50:3 in 'run'
  from src/compiler/crystal.cr:11:1 in '__crystal_main'
  from src/crystal/main.cr:115:5 in 'main_user_code'
  from src/crystal/main.cr:101:7 in 'main'
  from src/crystal/main.cr:127:3 in 'main'
  from /lib/x86_64-linux-gnu/libc.so.6 in '??'
  from /lib/x86_64-linux-gnu/libc.so.6 in '__libc_start_main'
  from /home/johannes/src/crystal-lang/crystal/.build/crystal in '_start'
  from ???
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

The code works fine in the interpreter, btw. ;)

@wolfgang371
Copy link
Author

Thanks for your quick reply!
So, maybe you want to change the title...

What about the "BUG: trying to downcast..." - is that related as well?

And what about this macro parenthesis issue - is that really intended?

macro m(x)
    puts(3 * {{x}})
    {% debug %}
end

m(2+1) # expecting 9
# -> puts(3 * 2 + 1)
# -> 7

@straight-shoota
Copy link
Member

What about the "BUG: trying to downcast..." - is that related as well?

Probably yes.

And what about this macro parenthesis issue - is that really intended?

Yes. But perhaps this can be improved, see #6254

@straight-shoota straight-shoota changed the title Compiler crash related to macros Codegen bug: Call parameter type does not match function signature Mar 21, 2023
@wolfgang371
Copy link
Author

wrt. macro arguments & parenthesis: as an intermediate step probably some explicit hint in the docs would help a bit.

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

3 participants