git.delta.rocks / jrsonnet / refs/commits / 0d591aa205cc

difftreelog

feat locationless stack frames

Yaroslav Bolyukin2021-01-25parent: #a682d0e.patch.diff
in: master

4 files changed

modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
132132
133#[derive(Clone, Debug)]133#[derive(Clone, Debug)]
134pub struct StackTraceElement {134pub struct StackTraceElement {
135 pub location: ExprLocation,135 pub location: Option<ExprLocation>,
136 pub desc: String,136 pub desc: String,
137}137}
138#[derive(Debug, Clone)]138#[derive(Debug, Clone)]
modifiedcrates/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,
 			})
 		})
modifiedcrates/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 => {
modifiedcrates/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(())
 	}