git.delta.rocks / jrsonnet / refs/commits / c6d0d24a32d3

difftreelog

refactor only keep used spans in IR

uxonrmwzYaroslav Bolyukin2026-03-22parent: #44f6e2c.patch.diff
in: master

12 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -730,6 +730,7 @@
 version = "0.5.0-pre97"
 dependencies = [
  "insta",
+ "jrsonnet-gcmodule",
  "jrsonnet-ir",
  "peg",
 ]
modifiedcrates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -38,7 +38,7 @@
 		Self::new(RangeArray::empty())
 	}
 
-	pub fn expr(ctx: Context, exprs: Rc<Vec<Spanned<Expr>>>) -> Self {
+	pub fn expr(ctx: Context, exprs: Rc<Vec<Expr>>) -> Self {
 		Self::new(ExprArray::new(ctx, exprs))
 	}
 
modifiedcrates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -118,11 +118,11 @@
 #[derive(Debug, Trace, Clone)]
 pub struct ExprArray {
 	ctx: Context,
-	src: Rc<Vec<Spanned<Expr>>>,
+	src: Rc<Vec<Expr>>,
 	cached: Cc<RefCell<Vec<ArrayThunk>>>,
 }
 impl ExprArray {
-	pub fn new(ctx: Context, src: Rc<Vec<Spanned<Expr>>>) -> Self {
+	pub fn new(ctx: Context, src: Rc<Vec<Expr>>) -> Self {
 		Self {
 			ctx,
 			cached: Cc::new(RefCell::new(vec![ArrayThunk::Waiting; src.len()])),
modifiedcrates/jrsonnet-evaluator/src/async_import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/async_import.rs
+++ b/crates/jrsonnet-evaluator/src/async_import.rs
@@ -139,7 +139,7 @@
 			if let Expr::Str(s) = &***v {
 				out.0.push(Import {
 					path: ResolvePathOwned::Str(s.to_string()),
-					expression: matches!(&**expr, Expr::Import(ImportKind::Normal, _)),
+					expression: todo!(),
 				});
 			}
 			// Non-string import will fail in runtime
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -53,7 +53,7 @@
 			Expr::Str(_)
 			| Expr::Num(_)
 			| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,
-			Expr::Arr(a) => a.iter().all(|e| is_trivial(&**e)),
+			Expr::Arr(a) => a.iter().all(|e| is_trivial(&*e)),
 			_ => false,
 		}
 	}
@@ -71,7 +71,7 @@
 			}
 			Val::Arr(ArrValue::eager(
 				n.iter()
-					.map(|e| evaluate_trivial(&**e))
+					.map(|e| evaluate_trivial(&*e))
 					.map(|e| e.expect("checked trivial"))
 					.collect(),
 			))
@@ -80,12 +80,7 @@
 	})
 }
 
-pub fn evaluate_method(
-	ctx: Context,
-	name: IStr,
-	params: ExprParams,
-	body: Rc<Spanned<Expr>>,
-) -> Val {
+pub fn evaluate_method(ctx: Context, name: IStr, params: ExprParams, body: Rc<Expr>) -> Val {
 	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {
 		name,
 		ctx,
@@ -97,18 +92,21 @@
 pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {
 	Ok(match field_name {
 		FieldName::Fixed(n) => Some(n.clone()),
-		FieldName::Dyn(expr) => in_frame(
-			CallLocation::new(&expr.span()),
-			|| "evaluating field name".to_string(),
-			|| {
-				let value = evaluate(ctx, expr)?;
-				if matches!(value, Val::Null) {
-					Ok(None)
-				} else {
-					Ok(Some(IStr::from_untyped(value)?))
-				}
-			},
-		)?,
+		FieldName::Dyn(expr) => {
+			// FIXME: Span
+			let value = evaluate(ctx, expr)?;
+			if matches!(value, Val::Null) {
+				None
+			} else {
+				Some(IStr::from_untyped(value)?)
+			}
+		} //
+		  // 	in_frame(
+		  // 	CallLocation::new(&expr.span()),
+		  // 	|| "evaluating field name".to_string(),
+		  // 	|| {
+		  // 	},
+		  // )?,
 	})
 }
 
@@ -119,46 +117,48 @@
 ) -> Result<()> {
 	match specs.first() {
 		None => callback(ctx)?,
-		Some(CompSpec::IfSpec(IfSpecData(cond))) => {
+		Some(CompSpec::IfSpec(Spanned(IfSpecData(cond), _))) => {
 			if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {
 				evaluate_comp(ctx, &specs[1..], callback)?;
 			}
 		}
-		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {
-			Val::Arr(list) => {
-				for item in list.iter_lazy() {
-					let fctx = Pending::new();
-					let mut new_bindings = FxHashMap::with_capacity(var.binds_len());
-					destruct(var, item, fctx.clone(), &mut new_bindings)?;
-					let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);
+		Some(CompSpec::ForSpec(Spanned(ForSpecData(var, expr), _))) => {
+			match evaluate(ctx.clone(), expr)? {
+				Val::Arr(list) => {
+					for item in list.iter_lazy() {
+						let fctx = Pending::new();
+						let mut new_bindings = FxHashMap::with_capacity(var.binds_len());
+						destruct(var, item, fctx.clone(), &mut new_bindings)?;
+						let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);
 
-					evaluate_comp(ctx, &specs[1..], callback)?;
+						evaluate_comp(ctx, &specs[1..], callback)?;
+					}
 				}
-			}
-			#[cfg(feature = "exp-object-iteration")]
-			Val::Obj(obj) => {
-				for field in obj.fields(
-					// TODO: Should there be ability to preserve iteration order?
-					#[cfg(feature = "exp-preserve-order")]
-					false,
-				) {
-					let fctx = Pending::new();
-					let mut new_bindings = FxHashMap::with_capacity(var.binds_len());
-					let obj = obj.clone();
-					let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![
-						Thunk::evaluated(Val::string(field.clone())),
-						Thunk!(move || obj.get(field).transpose().expect(
-							"field exists, as field name was obtained from object.fields()",
-						)),
-					])));
-					destruct(var, value, fctx.clone(), &mut new_bindings)?;
-					let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);
+				#[cfg(feature = "exp-object-iteration")]
+				Val::Obj(obj) => {
+					for field in obj.fields(
+						// TODO: Should there be ability to preserve iteration order?
+						#[cfg(feature = "exp-preserve-order")]
+						false,
+					) {
+						let fctx = Pending::new();
+						let mut new_bindings = FxHashMap::with_capacity(var.binds_len());
+						let obj = obj.clone();
+						let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![
+							Thunk::evaluated(Val::string(field.clone())),
+							Thunk!(move || obj.get(field).transpose().expect(
+								"field exists, as field name was obtained from object.fields()",
+							)),
+						])));
+						destruct(var, value, fctx.clone(), &mut new_bindings)?;
+						let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);
 
-					evaluate_comp(ctx, &specs[1..], callback)?;
+						evaluate_comp(ctx, &specs[1..], callback)?;
+					}
 				}
+				_ => bail!(InComprehensionCanOnlyIterateOverArray),
 			}
-			_ => bail!(InComprehensionCanOnlyIterateOverArray),
-		},
+		}
 	}
 	Ok(())
 }
