git.delta.rocks / jrsonnet / refs/commits / 3e23ebe1b496

difftreelog

refactor add proper getters for LocExpr

Yaroslav Bolyukin2024-05-27parent: #ea44e44.patch.diff
in: master

10 files changed

modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -53,8 +53,6 @@
 serde.workspace = true
 
 anyhow = { workspace = true, optional = true }
-# Serialized stdlib
-bincode = { workspace = true, optional = true }
 # Explaining traces
 annotate-snippets = { workspace = true, optional = true }
 # Better explaining traces
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -7,7 +7,7 @@
 
 use jrsonnet_gcmodule::Trace;
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{BinaryOpType, ExprLocation, LocExpr, Source, SourcePath, UnaryOpType};
+use jrsonnet_parser::{BinaryOpType, LocExpr, Source, SourcePath, Span, UnaryOpType};
 use jrsonnet_types::ValType;
 use thiserror::Error;
 
@@ -275,7 +275,7 @@
 pub struct StackTraceElement {
 	/// Source of this frame
 	/// Some frames only act as description, without attached source
-	pub location: Option<ExprLocation>,
+	pub location: Option<Span>,
 	/// Frame description
 	pub desc: String,
 }
@@ -324,20 +324,20 @@
 impl std::error::Error for Error {}
 
 pub trait ErrorSource {
-	fn to_location(self) -> Option<ExprLocation>;
+	fn to_location(self) -> Option<Span>;
 }
 impl ErrorSource for &LocExpr {
-	fn to_location(self) -> Option<ExprLocation> {
-		Some(self.1.clone())
+	fn to_location(self) -> Option<Span> {
+		Some(self.span())
 	}
 }
-impl ErrorSource for &ExprLocation {
-	fn to_location(self) -> Option<ExprLocation> {
+impl ErrorSource for &Span {
+	fn to_location(self) -> Option<Span> {
 		Some(self.clone())
 	}
 }
 impl ErrorSource for CallLocation<'_> {
-	fn to_location(self) -> Option<ExprLocation> {
+	fn to_location(self) -> Option<Span> {
 		self.0.cloned()
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -26,7 +26,7 @@
 
 pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {
 	fn is_trivial(expr: &LocExpr) -> bool {
-		match &*expr.0 {
+		match expr.expr() {
 			Expr::Str(_)
 			| Expr::Num(_)
 			| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,
@@ -35,7 +35,7 @@
 			_ => false,
 		}
 	}
-	Some(match &*expr.0 {
+	Some(match expr.expr() {
 		Expr::Str(s) => Val::string(s.clone()),
 		Expr::Num(n) => {
 			Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))
@@ -72,7 +72,7 @@
 	Ok(match field_name {
 		FieldName::Fixed(n) => Some(n.clone()),
 		FieldName::Dyn(expr) => State::push(
-			CallLocation::new(&expr.1),
+			CallLocation::new(&expr.span()),
 			|| "evaluating field name".to_string(),
 			|| {
 				let value = evaluate(ctx, expr)?;
@@ -231,7 +231,7 @@
 				.field(name.clone())
 				.with_add(*plus)
 				.with_visibility(*visibility)
-				.with_location(value.1.clone())
+				.with_location(value.span())
 				.bindable(UnboundValue {
 					uctx,
 					value: value.clone(),
@@ -266,7 +266,7 @@
 			builder
 				.field(name.clone())
 				.with_visibility(*visibility)
-				.with_location(value.1.clone())
+				.with_location(value.span())
 				.bindable(UnboundMethod {
 					uctx,
 					value: value.clone(),
@@ -385,13 +385,13 @@
 	let value = &assertion.0;
 	let msg = &assertion.1;
 	let assertion_result = State::push(
-		CallLocation::new(&value.1),
+		CallLocation::new(&value.span()),
 		|| "assertion condition".to_owned(),
 		|| bool::from_untyped(evaluate(ctx.clone(), value)?),
 	)?;
 	if !assertion_result {
 		State::push(
-			CallLocation::new(&value.1),
+			CallLocation::new(&value.span()),
 			|| "assertion failure".to_owned(),
 			|| {
 				if let Some(msg) = msg {
@@ -406,8 +406,7 @@
 
 pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {
 	use Expr::*;
-	let LocExpr(raw_expr, _loc) = expr;
-	Ok(match &**raw_expr {
+	Ok(match expr.expr() {
 		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),
 		_ => evaluate(ctx, expr)?,
 	})
@@ -420,8 +419,8 @@
 	if let Some(trivial) = evaluate_trivial(expr) {
 		return Ok(trivial);
 	}
-	let LocExpr(expr, loc) = expr;
-	Ok(match &**expr {
+	let loc = expr.span();
+	Ok(match expr.expr() {
 		Literal(LiteralType::This) => {
 			Val::Obj(ctx.this().ok_or(CantUseSelfOutsideOfObject)?.clone())
 		}
@@ -448,7 +447,7 @@
 		// because the standalone super literal is not supported, that is because in other
 		// implementations `in super` treated differently from in `smth_else`.
 		BinaryOp(field, BinaryOpType::In, e)
-			if matches!(&*e.0, Expr::Literal(LiteralType::Super)) =>
+			if matches!(e.expr(), Expr::Literal(LiteralType::Super)) =>
 		{
 			let Some(super_obj) = ctx.super_obj() else {
 				return Ok(Val::Bool(false));
@@ -459,52 +458,50 @@
 		BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,
 		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,
 		Var(name) => State::push(
-			CallLocation::new(loc),
+			CallLocation::new(&loc),
 			|| format!("variable <{name}> access"),
 			|| ctx.binding(name.clone())?.evaluate(),
 		)?,
 		Index { indexable, parts } => {
 			let mut parts = parts.iter();
-			let mut indexable = match &indexable {
-				// Cheaper to execute than creating object with overriden `this`
-				LocExpr(v, _) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {
-					let part = parts.next().expect("at least part should exist");
-					let Some(super_obj) = ctx.super_obj() else {
-						#[cfg(feature = "exp-null-coaelse")]
-						if part.null_coaelse {
-							return Ok(Val::Null);
-						}
-						bail!(NoSuperFound)
-					};
-					let name = evaluate(ctx.clone(), &part.value)?;
+			let mut indexable = if matches!(indexable.expr(), Expr::Literal(LiteralType::Super)) {
+				let part = parts.next().expect("at least part should exist");
+				let Some(super_obj) = ctx.super_obj() else {
+					#[cfg(feature = "exp-null-coaelse")]
+					if part.null_coaelse {
+						return Ok(Val::Null);
+					}
+					bail!(NoSuperFound)
+				};
+				let name = evaluate(ctx.clone(), &part.value)?;
 
-					let Val::Str(name) = name else {
-						bail!(ValueIndexMustBeTypeGot(
-							ValType::Obj,
-							ValType::Str,
-							name.value_type(),
-						))
-					};
+				let Val::Str(name) = name else {
+					bail!(ValueIndexMustBeTypeGot(
+						ValType::Obj,
+						ValType::Str,
+						name.value_type(),
+					))
+				};
 
-					let this = ctx
-						.this()
-						.expect("no this found, while super present, should not happen");
-					let name = name.into_flat();
-					match super_obj
-						.get_for(name.clone(), this.clone())
-						.with_description_src(&part.value, || format!("field <{name}> access"))?
-					{
-						Some(v) => v,
-						#[cfg(feature = "exp-null-coaelse")]
-						None if part.null_coaelse => return Ok(Val::Null),
-						None => {
-							let suggestions = suggest_object_fields(super_obj, name.clone());
+				let this = ctx
+					.this()
+					.expect("no this found, while super present, should not happen");
+				let name = name.into_flat();
+				match super_obj
+					.get_for(name.clone(), this.clone())
+					.with_description_src(&part.value, || format!("field <{name}> access"))?
+				{
+					Some(v) => v,
+					#[cfg(feature = "exp-null-coaelse")]
+					None if part.null_coaelse => return Ok(Val::Null),
+					None => {
+						let suggestions = suggest_object_fields(super_obj, name.clone());
 
-							bail!(NoSuchField(name, suggestions))
-						}
+						bail!(NoSuchField(name, suggestions))
 					}
 				}
-				e => evaluate(ctx.clone(), e)?,
+			} else {
+				evaluate(ctx.clone(), indexable)?
 			};
 
 			for part in parts {
@@ -639,7 +636,7 @@
 			&Val::Obj(evaluate_object(ctx, b)?),
 		)?,
 		Apply(value, args, tailstrict) => {
-			evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?
+			evaluate_apply(ctx, value, args, CallLocation::new(&loc), *tailstrict)?
 		}
 		Function(params, body) => {
 			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())
@@ -649,7 +646,7 @@
 			evaluate(ctx, returned)?
 		}
 		ErrorStmt(e) => State::push(
-			CallLocation::new(loc),
+			CallLocation::new(&loc),
 			|| "error statement".to_owned(),
 			|| bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),
 		)?,
@@ -659,7 +656,7 @@
 			cond_else,
 		} => {
 			if State::push(
-				CallLocation::new(loc),
+				CallLocation::new(&loc),
 				|| "if condition".to_owned(),
 				|| bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),
 			)? {
@@ -690,7 +687,7 @@
 			}
 
 			let indexable = evaluate(ctx.clone(), value)?;
-			let loc = CallLocation::new(loc);
+			let loc = CallLocation::new(&loc);
 
 			let start = parse_idx(loc, &ctx, desc.start.as_ref(), "start")?;
 			let end = parse_idx(loc, &ctx, desc.end.as_ref(), "end")?;
@@ -699,7 +696,7 @@
 			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?
 		}
 		i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {
-			let Expr::Str(path) = &*path.0 else {
+			let Expr::Str(path) = &path.expr() else {
 				bail!("computed imports are not supported")
 			};
 			let tmp = loc.clone().0;
@@ -707,7 +704,7 @@
 			let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;
 			match i {
 				Import(_) => State::push(
-					CallLocation::new(loc),
+					CallLocation::new(&loc),
 					|| format!("import {:?}", path.clone()),
 					|| s.import_resolved(resolved_path),
 				)?,
modifiedcrates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -4,7 +4,7 @@
 use jrsonnet_gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
 pub use jrsonnet_macros::builtin;
-use jrsonnet_parser::{Destruct, Expr, ExprLocation, LocExpr, ParamsDesc};
+use jrsonnet_parser::{Destruct, Expr, LocExpr, ParamsDesc, Span};
 
 use self::{
 	arglike::OptionalContext,
@@ -22,10 +22,10 @@
 /// Function callsite location.
 /// Either from other jsonnet code, specified by expression location, or from native (without location).
 #[derive(Clone, Copy)]
-pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);
+pub struct CallLocation<'l>(pub Option<&'l Span>);
 impl<'l> CallLocation<'l> {
 	/// Construct new location for calls coming from specified jsonnet expression location.
-	pub const fn new(loc: &'l ExprLocation) -> Self {
+	pub const fn new(loc: &'l Span) -> Self {
 		Self(Some(loc))
 	}
 }
@@ -225,7 +225,7 @@
 					#[cfg(feature = "exp-destruct")]
 					_ => return false,
 				};
-				&desc.body.0 as &Expr == &Expr::Var(id.clone())
+				desc.body.expr() == &Expr::Var(id.clone())
 			}
 			_ => false,
 		}
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/lib.rs
1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8#[cfg(feature = "async-import")]9pub mod async_import;10mod ctx;11mod dynamic;12pub mod error;13mod evaluate;14pub mod function;15pub mod gc;16mod import;17mod integrations;18pub mod manifest;19mod map;20mod obj;21pub mod stack;22pub mod stdlib;23mod tla;24pub mod trace;25pub mod typed;26pub mod val;2728use std::{29	any::Any,30	cell::{RefCell, RefMut},31	fmt::{self, Debug},32	path::Path,33};3435pub use ctx::*;36pub use dynamic::*;37pub use error::{Error, ErrorKind::*, Result, ResultExt};38pub use evaluate::*;39use function::CallLocation;40use gc::{GcHashMap, TraceBox};41use hashbrown::hash_map::RawEntryMut;42pub use import::*;43use jrsonnet_gcmodule::{Cc, Trace};44pub use jrsonnet_interner::{IBytes, IStr};45#[doc(hidden)]46pub use jrsonnet_macros;47pub use jrsonnet_parser as parser;48use jrsonnet_parser::{ExprLocation, LocExpr, ParserSettings, Source, SourcePath};49pub use obj::*;50use stack::check_depth;51pub use tla::apply_tla;52pub use val::{Thunk, Val};5354/// Thunk without bound `super`/`this`55/// object inheritance may be overriden multiple times, and will be fixed only on field read56pub trait Unbound: Trace {57	/// Type of value after object context is bound58	type Bound;59	/// Create value bound to specified object context60	fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;61}6263/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code64/// Standard jsonnet fields are always unbound65#[derive(Clone, Trace)]66pub enum MaybeUnbound {67	/// Value needs to be bound to `this`/`super`68	Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),69	/// Value is object-independent70	Bound(Thunk<Val>),71}7273impl Debug for MaybeUnbound {74	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {75		write!(f, "MaybeUnbound")76	}77}78impl MaybeUnbound {79	/// Attach object context to value, if required80	pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {81		match self {82			Self::Unbound(v) => v.bind(sup, this),83			Self::Bound(v) => Ok(v.evaluate()?),84		}85	}86}8788/// During import, this trait will be called to create initial context for file.89/// It may initialize global variables, stdlib for example.90pub trait ContextInitializer: Trace {91	/// For which size the builder should be preallocated92	fn reserve_vars(&self) -> usize {93		094	}95	/// Initialize default file context.96	/// Has default implementation, which calls `populate`.97	/// Prefer to always implement `populate` instead.98	fn initialize(&self, state: State, for_file: Source) -> Context {99		let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());100		self.populate(for_file, &mut builder);101		builder.build()102	}103	/// For composability: extend builder. May panic if this initialization is not supported,104	/// and the context may only be created via `initialize`.105	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);106	/// Allows upcasting from abstract to concrete context initializer.107	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.108	fn as_any(&self) -> &dyn Any;109}110111/// Context initializer which adds nothing.112impl ContextInitializer for () {113	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}114	fn as_any(&self) -> &dyn Any {115		self116	}117}118119macro_rules! impl_context_initializer {120	($($gen:ident)*) => {121		#[allow(non_snake_case)]122		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {123			fn reserve_vars(&self) -> usize {124				let mut out = 0;125				let ($($gen,)*) = self;126				$(out += $gen.reserve_vars();)*127				out128			}129			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {130				let ($($gen,)*) = self;131				$($gen.populate(for_file.clone(), builder);)*132			}133			fn as_any(&self) -> &dyn Any {134				self135			}136		}137	};138	($($cur:ident)* @ $c:ident $($rest:ident)*) => {139		impl_context_initializer!($($cur)*);140		impl_context_initializer!($($cur)* $c @ $($rest)*);141	};142	($($cur:ident)* @) => {143		impl_context_initializer!($($cur)*);144	}145}146impl_context_initializer! {147	A @ B C D E F G148}149150#[derive(Trace)]151struct FileData {152	string: Option<IStr>,153	bytes: Option<IBytes>,154	parsed: Option<LocExpr>,155	evaluated: Option<Val>,156157	evaluating: bool,158}159impl FileData {160	fn new_string(data: IStr) -> Self {161		Self {162			string: Some(data),163			bytes: None,164			parsed: None,165			evaluated: None,166			evaluating: false,167		}168	}169	fn new_bytes(data: IBytes) -> Self {170		Self {171			string: None,172			bytes: Some(data),173			parsed: None,174			evaluated: None,175			evaluating: false,176		}177	}178	pub(crate) fn get_string(&mut self) -> Option<IStr> {179		if self.string.is_none() {180			self.string = Some(181				self.bytes182					.as_ref()183					.expect("either string or bytes should be set")184					.clone()185					.cast_str()?,186			);187		}188		Some(self.string.clone().expect("just set"))189	}190}191192#[derive(Trace)]193pub struct EvaluationStateInternals {194	/// Internal state195	file_cache: RefCell<GcHashMap<SourcePath, FileData>>,196	/// Context initializer, which will be used for imports and everything197	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`198	context_initializer: TraceBox<dyn ContextInitializer>,199	/// Used to resolve file locations/contents200	import_resolver: TraceBox<dyn ImportResolver>,201}202203/// Maintains stack trace and import resolution204#[derive(Clone, Trace)]205pub struct State(Cc<EvaluationStateInternals>);206207impl State {208	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise209	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {210		let mut file_cache = self.file_cache();211		let mut file = file_cache.raw_entry_mut().from_key(&path);212213		let file = match file {214			RawEntryMut::Occupied(ref mut d) => d.get_mut(),215			RawEntryMut::Vacant(v) => {216				let data = self.import_resolver().load_file_contents(&path)?;217				v.insert(218					path.clone(),219					FileData::new_string(220						std::str::from_utf8(&data)221							.map_err(|_| ImportBadFileUtf8(path.clone()))?222							.into(),223					),224				)225				.1226			}227		};228		Ok(file229			.get_string()230			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)231	}232	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise233	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {234		let mut file_cache = self.file_cache();235		let mut file = file_cache.raw_entry_mut().from_key(&path);236237		let file = match file {238			RawEntryMut::Occupied(ref mut d) => d.get_mut(),239			RawEntryMut::Vacant(v) => {240				let data = self.import_resolver().load_file_contents(&path)?;241				v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))242					.1243			}244		};245		if let Some(str) = &file.bytes {246			return Ok(str.clone());247		}248		if file.bytes.is_none() {249			file.bytes = Some(250				file.string251					.as_ref()252					.expect("either string or bytes should be set")253					.clone()254					.cast_bytes(),255			);256		}257		Ok(file.bytes.as_ref().expect("just set").clone())258	}259	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise260	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {261		let mut file_cache = self.file_cache();262		let mut file = file_cache.raw_entry_mut().from_key(&path);263264		let file = match file {265			RawEntryMut::Occupied(ref mut d) => d.get_mut(),266			RawEntryMut::Vacant(v) => {267				let data = self.import_resolver().load_file_contents(&path)?;268				v.insert(269					path.clone(),270					FileData::new_string(271						std::str::from_utf8(&data)272							.map_err(|_| ImportBadFileUtf8(path.clone()))?273							.into(),274					),275				)276				.1277			}278		};279		if let Some(val) = &file.evaluated {280			return Ok(val.clone());281		}282		let code = file283			.get_string()284			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;285		let file_name = Source::new(path.clone(), code.clone());286		if file.parsed.is_none() {287			file.parsed = Some(288				jrsonnet_parser::parse(289					&code,290					&ParserSettings {291						source: file_name.clone(),292					},293				)294				.map_err(|e| ImportSyntaxError {295					path: file_name.clone(),296					error: Box::new(e),297				})?,298			);299		}300		let parsed = file.parsed.as_ref().expect("just set").clone();301		if file.evaluating {302			bail!(InfiniteRecursionDetected)303		}304		file.evaluating = true;305		// Dropping file cache guard here, as evaluation may use this map too306		drop(file_cache);307		let res = evaluate(self.create_default_context(file_name), &parsed);308309		let mut file_cache = self.file_cache();310		let mut file = file_cache.raw_entry_mut().from_key(&path);311312		let RawEntryMut::Occupied(file) = &mut file else {313			unreachable!("this file was just here!")314		};315		let file = file.get_mut();316		file.evaluating = false;317		match res {318			Ok(v) => {319				file.evaluated = Some(v.clone());320				Ok(v)321			}322			Err(e) => Err(e),323		}324	}325326	/// Has same semantics as `import 'path'` called from `from` file327	pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {328		let resolved = self.resolve_from(from, path)?;329		self.import_resolved(resolved)330	}331	pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {332		let resolved = self.resolve(path)?;333		self.import_resolved(resolved)334	}335336	/// Creates context with all passed global variables337	pub fn create_default_context(&self, source: Source) -> Context {338		self.context_initializer().initialize(self.clone(), source)339	}340341	/// Creates context with all passed global variables, calling custom modifier342	pub fn create_default_context_with(343		&self,344		source: Source,345		context_initializer: impl ContextInitializer,346	) -> Context {347		let default_initializer = self.context_initializer();348		let mut builder = ContextBuilder::with_capacity(349			self.clone(),350			default_initializer.reserve_vars() + context_initializer.reserve_vars(),351		);352		default_initializer.populate(source.clone(), &mut builder);353		context_initializer.populate(source, &mut builder);354355		builder.build()356	}357358	/// Executes code creating a new stack frame359	pub fn push<T>(360		e: CallLocation<'_>,361		frame_desc: impl FnOnce() -> String,362		f: impl FnOnce() -> Result<T>,363	) -> Result<T> {364		let _guard = check_depth()?;365366		f().with_description_src(e, frame_desc)367	}368369	/// Executes code creating a new stack frame370	pub fn push_val(371		&self,372		e: &ExprLocation,373		frame_desc: impl FnOnce() -> String,374		f: impl FnOnce() -> Result<Val>,375	) -> Result<Val> {376		let _guard = check_depth()?;377378		f().with_description_src(e, frame_desc)379	}380	/// Executes code creating a new stack frame381	pub fn push_description<T>(382		frame_desc: impl FnOnce() -> String,383		f: impl FnOnce() -> Result<T>,384	) -> Result<T> {385		let _guard = check_depth()?;386387		f().with_description(frame_desc)388	}389}390391/// Internals392impl State {393	fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {394		self.0.file_cache.borrow_mut()395	}396}397398#[derive(Trace)]399pub struct InitialUnderscore(pub Thunk<Val>);400impl ContextInitializer for InitialUnderscore {401	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {402		builder.bind("_", self.0.clone());403	}404405	fn as_any(&self) -> &dyn Any {406		self407	}408}409410/// Raw methods evaluate passed values but don't perform TLA execution411impl State {412	/// Parses and evaluates the given snippet413	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {414		let code = code.into();415		let source = Source::new_virtual(name.into(), code.clone());416		let parsed = jrsonnet_parser::parse(417			&code,418			&ParserSettings {419				source: source.clone(),420			},421		)422		.map_err(|e| ImportSyntaxError {423			path: source.clone(),424			error: Box::new(e),425		})?;426		evaluate(self.create_default_context(source), &parsed)427	}428	/// Parses and evaluates the given snippet with custom context modifier429	pub fn evaluate_snippet_with(430		&self,431		name: impl Into<IStr>,432		code: impl Into<IStr>,433		context_initializer: impl ContextInitializer,434	) -> Result<Val> {435		let code = code.into();436		let source = Source::new_virtual(name.into(), code.clone());437		let parsed = jrsonnet_parser::parse(438			&code,439			&ParserSettings {440				source: source.clone(),441			},442		)443		.map_err(|e| ImportSyntaxError {444			path: source.clone(),445			error: Box::new(e),446		})?;447		evaluate(448			self.create_default_context_with(source, context_initializer),449			&parsed,450		)451	}452}453454/// Settings utilities455impl State {456	// Only panics in case of [`ImportResolver`] contract violation457	#[allow(clippy::missing_panics_doc)]458	pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {459		self.import_resolver().resolve_from(from, path.as_ref())460	}461462	// Only panics in case of [`ImportResolver`] contract violation463	#[allow(clippy::missing_panics_doc)]464	pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {465		self.import_resolver().resolve(path.as_ref())466	}467	pub fn import_resolver(&self) -> &dyn ImportResolver {468		&*self.0.import_resolver469	}470	pub fn context_initializer(&self) -> &dyn ContextInitializer {471		&*self.0.context_initializer472	}473}474475impl State {476	pub fn builder() -> StateBuilder {477		StateBuilder::default()478	}479}480481impl Default for State {482	fn default() -> Self {483		Self::builder().build()484	}485}486487#[derive(Default)]488pub struct StateBuilder {489	import_resolver: Option<TraceBox<dyn ImportResolver>>,490	context_initializer: Option<TraceBox<dyn ContextInitializer>>,491}492impl StateBuilder {493	pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {494		let _ = self.import_resolver.insert(tb!(import_resolver));495		self496	}497	pub fn context_initializer(498		&mut self,499		context_initializer: impl ContextInitializer,500	) -> &mut Self {501		let _ = self.context_initializer.insert(tb!(context_initializer));502		self503	}504	pub fn build(mut self) -> State {505		State(Cc::new(EvaluationStateInternals {506			file_cache: RefCell::new(GcHashMap::new()),507			context_initializer: self.context_initializer.take().unwrap_or_else(|| tb!(())),508			import_resolver: self509				.import_resolver510				.take()511				.unwrap_or_else(|| tb!(DummyImportResolver)),512		}))513	}514}
after · crates/jrsonnet-evaluator/src/lib.rs
1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8#[cfg(feature = "async-import")]9pub mod async_import;10mod ctx;11mod dynamic;12pub mod error;13mod evaluate;14pub mod function;15pub mod gc;16mod import;17mod integrations;18pub mod manifest;19mod map;20mod obj;21pub mod stack;22pub mod stdlib;23mod tla;24pub mod trace;25pub mod typed;26pub mod val;2728use std::{29	any::Any,30	cell::{RefCell, RefMut},31	fmt::{self, Debug},32	path::Path,33};3435pub use ctx::*;36pub use dynamic::*;37pub use error::{Error, ErrorKind::*, Result, ResultExt};38pub use evaluate::*;39use function::CallLocation;40use gc::{GcHashMap, TraceBox};41use hashbrown::hash_map::RawEntryMut;42pub use import::*;43use jrsonnet_gcmodule::{Cc, Trace};44pub use jrsonnet_interner::{IBytes, IStr};45#[doc(hidden)]46pub use jrsonnet_macros;47pub use jrsonnet_parser as parser;48use jrsonnet_parser::{LocExpr, ParserSettings, Source, SourcePath, Span};49pub use obj::*;50use stack::check_depth;51pub use tla::apply_tla;52pub use val::{Thunk, Val};5354/// Thunk without bound `super`/`this`55/// object inheritance may be overriden multiple times, and will be fixed only on field read56pub trait Unbound: Trace {57	/// Type of value after object context is bound58	type Bound;59	/// Create value bound to specified object context60	fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;61}6263/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code64/// Standard jsonnet fields are always unbound65#[derive(Clone, Trace)]66pub enum MaybeUnbound {67	/// Value needs to be bound to `this`/`super`68	Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),69	/// Value is object-independent70	Bound(Thunk<Val>),71}7273impl Debug for MaybeUnbound {74	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {75		write!(f, "MaybeUnbound")76	}77}78impl MaybeUnbound {79	/// Attach object context to value, if required80	pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {81		match self {82			Self::Unbound(v) => v.bind(sup, this),83			Self::Bound(v) => Ok(v.evaluate()?),84		}85	}86}8788/// During import, this trait will be called to create initial context for file.89/// It may initialize global variables, stdlib for example.90pub trait ContextInitializer: Trace {91	/// For which size the builder should be preallocated92	fn reserve_vars(&self) -> usize {93		094	}95	/// Initialize default file context.96	/// Has default implementation, which calls `populate`.97	/// Prefer to always implement `populate` instead.98	fn initialize(&self, state: State, for_file: Source) -> Context {99		let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());100		self.populate(for_file, &mut builder);101		builder.build()102	}103	/// For composability: extend builder. May panic if this initialization is not supported,104	/// and the context may only be created via `initialize`.105	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);106	/// Allows upcasting from abstract to concrete context initializer.107	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.108	fn as_any(&self) -> &dyn Any;109}110111/// Context initializer which adds nothing.112impl ContextInitializer for () {113	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}114	fn as_any(&self) -> &dyn Any {115		self116	}117}118119macro_rules! impl_context_initializer {120	($($gen:ident)*) => {121		#[allow(non_snake_case)]122		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {123			fn reserve_vars(&self) -> usize {124				let mut out = 0;125				let ($($gen,)*) = self;126				$(out += $gen.reserve_vars();)*127				out128			}129			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {130				let ($($gen,)*) = self;131				$($gen.populate(for_file.clone(), builder);)*132			}133			fn as_any(&self) -> &dyn Any {134				self135			}136		}137	};138	($($cur:ident)* @ $c:ident $($rest:ident)*) => {139		impl_context_initializer!($($cur)*);140		impl_context_initializer!($($cur)* $c @ $($rest)*);141	};142	($($cur:ident)* @) => {143		impl_context_initializer!($($cur)*);144	}145}146impl_context_initializer! {147	A @ B C D E F G148}149150#[derive(Trace)]151struct FileData {152	string: Option<IStr>,153	bytes: Option<IBytes>,154	parsed: Option<LocExpr>,155	evaluated: Option<Val>,156157	evaluating: bool,158}159impl FileData {160	fn new_string(data: IStr) -> Self {161		Self {162			string: Some(data),163			bytes: None,164			parsed: None,165			evaluated: None,166			evaluating: false,167		}168	}169	fn new_bytes(data: IBytes) -> Self {170		Self {171			string: None,172			bytes: Some(data),173			parsed: None,174			evaluated: None,175			evaluating: false,176		}177	}178	pub(crate) fn get_string(&mut self) -> Option<IStr> {179		if self.string.is_none() {180			self.string = Some(181				self.bytes182					.as_ref()183					.expect("either string or bytes should be set")184					.clone()185					.cast_str()?,186			);187		}188		Some(self.string.clone().expect("just set"))189	}190}191192#[derive(Trace)]193pub struct EvaluationStateInternals {194	/// Internal state195	file_cache: RefCell<GcHashMap<SourcePath, FileData>>,196	/// Context initializer, which will be used for imports and everything197	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`198	context_initializer: TraceBox<dyn ContextInitializer>,199	/// Used to resolve file locations/contents200	import_resolver: TraceBox<dyn ImportResolver>,201}202203/// Maintains stack trace and import resolution204#[derive(Clone, Trace)]205pub struct State(Cc<EvaluationStateInternals>);206207impl State {208	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise209	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {210		let mut file_cache = self.file_cache();211		let mut file = file_cache.raw_entry_mut().from_key(&path);212213		let file = match file {214			RawEntryMut::Occupied(ref mut d) => d.get_mut(),215			RawEntryMut::Vacant(v) => {216				let data = self.import_resolver().load_file_contents(&path)?;217				v.insert(218					path.clone(),219					FileData::new_string(220						std::str::from_utf8(&data)221							.map_err(|_| ImportBadFileUtf8(path.clone()))?222							.into(),223					),224				)225				.1226			}227		};228		Ok(file229			.get_string()230			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)231	}232	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise233	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {234		let mut file_cache = self.file_cache();235		let mut file = file_cache.raw_entry_mut().from_key(&path);236237		let file = match file {238			RawEntryMut::Occupied(ref mut d) => d.get_mut(),239			RawEntryMut::Vacant(v) => {240				let data = self.import_resolver().load_file_contents(&path)?;241				v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))242					.1243			}244		};245		if let Some(str) = &file.bytes {246			return Ok(str.clone());247		}248		if file.bytes.is_none() {249			file.bytes = Some(250				file.string251					.as_ref()252					.expect("either string or bytes should be set")253					.clone()254					.cast_bytes(),255			);256		}257		Ok(file.bytes.as_ref().expect("just set").clone())258	}259	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise260	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {261		let mut file_cache = self.file_cache();262		let mut file = file_cache.raw_entry_mut().from_key(&path);263264		let file = match file {265			RawEntryMut::Occupied(ref mut d) => d.get_mut(),266			RawEntryMut::Vacant(v) => {267				let data = self.import_resolver().load_file_contents(&path)?;268				v.insert(269					path.clone(),270					FileData::new_string(271						std::str::from_utf8(&data)272							.map_err(|_| ImportBadFileUtf8(path.clone()))?273							.into(),274					),275				)276				.1277			}278		};279		if let Some(val) = &file.evaluated {280			return Ok(val.clone());281		}282		let code = file283			.get_string()284			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;285		let file_name = Source::new(path.clone(), code.clone());286		if file.parsed.is_none() {287			file.parsed = Some(288				jrsonnet_parser::parse(289					&code,290					&ParserSettings {291						source: file_name.clone(),292					},293				)294				.map_err(|e| ImportSyntaxError {295					path: file_name.clone(),296					error: Box::new(e),297				})?,298			);299		}300		let parsed = file.parsed.as_ref().expect("just set").clone();301		if file.evaluating {302			bail!(InfiniteRecursionDetected)303		}304		file.evaluating = true;305		// Dropping file cache guard here, as evaluation may use this map too306		drop(file_cache);307		let res = evaluate(self.create_default_context(file_name), &parsed);308309		let mut file_cache = self.file_cache();310		let mut file = file_cache.raw_entry_mut().from_key(&path);311312		let RawEntryMut::Occupied(file) = &mut file else {313			unreachable!("this file was just here!")314		};315		let file = file.get_mut();316		file.evaluating = false;317		match res {318			Ok(v) => {319				file.evaluated = Some(v.clone());320				Ok(v)321			}322			Err(e) => Err(e),323		}324	}325326	/// Has same semantics as `import 'path'` called from `from` file327	pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {328		let resolved = self.resolve_from(from, path)?;329		self.import_resolved(resolved)330	}331	pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {332		let resolved = self.resolve(path)?;333		self.import_resolved(resolved)334	}335336	/// Creates context with all passed global variables337	pub fn create_default_context(&self, source: Source) -> Context {338		self.context_initializer().initialize(self.clone(), source)339	}340341	/// Creates context with all passed global variables, calling custom modifier342	pub fn create_default_context_with(343		&self,344		source: Source,345		context_initializer: impl ContextInitializer,346	) -> Context {347		let default_initializer = self.context_initializer();348		let mut builder = ContextBuilder::with_capacity(349			self.clone(),350			default_initializer.reserve_vars() + context_initializer.reserve_vars(),351		);352		default_initializer.populate(source.clone(), &mut builder);353		context_initializer.populate(source, &mut builder);354355		builder.build()356	}357358	/// Executes code creating a new stack frame359	pub fn push<T>(360		e: CallLocation<'_>,361		frame_desc: impl FnOnce() -> String,362		f: impl FnOnce() -> Result<T>,363	) -> Result<T> {364		let _guard = check_depth()?;365366		f().with_description_src(e, frame_desc)367	}368369	/// Executes code creating a new stack frame370	pub fn push_val(371		&self,372		e: &Span,373		frame_desc: impl FnOnce() -> String,374		f: impl FnOnce() -> Result<Val>,375	) -> Result<Val> {376		let _guard = check_depth()?;377378		f().with_description_src(e, frame_desc)379	}380	/// Executes code creating a new stack frame381	pub fn push_description<T>(382		frame_desc: impl FnOnce() -> String,383		f: impl FnOnce() -> Result<T>,384	) -> Result<T> {385		let _guard = check_depth()?;386387		f().with_description(frame_desc)388	}389}390391/// Internals392impl State {393	fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {394		self.0.file_cache.borrow_mut()395	}396}397398#[derive(Trace)]399pub struct InitialUnderscore(pub Thunk<Val>);400impl ContextInitializer for InitialUnderscore {401	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {402		builder.bind("_", self.0.clone());403	}404405	fn as_any(&self) -> &dyn Any {406		self407	}408}409410/// Raw methods evaluate passed values but don't perform TLA execution411impl State {412	/// Parses and evaluates the given snippet413	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {414		let code = code.into();415		let source = Source::new_virtual(name.into(), code.clone());416		let parsed = jrsonnet_parser::parse(417			&code,418			&ParserSettings {419				source: source.clone(),420			},421		)422		.map_err(|e| ImportSyntaxError {423			path: source.clone(),424			error: Box::new(e),425		})?;426		evaluate(self.create_default_context(source), &parsed)427	}428	/// Parses and evaluates the given snippet with custom context modifier429	pub fn evaluate_snippet_with(430		&self,431		name: impl Into<IStr>,432		code: impl Into<IStr>,433		context_initializer: impl ContextInitializer,434	) -> Result<Val> {435		let code = code.into();436		let source = Source::new_virtual(name.into(), code.clone());437		let parsed = jrsonnet_parser::parse(438			&code,439			&ParserSettings {440				source: source.clone(),441			},442		)443		.map_err(|e| ImportSyntaxError {444			path: source.clone(),445			error: Box::new(e),446		})?;447		evaluate(448			self.create_default_context_with(source, context_initializer),449			&parsed,450		)451	}452}453454/// Settings utilities455impl State {456	// Only panics in case of [`ImportResolver`] contract violation457	#[allow(clippy::missing_panics_doc)]458	pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {459		self.import_resolver().resolve_from(from, path.as_ref())460	}461462	// Only panics in case of [`ImportResolver`] contract violation463	#[allow(clippy::missing_panics_doc)]464	pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {465		self.import_resolver().resolve(path.as_ref())466	}467	pub fn import_resolver(&self) -> &dyn ImportResolver {468		&*self.0.import_resolver469	}470	pub fn context_initializer(&self) -> &dyn ContextInitializer {471		&*self.0.context_initializer472	}473}474475impl State {476	pub fn builder() -> StateBuilder {477		StateBuilder::default()478	}479}480481impl Default for State {482	fn default() -> Self {483		Self::builder().build()484	}485}486487#[derive(Default)]488pub struct StateBuilder {489	import_resolver: Option<TraceBox<dyn ImportResolver>>,490	context_initializer: Option<TraceBox<dyn ContextInitializer>>,491}492impl StateBuilder {493	pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {494		let _ = self.import_resolver.insert(tb!(import_resolver));495		self496	}497	pub fn context_initializer(498		&mut self,499		context_initializer: impl ContextInitializer,500	) -> &mut Self {501		let _ = self.context_initializer.insert(tb!(context_initializer));502		self503	}504	pub fn build(mut self) -> State {505		State(Cc::new(EvaluationStateInternals {506			file_cache: RefCell::new(GcHashMap::new()),507			context_initializer: self.context_initializer.take().unwrap_or_else(|| tb!(())),508			import_resolver: self509				.import_resolver510				.take()511				.unwrap_or_else(|| tb!(DummyImportResolver)),512		}))513	}514}
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -8,7 +8,7 @@
 
 use jrsonnet_gcmodule::{Cc, Trace, Weak};
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ExprLocation, Visibility};
+use jrsonnet_parser::{Span, Visibility};
 use rustc_hash::FxHashMap;
 
 use crate::{
@@ -135,7 +135,7 @@
 	flags: ObjFieldFlags,
 	original_index: FieldIndex,
 	pub invoke: MaybeUnbound,
-	pub location: Option<ExprLocation>,
+	pub location: Option<Span>,
 }
 
 pub trait ObjectAssertion: Trace {
@@ -896,7 +896,7 @@
 	add: bool,
 	visibility: Visibility,
 	original_index: FieldIndex,
-	location: Option<ExprLocation>,
+	location: Option<Span>,
 }
 
 #[allow(clippy::missing_const_for_fn)]
@@ -926,7 +926,7 @@
 	pub fn hide(self) -> Self {
 		self.with_visibility(Visibility::Hidden)
 	}
-	pub fn with_location(mut self, location: ExprLocation) -> Self {
+	pub fn with_location(mut self, location: Span) -> Self {
 		self.location = Some(location);
 		self
 	}
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -5,7 +5,7 @@
 };
 
 use jrsonnet_gcmodule::Trace;
-use jrsonnet_parser::{CodeLocation, ExprLocation, Source};
+use jrsonnet_parser::{CodeLocation, Source, Span};
 
 use crate::{error::ErrorKind, Error};
 
@@ -380,7 +380,7 @@
 		error: &Error,
 	) -> Result<(), std::fmt::Error> {
 		struct ResetData {
-			loc: ExprLocation,
+			loc: Span,
 		}
 		use hi_doc::{source_to_ansi, Formatting, SnippetBuilder, Text};
 
@@ -399,7 +399,7 @@
 		}
 		let trace = &error.trace();
 		let snippet_builder: RefCell<Option<SnippetBuilder>> = RefCell::new(None);
-		let mut last_location: Option<ExprLocation> = None;
+		let mut last_location: Option<Span> = None;
 		let mut flush_builder = |data: Option<ResetData>| {
 			use std::fmt::Write;
 			let mut out = String::new();
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -376,7 +376,7 @@
 				State, Val,
 				function::{builtin::{Builtin, StaticBuiltin, BuiltinParam, ParamName, ParamDefault}, CallLocation, ArgsLike, parse::parse_builtin_call},
 				Result, Context, typed::Typed,
-				parser::ExprLocation,
+				parser::Span,
 			};
 			const PARAMS: &'static [BuiltinParam] = &[
 				#(#params_desc)*
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -385,17 +385,16 @@
 #[derive(Clone, PartialEq, Eq, Trace)]
 #[trace(skip)]
 #[repr(C)]
-pub struct ExprLocation(pub Source, pub u32, pub u32);
-impl ExprLocation {
-	pub fn belongs_to(&self, other: &ExprLocation) -> bool {
+pub struct Span(pub Source, pub u32, pub u32);
+impl Span {
+	pub fn belongs_to(&self, other: &Span) -> bool {
 		other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2
 	}
 }
 
-#[cfg(target_pointer_width = "64")]
-static_assertions::assert_eq_size!(ExprLocation, [u8; 16]);
+static_assertions::assert_eq_size!(Span, (usize, usize));
 
-impl Debug for ExprLocation {
+impl Debug for Span {
 	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 		write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)
 	}
@@ -403,19 +402,32 @@
 
 /// Holds AST expression and its location in source file
 #[derive(Clone, PartialEq, Trace)]
-pub struct LocExpr(pub Rc<Expr>, pub ExprLocation);
+pub struct LocExpr(Rc<(Expr, Span)>);
+impl LocExpr {
+	pub fn new(expr: Expr, span: Span) -> Self {
+		Self(Rc::new((expr, span)))
+	}
+	#[inline]
+	pub fn span(&self) -> Span {
+		self.0 .1.clone()
+	}
+	#[inline]
+	pub fn expr(&self) -> &Expr {
+		&self.0 .0
+	}
+}
 
-#[cfg(target_pointer_width = "64")]
-static_assertions::assert_eq_size!(LocExpr, [u8; 24]);
+static_assertions::assert_eq_size!(LocExpr, usize);
 
 impl Debug for LocExpr {
 	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+		let expr = self.expr();
 		if f.alternate() {
-			write!(f, "{:#?}", self.0)?;
+			write!(f, "{:#?}", expr)?;
 		} else {
-			write!(f, "{:?}", self.0)?;
+			write!(f, "{:?}", expr)?;
 		}
-		write!(f, " from {:?}", self.1)?;
+		write!(f, " from {:?}", self.span())?;
 		Ok(())
 	}
 }
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -232,7 +232,7 @@
 		pub rule var_expr(s: &ParserSettings) -> Expr
 			= n:id() { expr::Expr::Var(n) }
 		pub rule id_loc(s: &ParserSettings) -> LocExpr
-			= a:position!() n:id() b:position!() { LocExpr(Rc::new(expr::Expr::Str(n)), ExprLocation(s.source.clone(), a as u32,b as u32)) }
+			= a:position!() n:id() b:position!() { LocExpr::new(expr::Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }
 		pub rule if_then_else_expr(s: &ParserSettings) -> Expr
 			= cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{
 				cond,
@@ -299,7 +299,7 @@
 		use UnaryOpType::*;
 		rule expr(s: &ParserSettings) -> LocExpr
 			= precedence! {
-				start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.source.clone(), start as u32, end as u32)) }
+				start:position!() v:@ end:position!() { LocExpr::new(v, Span(s.source.clone(), start as u32, end as u32)) }
 				--
 				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}
 				a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {
@@ -370,10 +370,7 @@
 /// Used for importstr values
 pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {
 	let len = str.len();
-	LocExpr(
-		Rc::new(Expr::Str(str)),
-		ExprLocation(settings.source.clone(), 0, len as u32),
-	)
+	LocExpr::new(Expr::Str(str), Span(settings.source.clone(), 0, len as u32))
 }
 
 #[cfg(test)]
@@ -398,9 +395,9 @@
 
 	macro_rules! el {
 		($expr:expr, $from:expr, $to:expr$(,)?) => {
-			LocExpr(
-				std::rc::Rc::new($expr),
-				ExprLocation(
+			LocExpr::new(
+				$expr,
+				Span(
 					Source::new_virtual("<test>".into(), IStr::empty()),
 					$from,
 					$to,