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

Internal interpreter error when reading constant with invalid initializer #12401

Open
HertzDevil opened this issue Aug 17, 2022 · 0 comments
Open

Comments

@HertzDevil
Copy link
Contributor

Constants defined in an interpreter session, like in compiled code, are not type-checked until the constant is read. If a compilation error occurs during initialization, subsequent reads of the same constant produce an internal error:

icr:1:0> A = 1.foo
=> nil
icr:2:0> A
error in line 1
Error: undefined method 'foo' for Int32
icr:3:0> A
Nil assertion failed (NilAssertionError)
  from src/nil.cr:108:5 in 'not_nil!'
  from src/compiler/crystal/interpreter/compiler.cr:1453:16 in 'get_const_index_and_compiled_def'
  from src/compiler/crystal/interpreter/compiler.cr:2604:27 in 'initialize_const_if_needed'
  from src/compiler/crystal/interpreter/compiler.cr:1435:17 in 'visit'
  from src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from src/compiler/crystal/interpreter/compiler.cr:157:5 in 'compile'
  from src/compiler/crystal/interpreter/interpreter.cr:215:5 in 'interpret'
  from src/compiler/crystal/interpreter/repl.cr:100:5 in 'interpret'
  from src/compiler/crystal/interpreter/repl.cr:40:17 in 'run'
  from src/compiler/crystal/command/repl.cr:37:7 in 'repl'
  from src/compiler/crystal/command.cr:100: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/quinton/crystal/crystal/.build/crystal in '_start'
  from ???

The delayed evaluation matches the behavior of compiled code, but it does look a little strange in a REPL:

icr:1:0> A = B # okay
=> nil
icr:2:0> B = 1
=> nil
icr:3:0> A
=> 1

The interpreter doesn't check the same initializer again after the first time it is evaluated:

icr:1:0> A = B
=> nil
icr:2:0> A
error in line 1
Error: undefined constant B

Did you mean 'A'?
icr:3:0> B = 1
=> nil
icr:4:0> A
Nil assertion failed (NilAssertionError)
oatmealine added a commit to oatmealine/gd-icon-renderer that referenced this issue Jan 21, 2024
never thought i'd be discovering a crystal compiler error, but here we are. this was impossible to debug btw
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

1 participant