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
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -132,7 +132,7 @@
 
 #[derive(Clone, Debug)]
 pub struct StackTraceElement {
-	pub location: ExprLocation,
+	pub location: Option<ExprLocation>,
 	pub desc: String,
 }
 #[derive(Debug, Clone)]
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
132 frame_desc: impl FnOnce() -> String,132 frame_desc: impl FnOnce() -> String,
133 f: impl FnOnce() -> Result<T>,133 f: impl FnOnce() -> Result<T>,
134) -> Result<T> {134) -> Result<T> {
135 if let Some(v) = e {
136 with_state(|s| s.push(v, frame_desc, f))135 with_state(|s| s.push(e, frame_desc, f))
137 } else {
138 f()
139 }
140}136}
137
138pub fn push_stack_frame<T>(
139 e: Option<&ExprLocation>,
140 frame_desc: impl FnOnce() -> String,
141 f: impl FnOnce() -> Result<T>,
142 ) -> Result<T> {
143 push(e, frame_desc, f)
144}
141145
142/// Maintains stack trace and import resolution146/// Maintains stack trace and import resolution
143#[derive(Default, Clone)]147#[derive(Default, Clone)]
271 /// Executes code creating a new stack frame275 /// Executes code creating a new stack frame
272 pub fn push<T>(276 pub fn push<T>(
273 &self,277 &self,
274 e: &ExprLocation,278 e: Option<&ExprLocation>,
275 frame_desc: impl FnOnce() -> String,279 frame_desc: impl FnOnce() -> String,
276 f: impl FnOnce() -> Result<T>,280 f: impl FnOnce() -> Result<T>,
277 ) -> Result<T> {281 ) -> Result<T> {
290 self.data_mut().stack_depth -= 1;294 self.data_mut().stack_depth -= 1;
291 if let Err(mut err) = result {295 if let Err(mut err) = result {
292 err.trace_mut().0.push(StackTraceElement {296 err.trace_mut().0.push(StackTraceElement {
293 location: e.clone(),297 location: e.cloned(),
294 desc: frame_desc(),298 desc: frame_desc(),
295 });299 });
296 return Err(err);300 return Err(err);
336 pub fn with_tla(&self, val: Val) -> Result<Val> {340 pub fn with_tla(&self, val: Val) -> Result<Val> {
337 self.run_in_state(|| {341 self.run_in_state(|| {
338 Ok(match val {342 Ok(match val {
339 Val::Func(func) => func.evaluate_map(343 Val::Func(func) => push(None, || "during TLA call".to_owned(), || Ok(func.evaluate_map(
340 self.create_default_context()?,344 self.create_default_context()?,
341 &self.settings().tla_vars,345 &self.settings().tla_vars,
342 true,346 true,
343 )?,347 )?))?,
344 v => v,348 v => v,
345 })349 })
346 })350 })
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(())
 	}