difftreelog
feat locationless stack frames
in: master
4 files changed
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth1use crate::{2 builtin::{format::FormatError, sort::SortError},3 typed::TypeLocError,4};5use jrsonnet_interner::IStr;6use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};7use jrsonnet_types::ValType;8use std::{path::PathBuf, rc::Rc};9use thiserror::Error;1011#[derive(Error, Debug, Clone)]12pub enum Error {13 #[error("intrinsic not found: {0}")]14 IntrinsicNotFound(IStr),15 #[error("argument reordering in intrisics not supported yet")]16 IntrinsicArgumentReorderingIsNotSupportedYet,1718 #[error("operator {0} does not operate on type {1}")]19 UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),20 #[error("binary operation {1} {0} {2} is not implemented")]21 BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),2223 #[error("no top level object in this context")]24 NoTopLevelObjectFound,25 #[error("self is only usable inside objects")]26 CantUseSelfOutsideOfObject,27 #[error("super is only usable inside objects")]28 CantUseSuperOutsideOfObject,2930 #[error("for loop can only iterate over arrays")]31 InComprehensionCanOnlyIterateOverArray,3233 #[error("array out of bounds: {0} is not within [0,{1})")]34 ArrayBoundsError(usize, usize),3536 #[error("assert failed: {0}")]37 AssertionFailed(IStr),3839 #[error("variable is not defined: {0}")]40 VariableIsNotDefined(IStr),41 #[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]42 TypeMismatch(&'static str, Vec<ValType>, ValType),43 #[error("no such field: {0}")]44 NoSuchField(IStr),4546 #[error("only functions can be called, got {0}")]47 OnlyFunctionsCanBeCalledGot(ValType),48 #[error("parameter {0} is not defined")]49 UnknownFunctionParameter(String),50 #[error("argument {0} is already bound")]51 BindingParameterASecondTime(IStr),52 #[error("too many args, function has {0}")]53 TooManyArgsFunctionHas(usize),54 #[error("founction argument is not passed: {0}")]55 FunctionParameterNotBoundInCall(IStr),5657 #[error("external variable is not defined: {0}")]58 UndefinedExternalVariable(IStr),59 #[error("native is not defined: {0}")]60 UndefinedExternalFunction(IStr),6162 #[error("field name should be string, got {0}")]63 FieldMustBeStringGot(ValType),6465 #[error("attempted to index array with string {0}")]66 AttemptedIndexAnArrayWithString(IStr),67 #[error("{0} index type should be {1}, got {2}")]68 ValueIndexMustBeTypeGot(ValType, ValType, ValType),69 #[error("cant index into {0}")]70 CantIndexInto(ValType),7172 #[error("super can't be used standalone")]73 StandaloneSuper,7475 #[error("can't resolve {1} from {0}")]76 ImportFileNotFound(PathBuf, PathBuf),77 #[error("resolved file not found: {0}")]78 ResolvedFileNotFound(PathBuf),79 #[error("imported file is not valid utf-8: {0:?}")]80 ImportBadFileUtf8(PathBuf),81 #[error("tried to import {1} from {0}, but imports is not supported")]82 ImportNotSupported(PathBuf, PathBuf),83 #[error(84 "syntax error, expected one of {}, got {:?}",85 .error.expected,86 .source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())87 )]88 ImportSyntaxError {89 path: Rc<PathBuf>,90 source_code: IStr,91 error: Box<jrsonnet_parser::ParseError>,92 },9394 #[error("runtime error: {0}")]95 RuntimeError(IStr),96 #[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]97 StackOverflow,98 #[error("tried to index by fractional value")]99 FractionalIndex,100 #[error("attempted to divide by zero")]101 DivisionByZero,102103 #[error("string manifest output is not an string")]104 StringManifestOutputIsNotAString,105 #[error("stream manifest output is not an array")]106 StreamManifestOutputIsNotAArray,107 #[error("multi manifest output is not an object")]108 MultiManifestOutputIsNotAObject,109110 #[error("cant recurse stream manifest")]111 StreamManifestOutputCannotBeRecursed,112 #[error("stream manifest output cannot consist of raw strings")]113 StreamManifestCannotNestString,114115 #[error("{0}")]116 ImportCallbackError(String),117 #[error("invalid unicode codepoint: {0}")]118 InvalidUnicodeCodepointGot(u32),119120 #[error("format error: {0}")]121 Format(#[from] FormatError),122 #[error("type error: {0}")]123 TypeError(TypeLocError),124 #[error("sort error: {0}")]125 Sort(#[from] SortError),126}127impl From<Error> for LocError {128 fn from(e: Error) -> Self {129 Self::new(e)130 }131}132133#[derive(Clone, Debug)]134pub struct StackTraceElement {135 pub location: ExprLocation,136 pub desc: String,137}138#[derive(Debug, Clone)]139pub struct StackTrace(pub Vec<StackTraceElement>);140141#[derive(Debug, Clone)]142pub struct LocError(Box<(Error, StackTrace)>);143impl LocError {144 pub fn new(e: Error) -> Self {145 Self(Box::new((e, StackTrace(vec![]))))146 }147148 pub const fn error(&self) -> &Error {149 &(self.0).0150 }151 pub fn error_mut(&mut self) -> &mut Error {152 &mut (self.0).0153 }154 pub const fn trace(&self) -> &StackTrace {155 &(self.0).1156 }157 pub fn trace_mut(&mut self) -> &mut StackTrace {158 &mut (self.0).1159 }160}161162pub type Result<V> = std::result::Result<V, LocError>;163164#[macro_export]165macro_rules! throw {166 ($e: expr) => {167 return Err($e.into());168 };169}1use crate::{2 builtin::{format::FormatError, sort::SortError},3 typed::TypeLocError,4};5use jrsonnet_interner::IStr;6use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};7use jrsonnet_types::ValType;8use std::{path::PathBuf, rc::Rc};9use thiserror::Error;1011#[derive(Error, Debug, Clone)]12pub enum Error {13 #[error("intrinsic not found: {0}")]14 IntrinsicNotFound(IStr),15 #[error("argument reordering in intrisics not supported yet")]16 IntrinsicArgumentReorderingIsNotSupportedYet,1718 #[error("operator {0} does not operate on type {1}")]19 UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),20 #[error("binary operation {1} {0} {2} is not implemented")]21 BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),2223 #[error("no top level object in this context")]24 NoTopLevelObjectFound,25 #[error("self is only usable inside objects")]26 CantUseSelfOutsideOfObject,27 #[error("super is only usable inside objects")]28 CantUseSuperOutsideOfObject,2930 #[error("for loop can only iterate over arrays")]31 InComprehensionCanOnlyIterateOverArray,3233 #[error("array out of bounds: {0} is not within [0,{1})")]34 ArrayBoundsError(usize, usize),3536 #[error("assert failed: {0}")]37 AssertionFailed(IStr),3839 #[error("variable is not defined: {0}")]40 VariableIsNotDefined(IStr),41 #[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]42 TypeMismatch(&'static str, Vec<ValType>, ValType),43 #[error("no such field: {0}")]44 NoSuchField(IStr),4546 #[error("only functions can be called, got {0}")]47 OnlyFunctionsCanBeCalledGot(ValType),48 #[error("parameter {0} is not defined")]49 UnknownFunctionParameter(String),50 #[error("argument {0} is already bound")]51 BindingParameterASecondTime(IStr),52 #[error("too many args, function has {0}")]53 TooManyArgsFunctionHas(usize),54 #[error("founction argument is not passed: {0}")]55 FunctionParameterNotBoundInCall(IStr),5657 #[error("external variable is not defined: {0}")]58 UndefinedExternalVariable(IStr),59 #[error("native is not defined: {0}")]60 UndefinedExternalFunction(IStr),6162 #[error("field name should be string, got {0}")]63 FieldMustBeStringGot(ValType),6465 #[error("attempted to index array with string {0}")]66 AttemptedIndexAnArrayWithString(IStr),67 #[error("{0} index type should be {1}, got {2}")]68 ValueIndexMustBeTypeGot(ValType, ValType, ValType),69 #[error("cant index into {0}")]70 CantIndexInto(ValType),7172 #[error("super can't be used standalone")]73 StandaloneSuper,7475 #[error("can't resolve {1} from {0}")]76 ImportFileNotFound(PathBuf, PathBuf),77 #[error("resolved file not found: {0}")]78 ResolvedFileNotFound(PathBuf),79 #[error("imported file is not valid utf-8: {0:?}")]80 ImportBadFileUtf8(PathBuf),81 #[error("tried to import {1} from {0}, but imports is not supported")]82 ImportNotSupported(PathBuf, PathBuf),83 #[error(84 "syntax error, expected one of {}, got {:?}",85 .error.expected,86 .source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())87 )]88 ImportSyntaxError {89 path: Rc<PathBuf>,90 source_code: IStr,91 error: Box<jrsonnet_parser::ParseError>,92 },9394 #[error("runtime error: {0}")]95 RuntimeError(IStr),96 #[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]97 StackOverflow,98 #[error("tried to index by fractional value")]99 FractionalIndex,100 #[error("attempted to divide by zero")]101 DivisionByZero,102103 #[error("string manifest output is not an string")]104 StringManifestOutputIsNotAString,105 #[error("stream manifest output is not an array")]106 StreamManifestOutputIsNotAArray,107 #[error("multi manifest output is not an object")]108 MultiManifestOutputIsNotAObject,109110 #[error("cant recurse stream manifest")]111 StreamManifestOutputCannotBeRecursed,112 #[error("stream manifest output cannot consist of raw strings")]113 StreamManifestCannotNestString,114115 #[error("{0}")]116 ImportCallbackError(String),117 #[error("invalid unicode codepoint: {0}")]118 InvalidUnicodeCodepointGot(u32),119120 #[error("format error: {0}")]121 Format(#[from] FormatError),122 #[error("type error: {0}")]123 TypeError(TypeLocError),124 #[error("sort error: {0}")]125 Sort(#[from] SortError),126}127impl From<Error> for LocError {128 fn from(e: Error) -> Self {129 Self::new(e)130 }131}132133#[derive(Clone, Debug)]134pub struct StackTraceElement {135 pub location: Option<ExprLocation>,136 pub desc: String,137}138#[derive(Debug, Clone)]139pub struct StackTrace(pub Vec<StackTraceElement>);140141#[derive(Debug, Clone)]142pub struct LocError(Box<(Error, StackTrace)>);143impl LocError {144 pub fn new(e: Error) -> Self {145 Self(Box::new((e, StackTrace(vec![]))))146 }147148 pub const fn error(&self) -> &Error {149 &(self.0).0150 }151 pub fn error_mut(&mut self) -> &mut Error {152 &mut (self.0).0153 }154 pub const fn trace(&self) -> &StackTrace {155 &(self.0).1156 }157 pub fn trace_mut(&mut self) -> &mut StackTrace {158 &mut (self.0).1159 }160}161162pub type Result<V> = std::result::Result<V, LocError>;163164#[macro_export]165macro_rules! throw {166 ($e: expr) => {167 return Err($e.into());168 };169}crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -132,11 +132,15 @@
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<T>,
) -> Result<T> {
- if let Some(v) = e {
- with_state(|s| s.push(v, frame_desc, f))
- } else {
- f()
- }
+ with_state(|s| s.push(e, frame_desc, f))
+}
+
+pub fn push_stack_frame<T>(
+ e: Option<&ExprLocation>,
+ frame_desc: impl FnOnce() -> String,
+ f: impl FnOnce() -> Result<T>,
+ ) -> Result<T> {
+ push(e, frame_desc, f)
}
/// Maintains stack trace and import resolution
@@ -271,7 +275,7 @@
/// Executes code creating a new stack frame
pub fn push<T>(
&self,
- e: &ExprLocation,
+ e: Option<&ExprLocation>,
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<T>,
) -> Result<T> {
@@ -290,7 +294,7 @@
self.data_mut().stack_depth -= 1;
if let Err(mut err) = result {
err.trace_mut().0.push(StackTraceElement {
- location: e.clone(),
+ location: e.cloned(),
desc: frame_desc(),
});
return Err(err);
@@ -336,11 +340,11 @@
pub fn with_tla(&self, val: Val) -> Result<Val> {
self.run_in_state(|| {
Ok(match val {
- Val::Func(func) => func.evaluate_map(
+ Val::Func(func) => push(None, || "during TLA call".to_owned(), || Ok(func.evaluate_map(
self.create_default_context()?,
&self.settings().tla_vars,
true,
- )?,
+ )?))?,
v => v,
})
})
crates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/location.rs
+++ b/crates/jrsonnet-evaluator/src/trace/location.rs
@@ -37,7 +37,7 @@
];
let mut with_no_known_line_ending = vec![];
let mut this_line_offset = 0;
- for (pos, ch) in file.chars().enumerate() {
+ for (pos, ch) in file.chars().enumerate().chain(std::iter::once((file.len(), ' '))) {
column += 1;
match offset_map.last() {
Some(x) if x.0 == pos => {
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -118,21 +118,18 @@
.trace()
.0
.iter()
- .map(|el| {
- let resolved_path = self.resolver.resolve(&el.location.0);
+ .map(|el| el.location.as_ref().map(|l| {
+ use std::fmt::Write;
+ let mut resolved_path = self.resolver.resolve(&l.0);
// TODO: Process all trace elements first
let location = evaluation_state
- .map_source_locations(&el.location.0, &[el.location.1, el.location.2]);
- (resolved_path, location)
- })
- .map(|(mut n, location)| {
- use std::fmt::Write;
- write!(n, ":").unwrap();
- print_code_location(&mut n, &location[0], &location[1]).unwrap();
- n
- })
+ .map_source_locations(&l.0, &[l.1, l.2]);
+ write!(resolved_path, ":").unwrap();
+ print_code_location(&mut resolved_path, &location[0], &location[1]).unwrap();
+ resolved_path
+ }))
.collect::<Vec<_>>();
- let align = file_names.iter().map(|e| e.len()).max().unwrap_or(0);
+ let align = file_names.iter().flatten().map(|e| e.len()).max().unwrap_or(0);
for (i, (el, file)) in error.trace().0.iter().zip(file_names).enumerate() {
if i != 0 {
writeln!(out)?;
@@ -141,7 +138,7 @@
out,
"{:<p$}{:<w$}: {}",
"",
- file,
+ file.unwrap_or_else(|| "".to_owned()),
el.desc,
p = self.padding,
w = align
@@ -165,17 +162,24 @@
writeln!(out)?;
}
let desc = &item.desc;
- let source = item.location.clone();
- let start_end = evaluation_state.map_source_locations(&source.0, &[source.1, source.2]);
+ if let Some (source) = &item.location {
+ let start_end = evaluation_state.map_source_locations(&source.0, &[source.1, source.2]);
- write!(
- out,
- " at {} ({}:{}:{})",
- desc,
- source.0.to_str().unwrap(),
- start_end[0].line,
- start_end[0].column,
- )?;
+ write!(
+ out,
+ " at {} ({}:{}:{})",
+ desc,
+ source.0.to_str().unwrap(),
+ start_end[0].line,
+ start_end[0].column,
+ )?;
+ } else {
+ write!(
+ out,
+ " at {}",
+ desc,
+ )?;
+ }
}
Ok(())
}
@@ -225,17 +229,19 @@
let trace = &error.trace();
for item in trace.0.iter() {
let desc = &item.desc;
- let source = item.location.clone();
- let start_end = evaluation_state.map_source_locations(&source.0, &[source.1, source.2]);
-
- self.print_snippet(
- out,
- &evaluation_state.get_source(&source.0).unwrap(),
- &source.0,
- &start_end[0],
- &start_end[1],
- desc,
- )?;
+ if let Some(source) = &item.location {
+ let start_end = evaluation_state.map_source_locations(&source.0, &[source.1, source.2]);
+ self.print_snippet(
+ out,
+ &evaluation_state.get_source(&source.0).unwrap(),
+ &source.0,
+ &start_end[0],
+ &start_end[1],
+ desc,
+ )?;
+ } else {
+ write!(out, "{}", desc)?;
+ }
}
Ok(())
}