Skip to content

Commit

Permalink
fix(vm): Don't emit line information for macro expanded code
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Jan 22, 2017
1 parent d0ca451 commit 4f056be
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
37 changes: 37 additions & 0 deletions tests/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,43 @@ fn line_hook() {
vec![1, 3, 4, 3, 1].into_iter().map(Line::from).collect::<Vec<_>>());
}

#[test]
fn implicit_prelude_lines_not_counted() {
let _ = env_logger::init();

let thread = new_vm();
{
let mut context = thread.context();
context.set_hook(Some(Box::new(move |_, debug_info| {
if debug_info.stack_info(0).unwrap().source_name() == "test" {
Err(vm::Error::Yield)
} else {
Ok(())
}
})));
context.set_hook_mask(LINE_FLAG);
}
let mut result = Compiler::new()
.run_expr::<i32>(&thread, "test", "1")
.map(|_| ());

let mut lines = Vec::new();
loop {
match result {
Ok(_) => break,
Err(Error::VM(vm::Error::Yield)) => {
let context = thread.context();
let debug_info = context.debug_info();
let stack_info = debug_info.stack_info(0).unwrap();
lines.push(stack_info.line().unwrap());
}
Err(err) => panic!("{}", err),
}
result = thread.resume().map_err(From::from);
}

assert_eq!(lines, vec![Line::from(0)]);
}

#[test]
fn read_variables() {
Expand Down
19 changes: 14 additions & 5 deletions vm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use base::kind::{ArcKind, KindEnv};
use base::types::{self, Alias, ArcType, Type, TypeEnv};
use base::scoped_map::ScopedMap;
use base::symbol::{Symbol, SymbolRef, SymbolModule};
use base::pos::Line;
use base::pos::{Line, NO_EXPANSION};
use base::source::Source;
use types::*;
use vm::GlobalVmState;
Expand Down Expand Up @@ -74,8 +74,11 @@ struct FunctionEnv {
/// The variables currently in scope in the this function.
stack: ScopedMap<Symbol, VmIndex>,
/// The current size of the stack. Not the same as `stack.len()`.
/// The current size of the stack. Not the same as `stack.len()`.
stack_size: VmIndex,
/// The variables which this function takes from the outer scope
free_vars: Vec<Symbol>,
/// The line where instructions are currently being emitted
current_line: Line,
function: CompiledFunction,
}
Expand Down Expand Up @@ -457,13 +460,19 @@ impl<'a> Compiler<'a> {
// Store a stack of expressions which need to be cleaned up after this "tailcall" loop is
// done
function.stack.enter_scope();
function.current_line = self.source
.line_number_at_byte(expr.span.start);
while let Some(next) = self.compile_(expr, function, tail_position)? {
expr = next;
// Don't update the current_line for macro expanded code as the lines in that code do not come
// from this module
if expr.span.expansion_id == NO_EXPANSION {
function.current_line = self.source
.line_number_at_byte(expr.span.start);
}
while let Some(next) = self.compile_(expr, function, tail_position)? {
expr = next;
if expr.span.expansion_id == NO_EXPANSION {
function.current_line = self.source
.line_number_at_byte(expr.span.start);
}
}
let count = function.exit_scope(self);
function.emit(Slide(count));
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions vm/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ impl SourceMap {
/// Defines the instruction at `instruction_index` to be at `current_line`.
/// This function must be called with indexes in increasing order
pub fn emit(&mut self, instruction_index: usize, current_line: Line) {
let last_emitted_line = self.map.last().map_or(Line::from(0), |&(_, x)| x);
if last_emitted_line != current_line {
let last_emitted_line = self.map.last().map(|&(_, x)| x);
if last_emitted_line != Some(current_line) {
self.map.push((instruction_index, current_line));
}
}
Expand Down

0 comments on commit 4f056be

Please sign in to comment.