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

Issues with invoking lib functions in other ways #311

Closed
papamarkou opened this issue Dec 9, 2014 · 6 comments
Closed

Issues with invoking lib functions in other ways #311

papamarkou opened this issue Dec 9, 2014 · 6 comments
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc.

Comments

@papamarkou
Copy link
Contributor

Crystal says i've hit a bug and I should report it by opening an issue, so although I don't know what is the origin of the bug, here is the error message:

Bug: LibDSFMT doesn't implement lookup_def_instance
*Exception#initialize<Exception, String, Nil>:Array(String) +21 [32]
*Exception::new<String>:Exception +125 [32]
*raise<String>:NoReturn +6 [32]
*Crystal::Type+@Crystal::Type#lookup_def_instance<Crystal::Type+, Crystal::DefInstanceKey>:NoReturn +89 [32]
*Crystal::Call#instantiate<Crystal::Call, Crystal::Matches, Crystal::Type+, Nil>:Array(Crystal::Def+) +4008 [32]
*Crystal::Call#lookup_matches_in_type<Crystal::Call, Crystal::Type+, Array(Crystal::Type+), Nil, String>:Array(Crystal::Def+) +25691 [32]
*Crystal::Call#lookup_matches_in<Crystal::Call, Crystal::Type+, Array(Crystal::Type+), Nil, String>:Array(Crystal::Def+) +11 [32]
*Crystal::Call#lookup_matches_in<Crystal::Call, Crystal::Type+, Array(Crystal::Type+)>:Array(Crystal::Def+) +161 [32]
*Crystal::Call#lookup_matches_without_splat<Crystal::Call, Array(Crystal::Type+)>:Array(Crystal::Def+)? +854 [32]
*Crystal::Call#lookup_matches<Crystal::Call>:Array(Crystal::Def+)? +431 [32]
*Crystal::Call#recalculate<Crystal::Call>:(Bool | Nil) +1834 [32]
*Crystal::TypeVisitor#visit<Crystal::TypeVisitor, Crystal::Call>:Bool +2718 [32]
*Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::ASTNode+, Crystal::TypeVisitor>:Nil +1792 [32]
*Crystal::TypeVisitor#visit<Crystal::TypeVisitor, Crystal::Path>:(Nil | Array(Crystal::Call) | Crystal::Type+ | Bool) +420 [32]
*Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::ASTNode+, Crystal::TypeVisitor>:Nil +3622 [32]
*Crystal::TypeVisitor#visit<Crystal::TypeVisitor, Crystal::Call>:Bool +630 [32]
*Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::ASTNode+, Crystal::TypeVisitor>:Nil +1792 [32]
*Crystal::Expressions#accept_children<Crystal::Expressions, Crystal::TypeVisitor>:Array(Crystal::ASTNode+) +108 [32]
*Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::ASTNode+, Crystal::TypeVisitor>:Nil +9151 [32]
*Crystal::Expressions#accept_children<Crystal::Expressions, Crystal::TypeVisitor>:Array(Crystal::ASTNode+) +108 [32]
*Crystal::Expressions@Crystal::ASTNode#accept<Crystal::Expressions, Crystal::TypeVisitor>:Nil +80 [32]
*Crystal::Program#infer_type<Crystal::Program, Crystal::Expressions>:Crystal::Expressions +32 [32]
*Crystal::Compiler#infer_type<Crystal::Compiler, Crystal::Program, Crystal::Expressions>:Crystal::Expressions +238 [32]
*Crystal::Compiler#compile<Crystal::Compiler, Array(Crystal::Compiler::Source), String>:Crystal::Compiler::Result +334 [32]
*Crystal::Command::CompilerConfig#compile<Crystal::Command::CompilerConfig, String>:Crystal::Compiler::Result +57 [32]
*Crystal::Command::run_command<Array(String)>:Nil +398 [32]
*Crystal::Command::run<Array(String)>:(Crystal::Compiler::Result | Bool | CFileIO | Nil) +161 [32]
*Crystal::Command::run:(Crystal::Compiler::Result | Bool | CFileIO | Nil) +34 [32]
__crystal_main +5597 [32]
main +40 [32]
__libc_start_main +245 [32]
_start +41 [32]
 +41 [32]

Error: you've found a bug in the Crystal compiler. Please open an issue: https://github.com/manastech/crystal/issues
@asterite
Copy link
Member

asterite commented Dec 9, 2014

Thanks for reporting!

Code to reproduce:

class A
  def self.foo
  end
end

lib B
  fun foo : Int32
end

(A || B).foo

What is your code doing? It's strange that you have a union of a lib type and a Crystal type.

@papamarkou
Copy link
Contributor Author

Hm, I think I was doing sth meaningless. I wanted to define a constant, whose value is computed via a libdSFMT function binding. Instead of putting the constant definition in the DSFMT struct, i put it in libdSFMT, which wasns't so clever. Initially I had the statement MINSIZE = dsfmt_get_min_array_size in libdSFMT, but once I moved it to the DSFMT struct (as in MINSIZE = LibDSFMT.dsfmt_get_min_array_size), the problem was sorted (and this is more reasonable than my first approach if I am not wrong :) ). The relevant code is in the latest commit in random.cr

@asterite
Copy link
Member

asterite commented Dec 9, 2014

Hmm... do you have a small code to reproduce this issue? One way is the code I posted, but I'm not sure it's exactly the same issue. I checked out random.cr and ran the specs but they compile. How can I trigger the error?

@papamarkou
Copy link
Contributor Author

Hey @asterite, so here is what I did to trigger the issue (some pseudocode below, in an attempt to disentangle the issue from dsfmt and generalize):

lib LibX
  MYCONSTANT = get_minimum_fee

  fun get_minimum_fee() : Int32
end

Basically, we have a binding to a C function and then we use it inside libX to call that function and assign its returned value to the constant MYCONSTANT. I guess I shouldn't be doing this in the first place?

Sorry for the late reply, it is due to the difference in time between Argentina and UK :)

@asterite
Copy link
Member

Thanks! That's another way to reproduce the bug. I'll fix both of them... eventually.

You should be able to do what you did there, to have a constant based on the return value of a function. A constant in Crystal means: once the value is set, it can't change. But it doesn't mean that the value must be known at compile time. So you can for example do:

NOW = Time.now
puts NOW
puts NOW

That's a perfectly valid constant, only it's not constant across different runs, but only in each run.

No worries about the late reply, there's no need to hurry :-)

@asterite asterite added the kind:bug A bug in the code. Does not apply to documentation, specs, etc. label Dec 10, 2014
@asterite asterite changed the title Crystal says "you've found a bug in the Crystal compiler" Issues with invoking lib functions in other ways Dec 10, 2014
@papamarkou
Copy link
Contributor Author

Yes, I am following what you say. Great, I will change the code in random.cr later, when the bug is fixed in Crystal!

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.
Projects
None yet
Development

No branches or pull requests

2 participants