Crystal: Compiler crash when defining a constant inside a constant

Created on 18 Jun 2016  路  6Comments  路  Source: crystal-lang/crystal

Code

This is how I accidentally discovered an issue. Not a very likely scenario right? It was a typo. I gave a variable a capitol letter which made it a const. :)

FOO = begin
  BAR = 1
end

puts FOO

Here is a more simple example.

A = B = 1

puts A

Output

Nil assertion failed
[4335949203] *raise<String>:NoReturn +163
[4336000352] *Nil#not_nil!<Nil>:NoReturn +16
[4345468781] *Crystal::ASTNode+@Crystal::ASTNode#transform<Crystal::ASTNode+, Crystal::CleanupTransformer>:Crystal::ASTNode+ +18381
[4345455796] *Crystal::ASTNode+@Crystal::ASTNode#transform<Crystal::ASTNode+, Crystal::CleanupTransformer>:Crystal::ASTNode+ +5396
[4345449632] *Crystal::CleanupTransformer#transform<Crystal::CleanupTransformer, Crystal::Expressions>:Crystal::Expressions +112
[4345451313] *Crystal::ASTNode+@Crystal::ASTNode#transform<Crystal::ASTNode+, Crystal::CleanupTransformer>:Crystal::ASTNode+ +913
[4345449632] *Crystal::CleanupTransformer#transform<Crystal::CleanupTransformer, Crystal::Expressions>:Crystal::Expressions +112
[4345448805] *Crystal::Program#cleanup<Crystal::Program, Crystal::Expressions>:Crystal::Expressions +341
[4337356536] *Crystal::Compiler#compile<Crystal::Compiler, Array(Crystal::Compiler::Source), String>:Crystal::Compiler::Result +2552
[4346334439] *Crystal::Command#run_command<Crystal::Command, Bool>:Nil +151
[4336187718] *Crystal::Command#run<Crystal::Command>:(Array(Crystal::ImplementationTrace) | Array(Crystal::Init::View+:Class) | Array(String) | Bool | Crystal::Compiler::Result | Hash(String, String) | IO::FileDescriptor+ | Nil) +18774
[4336047297] main +17521

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

Version

Crystal 0.18.2 (2016-06-17)
bug compiler

Most helpful comment

I'd rather make it a compile error than leaving it as a compiler error. It does no harm to leave it open :).

All 6 comments

Got this for triage, still reproduces on HEAD.

Yeah, I didn't fix it yet because I don't know if this should be fixed, or if declaring a constant inside another constant should be an error. I don't see a very strong reason to allow that.

We allow a = b = 1, it would only be consistent.

The thing is that constants are only type-checked when you use them. This makes it possible to omit some constants in the final binary. For example if you don't use ARGF then it's never created.

But when you define a constant inside another constant, this constant doesn't exist unless you use the first constant.

Because I don't think defining constants in this way is super useful (in fact I don't know why there would be a reason to have two constants with the same value, defined one after the other), I think I'd prefer this to be a compile-time error.

This makes sense to me. Can this issue be closed?

I'd rather make it a compile error than leaving it as a compiler error. It does no harm to leave it open :).

Was this page helpful?
0 / 5 - 0 ratings