git.delta.rocks / jrsonnet / refs/commits / 7cdcae351387

difftreelog

feat simplify Thunk creation with closure syntax

Yaroslav Bolyukin2024-08-26parent: #7d331b6.patch.diff
in: master

9 files changed

modifiedcrates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -7,7 +7,7 @@
 use super::ArrValue;
 use crate::{
 	error::ErrorKind::InfiniteRecursionDetected, evaluate, function::FuncVal, typed::Typed,
-	val::ThunkValue, Context, Error, ObjValue, Result, Thunk, Val,
+	Context, Error, ObjValue, Result, Thunk, Val,
 };
 
 pub trait ArrayLike: Any + Trace + Debug {
@@ -182,23 +182,6 @@
 		Ok(Some(new_value))
 	}
 	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
-		#[derive(Trace)]
-		struct ArrayElement {
-			arr_thunk: ExprArray,
-			index: usize,
-		}
-
-		impl ThunkValue for ArrayElement {
-			type Output = Val;
-
-			fn get(self: Box<Self>) -> Result<Self::Output> {
-				self.arr_thunk
-					.get(self.index)
-					.transpose()
-					.expect("index checked")
-			}
-		}
-
 		if index >= self.len() {
 			return None;
 		}
@@ -208,9 +191,9 @@
 			ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}
 		};
 
