mattwparas / steel

An embedded scheme interpreter in Rust
Apache License 2.0
1.2k stars 55 forks source link

interpreter panic #73

Closed evanrichter closed 1 year ago

evanrichter commented 1 year ago

found a panic in the interpreter while playing around, and minimized it to this sequence:

(define (x) (- x 2)) ; define a weird function
(x)                  ; function is called (TypeMismatch reported)
(define (x) (- x 2)) ; panic when redefining the function

run with ./target/release/steel < crash.scm

thread 'main' panicked at 'range end index 9 out of range for slice of length 5', crates/steel-core/src/steel_vm/vm.rs:2349:17
stack backtrace:
   0: rust_begin_unwind
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/panicking.rs:578:5
   1: core::panicking::panic_fmt
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/panicking.rs:67:14
   2: core::slice::index::slice_end_index_len_fail_rt
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/slice/index.rs:78:5
   3: core::slice::index::slice_end_index_len_fail
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/slice/index.rs:70:9
   4: steel::steel_vm::vm::VmCore::handle_pure_function
   5: steel::steel_vm::vm::VmCore::vm
             at ./crates/steel-core/src/steel_vm/vm.rs:1993:22
   6: steel::steel_vm::vm::SteelThread::execute
             at ./crates/steel-core/src/steel_vm/vm.rs:457:26
   7: steel::steel_vm::vm::SteelThread::run_executable::{{closure}}
             at ./crates/steel-core/src/steel_vm/vm.rs:361:22
   8: core::iter::adapters::map::map_try_fold::{{closure}}
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/iter/adapters/map.rs:91:28
   9: core::iter::traits::iterator::Iterator::try_fold
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/iter/traits/iterator.rs:2304:21
  10: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::try_fold
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/iter/adapters/map.rs:117:9
  11: <core::iter::adapters::GenericShunt<I,R> as core::iter::traits::iterator::Iterator>::try_fold
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/iter/adapters/mod.rs:195:9
  12: core::iter::traits::iterator::Iterator::try_for_each
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/iter/traits/iterator.rs:2366:9
  13: <core::iter::adapters::GenericShunt<I,R> as core::iter::traits::iterator::Iterator>::next
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/iter/adapters/mod.rs:178:14
  14: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/alloc/src/vec/spec_from_iter_nested.rs:26:32
  15: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/alloc/src/vec/spec_from_iter.rs:33:9
  16: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/alloc/src/vec/mod.rs:2712:9
  17: core::iter::traits::iterator::Iterator::collect
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/iter/traits/iterator.rs:1896:9
  18: <core::result::Result<V,E> as core::iter::traits::collect::FromIterator<core::result::Result<A,E>>>::from_iter::{{closure}}
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/result.rs:1969:51
  19: core::iter::adapters::try_process
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/iter/adapters/mod.rs:164:17
  20: <core::result::Result<V,E> as core::iter::traits::collect::FromIterator<core::result::Result<A,E>>>::from_iter
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/result.rs:1969:9
  21: core::iter::traits::iterator::Iterator::collect
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/iter/traits/iterator.rs:1896:9
  22: steel::steel_vm::vm::SteelThread::run_executable
             at ./crates/steel-core/src/steel_vm/vm.rs:362:14
  23: steel::steel_vm::engine::Engine::run_raw_program
             at ./crates/steel-core/src/steel_vm/engine.rs:1051:9
  24: steel::steel_vm::engine::Engine::run_raw_program_from_exprs
             at ./crates/steel-core/src/steel_vm/engine.rs:1015:9
  25: steel::parser::kernel::Kernel::load_program_for_comptime
             at ./crates/steel-core/src/parser/kernel.rs:147:9
  26: steel::compiler::compiler::Compiler::apply_const_evaluation
             at ./crates/steel-core/src/compiler/compiler.rs:685:17
  27: steel::compiler::compiler::Compiler::compile_raw_program
             at ./crates/steel-core/src/compiler/compiler.rs:642:13
  28: steel::compiler::compiler::Compiler::compile_executable
             at ./crates/steel-core/src/compiler/compiler.rs:391:9
  29: steel::steel_vm::engine::Engine::compile_and_run_raw_program
             at ./crates/steel-core/src/steel_vm/engine.rs:1020:23
  30: steel_repl::repl::finish_or_interrupt
             at ./crates/steel-repl/src/repl.rs:54:15
  31: steel_repl::repl::repl_base
             at ./crates/steel-repl/src/repl.rs:176:25
  32: steel_interpreter::run
             at ./src/lib.rs:59:13
  33: steel::main
             at ./src/main.rs:21:5
  34: core::ops::function::FnOnce::call_once
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/ops/function.rs:250:5

( I set debug = true for this build so line numbers are present)

mattwparas commented 1 year ago

Thanks for the report! Good catch, I thought I fixed this a while back but guess I didn't catch all the cases. The identifiers are getting interned and on error they get rolled back, looks like its leaving it in a funny state. I'll take a look

mattwparas commented 1 year ago

Looks like once we redefine the function, we're getting a fresh object, and my function creation logic for selecting the spans was perhaps not handling that - will have an MR up shortly to address as I think I've fixed it here.

evanrichter commented 1 year ago

confirmed on my end as well, amazing!