@@ -221,7 +221,7 @@
 			#[derive(Trace)]
 			struct UnboundValue<B: Trace> {
 				uctx: B,
-				value: Rc<Spanned<Expr>>,
+				value: Rc<Expr>,
 				name: IStr,
 			}
 			impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {
@@ -235,7 +235,8 @@
 				.field(name.clone())
 				.with_add(*plus)
 				.with_visibility(*visibility)
-				.with_location(value.span())
+				// FIXME
+				// .with_location(value.span())
 				.bindable(UnboundValue {
 					uctx,
 					value: value.clone(),
@@ -251,7 +252,7 @@
 			#[derive(Trace)]
 			struct UnboundMethod<B: Trace> {
 				uctx: B,
-				value: Rc<Spanned<Expr>>,
+				value: Rc<Expr>,
 				params: ExprParams,
 				name: IStr,
 			}
@@ -270,7 +271,7 @@
 			builder
 				.field(name.clone())
 				.with_visibility(*visibility)
-				.with_location(value.span())
+				// .with_location(value.span())
 				.bindable(UnboundMethod {
 					uctx,
 					value: value.clone(),
@@ -337,7 +338,7 @@
 
 pub fn evaluate_apply(
 	ctx: Context,
-	value: &Spanned<Expr>,
+	value: &Expr,
 	args: &ArgsDesc,
 	loc: CallLocation<'_>,
 	tailstrict: bool,
@@ -379,16 +380,16 @@
 	Ok(())
 }
 
-pub fn evaluate_named_param(ctx: Context, expr: &Spanned<Expr>, name: ParamName) -> Result<Val> {
+pub fn evaluate_named_param(ctx: Context, expr: &Expr, name: ParamName) -> Result<Val> {
 	match name {
 		ParamName::Named(name) => evaluate_named(ctx, expr, name),
 		ParamName::Unnamed => evaluate(ctx, expr),
 	}
 }
 
-pub fn evaluate_named(ctx: Context, expr: &Spanned<Expr>, name: IStr) -> Result<Val> {
+pub fn evaluate_named(ctx: Context, expr: &Expr, name: IStr) -> Result<Val> {
 	use Expr::*;
-	Ok(match &**expr {
+	Ok(match &*expr {
 		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),
 		_ => evaluate(ctx, expr)?,
 	})
@@ -417,7 +418,7 @@
 		// because the standalone super literal is not supported, that is because in other
 		// implementations `in super` treated differently from `in smth_else`.
 		BinaryOp(bin)
-			if matches!(&*bin.rhs, Expr::Literal(LiteralType::Super))
+			if matches!(&bin.rhs, Expr::Literal(LiteralType::Super))
 				&& bin.op == BinaryOpType::In =>
 		{
 			let sup_this = ctx.try_sup_this()?;
@@ -433,12 +434,12 @@
 		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,
 		Var(name) => in_frame(
 			CallLocation::new(&name.span()),
-			|| format!("local <{name}> access"),
+			|| format!("local <{}> access", &**name),
 			|| ctx.binding((**name).clone())?.evaluate(),
 		)?,
 		Index { indexable, parts } => ensure_sufficient_stack(|| {
 			let mut parts = parts.iter();
-			let mut indexable = if matches!(&***indexable, Expr::Literal(LiteralType::Super)) {
+			let mut indexable = if matches!(&**indexable, Expr::Literal(LiteralType::Super)) {
 				let part = parts.next().expect("at least part should exist");
 				// sup_this existence check might also be skipped here for null-coalesce...
 				// But I believe this might cause errors.
@@ -463,7 +464,7 @@
 				let name = name.into_flat();
 				match sup_this
 					.get_super(name.clone())
-					.with_description_src(&part.value, || format!("field <{name}> access"))?
+					.with_description_src(&part.span, || format!("field <{name}> access"))?
 				{
 					Some(v) => v,
 					#[cfg(feature = "exp-null-coaelse")]
@@ -485,7 +486,7 @@
 				indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {
 					(Val::Obj(v), Val::Str(key)) => match v
 						.get(key.clone().into_flat())
-						.with_description_src(&part.value, || format!("field <{key}> access"))?
+						.with_description_src(&part.span, || format!("field <{key}> access"))?
 					{
 						Some(v) => v,
 						#[cfg(feature = "exp-null-coaelse")]
@@ -497,7 +498,7 @@
 								key.clone().into_flat(),
 								suggestions,
 							)))
-							.with_description_src(&part.value, || format!("field <{key}> access"));
+							.with_description_src(&part.span, || format!("field <{key}> access"));
 						}
 					},
 					(Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(
@@ -605,17 +606,21 @@
 			evaluate_assert(ctx.clone(), &assert.assert)?;
 			evaluate(ctx, &assert.rest)?
 		}
-		ErrorStmt(e) => in_frame(
-			CallLocation::new(&e.span()),
+		ErrorStmt(s, e) => in_frame(
+			CallLocation::new(&s),
 			|| "error statement".to_owned(),
 			|| bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),
 		)?,
 		IfElse(if_else) => {
-			if in_frame(
-				CallLocation::new(&if_else.cond.0.span()),
-				|| "if condition".to_owned(),
-				|| bool::from_untyped(evaluate(ctx.clone(), &if_else.cond.0)?),
-			)? {
+			if
+			// FIXME
+			//in_frame(
+			// CallLocation::new(&if_else.cond.0.span()),
+			// || "if condition".to_owned(),
+			// ||
+			bool::from_untyped(evaluate(ctx.clone(), &if_else.cond.0)?)?
+			// )?
+			{
 				evaluate(ctx, &if_else.cond_then)?
 			} else {
 				match &if_else.cond_else {
@@ -626,14 +631,13 @@
 		}
 		Slice(slice) => {
 			fn parse_idx<T: Typed + FromUntyped>(
-				loc: CallLocation<'_>,
 				ctx: Context,
 				expr: Option<&Spanned<Expr>>,
 				desc: &'static str,
 			) -> Result<Option<T>> {
 				if let Some(value) = expr {
 					Ok(in_frame(
-						loc,
+						CallLocation::new(&value.span()),
 						|| format!("slice {desc}"),
 						|| <Option<T>>::from_untyped(evaluate(ctx, value)?),
 					)?)
@@ -643,24 +647,23 @@
 			}
 
 			let indexable = evaluate(ctx.clone(), &slice.value)?;
-			let loc = CallLocation::new(&loc);
 
-			let start = parse_idx(loc, ctx.clone(), slice.slice.start.as_ref(), "start")?;
-			let end = parse_idx(loc, ctx.clone(), slice.slice.end.as_ref(), "end")?;
-			let step = parse_idx(loc, ctx, slice.slice.step.as_ref(), "step")?;
+			let start = parse_idx(ctx.clone(), slice.slice.start.as_ref(), "start")?;
+			let end = parse_idx(ctx.clone(), slice.slice.end.as_ref(), "end")?;
+			let step = parse_idx(ctx, slice.slice.step.as_ref(), "step")?;
 
 			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?
 		}
 		Import(kind, path) => {
-			let Expr::Str(path) = &***path else {
+			let Expr::Str(path) = &**path else {
 				bail!("computed imports are not supported")
 			};
-			let tmp = loc.clone().0;
 			with_state(|s| {
-				let resolved_path = s.resolve_from(tmp.source_path(), path)?;
-				Ok(match kind {
+				let span = kind.span();
+				let resolved_path = s.resolve_from(span.0.source_path(), path)?;
+				Ok(match &**kind {
 					ImportKind::Normal => in_frame(
-						CallLocation::new(&loc),
+						CallLocation::new(&span),
 						|| format!("import {:?}", path.clone()),
 						|| s.import_resolved(resolved_path),
 					)?,
modifiedcrates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -147,9 +147,9 @@
 
 pub fn evaluate_binary_op_special(
 	ctx: Context,
-	a: &Spanned<Expr>,
+	a: &Expr,
 	op: BinaryOpType,
-	b: &Spanned<Expr>,
+	b: &Expr,
 ) -> Result<Val> {
 	use BinaryOpType::*;
 	use Val::*;
modifiedcrates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -3,8 +3,8 @@
 use educe::Educe;
 use jrsonnet_gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
+use jrsonnet_ir::{ArgsDesc, Destruct, Expr, ExprParams, Span, Spanned};
 pub use jrsonnet_macros::builtin;
-use jrsonnet_ir::{ArgsDesc, Destruct, Expr, ExprParams, Span, Spanned};
 
 use self::{
 	builtin::{Builtin, StaticBuiltin},
@@ -71,7 +71,7 @@
 	/// Function parameter definition
 	pub params: ExprParams,
 	/// Function body
-	pub body: Rc<Spanned<Expr>>,
+	pub body: Rc<Expr>,
 }
 impl FuncDesc {
 	/// Create body context, but fill arguments without defaults with lazy error
@@ -256,7 +256,7 @@
 					#[cfg(feature = "exp-destruct")]
 					_ => return false,
 				};
-				**desc.body == Expr::Var(id.clone())
+				matches!(&*desc.body, Expr::Var(v) if &**v == id)
 			}
 			_ => false,
 		}
modifiedcrates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -2,7 +2,7 @@
 
 use jrsonnet_ir::{
 	function::{FunctionSignature, ParamName},
-	ArgsDesc, Expr, ExprParams, Spanned,
+	ArgsDesc, Expr, ExprParams,
 };
 use rustc_hash::FxHashMap;
 
@@ -15,7 +15,7 @@
 	Context, Pending, Thunk, Val,
 };
 
-fn eval_arg(ctx: Context, arg: &Rc<Spanned<Expr>>, tailstrict: bool) -> Result<Thunk<Val>> {
+fn eval_arg(ctx: Context, arg: &Rc<Expr>, tailstrict: bool) -> Result<Thunk<Val>> {
 	if tailstrict {
 		Ok(Thunk::evaluated(evaluate(ctx, arg)?))
 	} else {
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/lib.rs
1//! jsonnet interpreter implementation2#![cfg_attr(nightly, feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8pub mod async_import;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13pub mod function;14pub mod gc;15mod import;16mod integrations;17pub mod manifest;18mod map;19mod obj;20pub mod stack;21pub mod stdlib;22pub mod tla;23pub mod trace;24pub mod typed;25pub mod val;2627use std::{28	any::Any,29	cell::{RefCell, RefMut},30	clone::Clone,31	collections::hash_map::Entry,32	fmt::{self, Debug},33	marker::PhantomData,34	rc::Rc,35};3637pub use ctx::*;38pub use dynamic::*;39pub use error::{Error, ErrorKind::*, Result, ResultExt};40pub use evaluate::*;41use function::CallLocation;42pub use import::*;43use jrsonnet_gcmodule::{cc_dyn, Cc, Trace};44pub use jrsonnet_interner::{IBytes, IStr};45#[doc(hidden)]46pub use jrsonnet_macros;47pub use jrsonnet_ir as parser;48use jrsonnet_ir::{Expr, Source, SourcePath, Spanned};49use jrsonnet_peg_parser::ParserSettings;50pub use obj::*;51pub use rustc_hash;52use rustc_hash::FxHashMap;53use stack::check_depth;54pub use tla::apply_tla;55pub use val::{Thunk, Val};5657use crate::gc::WithCapacityExt as _;5859cc_dyn!(60	#[derive(Clone)]61	CcUnbound<V>,62	Unbound<Bound = V>63);6465/// Thunk without bound `super`/`this`66/// object inheritance may be overriden multiple times, and will be fixed only on field read67pub trait Unbound: Trace {68	/// Type of value after object context is bound69	type Bound;70	/// Create value bound to specified object context71	fn bind(&self, sup_this: SupThis) -> Result<Self::Bound>;72}7374/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code75/// Standard jsonnet fields are always unbound76#[derive(Clone, Trace)]77pub enum MaybeUnbound {78	/// Value needs to be bound to `this`/`super`79	Unbound(CcUnbound<Val>),80	/// Value is object-independent81	Bound(Thunk<Val>),82}8384impl Debug for MaybeUnbound {85	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {86		write!(f, "MaybeUnbound")87	}88}89impl MaybeUnbound {90	/// Attach object context to value, if required91	pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {92		match self {93			Self::Unbound(v) => v.0.bind(sup_this),94			Self::Bound(v) => Ok(v.evaluate()?),95		}96	}97}9899cc_dyn!(CcContextInitializer, ContextInitializer);100101/// During import, this trait will be called to create initial context for file.102/// It may initialize global variables, stdlib for example.103pub trait ContextInitializer: Trace {104	/// For which size the builder should be preallocated105	fn reserve_vars(&self) -> usize {106		0107	}108	/// Initialize default file context.109	/// Has default implementation, which calls `populate`.110	/// Prefer to always implement `populate` instead.111	fn initialize(&self, for_file: Source) -> Context {112		let mut builder = ContextBuilder::with_capacity(self.reserve_vars());113		self.populate(for_file, &mut builder);114		builder.build()115	}116	/// For composability: extend builder. May panic if this initialization is not supported,117	/// and the context may only be created via `initialize`.118	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);119	/// Allows upcasting from abstract to concrete context initializer.120	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.121	fn as_any(&self) -> &dyn Any;122}123124/// Context initializer which adds nothing.125impl ContextInitializer for () {126	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}127	fn as_any(&self) -> &dyn Any {128		self129	}130}131132impl<T> ContextInitializer for Option<T>133where134	T: ContextInitializer,135{136	fn initialize(&self, for_file: Source) -> Context {137		if let Some(ctx) = self {138			ctx.initialize(for_file)139		} else {140			().initialize(for_file)141		}142	}143144	fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {145		if let Some(ctx) = self {146			ctx.populate(for_file, builder);147		}148	}149150	fn as_any(&self) -> &dyn Any {151		self152	}153}154155macro_rules! impl_context_initializer {156	($($gen:ident)*) => {157		#[allow(non_snake_case)]158		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {159			fn reserve_vars(&self) -> usize {160				let mut out = 0;161				let ($($gen,)*) = self;162				$(out += $gen.reserve_vars();)*163				out164			}165			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {166				let ($($gen,)*) = self;167				$($gen.populate(for_file.clone(), builder);)*168			}169			fn as_any(&self) -> &dyn Any {170				self171			}172		}173	};174	($($cur:ident)* @ $c:ident $($rest:ident)*) => {175		impl_context_initializer!($($cur)*);176		impl_context_initializer!($($cur)* $c @ $($rest)*);177	};178	($($cur:ident)* @) => {179		impl_context_initializer!($($cur)*);180	}181}182impl_context_initializer! {183	A @ B C D E F G184}185186#[derive(Trace)]187struct FileData {188	string: Option<IStr>,189	bytes: Option<IBytes>,190	parsed: Option<Rc<Spanned<Expr>>>,191	evaluated: Option<Val>,192193	evaluating: bool,194}195impl FileData {196	fn new_string(data: IStr) -> Self {197		Self {198			string: Some(data),199			bytes: None,200			parsed: None,201			evaluated: None,202			evaluating: false,203		}204	}205	fn new_bytes(data: IBytes) -> Self {206		Self {207			string: None,208			bytes: Some(data),209			parsed: None,210			evaluated: None,211			evaluating: false,212		}213	}214	pub(crate) fn get_string(&mut self) -> Option<IStr> {215		if self.string.is_none() {216			self.string = Some(217				self.bytes218					.as_ref()219					.expect("either string or bytes should be set")220					.clone()221					.cast_str()?,222			);223		}224		Some(self.string.clone().expect("just set"))225	}226}227228#[derive(Trace)]229pub struct EvaluationStateInternals {230	/// Internal state231	file_cache: RefCell<FxHashMap<SourcePath, FileData>>,232	/// Context initializer, which will be used for imports and everything233	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`234	context_initializer: CcContextInitializer,235	/// Used to resolve file locations/contents236	import_resolver: Rc<dyn ImportResolver>,237}238239/// Maintains stack trace and import resolution240#[derive(Clone, Trace)]241pub struct State(Cc<EvaluationStateInternals>);242243thread_local! {244	pub static DEFAULT_STATE: State = State::builder().build();245	pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};246}247pub struct StateEnterGuard(PhantomData<()>);248impl Drop for StateEnterGuard {249	fn drop(&mut self) {250		STATE.with_borrow_mut(|v| *v = None);251	}252}253254pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {255	if let Some(state) = STATE.with_borrow(Clone::clone) {256		v(state)257	} else {258		let s = DEFAULT_STATE.with(Clone::clone);259		v(s)260	}261}262263impl State {264	pub fn enter(&self) -> StateEnterGuard {265		self.try_enter().expect("entered state already exists")266	}267	pub fn try_enter(&self) -> Option<StateEnterGuard> {268		STATE.with_borrow_mut(|v| {269			if v.is_none() {270				*v = Some(self.clone());271				Some(StateEnterGuard(PhantomData))272			} else {273				None274			}275		})276	}277	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise278	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {279		let mut file_cache = self.file_cache();280		let mut file = file_cache.entry(path.clone());281282		let file = match file {283			Entry::Occupied(ref mut d) => d.get_mut(),284			Entry::Vacant(v) => {285				let data = self.import_resolver().load_file_contents(&path)?;286				v.insert(FileData::new_string(287					std::str::from_utf8(&data)288						.map_err(|_| ImportBadFileUtf8(path.clone()))?289						.into(),290				))291			}292		};293		Ok(file294			.get_string()295			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)296	}297	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise298	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {299		let mut file_cache = self.file_cache();300		let mut file = file_cache.entry(path.clone());301302		let file = match file {303			Entry::Occupied(ref mut d) => d.get_mut(),304			Entry::Vacant(v) => {305				let data = self.import_resolver().load_file_contents(&path)?;306				v.insert(FileData::new_bytes(data.as_slice().into()))307			}308		};309		if let Some(str) = &file.bytes {310			return Ok(str.clone());311		}312		if file.bytes.is_none() {313			file.bytes = Some(314				file.string315					.as_ref()316					.expect("either string or bytes should be set")317					.clone()318					.cast_bytes(),319			);320		}321		Ok(file.bytes.as_ref().expect("just set").clone())322	}323	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise324	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {325		let mut file_cache = self.file_cache();326		let mut file = file_cache.entry(path.clone());327328		let file = match file {329			Entry::Occupied(ref mut d) => d.get_mut(),330			Entry::Vacant(v) => {331				let data = self.import_resolver().load_file_contents(&path)?;332				v.insert(FileData::new_string(333					std::str::from_utf8(&data)334						.map_err(|_| ImportBadFileUtf8(path.clone()))?335						.into(),336				))337			}338		};339		if let Some(val) = &file.evaluated {340			return Ok(val.clone());341		}342		let code = file343			.get_string()344			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;345		let file_name = Source::new(path.clone(), code.clone());346		if file.parsed.is_none() {347			file.parsed = Some(348				jrsonnet_peg_parser::parse(349					&code,350					&ParserSettings {351						source: file_name.clone(),352					},353				)354				.map(Rc::new)355				.map_err(|e| ImportSyntaxError {356					path: file_name.clone(),357					error: Box::new(e),358				})?,359			);360		}361		let parsed = file.parsed.as_ref().expect("just set").clone();362		if file.evaluating {363			bail!(InfiniteRecursionDetected)364		}365		file.evaluating = true;366		// Dropping file cache guard here, as evaluation may use this map too367		drop(file_cache);368		let res = evaluate(self.create_default_context(file_name), &parsed);369370		let mut file_cache = self.file_cache();371		let mut file = file_cache.entry(path);372373		let Entry::Occupied(file) = &mut file else {374			unreachable!("this file was just here")375		};376		let file = file.get_mut();377		file.evaluating = false;378		match res {379			Ok(v) => {380				file.evaluated = Some(v.clone());381				Ok(v)382			}383			Err(e) => Err(e),384		}385	}386387	/// Has same semantics as `import 'path'` called from `from` file388	pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {389		let resolved = self.resolve_from(from, &path)?;390		self.import_resolved(resolved)391	}392	pub fn import(&self, path: impl AsPathLike) -> Result<Val> {393		let resolved = self.resolve_from_default(&path)?;394		self.import_resolved(resolved)395	}396397	/// Creates context with all passed global variables398	pub fn create_default_context(&self, source: Source) -> Context {399		self.context_initializer().initialize(source)400	}401402	/// Creates context with all passed global variables, calling custom modifier403	pub fn create_default_context_with(404		&self,405		source: Source,406		context_initializer: impl ContextInitializer,407	) -> Context {408		let default_initializer = self.context_initializer();409		let mut builder = ContextBuilder::with_capacity(410			default_initializer.reserve_vars() + context_initializer.reserve_vars(),411		);412		default_initializer.populate(source.clone(), &mut builder);413		context_initializer.populate(source, &mut builder);414415		builder.build()416	}417}418419/// Internals420impl State {421	fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {422		self.0.file_cache.borrow_mut()423	}424}425/// Executes code creating a new stack frame, to be replaced with try{}426pub fn in_frame<T>(427	e: CallLocation<'_>,428	frame_desc: impl FnOnce() -> String,429	f: impl FnOnce() -> Result<T>,430) -> Result<T> {431	let _guard = check_depth()?;432433	f().with_description_src(e, frame_desc)434}435436/// Executes code creating a new stack frame, to be replaced with try{}437pub fn in_description_frame<T>(438	frame_desc: impl FnOnce() -> String,439	f: impl FnOnce() -> Result<T>,440) -> Result<T> {441	let _guard = check_depth()?;442443	f().with_description(frame_desc)444}445446#[derive(Trace)]447pub struct InitialUnderscore(pub Thunk<Val>);448impl ContextInitializer for InitialUnderscore {449	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {450		builder.bind("_", self.0.clone());451	}452453	fn as_any(&self) -> &dyn Any {454		self455	}456}457458/// Raw methods evaluate passed values but don't perform TLA execution459impl State {460	/// Parses and evaluates the given snippet461	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {462		let code = code.into();463		let source = Source::new_virtual(name.into(), code.clone());464		let parsed = jrsonnet_peg_parser::parse(465			&code,466			&ParserSettings {467				source: source.clone(),468			},469		)470		.map_err(|e| ImportSyntaxError {471			path: source.clone(),472			error: Box::new(e),473		})?;474		evaluate(self.create_default_context(source), &parsed)475	}476	/// Parses and evaluates the given snippet with custom context modifier477	pub fn evaluate_snippet_with(478		&self,479		name: impl Into<IStr>,480		code: impl Into<IStr>,481		context_initializer: impl ContextInitializer,482	) -> Result<Val> {483		let code = code.into();484		let source = Source::new_virtual(name.into(), code.clone());485		let parsed = jrsonnet_peg_parser::parse(486			&code,487			&ParserSettings {488				source: source.clone(),489			},490		)491		.map_err(|e| ImportSyntaxError {492			path: source.clone(),493			error: Box::new(e),494		})?;495		evaluate(496			self.create_default_context_with(source, context_initializer),497			&parsed,498		)499	}500}501502/// Settings utilities503impl State {504	// Only panics in case of [`ImportResolver`] contract violation505	#[allow(clippy::missing_panics_doc)]506	pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {507		self.import_resolver().resolve_from(from, path)508	}509	#[allow(clippy::missing_panics_doc)]510	pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {511		self.import_resolver().resolve_from_default(path)512	}513	pub fn import_resolver(&self) -> &dyn ImportResolver {514		&*self.0.import_resolver515	}516	pub fn context_initializer(&self) -> &dyn ContextInitializer {517		&*self.0.context_initializer.0518	}519}520521impl State {522	pub fn builder() -> StateBuilder {523		StateBuilder::default()524	}525}526527impl Default for State {528	fn default() -> Self {529		Self::builder().build()530	}531}532533#[derive(Default)]534pub struct StateBuilder {535	import_resolver: Option<Rc<dyn ImportResolver>>,536	context_initializer: Option<CcContextInitializer>,537}538impl StateBuilder {539	pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {540		let _ = self.import_resolver.insert(Rc::new(import_resolver));541		self542	}543	pub fn context_initializer(544		&mut self,545		context_initializer: impl ContextInitializer,546	) -> &mut Self {547		let _ = self548			.context_initializer549			.insert(CcContextInitializer::new(context_initializer));550		self551	}552	pub fn build(mut self) -> State {553		State(Cc::new(EvaluationStateInternals {554			file_cache: RefCell::new(FxHashMap::new()),555			context_initializer: self556				.context_initializer557				.take()558				.unwrap_or_else(|| CcContextInitializer::new(())),559			import_resolver: self560				.import_resolver561				.take()562				.unwrap_or_else(|| Rc::new(DummyImportResolver)),563		}))564	}565}
after · crates/jrsonnet-evaluator/src/lib.rs
1//! jsonnet interpreter implementation2#![cfg_attr(nightly, feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8// pub mod async_import;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13pub mod function;14pub mod gc;15mod import;16mod integrations;17pub mod manifest;18mod map;19mod obj;20pub mod stack;21pub mod stdlib;22pub mod tla;23pub mod trace;24pub mod typed;25pub mod val;2627use std::{28	any::Any,29	cell::{RefCell, RefMut},30	clone::Clone,31	collections::hash_map::Entry,32	fmt::{self, Debug},33	marker::PhantomData,34	rc::Rc,35};3637pub use ctx::*;38pub use dynamic::*;39pub use error::{Error, ErrorKind::*, Result, ResultExt};40pub use evaluate::*;41use function::CallLocation;42pub use import::*;43use jrsonnet_gcmodule::{cc_dyn, Cc, Trace};44pub use jrsonnet_interner::{IBytes, IStr};45#[doc(hidden)]46pub use jrsonnet_macros;47pub use jrsonnet_ir as parser;48use jrsonnet_ir::{Expr, Source, SourcePath, Spanned};49use jrsonnet_peg_parser::ParserSettings;50pub use obj::*;51pub use rustc_hash;52use rustc_hash::FxHashMap;53use stack::check_depth;54pub use tla::apply_tla;55pub use val::{Thunk, Val};5657use crate::gc::WithCapacityExt as _;5859cc_dyn!(60	#[derive(Clone)]61	CcUnbound<V>,62	Unbound<Bound = V>63);6465/// Thunk without bound `super`/`this`66/// object inheritance may be overriden multiple times, and will be fixed only on field read67pub trait Unbound: Trace {68	/// Type of value after object context is bound69	type Bound;70	/// Create value bound to specified object context71	fn bind(&self, sup_this: SupThis) -> Result<Self::Bound>;72}7374/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code75/// Standard jsonnet fields are always unbound76#[derive(Clone, Trace)]77pub enum MaybeUnbound {78	/// Value needs to be bound to `this`/`super`79	Unbound(CcUnbound<Val>),80	/// Value is object-independent81	Bound(Thunk<Val>),82}8384impl Debug for MaybeUnbound {85	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {86		write!(f, "MaybeUnbound")87	}88}89impl MaybeUnbound {90	/// Attach object context to value, if required91	pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {92		match self {93			Self::Unbound(v) => v.0.bind(sup_this),94			Self::Bound(v) => Ok(v.evaluate()?),95		}96	}97}9899cc_dyn!(CcContextInitializer, ContextInitializer);100101/// During import, this trait will be called to create initial context for file.102/// It may initialize global variables, stdlib for example.103pub trait ContextInitializer: Trace {104	/// For which size the builder should be preallocated105	fn reserve_vars(&self) -> usize {106		0107	}108	/// Initialize default file context.109	/// Has default implementation, which calls `populate`.110	/// Prefer to always implement `populate` instead.111	fn initialize(&self, for_file: Source) -> Context {112		let mut builder = ContextBuilder::with_capacity(self.reserve_vars());113		self.populate(for_file, &mut builder);114		builder.build()115	}116	/// For composability: extend builder. May panic if this initialization is not supported,117	/// and the context may only be created via `initialize`.118	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);119	/// Allows upcasting from abstract to concrete context initializer.120	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.121	fn as_any(&self) -> &dyn Any;122}123124/// Context initializer which adds nothing.125impl ContextInitializer for () {126	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}127	fn as_any(&self) -> &dyn Any {128		self129	}130}131132impl<T> ContextInitializer for Option<T>133where134	T: ContextInitializer,135{136	fn initialize(&self, for_file: Source) -> Context {137		if let Some(ctx) = self {138			ctx.initialize(for_file)139		} else {140			().initialize(for_file)141		}142	}143144	fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {145		if let Some(ctx) = self {146			ctx.populate(for_file, builder);147		}148	}149150	fn as_any(&self) -> &dyn Any {151		self152	}153}154155macro_rules! impl_context_initializer {156	($($gen:ident)*) => {157		#[allow(non_snake_case)]158		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {159			fn reserve_vars(&self) -> usize {160				let mut out = 0;161				let ($($gen,)*) = self;162				$(out += $gen.reserve_vars();)*163				out164			}165			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {166				let ($($gen,)*) = self;167				$($gen.populate(for_file.clone(), builder);)*168			}169			fn as_any(&self) -> &dyn Any {170				self171			}172		}173	};174	($($cur:ident)* @ $c:ident $($rest:ident)*) => {175		impl_context_initializer!($($cur)*);176		impl_context_initializer!($($cur)* $c @ $($rest)*);177	};178	($($cur:ident)* @) => {179		impl_context_initializer!($($cur)*);180	}181}182impl_context_initializer! {183	A @ B C D E F G184}185186#[derive(Trace)]187struct FileData {188	string: Option<IStr>,189	bytes: Option<IBytes>,190	parsed: Option<Rc<Expr>>,191	evaluated: Option<Val>,192193	evaluating: bool,194}195impl FileData {196	fn new_string(data: IStr) -> Self {197		Self {198			string: Some(data),199			bytes: None,200			parsed: None,201			evaluated: None,202			evaluating: false,203		}204	}205	fn new_bytes(data: IBytes) -> Self {206		Self {207			string: None,208			bytes: Some(data),209			parsed: None,210			evaluated: None,211			evaluating: false,212		}213	}214	pub(crate) fn get_string(&mut self) -> Option<IStr> {215		if self.string.is_none() {216			self.string = Some(217				self.bytes218					.as_ref()219					.expect("either string or bytes should be set")220					.clone()221					.cast_str()?,222			);223		}224		Some(self.string.clone().expect("just set"))225	}226}227228#[derive(Trace)]229pub struct EvaluationStateInternals {230	/// Internal state231	file_cache: RefCell<FxHashMap<SourcePath, FileData>>,232	/// Context initializer, which will be used for imports and everything233	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`234	context_initializer: CcContextInitializer,235	/// Used to resolve file locations/contents236	import_resolver: Rc<dyn ImportResolver>,237}238239/// Maintains stack trace and import resolution240#[derive(Clone, Trace)]241pub struct State(Cc<EvaluationStateInternals>);242243thread_local! {244	pub static DEFAULT_STATE: State = State::builder().build();245	pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};246}247pub struct StateEnterGuard(PhantomData<()>);248impl Drop for StateEnterGuard {249	fn drop(&mut self) {250		STATE.with_borrow_mut(|v| *v = None);251	}252}253254pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {255	if let Some(state) = STATE.with_borrow(Clone::clone) {256		v(state)257	} else {258		let s = DEFAULT_STATE.with(Clone::clone);259		v(s)260	}261}262263impl State {264	pub fn enter(&self) -> StateEnterGuard {265		self.try_enter().expect("entered state already exists")266	}267	pub fn try_enter(&self) -> Option<StateEnterGuard> {268		STATE.with_borrow_mut(|v| {269			if v.is_none() {270				*v = Some(self.clone());271				Some(StateEnterGuard(PhantomData))272			} else {273				None274			}275		})276	}277	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise278	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {279		let mut file_cache = self.file_cache();280		let mut file = file_cache.entry(path.clone());281282		let file = match file {283			Entry::Occupied(ref mut d) => d.get_mut(),284			Entry::Vacant(v) => {285				let data = self.import_resolver().load_file_contents(&path)?;286				v.insert(FileData::new_string(287					std::str::from_utf8(&data)288						.map_err(|_| ImportBadFileUtf8(path.clone()))?289						.into(),290				))291			}292		};293		Ok(file294			.get_string()295			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)296	}297	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise298	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {299		let mut file_cache = self.file_cache();300		let mut file = file_cache.entry(path.clone());301302		let file = match file {303			Entry::Occupied(ref mut d) => d.get_mut(),304			Entry::Vacant(v) => {305				let data = self.import_resolver().load_file_contents(&path)?;306				v.insert(FileData::new_bytes(data.as_slice().into()))307			}308		};309		if let Some(str) = &file.bytes {310			return Ok(str.clone());311		}312		if file.bytes.is_none() {313			file.bytes = Some(314				file.string315					.as_ref()316					.expect("either string or bytes should be set")317					.clone()318					.cast_bytes(),319			);320		}321		Ok(file.bytes.as_ref().expect("just set").clone())322	}323	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise324	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {325		let mut file_cache = self.file_cache();326		let mut file = file_cache.entry(path.clone());327328		let file = match file {329			Entry::Occupied(ref mut d) => d.get_mut(),330			Entry::Vacant(v) => {331				let data = self.import_resolver().load_file_contents(&path)?;332				v.insert(FileData::new_string(333					std::str::from_utf8(&data)334						.map_err(|_| ImportBadFileUtf8(path.clone()))?335						.into(),336				))337			}338		};339		if let Some(val) = &file.evaluated {340			return Ok(val.clone());341		}342		let code = file343			.get_string()344			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;345		let file_name = Source::new(path.clone(), code.clone());346		if file.parsed.is_none() {347			file.parsed = Some(348				jrsonnet_peg_parser::parse(349					&code,350					&ParserSettings {351						source: file_name.clone(),352					},353				)354				.map(Rc::new)355				.map_err(|e| ImportSyntaxError {356					path: file_name.clone(),357					error: Box::new(e),358				})?,359			);360		}361		let parsed = file.parsed.as_ref().expect("just set").clone();362		if file.evaluating {363			bail!(InfiniteRecursionDetected)364		}365		file.evaluating = true;366		// Dropping file cache guard here, as evaluation may use this map too367		drop(file_cache);368		let res = evaluate(self.create_default_context(file_name), &parsed);369370		let mut file_cache = self.file_cache();371		let mut file = file_cache.entry(path);372373		let Entry::Occupied(file) = &mut file else {374			unreachable!("this file was just here")375		};376		let file = file.get_mut();377		file.evaluating = false;378		match res {379			Ok(v) => {380				file.evaluated = Some(v.clone());381				Ok(v)382			}383			Err(e) => Err(e),384		}385	}386387	/// Has same semantics as `import 'path'` called from `from` file388	pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {389		let resolved = self.resolve_from(from, &path)?;390		self.import_resolved(resolved)391	}392	pub fn import(&self, path: impl AsPathLike) -> Result<Val> {393		let resolved = self.resolve_from_default(&path)?;394		self.import_resolved(resolved)395	}396397	/// Creates context with all passed global variables398	pub fn create_default_context(&self, source: Source) -> Context {399		self.context_initializer().initialize(source)400	}401402	/// Creates context with all passed global variables, calling custom modifier403	pub fn create_default_context_with(404		&self,405		source: Source,406		context_initializer: impl ContextInitializer,407	) -> Context {408		let default_initializer = self.context_initializer();409		let mut builder = ContextBuilder::with_capacity(410			default_initializer.reserve_vars() + context_initializer.reserve_vars(),411		);412		default_initializer.populate(source.clone(), &mut builder);413		context_initializer.populate(source, &mut builder);414415		builder.build()416	}417}418419/// Internals420impl State {421	fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {422		self.0.file_cache.borrow_mut()423	}424}425/// Executes code creating a new stack frame, to be replaced with try{}426pub fn in_frame<T>(427	e: CallLocation<'_>,428	frame_desc: impl FnOnce() -> String,429	f: impl FnOnce() -> Result<T>,430) -> Result<T> {431	let _guard = check_depth()?;432433	f().with_description_src(e, frame_desc)434}435436/// Executes code creating a new stack frame, to be replaced with try{}437pub fn in_description_frame<T>(438	frame_desc: impl FnOnce() -> String,439	f: impl FnOnce() -> Result<T>,440) -> Result<T> {441	let _guard = check_depth()?;442443	f().with_description(frame_desc)444}445446#[derive(Trace)]447pub struct InitialUnderscore(pub Thunk<Val>);448impl ContextInitializer for InitialUnderscore {449	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {450		builder.bind("_", self.0.clone());451	}452453	fn as_any(&self) -> &dyn Any {454		self455	}456}457458/// Raw methods evaluate passed values but don't perform TLA execution459impl State {460	/// Parses and evaluates the given snippet461	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {462		let code = code.into();463		let source = Source::new_virtual(name.into(), code.clone());464		let parsed = jrsonnet_peg_parser::parse(465			&code,466			&ParserSettings {467				source: source.clone(),468			},469		)470		.map_err(|e| ImportSyntaxError {471			path: source.clone(),472			error: Box::new(e),473		})?;474		evaluate(self.create_default_context(source), &parsed)475	}476	/// Parses and evaluates the given snippet with custom context modifier477	pub fn evaluate_snippet_with(478		&self,479		name: impl Into<IStr>,480		code: impl Into<IStr>,481		context_initializer: impl ContextInitializer,482	) -> Result<Val> {483		let code = code.into();484		let source = Source::new_virtual(name.into(), code.clone());485		let parsed = jrsonnet_peg_parser::parse(486			&code,487			&ParserSettings {488				source: source.clone(),489			},490		)491		.map_err(|e| ImportSyntaxError {492			path: source.clone(),493			error: Box::new(e),494		})?;495		evaluate(496			self.create_default_context_with(source, context_initializer),497			&parsed,498		)499	}500}501502/// Settings utilities503impl State {504	// Only panics in case of [`ImportResolver`] contract violation505	#[allow(clippy::missing_panics_doc)]506	pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {507		self.import_resolver().resolve_from(from, path)508	}509	#[allow(clippy::missing_panics_doc)]510	pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {511		self.import_resolver().resolve_from_default(path)512	}513	pub fn import_resolver(&self) -> &dyn ImportResolver {514		&*self.0.import_resolver515	}516	pub fn context_initializer(&self) -> &dyn ContextInitializer {517		&*self.0.context_initializer.0518	}519}520521impl State {522	pub fn builder() -> StateBuilder {523		StateBuilder::default()524	}525}526527impl Default for State {528	fn default() -> Self {529		Self::builder().build()530	}531}532533#[derive(Default)]534pub struct StateBuilder {535	import_resolver: Option<Rc<dyn ImportResolver>>,536	context_initializer: Option<CcContextInitializer>,537}538impl StateBuilder {539	pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {540		let _ = self.import_resolver.insert(Rc::new(import_resolver));541		self542	}543	pub fn context_initializer(544		&mut self,545		context_initializer: impl ContextInitializer,546	) -> &mut Self {547		let _ = self548			.context_initializer549			.insert(CcContextInitializer::new(context_initializer));550		self551	}552	pub fn build(mut self) -> State {553		State(Cc::new(EvaluationStateInternals {554			file_cache: RefCell::new(FxHashMap::new()),555			context_initializer: self556				.context_initializer557				.take()558				.unwrap_or_else(|| CcContextInitializer::new(())),559			import_resolver: self560				.import_resolver561				.take()562				.unwrap_or_else(|| Rc::new(DummyImportResolver)),563		}))564	}565}
modifiedcrates/jrsonnet-ir/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-ir/src/expr.rs
+++ b/crates/jrsonnet-ir/src/expr.rs
@@ -17,7 +17,7 @@
 	/// {fixed: 2}
 	Fixed(IStr),
 	/// {["dyn"+"amic"]: 3}
-	Dyn(Spanned<Expr>),
+	Dyn(Expr),
 }
 
 #[derive(Debug, Clone, Copy, PartialEq, Eq, Acyclic)]
@@ -46,7 +46,7 @@
 	pub plus: bool,
 	pub params: Option<ExprParams>,
 	pub visibility: Visibility,
-	pub value: Rc<Spanned<Expr>>,
+	pub value: Rc<Expr>,
 }
 
 #[derive(Debug, PartialEq, Acyclic)]
@@ -152,7 +152,7 @@
 #[derive(Debug, PartialEq, Acyclic)]
 pub struct ExprParam {
 	pub destruct: Destruct,
-	pub default: Option<Rc<Spanned<Expr>>>,
+	pub default: Option<Rc<Expr>>,
 }
 
 /// Defined function parameters
@@ -194,11 +194,11 @@
 
 #[derive(Debug, PartialEq, Acyclic)]
 pub struct ArgsDesc {
-	pub unnamed: Vec<Rc<Spanned<Expr>>>,
-	pub named: Vec<(IStr, Rc<Spanned<Expr>>)>,
+	pub unnamed: Vec<Rc<Expr>>,
+	pub named: Vec<(IStr, Rc<Expr>)>,
 }
 impl ArgsDesc {
-	pub fn new(unnamed: Vec<Rc<Spanned<Expr>>>, named: Vec<(IStr, Rc<Spanned<Expr>>)>) -> Self {
+	pub fn new(unnamed: Vec<Rc<Expr>>, named: Vec<(IStr, Rc<Expr>)>) -> Self {
 		Self { unnamed, named }
 	}
 }
@@ -277,12 +277,12 @@
 pub enum BindSpec {
 	Field {
 		into: Destruct,
-		value: Rc<Spanned<Expr>>,
+		value: Rc<Expr>,
 	},
 	Function {
 		name: IStr,
 		params: ExprParams,
-		value: Rc<Spanned<Expr>>,
+		value: Rc<Expr>,
 	},
 }
 impl BindSpec {
@@ -295,15 +295,15 @@
 }
 
 #[derive(Debug, PartialEq, Acyclic)]
-pub struct IfSpecData(pub Spanned<Expr>);
+pub struct IfSpecData(pub Expr);
 
 #[derive(Debug, PartialEq, Acyclic)]
-pub struct ForSpecData(pub Destruct, pub Spanned<Expr>);
+pub struct ForSpecData(pub Destruct, pub Expr);
 
 #[derive(Debug, PartialEq, Acyclic)]
 pub enum CompSpec {
-	IfSpec(IfSpecData),
-	ForSpec(ForSpecData),
+	IfSpec(Spanned<IfSpecData>),
+	ForSpec(Spanned<ForSpecData>),
 }
 
 #[derive(Debug, PartialEq, Acyclic)]
@@ -346,14 +346,14 @@
 #[derive(Debug, PartialEq, Acyclic)]
 pub struct AssertExpr {
 	pub assert: AssertStmt,
-	pub rest: Spanned<Expr>,
+	pub rest: Expr,
 }
 
 #[derive(Debug, PartialEq, Acyclic)]
 pub struct BinaryOp {
-	pub lhs: Spanned<Expr>,
+	pub lhs: Expr,
 	pub op: BinaryOpType,
-	pub rhs: Spanned<Expr>,
+	pub rhs: Expr,
 }
 
 #[derive(Debug, PartialEq, Acyclic)]
@@ -366,13 +366,13 @@
 #[derive(Debug, PartialEq, Acyclic)]
 pub struct IfElse {
 	pub cond: IfSpecData,
-	pub cond_then: Spanned<Expr>,
-	pub cond_else: Option<Spanned<Expr>>,
+	pub cond_then: Expr,
+	pub cond_else: Option<Expr>,
 }
 
 #[derive(Debug, PartialEq, Acyclic)]
 pub struct Slice {
-	pub value: Spanned<Expr>,
+	pub value: Expr,
 	pub slice: SliceDesc,
 }
 
@@ -389,7 +389,7 @@
 	Var(Spanned<IStr>),
 
 	/// Array of expressions: [1, 2, "Hello"]
-	Arr(Rc<Vec<Spanned<Expr>>>),
+	Arr(Rc<Vec<Expr>>),
 	/// Array comprehension:
 	/// ```jsonnet
 	///  ingredients: [
@@ -401,35 +401,35 @@
 	///    ]
 	///  ],
 	/// ```
-	ArrComp(Rc<Spanned<Expr>>, Vec<CompSpec>),
+	ArrComp(Rc<Expr>, Vec<CompSpec>),
 
 	/// Object: {a: 2}
 	Obj(ObjBody),
 	/// Object extension: var1 {b: 2}
-	ObjExtend(Rc<Spanned<Expr>>, ObjBody),
+	ObjExtend(Rc<Expr>, ObjBody),
 
 	/// -2
-	UnaryOp(UnaryOpType, Box<Spanned<Expr>>),
+	UnaryOp(UnaryOpType, Box<Expr>),
 	/// 2 - 2
 	BinaryOp(Box<BinaryOp>),
 	/// assert 2 == 2 : "Math is broken"
 	AssertExpr(Rc<AssertExpr>),
 	/// local a = 2; { b: a }
-	LocalExpr(Vec<BindSpec>, Box<Spanned<Expr>>),
+	LocalExpr(Vec<BindSpec>, Box<Expr>),
 
 	/// import* "hello"
-	Import(ImportKind, Box<Spanned<Expr>>),
+	Import(Spanned<ImportKind>, Box<Expr>),
 	/// error "I'm broken"
-	ErrorStmt(Box<Spanned<Expr>>),
+	ErrorStmt(Span, Box<Expr>),
 	/// a(b, c)
-	Apply(Box<Spanned<Expr>>, Spanned<ArgsDesc>, bool),
+	Apply(Box<Expr>, Spanned<ArgsDesc>, bool),
 	/// a[b], a.b, a?.b
 	Index {
-		indexable: Box<Spanned<Expr>>,
+		indexable: Box<Expr>,
 		parts: Vec<IndexPart>,
 	},
 	/// function(x) x
-	Function(ExprParams, Rc<Spanned<Expr>>),
+	Function(ExprParams, Rc<Expr>),
 	/// if true == false then 1 else 2
 	IfElse(Box<IfElse>),
 	Slice(Box<Slice>),
@@ -437,7 +437,8 @@
 
 #[derive(Debug, PartialEq, Acyclic)]
 pub struct IndexPart {
-	pub value: Spanned<Expr>,
+	pub span: Span,
+	pub value: Expr,
 	#[cfg(feature = "exp-null-coaelse")]
 	pub null_coaelse: bool,
 }
@@ -461,7 +462,7 @@
 }
 
 #[derive(Clone, PartialEq, Acyclic)]
-pub struct Spanned<T: Acyclic>(T, Span);
+pub struct Spanned<T: Acyclic>(pub T, pub Span);
 impl<T: Acyclic> Deref for Spanned<T> {
 	type Target = T;
 	fn deref(&self) -> &Self::Target {
modifiedcrates/jrsonnet-peg-parser/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-peg-parser/Cargo.toml
+++ b/crates/jrsonnet-peg-parser/Cargo.toml
@@ -7,6 +7,7 @@
 version.workspace = true
 
 [dependencies]
+jrsonnet-gcmodule.workspace = true
 jrsonnet-ir.workspace = true
 peg.workspace = true
 
modifiedcrates/jrsonnet-peg-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-peg-parser/src/lib.rs
+++ b/crates/jrsonnet-peg-parser/src/lib.rs
@@ -1,7 +1,9 @@
+use jrsonnet_gcmodule::Acyclic;
 use jrsonnet_ir::{
-	BinaryOp, Expr, ExprParams, IStr, IndexPart, Member, Slice, SliceDesc, Source, Span, Spanned,
-	ExprParam, ArgsDesc, AssertExpr, ImportKind, LiteralType, IfElse, CompSpec, ForSpecData, IfSpecData, ObjMembers, ObjBody,
-	ObjComp, FieldMember, Visibility, FieldName, unescape, AssertStmt, BindSpec, Destruct, DestructRest,
+	unescape, ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BindSpec, CompSpec, Destruct,
+	DestructRest, Expr, ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, IfElse,
+	IfSpecData, ImportKind, IndexPart, LiteralType, Member, ObjBody, ObjComp, ObjMembers, Slice,
+	SliceDesc, Source, Span, Spanned, Visibility,
 };
 use peg::parser;
 use std::rc::Rc;
@@ -63,7 +65,7 @@
 			= params:param(s) ** comma() comma()? { ExprParams::new(params) }
 			/ { ExprParams::new(Vec::new()) }
 
-		pub rule arg(s: &ParserSettings) -> (Option<IStr>, Rc<Spanned<Expr>>)
+		pub rule arg(s: &ParserSettings) -> (Option<IStr>, Rc<Expr>)
 			= name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, Rc::new(expr))}
 
 		pub rule args(s: &ParserSettings) -> ArgsDesc
@@ -133,7 +135,7 @@
 			/ name:id() _ "(" _ params:params(s) _ ")" _ "=" _ value:expr(s) {BindSpec::Function{name, params, value: Rc::new(value)}}
 
 		pub rule assertion(s: &ParserSettings) -> AssertStmt
-			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { AssertStmt(cond, msg) }
+			= keyword("assert") _ cond:spanned(<expr(s)>, s) msg:(_ ":" _ e:spanned(<expr(s)>, s) {e})? { AssertStmt(cond, msg) }
 
 		pub rule whole_line() -> &'input str
 			= str:$((!['\n'][_])* "\n") {str}
@@ -241,7 +243,7 @@
 		pub rule forspec(s: &ParserSettings) -> ForSpecData
 			= keyword("for") _ id:destruct(s) _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}
 		rule compspec(s: &ParserSettings) -> CompSpec
-			= i:ifspec(s) { CompSpec::IfSpec(i) } / f:forspec(s) {CompSpec::ForSpec(f)}
+			= i:spanned(<ifspec(s)>, s) { CompSpec::IfSpec(i) } / f:spanned(<forspec(s)>, s) {CompSpec::ForSpec(f)}
 		pub rule compspecs(s: &ParserSettings) -> Vec<CompSpec>
 			= specs:compspec(s) ++ _ {?
 				if !matches!(specs[0], CompSpec::ForSpec(_)) {
@@ -267,8 +269,12 @@
 			} else {
 				Err("!!!numbers are finite")
 			}}
+
+		rule spanned<T: Acyclic>(x: rule<T>, s: &ParserSettings) -> Spanned<T>
+			= a:position!() n:x() b:position!() { Spanned::new(n, Span(s.source.clone(), a as u32, b as u32)) }
+
 		pub rule var_expr(s: &ParserSettings) -> Expr
-			= n:id() { Expr::Var(n) }
+			= n:spanned(<id()>, s) { Expr::Var(n) }
 		pub rule id_loc(s: &ParserSettings) -> Spanned<Expr>
 			= a:position!() n:id() b:position!() { Spanned::new(Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }
 		pub rule if_then_else_expr(s: &ParserSettings) -> Expr
@@ -302,7 +308,7 @@
 			/ array_expr(s)
 			/ array_comp_expr(s)
 
-			/ kind:import_kind() _ path:expr(s) {Expr::Import(kind, Box::new(path))}
+			/ kind:spanned(<import_kind()>, s) _ path:expr(s) {Expr::Import(kind, Box::new(path))}
 
 			/ var_expr(s)
 			/ local_expr(s)
@@ -313,10 +319,10 @@
 				assert, rest
 			})) }
 
-			/ keyword("error") _ expr:expr(s) { Expr::ErrorStmt(Box::new(expr)) }
+			/ err_kw:spanned(<keyword("error")>, s) _ expr:expr(s) { Expr::ErrorStmt(err_kw.1, Box::new(expr)) }
 
 		rule slice_part(s: &ParserSettings) -> Option<Spanned<Expr>>
-			= _ e:(e:expr(s) _{e})? {e}
+			= _ e:(e:spanned(<expr(s)>, s) _{e})? {e}
 		pub rule slice_desc(s: &ParserSettings) -> SliceDesc
 			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {
 				let (end, step) = if let Some((end, step)) = pair {
@@ -340,11 +346,8 @@
 			}
 		use jrsonnet_ir::BinaryOpType::*;
 		use jrsonnet_ir::UnaryOpType::*;
-		rule expr(s: &ParserSettings) -> Spanned<Expr>
+		rule expr(s: &ParserSettings) -> Expr
 			= precedence! {
-				"(" _ e:expr(s) _ ")" {e}
-				start:position!() v:@ end:position!() { Spanned::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:@ {
 					#[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);
@@ -385,29 +388,32 @@
 				--
 				value:(@) _ "[" _ slice:slice_desc(s) _ "]" {Expr::Slice(Box::new(Slice{value, slice}))}
 				indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable: Box::new(indexable), parts}}
-				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(Box::new(a), args, ts.is_some())}
+				a:(@) _ args:spanned(<"(" _ a:args(s) _ ")" {a}>, s) ts:(_ keyword("tailstrict"))? {Expr::Apply(Box::new(a), args, ts.is_some())}
 				a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(Rc::new(a), body)}
 				--
 				e:expr_basic(s) {e}
+				"(" _ e:expr(s) _ ")" {e}
 			}
 		pub rule index_part(s: &ParserSettings) -> IndexPart
 		= n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {
-			value,
+			span: value.1,
+			value: value.0,
 			#[cfg(feature = "exp-null-coaelse")]
 			null_coaelse: n.is_some(),
 		}}
-		/ n:("?" _ "." _ ensure_null_coaelse())? "[" _ value:expr(s) _ "]" {IndexPart {
-			value,
+		/ n:("?" _ "." _ ensure_null_coaelse())? value:spanned(<"[" _ v:expr(s) _ "]" {v}>, s) {IndexPart {
+			span: value.1,
+			value: value.0,
 			#[cfg(feature = "exp-null-coaelse")]
 			null_coaelse: n.is_some(),
 		}}
 
-		pub rule jsonnet(s: &ParserSettings) -> Spanned<Expr> = _ e:expr(s) _ {e}
+		pub rule jsonnet(s: &ParserSettings) -> Expr = _ e:expr(s) _ {e}
 	}
 }
 
 pub type ParseError = peg::error::ParseError<peg::str::LineCol>;
-pub fn parse(str: &str, settings: &ParserSettings) -> Result<Spanned<Expr>, ParseError> {
+pub fn parse(str: &str, settings: &ParserSettings) -> Result<Expr, ParseError> {
 	jsonnet_parser::jsonnet(str, settings)
 }
 /// Used for importstr values