-		Some(Thunk::new(ArrayElement {
-			arr_thunk: self.clone(),
-			index,
+		let arr_thunk = self.clone();
+		Some(Thunk!(move || {
+			arr_thunk.get(index).transpose().expect("index checked")
 		}))
 	}
 	fn get_cheap(&self, _index: usize) -> Option<Val> {
@@ -492,23 +475,6 @@
 		Ok(Some(new_value))
 	}
 	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
-		#[derive(Trace)]
-		struct ArrayElement<const WITH_INDEX: bool> {
-			arr_thunk: MappedArray<WITH_INDEX>,
-			index: usize,
-		}
-
-		impl<const WITH_INDEX: bool> ThunkValue for ArrayElement<WITH_INDEX> {
-			type Output = Val;
-
-			fn get(self: Box<Self>) -> Result<Self::Output> {
-				self.arr_thunk
-					.get(self.index)
-					.transpose()
-					.expect("index checked")
-			}
-		}
-
 		if index >= self.len() {
 			return None;
 		}
@@ -518,9 +484,9 @@
 			ArrayThunk::Waiting(()) | ArrayThunk::Pending => {}
 		};
 
-		Some(Thunk::new(ArrayElement {
-			arr_thunk: self.clone(),
-			index,
+		let arr_thunk = self.clone();
+		Some(Thunk!(move || {
+			arr_thunk.get(index).transpose().expect("index checked")
 		}))
 	}
 
modifiedcrates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -1,13 +1,11 @@
-use jrsonnet_gcmodule::Trace;
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{BindSpec, Destruct, LocExpr, ParamsDesc};
+use jrsonnet_parser::{BindSpec, Destruct};
 
 use crate::{
 	bail,
 	error::{ErrorKind::*, Result},
 	evaluate, evaluate_method, evaluate_named,
 	gc::GcHashMap,
-	val::ThunkValue,
 	Context, Pending, Thunk, Val,
 };
 
@@ -31,65 +29,34 @@
 		#[cfg(feature = "exp-destruct")]
 		Destruct::Array { start, rest, end } => {
 			use jrsonnet_parser::DestructRest;
-
-			use crate::arr::ArrValue;
-
-			#[derive(Trace)]
-			struct DataThunk {
-				parent: Thunk<Val>,
-				min_len: usize,
-				has_rest: bool,
-			}
-			impl ThunkValue for DataThunk {
-				type Output = ArrValue;
 
-				fn get(self: Box<Self>) -> Result<Self::Output> {
-					let v = self.parent.evaluate()?;
-					let Val::Arr(arr) = v else {
-						bail!("expected array");
-					};
-					if !self.has_rest {
-						if arr.len() != self.min_len {
-							bail!("expected {} elements, got {}", self.min_len, arr.len())
-						}
-					} else if arr.len() < self.min_len {
-						bail!(
-							"expected at least {} elements, but array was only {}",
-							self.min_len,
-							arr.len()
-						)
+			let min_len = start.len() + end.len();
+			let has_rest = rest.is_some();
+			let full = Thunk!(move || {
+				let v = parent.evaluate()?;
+				let Val::Arr(arr) = v else {
+					bail!("expected array");
+				};
+				if !has_rest {
+					if arr.len() != min_len {
+						bail!("expected {} elements, got {}", min_len, arr.len())
 					}
-					Ok(arr)
+				} else if arr.len() < min_len {
+					bail!(
+						"expected at least {} elements, but array was only {}",
+						min_len,
+						arr.len()
+					)
 				}
-			}
-
-			let full = Thunk::new(DataThunk {
-				min_len: start.len() + end.len(),
-				has_rest: rest.is_some(),
-				parent,
+				Ok(arr)
 			});
 
 			{
-				#[derive(Trace)]
-				struct BaseThunk {
-					full: Thunk<ArrValue>,
-					index: usize,
-				}
-				impl ThunkValue for BaseThunk {
-					type Output = Val;
-
-					fn get(self: Box<Self>) -> Result<Self::Output> {
-						let full = self.full.evaluate()?;
-						Ok(full.get(self.index)?.expect("length is checked"))
-					}
-				}
 				for (i, d) in start.iter().enumerate() {
+					let full = full.clone();
 					destruct(
 						d,
-						Thunk::new(BaseThunk {
-							full: full.clone(),
-							index: i,
-						}),
+						Thunk!(move || Ok(full.evaluate()?.get(i)?.expect("length is checked"))),
 						fctx.clone(),
 						new_bindings,
 					)?;
@@ -98,32 +65,19 @@
 
 			match rest {
 				Some(DestructRest::Keep(v)) => {
-					#[derive(Trace)]
-					struct RestThunk {
-						full: Thunk<ArrValue>,
-						start: usize,
-						end: usize,
-					}
-					impl ThunkValue for RestThunk {
-						type Output = Val;
-
-						fn get(self: Box<Self>) -> Result<Self::Output> {
-							let full = self.full.evaluate()?;
-							let to = full.len() - self.end;
+					let start = start.len();
+					let end = end.len();
+					let full = full.clone();
+					destruct(
+						&Destruct::Full(v.clone()),
+						Thunk!(move || {
+							let full = full.evaluate()?;
+							let to = full.len() - end;
 							Ok(Val::Arr(full.slice(
-								Some(self.start as i32),
+								Some(start as i32),
 								Some(to as i32),
 								None,
 							)))
-						}
-					}
-
-					destruct(
-						&Destruct::Full(v.clone()),
-						Thunk::new(RestThunk {
-							full: full.clone(),
-							start: start.len(),
-							end: end.len(),
 						}),
 						fctx.clone(),
 						new_bindings,
@@ -133,29 +87,14 @@
 			}
 
 			{
-				#[derive(Trace)]
-				struct EndThunk {
-					full: Thunk<ArrValue>,
-					index: usize,
-					end: usize,
-				}
-				impl ThunkValue for EndThunk {
-					type Output = Val;
-
-					fn get(self: Box<Self>) -> Result<Self::Output> {
-						let full = self.full.evaluate()?;
-						Ok(full
-							.get(full.len() - self.end + self.index)?
-							.expect("length is checked"))
-					}
-				}
 				for (i, d) in end.iter().enumerate() {
+					let full = full.clone();
+					let end = end.len();
 					destruct(
 						d,
-						Thunk::new(EndThunk {
-							full: full.clone(),
-							index: i,
-							end: end.len(),
+						Thunk!(move || {
+							let full = full.evaluate()?;
+							Ok(full.get(full.len() - end + i)?.expect("length is checked"))
 						}),
 						fctx.clone(),
 						new_bindings,
@@ -165,71 +104,46 @@
 		}
 		#[cfg(feature = "exp-destruct")]
 		Destruct::Object { fields, rest } => {
-			use crate::obj::ObjValue;
-
-			#[derive(Trace)]
-			struct DataThunk {
-				parent: Thunk<Val>,
-				field_names: Vec<(IStr, bool)>,
-				has_rest: bool,
-			}
-			impl ThunkValue for DataThunk {
-				type Output = ObjValue;
-
-				fn get(self: Box<Self>) -> Result<Self::Output> {
-					let v = self.parent.evaluate()?;
-					let Val::Obj(obj) = v else {
-						bail!("expected object");
-					};
-					for (field, has_default) in &self.field_names {
-						if !has_default && !obj.has_field_ex(field.clone(), true) {
-							bail!("missing field: {field}");
-						}
-					}
-					if !self.has_rest {
-						let len = obj.len();
-						if len > self.field_names.len() {
-							bail!("too many fields, and rest not found");
-						}
-					}
-					Ok(obj)
-				}
-			}
 			let field_names: Vec<_> = fields
 				.iter()
 				.map(|f| (f.0.clone(), f.2.is_some()))
 				.collect();
-			let full = Thunk::new(DataThunk {
-				parent,
-				field_names,
-				has_rest: rest.is_some(),
+			let has_rest = rest.is_some();
+			let full = Thunk!(move || {
+				let v = parent.evaluate()?;
+				let Val::Obj(obj) = v else {
+					bail!("expected object");
+				};
+				for (field, has_default) in &field_names {
+					if !has_default && !obj.has_field_ex(field.clone(), true) {
+						bail!("missing field: {field}");
+					}
+				}
+				if !has_rest {
+					let len = obj.len();
+					if len > field_names.len() {
+						bail!("too many fields, and rest not found");
+					}
+				}
+				Ok(obj)
 			});
 
 			for (field, d, default) in fields {
-				#[derive(Trace)]
-				struct FieldThunk {
-					full: Thunk<ObjValue>,
-					field: IStr,
-					default: Option<(Pending<Context>, LocExpr)>,
-				}
-				impl ThunkValue for FieldThunk {
-					type Output = Val;
-
-					fn get(self: Box<Self>) -> Result<Self::Output> {
-						let full = self.full.evaluate()?;
-						if let Some(field) = full.get(self.field)? {
+				let default = default.clone().map(|e| (fctx.clone(), e));
+				let value = {
+					let field = field.clone();
+					let full = full.clone();
+					Thunk!(move || {
+						let full = full.evaluate()?;
+						if let Some(field) = full.get(field)? {
 							Ok(field)
 						} else {
-							let (fctx, expr) = self.default.as_ref().expect("shape is checked");
+							let (fctx, expr) = default.as_ref().expect("shape is checked");
 							Ok(evaluate(fctx.clone().unwrap(), expr)?)
 						}
-					}
-				}
-				let value = Thunk::new(FieldThunk {
-					full: full.clone(),
-					field: field.clone(),
-					default: default.clone().map(|e| (fctx.clone(), e)),
-				});
+					})
+				};
+
 				if let Some(d) = d {
 					destruct(d, value, fctx.clone(), new_bindings)?;
 				} else {
@@ -253,26 +167,15 @@
 ) -> Result<()> {
 	match d {
 		BindSpec::Field { into, value } => {
-			#[derive(Trace)]
-			struct EvaluateThunkValue {
-				name: Option<IStr>,
-				fctx: Pending<Context>,
-				expr: LocExpr,
-			}
-			impl ThunkValue for EvaluateThunkValue {
-				type Output = Val;
-				fn get(self: Box<Self>) -> Result<Self::Output> {
-					self.name.map_or_else(
-						|| evaluate(self.fctx.unwrap(), &self.expr),
-						|name| evaluate_named(self.fctx.unwrap(), &self.expr, name),
-					)
-				}
-			}
-			let data = Thunk::new(EvaluateThunkValue {
-				name: into.name(),
-				fctx: fctx.clone(),
-				expr: value.clone(),
-			});
+			let name = into.name();
+			let value = value.clone();
+			let data = {
+				let fctx = fctx.clone();
+				Thunk!(move || name.map_or_else(
+					|| evaluate(fctx.unwrap(), &value),
+					|name| evaluate_named(fctx.unwrap(), &value, name),
+				))
+			};
 			destruct(into, data, fctx, new_bindings)?;
 		}
 		BindSpec::Function {
@@ -280,37 +183,15 @@
 			params,
 			value,
 		} => {
-			#[derive(Trace)]
-			struct MethodThunk {
-				fctx: Pending<Context>,
-				name: IStr,
-				params: ParamsDesc,
-				value: LocExpr,
-			}
-			impl ThunkValue for MethodThunk {
-				type Output = Val;
-
-				fn get(self: Box<Self>) -> Result<Self::Output> {
-					Ok(evaluate_method(
-						self.fctx.unwrap(),
-						self.name,
-						self.params,
-						self.value,
-					))
-				}
-			}
-
-			let old = new_bindings.insert(
-				name.clone(),
-				Thunk::new(MethodThunk {
-					fctx,
-					name: name.clone(),
-					params: params.clone(),
-					value: value.clone(),
-				}),
-			);
+			let params = params.clone();
+			let name = name.clone();
+			let value = value.clone();
+			let old = new_bindings.insert(name.clone(), {
+				let name = name.clone();
+				Thunk!(move || Ok(evaluate_method(fctx.unwrap(), name, params, value)))
+			});
 			if old.is_some() {
-				bail!(DuplicateLocalVar(name.clone()))
+				bail!(DuplicateLocalVar(name))
 			}
 		}
 	}
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -18,7 +18,7 @@
 	function::{CallLocation, FuncDesc, FuncVal},
 	in_frame,
 	typed::Typed,
-	val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk, ThunkValue},
+	val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk},
 	Context, Error, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,
 	ResultExt, Unbound, Val,
 };
@@ -139,29 +139,14 @@
 					#[cfg(feature = "exp-preserve-order")]
 					false,
 				) {
-					#[derive(Trace)]
-					struct ObjectFieldThunk {
-						obj: ObjValue,
-						field: IStr,
-					}
-					impl ThunkValue for ObjectFieldThunk {
-						type Output = Val;
-
-						fn get(self: Box<Self>) -> Result<Self::Output> {
-							self.obj.get(self.field).transpose().expect(
-								"field exists, as field name was obtained from object.fields()",
-							)
-						}
-					}
-
 					let fctx = Pending::new();
 					let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());
+					let obj = obj.clone();
 					let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![
 						Thunk::evaluated(Val::string(field.clone())),
-						Thunk::new(ObjectFieldThunk {
-							field: field.clone(),
-							obj: obj.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
@@ -609,21 +594,8 @@
 			if items.is_empty() {
 				Val::Arr(ArrValue::empty())
 			} else if items.len() == 1 {
-				#[derive(Trace)]
-				struct ArrayElement {
-					ctx: Context,
-					item: LocExpr,
-				}
-				impl ThunkValue for ArrayElement {
-					type Output = Val;
-					fn get(self: Box<Self>) -> Result<Val> {
-						evaluate(self.ctx, &self.item)
-					}
-				}
-				Val::Arr(ArrValue::lazy(vec![Thunk::new(ArrayElement {
-					ctx,
-					item: items[0].clone(),
-				})]))
+				let item = items[0].clone();
+				Val::Arr(ArrValue::lazy(vec![Thunk!(move || evaluate(ctx, &item))]))
 			} else {
 				Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))
 			}
@@ -631,21 +603,8 @@
 		ArrComp(expr, comp_specs) => {
 			let mut out = Vec::new();
 			evaluate_comp(ctx, comp_specs, &mut |ctx| {
-				#[derive(Trace)]
-				struct EvaluateThunk {
-					ctx: Context,
-					expr: LocExpr,
-				}
-				impl ThunkValue for EvaluateThunk {
-					type Output = Val;
-					fn get(self: Box<Self>) -> Result<Val> {
-						evaluate(self.ctx, &self.expr)
-					}
-				}
-				out.push(Thunk::new(EvaluateThunk {
-					ctx,
-					expr: expr.clone(),
-				}));
+				let expr = expr.clone();
+				out.push(Thunk!(move || evaluate(ctx, &expr)));
 				Ok(())
 			})?;
 			Val::Arr(ArrValue::lazy(out))
modifiedcrates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -90,7 +90,8 @@
 		let fctx = Context::new_future();
 		let mut defaults = GcHashMap::with_capacity(
 			params.iter().map(|p| p.0.capacity_hint()).sum::<usize>()
-				- filled_named - filled_positionals,
+				- filled_named
+				- filled_positionals,
 		);
 
 		for (idx, param) in params.iter().enumerate().filter(|p| p.1 .1.is_some()) {
@@ -232,22 +233,6 @@
 /// Creates Context, which has all argument default values applied
 /// and with unbound values causing error to be returned
 pub fn parse_default_function_call(body_ctx: Context, params: &ParamsDesc) -> Result<Context> {
-	#[derive(Trace)]
-	struct DependsOnUnbound(IStr, ParamsDesc);
-	impl ThunkValue for DependsOnUnbound {
-		type Output = Val;
-		fn get(self: Box<Self>) -> Result<Val> {
-			Err(FunctionParameterNotBoundInCall(
-				Some(self.0.clone()),
-				self.1
-					.iter()
-					.map(|p| (p.0.name(), ParamDefault::exists(p.1.is_some())))
-					.collect(),
-			)
-			.into())
-		}
-	}
-
 	let fctx = Context::new_future();
 
 	let mut bindings = GcHashMap::with_capacity(params.iter().map(|p| p.0.capacity_hint()).sum());
@@ -267,10 +252,18 @@
 		} else {
 			destruct(
 				&param.0,
-				Thunk::new(DependsOnUnbound(
-					param.0.name().unwrap_or_else(|| "<destruct>".into()),
-					params.clone(),
-				)),
+				{
+					let param_name = param.0.name().unwrap_or_else(|| "<destruct>".into());
+					let params = params.clone();
+					Thunk!(move || Err(FunctionParameterNotBoundInCall(
+						Some(param_name),
+						params
+							.iter()
+							.map(|p| (p.0.name(), ParamDefault::exists(p.1.is_some())))
+							.collect(),
+					)
+					.into()))
+				},
 				fctx.clone(),
 				&mut bindings,
 			)?;
modifiedcrates/jrsonnet-evaluator/src/gc.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/gc.rs
+++ b/crates/jrsonnet-evaluator/src/gc.rs
@@ -158,3 +158,5 @@
 		Self::new()
 	}
 }
+
+pub fn assert_trace<T: Trace>(_v: &T) {}
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -20,7 +20,7 @@
 	in_frame,
 	operator::evaluate_add_op,
 	tb,
-	val::{ArrValue, ThunkValue},
+	val::ArrValue,
 	MaybeUnbound, Result, Thunk, Unbound, Val,
 };
 
@@ -444,45 +444,16 @@
 		})
 	}
 	pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {
-		#[derive(Trace)]
-		struct ThunkGet {
-			obj: ObjValue,
-			key: IStr,
-		}
-		impl ThunkValue for ThunkGet {
-			type Output = Val;
-
-			fn get(self: Box<Self>) -> Result<Self::Output> {
-				Ok(self.obj.get(self.key)?.expect("field exists"))
-			}
-		}
-
 		if !self.has_field_ex(key.clone(), true) {
 			return None;
 		}
-		Some(Thunk::new(ThunkGet {
-			obj: self.clone(),
-			key,
-		}))
+		let obj = self.clone();
+
+		Some(Thunk!(move || Ok(obj.get(key)?.expect("field exists"))))
 	}
 	pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {
-		#[derive(Trace)]
-		struct ThunkGet {
-			obj: ObjValue,
-			key: IStr,
-		}
-		impl ThunkValue for ThunkGet {
-			type Output = Val;
-
-			fn get(self: Box<Self>) -> Result<Self::Output> {
-				self.obj.get_or_bail(self.key)
-			}
-		}
-
-		Thunk::new(ThunkGet {
-			obj: self.clone(),
-			key,
-		})
+		let obj = self.clone();
+		Thunk!(move || obj.get_or_bail(key))
 	}
 	pub fn ptr_eq(a: &Self, b: &Self) -> bool {
 		Cc::ptr_eq(&a.0, &b.0)
@@ -733,11 +704,10 @@
 		self.value_cache
 			.borrow_mut()
 			.insert(cache_key.clone(), CacheValue::Pending);
-		let value = self.get_for_uncached(key, this).map_err(|e| {
+		let value = self.get_for_uncached(key, this).inspect_err(|e| {
 			self.value_cache
 				.borrow_mut()
 				.insert(cache_key.clone(), CacheValue::Errored(e.clone()));
-			e
 		})?;
 		self.value_cache.borrow_mut().insert(
 			cache_key,
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -11,6 +11,7 @@
 use derivative::Derivative;
 use jrsonnet_gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
+pub use jrsonnet_macros::Thunk;
 use jrsonnet_types::ValType;
 use thiserror::Error;
 
@@ -32,6 +33,27 @@
 }
 
 #[derive(Trace)]
+pub struct ThunkValueClosure<D: Trace, O: 'static> {
+	env: D,
+	// Carries no data, as it is not a real closure, all the
+	// captured environment is stored in `env` field.
+	#[trace(skip)]
+	closure: fn(D) -> Result<O>,
+}
+impl<D: Trace, O: 'static> ThunkValueClosure<D, O> {
+	pub fn new(env: D, closure: fn(D) -> Result<O>) -> Self {
+		Self { env, closure }
+	}
+}
+impl<D: Trace, O: 'static> ThunkValue for ThunkValueClosure<D, O> {
+	type Output = O;
+
+	fn get(self: Box<Self>) -> Result<Self::Output> {
+		(self.closure)(self.env)
+	}
+}
+
+#[derive(Trace)]
 enum ThunkInner<T: Trace> {
 	Computed(T),
 	Errored(Error),
@@ -113,28 +135,11 @@
 		M: ThunkMapper<Input>,
 		M::Output: Trace,
 	{
-		#[derive(Trace)]
-		struct Mapped<Input: Trace, Mapper: Trace> {
-			inner: Thunk<Input>,
-			mapper: Mapper,
-		}
-		impl<Input, Mapper> ThunkValue for Mapped<Input, Mapper>
-		where
-			Input: Trace + Clone,
-			Mapper: ThunkMapper<Input>,
-		{
-			type Output = Mapper::Output;
-
-			fn get(self: Box<Self>) -> Result<Self::Output> {
-				let value = self.inner.evaluate()?;
-				let mapped = self.mapper.map(value)?;
-				Ok(mapped)
-			}
-		}
-
-		Thunk::new(Mapped::<Input, M> {
-			inner: self,
-			mapper,
+		let inner = self;
+		Thunk!(move || {
+			let value = inner.evaluate()?;
+			let mapped = mapper.map(value)?;
+			Ok(mapped)
 		})
 	}
 }
modifiedcrates/jrsonnet-macros/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-macros/Cargo.toml
+++ b/crates/jrsonnet-macros/Cargo.toml
@@ -17,3 +17,4 @@
 proc-macro2.workspace = true
 quote.workspace = true
 syn = { workspace = true, features = ["full"] }
+syn-dissect-closure.workspace = true
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-macros/src/lib.rs
1use std::string::String;23use proc_macro2::TokenStream;4use quote::quote;5use syn::{6	parenthesized,7	parse::{Parse, ParseStream},8	parse_macro_input,9	punctuated::Punctuated,10	spanned::Spanned,11	token::{self, Comma},12	Attribute, DeriveInput, Error, Expr, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, Path,13	PathArguments, Result, ReturnType, Token, Type,14};1516fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>17where18	Ident: PartialEq<I>,19{20	let attrs = attrs21		.iter()22		.filter(|a| a.path().is_ident(&ident))23		.collect::<Vec<_>>();24	if attrs.len() > 1 {25		return Err(Error::new(26			attrs[1].span(),27			"this attribute may be specified only once",28		));29	} else if attrs.is_empty() {30		return Ok(None);31	}32	let attr = attrs[0];33	let attr = attr.parse_args::<A>()?;3435	Ok(Some(attr))36}37fn remove_attr<I>(attrs: &mut Vec<Attribute>, ident: I)38where39	Ident: PartialEq<I>,40{41	attrs.retain(|a| !a.path().is_ident(&ident));42}4344fn path_is(path: &Path, needed: &str) -> bool {45	path.leading_colon.is_none()46		&& !path.segments.is_empty()47		&& path.segments.iter().last().unwrap().ident == needed48}4950fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {51	match ty {52		Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {53			let args = &path.path.segments.iter().last().unwrap().arguments;54			Some(args)55		}56		_ => None,57	}58}5960fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {61	let Some(args) = type_is_path(ty, "Option") else {62		return Ok(None);63	};64	// It should have only on angle-bracketed param ("<String>"):65	let PathArguments::AngleBracketed(params) = args else {66		return Err(Error::new(args.span(), "missing option generic"));67	};68	let generic_arg = params.args.iter().next().unwrap();69	// This argument must be a type:70	let GenericArgument::Type(ty) = generic_arg else {71		return Err(Error::new(72			generic_arg.span(),73			"option generic should be a type",74		));75	};76	Ok(Some(ty))77}7879struct Field {80	attrs: Vec<Attribute>,81	name: Ident,82	_colon: Token![:],83	ty: Type,84}85impl Parse for Field {86	fn parse(input: ParseStream) -> syn::Result<Self> {87		Ok(Self {88			attrs: input.call(Attribute::parse_outer)?,89			name: input.parse()?,90			_colon: input.parse()?,91			ty: input.parse()?,92		})93	}94}9596mod kw {97	syn::custom_keyword!(fields);98	syn::custom_keyword!(rename);99	syn::custom_keyword!(flatten);100	syn::custom_keyword!(add);101	syn::custom_keyword!(hide);102	syn::custom_keyword!(ok);103}104105struct EmptyAttr;106impl Parse for EmptyAttr {107	fn parse(_input: ParseStream) -> Result<Self> {108		Ok(Self)109	}110}111112struct BuiltinAttrs {113	fields: Vec<Field>,114}115impl Parse for BuiltinAttrs {116	fn parse(input: ParseStream) -> syn::Result<Self> {117		if input.is_empty() {118			return Ok(Self { fields: Vec::new() });119		}120		input.parse::<kw::fields>()?;121		let fields;122		parenthesized!(fields in input);123		let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;124		Ok(Self {125			fields: p.into_iter().collect(),126		})127	}128}129130enum Optionality {131	Required,132	Optional,133	Default(Expr),134}135136enum ArgInfo {137	Normal {138		ty: Box<Type>,139		optionality: Optionality,140		name: Option<String>,141		cfg_attrs: Vec<Attribute>,142	},143	Lazy {144		is_option: bool,145		name: Option<String>,146	},147	Context,148	Location,149	This,150}151152impl ArgInfo {153	fn parse(name: &str, arg: &mut FnArg) -> Result<Self> {154		let FnArg::Typed(arg) = arg else {155			unreachable!()156		};157		let ident = match &arg.pat as &Pat {158			Pat::Ident(i) => Some(i.ident.clone()),159			_ => None,160		};161		let ty = &arg.ty;162		if type_is_path(ty, "Context").is_some() {163			return Ok(Self::Context);164		} else if type_is_path(ty, "CallLocation").is_some() {165			return Ok(Self::Location);166		} else if type_is_path(ty, "Thunk").is_some() {167			return Ok(Self::Lazy {168				is_option: false,169				name: ident.map(|v| v.to_string()),170			});171		}172173		match ty as &Type {174			Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),175			_ => {}176		}177178		let (optionality, ty) = if let Some(default) = parse_attr::<_, _>(&arg.attrs, "default")? {179			remove_attr(&mut arg.attrs, "default");180			(Optionality::Default(default), ty.clone())181		} else if let Some(ty) = extract_type_from_option(ty)? {182			if type_is_path(ty, "Thunk").is_some() {183				return Ok(Self::Lazy {184					is_option: true,185					name: ident.map(|v| v.to_string()),186				});187			}188189			(Optionality::Optional, Box::new(ty.clone()))190		} else {191			(Optionality::Required, ty.clone())192		};193194		let cfg_attrs = arg195			.attrs196			.iter()197			.filter(|a| a.path().is_ident("cfg"))198			.cloned()199			.collect();200201		Ok(Self::Normal {202			ty,203			optionality,204			name: ident.map(|v| v.to_string()),205			cfg_attrs,206		})207	}208}209210#[proc_macro_attribute]211pub fn builtin(212	attr: proc_macro::TokenStream,213	item: proc_macro::TokenStream,214) -> proc_macro::TokenStream {215	let attr = parse_macro_input!(attr as BuiltinAttrs);216	let item_fn = parse_macro_input!(item as ItemFn);217218	match builtin_inner(attr, item_fn) {219		Ok(v) => v.into(),220		Err(e) => e.into_compile_error().into(),221	}222}223224#[allow(clippy::too_many_lines)]225fn builtin_inner(attr: BuiltinAttrs, mut fun: ItemFn) -> syn::Result<TokenStream> {226	let ReturnType::Type(_, result) = &fun.sig.output else {227		return Err(Error::new(228			fun.sig.span(),229			"builtin should return something",230		));231	};232233	let name = fun.sig.ident.to_string();234	let args = fun235		.sig236		.inputs237		.iter_mut()238		.map(|arg| ArgInfo::parse(&name, arg))239		.collect::<Result<Vec<_>>>()?;240241	let params_desc = args.iter().filter_map(|a| match a {242		ArgInfo::Normal {243			optionality,244			name,245			cfg_attrs,246			..247		} => {248			let name = name249				.as_ref()250				.map_or_else(|| quote! {None}, |n| quote! {ParamName::new_static(#n)});251			let default = match optionality {252				Optionality::Required => quote!(ParamDefault::None),253				Optionality::Optional => quote!(ParamDefault::Exists),254				Optionality::Default(e) => quote!(ParamDefault::Literal(stringify!(#e))),255			};256			Some(quote! {257				#(#cfg_attrs)*258				BuiltinParam::new(#name, #default),259			})260		}261		ArgInfo::Lazy { is_option, name } => {262			let name = name263				.as_ref()264				.map_or_else(|| quote! {None}, |n| quote! {ParamName::new_static(#n)});265			Some(quote! {266				BuiltinParam::new(#name, ParamDefault::exists(#is_option)),267			})268		}269		ArgInfo::Context | ArgInfo::Location | ArgInfo::This => None,270	});271272	let mut id = 0usize;273	let pass = args274		.iter()275		.map(|a| match a {276			ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {277				let cid = id;278				id += 1;279				(quote! {#cid}, a)280			}281			ArgInfo::Context | ArgInfo::Location | ArgInfo::This => {282				(quote! {compile_error!("should not use id")}, a)283			}284		})285		.map(|(id, a)| match a {286			ArgInfo::Normal {287				ty,288				optionality,289				name,290				cfg_attrs,291			} => {292				let name = name.as_ref().map_or("<unnamed>", String::as_str);293				let eval = quote! {jrsonnet_evaluator::in_description_frame(294					|| format!("argument <{}> evaluation", #name),295					|| <#ty>::from_untyped(value.evaluate()?),296				)?};297				let value = match optionality {298					Optionality::Required => quote! {{299						let value = parsed[#id].as_ref().expect("args shape is checked");300						#eval301					},},302					Optionality::Optional => quote! {if let Some(value) = &parsed[#id] {303						Some(#eval)304					} else {305						None306					},},307					Optionality::Default(expr) => quote! {if let Some(value) = &parsed[#id] {308						#eval309					} else {310						let v: #ty = #expr;311						v312					},},313				};314				quote! {315					#(#cfg_attrs)*316					#value317				}318			}319			ArgInfo::Lazy { is_option, .. } => {320				if *is_option {321					quote! {if let Some(value) = &parsed[#id] {322						Some(value.clone())323					} else {324						None325					},}326				} else {327					quote! {328						parsed[#id].as_ref().expect("args shape is correct").clone(),329					}330				}331			}332			ArgInfo::Context => quote! {ctx.clone(),},333			ArgInfo::Location => quote! {location,},334			ArgInfo::This => quote! {self,},335		});336337	let fields = attr.fields.iter().map(|field| {338		let attrs = &field.attrs;339		let name = &field.name;340		let ty = &field.ty;341		quote! {342			#(#attrs)*343			pub #name: #ty,344		}345	});346347	let name = &fun.sig.ident;348	let vis = &fun.vis;349	let static_ext = if attr.fields.is_empty() {350		quote! {351			impl #name {352				pub const INST: &'static dyn StaticBuiltin = &#name {};353			}354			impl StaticBuiltin for #name {}355		}356	} else {357		quote! {}358	};359	let static_derive_copy = if attr.fields.is_empty() {360		quote! {, Copy}361	} else {362		quote! {}363	};364365	Ok(quote! {366		#fun367368		#[doc(hidden)]369		#[allow(non_camel_case_types)]370		#[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]371		#vis struct #name {372			#(#fields)*373		}374		const _: () = {375			use ::jrsonnet_evaluator::{376				State, Val,377				function::{builtin::{Builtin, StaticBuiltin, BuiltinParam, ParamName, ParamDefault}, CallLocation, ArgsLike, parse::parse_builtin_call},378				Result, Context, typed::Typed,379				parser::Span,380			};381			const PARAMS: &'static [BuiltinParam] = &[382				#(#params_desc)*383			];384385			#static_ext386			impl Builtin for #name387			where388				Self: 'static389			{390				fn name(&self) -> &str {391					stringify!(#name)392				}393				fn params(&self) -> &[BuiltinParam] {394					PARAMS395				}396				#[allow(unused_variables)]397				fn call(&self, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {398					let parsed = parse_builtin_call(ctx.clone(), &PARAMS, args, false)?;399400					let result: #result = #name(#(#pass)*);401					<_ as Typed>::into_result(result)402				}403				fn as_any(&self) -> &dyn ::std::any::Any {404					self405				}406			}407		};408	})409}410411#[derive(Default)]412#[allow(clippy::struct_excessive_bools)]413struct TypedAttr {414	rename: Option<String>,415	flatten: bool,416	/// flatten(ok) strategy for flattened optionals417	/// field would be None in case of any parsing error (as in serde)418	flatten_ok: bool,419	// Should it be `field+:` instead of `field:`420	add: bool,421	// Should it be `field::` instead of `field:`422	hide: bool,423}424impl Parse for TypedAttr {425	fn parse(input: ParseStream) -> syn::Result<Self> {426		let mut out = Self::default();427		loop {428			let lookahead = input.lookahead1();429			if lookahead.peek(kw::rename) {430				input.parse::<kw::rename>()?;431				input.parse::<Token![=]>()?;432				let name = input.parse::<LitStr>()?;433				if out.rename.is_some() {434					return Err(Error::new(435						name.span(),436						"rename attribute may only be specified once",437					));438				}439				out.rename = Some(name.value());440			} else if lookahead.peek(kw::flatten) {441				input.parse::<kw::flatten>()?;442				out.flatten = true;443				if input.peek(token::Paren) {444					let content;445					parenthesized!(content in input);446					let lookahead = content.lookahead1();447					if lookahead.peek(kw::ok) {448						content.parse::<kw::ok>()?;449						out.flatten_ok = true;450					} else {451						return Err(lookahead.error());452					}453				}454			} else if lookahead.peek(kw::add) {455				input.parse::<kw::add>()?;456				out.add = true;457			} else if lookahead.peek(kw::hide) {458				input.parse::<kw::hide>()?;459				out.hide = true;460			} else if input.is_empty() {461				break;462			} else {463				return Err(lookahead.error());464			}465			if input.peek(Token![,]) {466				input.parse::<Token![,]>()?;467			} else {468				break;469			}470		}471		Ok(out)472	}473}474475struct TypedField {476	attr: TypedAttr,477	ident: Ident,478	ty: Type,479	is_option: bool,480	is_lazy: bool,481}482impl TypedField {483	fn parse(field: &syn::Field) -> Result<Self> {484		let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();485		let Some(ident) = field.ident.clone() else {486			return Err(Error::new(487				field.span(),488				"this field should appear in output object, but it has no visible name",489			));490		};491		let (is_option, ty) = extract_type_from_option(&field.ty)?492			.map_or_else(|| (false, field.ty.clone()), |ty| (true, ty.clone()));493		if is_option && attr.flatten {494			if !attr.flatten_ok {495				return Err(Error::new(496					field.span(),497					"strategy should be set when flattening Option",498				));499			}500		} else if attr.flatten_ok {501			return Err(Error::new(502				field.span(),503				"flatten(ok) is only useable on optional fields",504			));505		}506507		let is_lazy = type_is_path(&ty, "Thunk").is_some();508509		Ok(Self {510			attr,511			ident,512			ty,513			is_option,514			is_lazy,515		})516	}517	/// None if this field is flattened in jsonnet output518	fn name(&self) -> Option<String> {519		if self.attr.flatten {520			return None;521		}522		Some(523			self.attr524				.rename525				.clone()526				.unwrap_or_else(|| self.ident.to_string()),527		)528	}529530	fn expand_field(&self) -> Option<TokenStream> {531		if self.is_option {532			return None;533		}534		let name = self.name()?;535		let ty = &self.ty;536		Some(quote! {537			(#name, <#ty as Typed>::TYPE)538		})539	}540	fn expand_parse(&self) -> TokenStream {541		let ident = &self.ident;542		let ty = &self.ty;543		if self.attr.flatten {544			// optional flatten is handled in same way as serde545			return if self.is_option {546				quote! {547					#ident: <#ty as TypedObj>::parse(&obj).ok(),548				}549			} else {550				quote! {551					#ident: <#ty as TypedObj>::parse(&obj)?,552				}553			};554		};555556		let name = self.name().unwrap();557		let value = if self.is_option {558			quote! {559				if let Some(value) = obj.get(#name.into())? {560					Some(<#ty as Typed>::from_untyped(value)?)561				} else {562					None563				}564			}565		} else {566			quote! {567				<#ty as Typed>::from_untyped(obj.get(#name.into())?.ok_or_else(|| ErrorKind::NoSuchField(#name.into(), vec![]))?)?568			}569		};570571		quote! {572			#ident: #value,573		}574	}575	fn expand_serialize(&self) -> TokenStream {576		let ident = &self.ident;577		let ty = &self.ty;578		self.name().map_or_else(579			|| {580				if self.is_option {581					quote! {582						if let Some(value) = self.#ident {583							<#ty as TypedObj>::serialize(value, out)?;584						}585					}586				} else {587					quote! {588						<#ty as TypedObj>::serialize(self.#ident, out)?;589					}590				}591			},592			|name| {593				let hide = if self.attr.hide {594					quote! {.hide()}595				} else {596					quote! {}597				};598				let add = if self.attr.add {599					quote! {.add()}600				} else {601					quote! {}602				};603				let value = if self.is_lazy {604					quote! {605						out.field(#name)606							#hide607							#add608							.try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;609					}610				} else {611					quote! {612						out.field(#name)613							#hide614							#add615							.try_value(<#ty as Typed>::into_untyped(value)?)?;616					}617				};618				if self.is_option {619					quote! {620						if let Some(value) = self.#ident {621							#value622						}623					}624				} else {625					quote! {626						{627							let value = self.#ident;628							#value629						}630					}631				}632			},633		)634	}635}636637#[proc_macro_derive(Typed, attributes(typed))]638pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {639	let input = parse_macro_input!(item as DeriveInput);640641	match derive_typed_inner(input) {642		Ok(v) => v.into(),643		Err(e) => e.to_compile_error().into(),644	}645}646647fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {648	let syn::Data::Struct(data) = &input.data else {649		return Err(Error::new(input.span(), "only structs supported"));650	};651652	let ident = &input.ident;653	let fields = data654		.fields655		.iter()656		.map(TypedField::parse)657		.collect::<Result<Vec<_>>>()?;658659	let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();660661	let typed = {662		let fields = fields663			.iter()664			.filter_map(TypedField::expand_field)665			.collect::<Vec<_>>();666		quote! {667			impl #impl_generics Typed for #ident #ty_generics #where_clause {668				const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[669					#(#fields,)*670				]);671672				fn from_untyped(value: Val) -> JrResult<Self> {673					let obj = value.as_obj().expect("shape is correct");674					Self::parse(&obj)675				}676677				fn into_untyped(value: Self) -> JrResult<Val> {678					let mut out = ObjValueBuilder::new();679					value.serialize(&mut out)?;680					Ok(Val::Obj(out.build()))681				}682683			}684		}685	};686687	let fields_parse = fields.iter().map(TypedField::expand_parse);688	let fields_serialize = fields689		.iter()690		.map(TypedField::expand_serialize)691		.collect::<Vec<_>>();692693	Ok(quote! {694		const _: () = {695			use ::jrsonnet_evaluator::{696				typed::{ComplexValType, Typed, TypedObj, CheckType},697				Val, State,698				error::{ErrorKind, Result as JrResult},699				ObjValueBuilder, ObjValue,700			};701702			#typed703704			impl #impl_generics TypedObj for #ident #ty_generics #where_clause {705				fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {706					#(#fields_serialize)*707708					Ok(())709				}710				fn parse(obj: &ObjValue) -> JrResult<Self> {711					Ok(Self {712						#(#fields_parse)*713					})714				}715			}716		};717	})718}719720struct FormatInput {721	formatting: LitStr,722	arguments: Vec<Expr>,723}724impl Parse for FormatInput {725	fn parse(input: ParseStream) -> Result<Self> {726		let formatting = input.parse()?;727		let mut arguments = Vec::new();728729		while input.peek(Token![,]) {730			input.parse::<Token![,]>()?;731			if input.is_empty() {732				// Trailing comma733				break;734			}735			let expr = input.parse()?;736			arguments.push(expr);737		}738739		if !input.is_empty() {740			return Err(syn::Error::new(input.span(), "unexpected trailing input"));741		}742743		Ok(Self {744			formatting,745			arguments,746		})747	}748}749fn is_format_str(i: &str) -> bool {750	let mut is_plain = true;751	// -1 = {752	// +1 = }753	let mut is_bracket = 0i8;754	for ele in i.chars() {755		match ele {756			'{' if is_bracket == -1 => {757				is_bracket = 0;758			}759			'}' if is_bracket == -1 => {760				is_plain = false;761				break;762			}763			'}' if is_bracket == 1 => {764				is_bracket = 0;765			}766			'{' if is_bracket == 1 => {767				is_plain = false;768				break;769			}770			'{' => {771				is_bracket = -1;772			}773			'}' => {774				is_bracket = 1;775			}776			_ if is_bracket != 0 => {777				is_plain = false;778				break;779			}780			_ => {}781		}782	}783	!is_plain || is_bracket != 0784}785impl FormatInput {786	fn expand(self) -> TokenStream {787		let format = self.formatting;788		if is_format_str(&format.value()) {789			let args = self.arguments;790			quote! {791				::jrsonnet_evaluator::IStr::from(format!(#format #(, #args)*))792			}793		} else {794			if let Some(first) = self.arguments.first() {795				return syn::Error::new(796					first.span(),797					"string has no formatting codes, it should not have the arguments",798				)799				.into_compile_error();800			}801			quote! {802				::jrsonnet_evaluator::IStr::from(#format)803			}804		}805	}806}807808/// `IStr` formatting helper809///810/// Using `format!("literal with no codes").into()` is slower than just `"literal with no codes".into()`811/// This macro looks for formatting codes in the input string, and uses812/// `format!()` only when necessary813#[proc_macro]814pub fn format_istr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {815	let input = parse_macro_input!(input as FormatInput);816	input.expand().into()817}