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

Compiler crash with to_unsafe and C type #8544

Closed
keidax opened this issue Dec 2, 2019 · 1 comment · Fixed by #11636
Closed

Compiler crash with to_unsafe and C type #8544

keidax opened this issue Dec 2, 2019 · 1 comment · Fixed by #11636
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:codegen

Comments

@keidax
Copy link
Contributor

keidax commented Dec 2, 2019

Reproduction:
https://gist.github.com/keidax/890f0a7195f64950effb3a302e879996

On macOS, I get this output:

$ crystal foo_object.cr
Module validation failed: Incorrect number of arguments passed to called function!
  call void @"*Pointer(Void)@Object::to_s<String::Builder>:Nil"(i32 %obj, %"String::Builder"* %self), !dbg !183
 (Exception)
  from Crystal::CodeGenVisitor#finish:Nil
  from Crystal::Compiler#codegen<Crystal::Program, Crystal::ASTNode+, Array(Crystal::Compiler::Source), String>:(Tuple(Array(Crystal::Compiler::CompilationUnit), Array(String)) | Nil)
  from Crystal::Compiler#compile<Array(Crystal::Compiler::Source), String>:Crystal::Compiler::Result
  from Crystal::Command#run_command<Bool>:Nil
  from Crystal::Command#run:(Bool | Crystal::Compiler::Result | Nil)
  from __crystal_main
  from main
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

This is on macOS Mojave 10.14.6
Output of crystal -v:

Crystal 0.31.1 (2019-10-02)

LLVM: 8.0.1
Default target: x86_64-apple-macosx
@jhass jhass added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler labels Dec 2, 2019
@HertzDevil
Copy link
Contributor

HertzDevil commented Dec 22, 2021

This breaks make spec, because the standard library spec does this:

describe "#to_unsafe" do
it "returns a pointer to the underlying LibXML::XMLTextReader" do
reader = Reader.new("<root/>")
reader.to_unsafe.should be_a(LibXML::XMLTextReader)
end
end

where LibXML::XMLTextReader is defined as:

type XMLTextReader = Void*

The actual conflict occurs within Spec::BeAExpectation#failure_message. Reduced:

lib LibFoo
  type Foo = Pointer(Void)
end

class Class
  # corresponds to `#to_s(io)`,
  # called by `String::Builder#<<`,
  # called by `String.interpolation(*args : *T) forall T`,
  # called by `Spec::BeAExpectation#failure_message`
  def foo(x)
    x
  end
end

x = 1
LibFoo::Foo.foo(x)
Pointer(Void).foo(x)

But note that the linked spec should fail; LibAsdf::VoidPtr and Pointer(Void) are sibling types, so neither is a subtype of the other. (To make it pass, use alias instead of type.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:codegen
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants