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

difftreelog

feat forObj evaluation

pvwvukvwYaroslav Bolyukin2026-05-06parent: #30703de.patch.diff
in: master

4 files changed

modifiedcrates/jrsonnet-evaluator/src/analyze.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/analyze.rs
+++ b/crates/jrsonnet-evaluator/src/analyze.rs
@@ -410,6 +410,15 @@
 		/// Is `over` does not depend on any variable introduced by an earlier for-spec in this comprehension chain
 		loop_invariant: bool,
 	},
+	#[cfg(feature = "exp-object-iteration")]
+	ForObj {
+		frame_shape: ClosureShape,
+		key: LocalSlot,
+		visibility: jrsonnet_ir::Visibility,
+		value: LDestruct,
+		over: LExpr,
+		loop_invariant: bool,
+	},
 }
 
 struct FrameAlloc<'s> {
@@ -1863,7 +1872,47 @@
 				(r, rest)
 			}
 			#[cfg(feature = "exp-object-iteration")]
-			CompSpec::ForObjSpec(_) => todo!(),
+			CompSpec::ForObjSpec(data) => {
+				let mut over_taint = AnalysisResult::default();
+				let over_l = analyze(&data.over, stack, &mut over_taint);
+				let loop_invariant = over_taint.local_dependent_depth > outer_depth;
+				taint.taint_by(over_taint);
+
+				let mut alloc = FrameAlloc::new(stack);
+				let closure = alloc.push_locals_closure();
+				let Some((_, key_slot)) = alloc.define_local(data.key.clone(), None) else {
+					stack.pop_closure(closure);
+					return go(idx + 1, specs, outer_depth, stack, taint, inside);
+				};
+				let Some(l_value) = alloc.alloc_destruct(&data.value) else {
+					stack.pop_closure(closure);
+					return go(idx + 1, specs, outer_depth, stack, taint, inside);
+				};
+				let mut pending = alloc.finish();
+
+				let var_analysis = AnalysisResult::default();
+				pending.record_spec_init(&LDestruct::Full(key_slot), var_analysis);
+				pending.record_spec_init(&l_value, var_analysis);
+
+				let body_frame = pending.finish();
+				let (r, mut rest) =
+					go(idx + 1, specs, outer_depth, body_frame.stack, taint, inside);
+				body_frame.finish();
+				let frame_shape = stack.pop_closure(closure);
+
+				rest.insert(
+					0,
+					LCompSpec::ForObj {
+						frame_shape,
+						key: key_slot,
+						visibility: data.visibility,
+						value: l_value,
+						over: over_l,
+						loop_invariant,
+					},
+				);
+				(r, rest)
+			}
 		}
 	}
 	let outer_depth = stack.depth;
@@ -1970,6 +2019,7 @@
 	use super::*;
 
 	#[test]
+	#[cfg(not(feature = "exp-null-coaelse"))]
 	fn snapshots() {
 		glob!("analysis_tests/*.jsonnet", |path| {
 			let code = fs::read_to_string(path).expect("read test file");
modifiedcrates/jrsonnet-evaluator/src/evaluate/compspec.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/compspec.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/compspec.rs
@@ -6,6 +6,9 @@
 	destructure::{destruct, evaluate_locals_unbound, fill_letrec_binds},
 	evaluate_field_member_static, evaluate_field_member_unbound,
 };
+#[cfg(feature = "exp-object-iteration")]
+use jrsonnet_interner::IStr;
+
 use crate::{
 	Context, ObjValue, ObjValueBuilder, Result, Thunk, Val,
 	analyze::{
@@ -18,6 +21,12 @@
 	evaluate::{evaluate, evaluate_trivial},
 };
 
+enum CachedOver {
+	Arr(ArrValue),
+	#[cfg(feature = "exp-object-iteration")]
+	Obj(ObjValue),
+}
+
 trait CompCollector {
 	fn reserve(&mut self, _guaranteed: usize) {}
 	fn collect(&mut self, ctx: Context) -> Result<()>;
@@ -215,7 +224,7 @@
 	Ok(Val::arr(items))
 }
 
-fn cache_overs(ctx: &Context, specs: &[LCompSpec]) -> Result<Vec<Option<ArrValue>>> {
+fn cache_overs(ctx: &Context, specs: &[LCompSpec]) -> Result<Vec<Option<CachedOver>>> {
 	specs
 		.iter()
 		.map(|spec| {
@@ -229,7 +238,23 @@
 					let Val::Arr(arr) = val else {
 						bail!(InComprehensionCanOnlyIterateOverArray)
 					};
-					Some(arr)
+					Some(CachedOver::Arr(arr))
+				}
+				#[cfg(feature = "exp-object-iteration")]
+				LCompSpec::ForObj {
+					over,
+					loop_invariant: true,
+					..
+				} => {
+					let val = evaluate(ctx.clone(), over)?;
+					let Val::Obj(obj) = val else {
+						bail!(TypeMismatch(
+							"object iteration over",
+							vec![jrsonnet_types::ValType::Obj],
+							val.value_type(),
+						))
+					};
+					Some(CachedOver::Obj(obj))
 				}
 				_ => None,
 			})
@@ -240,7 +265,7 @@
 fn evaluate_compspecs_eager(
 	ctx: Context,
 	specs: &[LCompSpec],
-	cached_overs: &[Option<ArrValue>],
+	cached_overs: &[Option<CachedOver>],
 	idx: usize,
 	guaranteed_reserve: usize,
 	collector: &mut dyn CompCollector,
@@ -269,7 +294,7 @@
 			over,
 			..
 		} => {
-			let arr = if let Some(cached) = &cached_overs[idx] {
+			let arr = if let Some(CachedOver::Arr(cached)) = &cached_overs[idx] {
 				cached.clone()
 			} else {
 				let arr_val = evaluate(ctx.clone(), over)?;
@@ -304,6 +329,10 @@
 				_ => unreachable!("eager compspecs are not possible with non-full patterns"),
 			}
 		}
+		#[cfg(feature = "exp-object-iteration")]
+		LCompSpec::ForObj { .. } => {
+			unreachable!("eager compspecs filter rejects ForObj");
+		}
 	}
 	Ok(())
 }
@@ -311,7 +340,7 @@
 fn evaluate_compspecs(
 	ctx: Context,
 	specs: &[LCompSpec],
-	cached_overs: &[Option<ArrValue>],
+	cached_overs: &[Option<CachedOver>],
 	idx: usize,
 	guaranteed_reserve: usize,
 	collector: &mut dyn CompCollector,
@@ -340,7 +369,7 @@
 			over,
 			..
 		} => {
-			let arr = if let Some(cached) = &cached_overs[idx] {
+			let arr = if let Some(CachedOver::Arr(cached)) = &cached_overs[idx] {
 				cached.clone()
 			} else {
 				let arr_val = evaluate(ctx.clone(), over)?;
@@ -365,6 +394,61 @@
 				)?;
 			}
 		}
+		#[cfg(feature = "exp-object-iteration")]
+		LCompSpec::ForObj {
+			frame_shape,
+			key,
+			visibility,
+			value,
+			over,
+			..
+		} => {
+			use jrsonnet_ir::Visibility;
+			let obj = if let Some(CachedOver::Obj(cached)) = &cached_overs[idx] {
+				cached.clone()
+			} else {
+				let val = evaluate(ctx.clone(), over)?;
+				let Val::Obj(obj) = val else {
+					bail!(TypeMismatch(
+						"object iteration over",
+						vec![ValType::Obj],
+						val.value_type(),
+					))
+				};
+				obj
+			};
+			let fields = obj.fields_with_visibility(
+				#[cfg(feature = "exp-preserve-order")]
+				false,
+			);
+			let pairs: Vec<(IStr, Visibility)> = fields
+				.into_iter()
+				.filter(|(_, v)| match visibility {
+					Visibility::Normal => v.is_visible(),
+					Visibility::Hidden => !v.is_visible(),
+					Visibility::Unhide => true,
+				})
+				.collect();
+			let inner_reserve = guaranteed_reserve.max(1) * pairs.len();
+			for (i, (field_name, _)) in pairs.into_iter().enumerate() {
+				let key_val = Val::string(field_name.clone());
+				let value_thunk = obj
+					.get_lazy(field_name.clone())
+					.expect("field exists, just enumerated");
+				let inner_ctx = ctx.pack_captures_sup_this(frame_shape).enter(|fill, ctx| {
+					fill.set(*key, Thunk::evaluated(key_val));
+					destruct(value, fill, value_thunk, &ctx);
+				});
+				evaluate_compspecs(
+					inner_ctx,
+					specs,
+					cached_overs,
+					idx + 1,
+					if i == 0 { inner_reserve } else { 0 },
+					collector,
+				)?;
+			}
+		}
 	}
 	Ok(())
 }
modifiedcrates/jrsonnet-evaluator/src/obj/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/obj/mod.rs
1use std::{2	any::Any,3	cell::{Cell, RefCell},4	clone::Clone,5	cmp::Reverse,6	collections::hash_map::Entry,7	fmt::{self, Debug},8	hash::{Hash, Hasher},9	num::Saturating,10	ops::ControlFlow,11};1213use educe::Educe;14use jrsonnet_gcmodule::{Acyclic, Cc, Trace, Weak, cc_dyn};15use jrsonnet_interner::IStr;16use jrsonnet_ir::Span;17use rustc_hash::{FxHashMap, FxHashSet};1819mod oop;2021pub use jrsonnet_ir::Visibility;22pub use oop::ObjValueBuilder;2324use crate::{25	CcUnbound, MaybeUnbound, Result, Thunk, Unbound, Val,26	arr::{PickObjectKeyValues, PickObjectValues},27	bail,28	error::{ErrorKind::*, suggest_object_fields},29	evaluate::operator::evaluate_add_op,30	identity_hash,31	val::{ArrValue, ThunkValue},32};3334#[cfg(not(feature = "exp-preserve-order"))]35pub mod ordering {36	#![allow(37		// This module works as stub for preserve-order feature38		clippy::unused_self,39	)]4041	use jrsonnet_gcmodule::Trace;4243	#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]44	pub struct FieldIndex(());45	impl FieldIndex {46		pub fn absolute(_v: u32) -> Self {47			Self(())48		}49		#[must_use]50		pub const fn next(self) -> Self {51			Self(())52		}53	}5455	#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]56	pub struct SuperDepth(());57	impl SuperDepth {58		pub(super) fn deepen(self) {}59	}60}6162#[cfg(feature = "exp-preserve-order")]63pub mod ordering {64	use jrsonnet_gcmodule::Trace;6566	#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]67	pub struct FieldIndex(u32);68	impl FieldIndex {69		pub fn absolute(v: u32) -> Self {70			Self(v)71		}72		#[must_use]73		pub fn next(self) -> Self {74			Self(self.0 + 1)75		}76	}7778	#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]79	pub struct SuperDepth(u32);80	impl SuperDepth {81		pub(super) fn deepen(&mut self) {82			self.0 += 1;83		}84	}85}8687use ordering::{FieldIndex, SuperDepth};8889#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]90pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);91impl FieldSortKey {92	pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {93		Self(Reverse(depth), index)94	}95}9697// 0 - add98//  12 - visibility99#[derive(Clone, Copy, Acyclic)]100pub struct ObjFieldFlags(u8);101impl ObjFieldFlags {102	fn new(add: bool, visibility: Visibility) -> Self {103		let mut v = 0;104		if add {105			v |= 1;106		}107		v |= match visibility {108			Visibility::Normal => 0b000,109			Visibility::Hidden => 0b010,110			Visibility::Unhide => 0b100,111		};112		Self(v)113	}114	pub fn add(&self) -> bool {115		self.0 & 1 != 0116	}117	pub fn visibility(&self) -> Visibility {118		match (self.0 & 0b110) >> 1 {119			0b00 => Visibility::Normal,120			0b01 => Visibility::Hidden,121			0b10 => Visibility::Unhide,122			_ => unreachable!(),123		}124	}125}126impl Debug for ObjFieldFlags {127	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {128		f.debug_struct("ObjFieldFlags")129			.field("add", &self.add())130			.field("visibility", &self.visibility())131			.finish()132	}133}134135#[allow(clippy::module_name_repetitions)]136#[derive(Debug, Trace)]137pub struct ObjMember {138	flags: ObjFieldFlags,139	pub invoke: MaybeUnbound,140	pub location: Option<Span>,141}142143cc_dyn!(CcObjectAssertion, ObjectAssertion);144pub trait ObjectAssertion: Trace {145	fn run(&self, sup_this: SupThis) -> Result<()>;146}147148// Field => This149150#[derive(Trace, Debug)]151enum CacheValue {152	Cached(Result<Option<Val>>),153	Pending,154}155156pub type EnumFieldsHandler<'a> =157	dyn FnMut(SuperDepth, FieldIndex, IStr, EnumFields) -> ControlFlow<()> + 'a;158159#[derive(Debug)]160pub enum EnumFields {161	Normal(Visibility),162	Omit(Skip),163}164165#[derive(Trace, Clone)]166pub enum GetFor {167	// Return value168	Final(Val),169	// Continue iterating over cores, add current value to sum stack170	SuperPlus(Val),171	// Ignore the field value, stop at this layer instead172	Omit(#[trace(skip)] Skip),173	NotFound,174}175176#[derive(Acyclic, Clone)]177pub enum FieldVisibility {178	Found(Visibility),179	Omit(Skip),180	NotFound,181}182183#[derive(Acyclic, Clone)]184pub enum HasFieldIncludeHidden {185	Exists,186	NotFound,187	Omit(Skip),188}189190type Skip = Saturating<usize>;191192pub trait ObjectCore: Trace + Any + Debug {193	// If callback returns false, iteration stops, and this call returns false.194	fn enum_fields_core(195		&self,196		super_depth: &mut SuperDepth,197		handler: &mut EnumFieldsHandler<'_>,198	) -> bool;199200	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden;201202	fn get_for_core(&self, key: IStr, sup_this: SupThis, omit_only: bool) -> Result<GetFor>;203	fn field_visibility_core(&self, field: IStr) -> FieldVisibility;204205	fn run_assertions_core(&self, sup_this: SupThis) -> Result<()>;206}207208#[derive(Clone, Trace)]209pub struct WeakObjValue(#[trace(skip)] Weak<ObjValueInner>);210impl Debug for WeakObjValue {211	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {212		f.debug_tuple("WeakObjValue").finish()213	}214}215216impl PartialEq for WeakObjValue {217	fn eq(&self, other: &Self) -> bool {218		Weak::ptr_eq(&self.0, &other.0)219	}220}221222impl Eq for WeakObjValue {}223impl Hash for WeakObjValue {224	fn hash<H: Hasher>(&self, hasher: &mut H) {225		// Safety: usize is POD226		let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };227		hasher.write_usize(addr);228	}229}230231cc_dyn!(232	#[derive(Clone, Debug)]233	CcObjectCore, ObjectCore,234	pub fn new() {...}235);236237#[derive(Trace, Educe)]238#[educe(Debug)]239struct ObjValueInner {240	cores: Vec<CcObjectCore>,241	assertions_ran: Cell<bool>,242	has_assertions: bool,243	value_cache: RefCell<FxHashMap<(IStr, CoreIdx), CacheValue>>,244}245246thread_local! {247	static RUNNING_ASSERTIONS: RefCell<FxHashSet<ObjValue>> = RefCell::default();248}249fn is_asserting(obj: &ObjValue) -> bool {250	RUNNING_ASSERTIONS.with_borrow(|v| v.contains(obj))251}252/// Returns false if already asserting253fn start_asserting(obj: &ObjValue) -> bool {254	RUNNING_ASSERTIONS.with_borrow_mut(|v| v.insert(obj.clone()))255}256fn finish_asserting(obj: &ObjValue) {257	RUNNING_ASSERTIONS.with_borrow_mut(|v| {258		let r = v.remove(obj);259		debug_assert!(260			r,261			"finish_asserting was called before start_asserting or twice"262		);263	});264}265266thread_local! {267	static EMPTY_OBJ: ObjValue = ObjValue(Cc::new(ObjValueInner {268		cores: vec![],269		assertions_ran: Cell::new(true),270		has_assertions: false,271		value_cache: RefCell::default(),272	}))273}274275#[allow(clippy::module_name_repetitions)]276#[derive(Clone, Trace, Debug, Educe)]277#[educe(PartialEq, Hash, Eq)]278pub struct ObjValue(279	#[educe(PartialEq(method(Cc::ptr_eq)), Hash(method(identity_hash)))] Cc<ObjValueInner>,280);281282impl ObjValue {283	pub fn empty() -> Self {284		EMPTY_OBJ.with(Clone::clone)285	}286	pub fn is_empty(&self) -> bool {287		self.0.cores.is_empty() || self.len() == 0288	}289}290291#[derive(Trace, Debug)]292pub(crate) struct StandaloneSuperCore {293	sup: CoreIdx,294	this: ObjValue,295}296impl ObjectCore for StandaloneSuperCore {297	fn enum_fields_core(298		&self,299		super_depth: &mut SuperDepth,300		handler: &mut EnumFieldsHandler<'_>,301	) -> bool {302		self.this.enum_fields_idx(super_depth, handler, self.sup)303	}304305	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {306		if self.this.has_field_include_hidden_idx(name, self.sup) {307			HasFieldIncludeHidden::Exists308		} else {309			HasFieldIncludeHidden::NotFound310		}311	}312313	fn get_for_core(&self, key: IStr, _sup_this: SupThis, omit_only: bool) -> Result<GetFor> {314		if omit_only {315			return Ok(GetFor::NotFound);316		}317		let v = self.this.get_idx(key, self.sup)?;318		Ok(v.map_or(GetFor::NotFound, GetFor::Final))319	}320321	fn field_visibility_core(&self, field: IStr) -> FieldVisibility {322		self.this323			.field_visibility_idx(field, self.sup)324			.map_or(FieldVisibility::NotFound, FieldVisibility::Found)325	}326327	fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {328		self.this.run_assertions()329	}330}331332#[derive(Debug, Acyclic)]333struct OmitFieldsCore {334	omit: FxHashSet<IStr>,335	prev_layers: usize,336}337impl ObjectCore for OmitFieldsCore {338	fn enum_fields_core(339		&self,340		super_depth: &mut SuperDepth,341		handler: &mut EnumFieldsHandler<'_>,342	) -> bool {343		let mut fi = FieldIndex::default();344		for f in &self.omit {345			if handler(346				*super_depth,347				fi,348				f.clone(),349				EnumFields::Omit(Saturating(self.prev_layers)),350			) == ControlFlow::Break(())351			{352				return false;353			}354			fi = fi.next();355		}356		true357	}358359	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {360		if self.omit.contains(&name) {361			return HasFieldIncludeHidden::Omit(Saturating(self.prev_layers));362		}363		HasFieldIncludeHidden::NotFound364	}365366	fn get_for_core(&self, key: IStr, _sup_this: SupThis, _omit_only: bool) -> Result<GetFor> {367		if self.omit.contains(&key) {368			return Ok(GetFor::Omit(Saturating(self.prev_layers)));369		}370		Ok(GetFor::NotFound)371	}372373	fn field_visibility_core(&self, field: IStr) -> FieldVisibility {374		if self.omit.contains(&field) {375			return FieldVisibility::Omit(Saturating(self.prev_layers));376		}377		FieldVisibility::NotFound378	}379380	fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {381		Ok(())382	}383}384385#[derive(Hash, PartialEq, Eq, Trace, Clone, Copy, Debug)]386struct CoreIdx {387	idx: usize,388}389impl CoreIdx {390	fn super_exists(self) -> bool {391		self.idx != 0392	}393}394#[derive(Trace, Clone, PartialEq, Eq, Hash, Debug)]395pub struct SupThis {396	sup: CoreIdx,397	this: ObjValue,398}399impl SupThis {400	/// Create a `SupThis` for a freshly constructed object (no super).401	pub fn new(this: ObjValue) -> Self {402		Self {403			sup: CoreIdx {404				idx: this.0.cores.len(),405			},406			this,407		}408	}409	pub fn has_super(&self) -> bool {410		self.sup.super_exists()411	}412	/// Implementation of `"field" in super` operation,413	/// works faster than standalone super path.414	///415	/// In case of no `super` existence, returns false.416	pub fn field_in_super(&self, field: IStr) -> bool {417		self.this.has_field_include_hidden_idx(field, self.sup)418	}419	/// Implementation of `super.field` operation,420	/// works faster than standalone super path.421	///422	/// In case of no `super` existence, returns `NoSuperFound`423	pub fn get_super(&self, field: IStr) -> Result<Option<Val>> {424		if !self.sup.super_exists() {425			bail!(NoSuperFound);426		}427		self.this.get_idx(field, self.sup)428	}429	/// `super` with `self` overriden for top-level lookups.430	/// Exists when super appears outside of `super.field`/`"field" in super` expressions431	/// Exclusive to jrsonnet.432	///433	/// Might return `NoSuperFound` error.434	pub fn standalone_super(&self) -> Result<ObjValue> {435		if !self.sup.super_exists() {436			bail!(NoSuperFound)437		}438		let mut out = ObjValue::builder();439		out.extend_with_core(StandaloneSuperCore {440			sup: self.sup,441			this: self.this.clone(),442		});443		Ok(out.build())444	}445	pub fn this(&self) -> &ObjValue {446		&self.this447	}448	pub fn downgrade(self) -> WeakSupThis {449		WeakSupThis {450			sup: self.sup,451			this: self.this.downgrade(),452		}453	}454}455#[derive(Trace, PartialEq, Eq, Hash, Debug)]456pub struct WeakSupThis {457	sup: CoreIdx,458	this: WeakObjValue,459}460461impl ObjValue {462	pub fn builder() -> ObjValueBuilder {463		ObjValueBuilder::new()464	}465	pub fn builder_with_capacity(capacity: usize) -> ObjValueBuilder {466		ObjValueBuilder::with_capacity(capacity)467	}468	pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {469		let mut out = ObjValueBuilder::with_capacity(1);470		out.with_super(self);471		let mut member = out.field(key);472		if value.flags.add() {473			member = member.add();474		}475		if let Some(loc) = value.location {476			member = member.with_location(loc);477		}478		let _ = member479			.with_visibility(value.flags.visibility())480			.binding(value.invoke);481		out.build()482	}483	pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {484		ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())485	}486487	pub fn extend(&mut self) -> ObjValueBuilder {488		let mut out = ObjValueBuilder::new();489		out.with_super(self.clone());490		out491	}492493	#[must_use]494	pub fn extend_from(&self, sup: Self) -> Self {495		let mut cores = Vec::with_capacity(sup.0.cores.len() + self.0.cores.len());496		cores.extend(sup.0.cores.iter().cloned());497		cores.extend(self.0.cores.iter().cloned());498499		let has_assertions = sup.0.has_assertions || self.0.has_assertions;500		ObjValue(Cc::new(ObjValueInner {501			cores,502			value_cache: RefCell::default(),503			assertions_ran: Cell::new(!has_assertions),504			has_assertions,505		}))506	}507	// #[must_use]508	// pub fn with_this(&self, this: Self) -> Self {509	// 	self.0.with_this(self.clone(), this)510	// }511	/// Returns amount of visible object fields512	/// If object only contains hidden fields - may return zero.513	pub fn len(&self) -> u32 {514		self.fields_visibility()515			.values()516			.filter(|d| d.visible())517			.count() as u32518	}519	/// For each field, calls callback.520	/// If callback returns false - ends iteration prematurely.521	///522	/// Returns false if ended prematurely523	pub fn enum_fields(&self, handler: &mut EnumFieldsHandler<'_>) -> bool {524		let mut super_depth = SuperDepth::default();525		self.enum_fields_idx(526			&mut super_depth,527			handler,528			CoreIdx {529				idx: self.0.cores.len(),530			},531		)532	}533534	fn iter_cores(&self, idx: CoreIdx) -> impl Iterator<Item = &CcObjectCore> {535		self.0.cores.iter().take(idx.idx).rev()536	}537	fn iter_cores_enumerate(&self, idx: CoreIdx) -> impl Iterator<Item = (CoreIdx, &CcObjectCore)> {538		self.0539			.cores540			.iter()541			.take(idx.idx)542			.enumerate()543			.rev()544			.map(|(idx, o)| (CoreIdx { idx }, o))545	}546547	fn enum_fields_idx(548		&self,549		super_depth: &mut SuperDepth,550		handler: &mut EnumFieldsHandler<'_>,551		idx: CoreIdx,552	) -> bool {553		for core in self.iter_cores(idx) {554			if !core.0.enum_fields_core(super_depth, handler) {555				return false;556			}557			super_depth.deepen();558		}559		true560	}561562	pub fn has_field_include_hidden(&self, name: IStr) -> bool {563		self.has_field_include_hidden_idx(564			name,565			CoreIdx {566				idx: self.0.cores.len(),567			},568		)569	}570	fn has_field_include_hidden_idx(&self, name: IStr, core: CoreIdx) -> bool {571		let mut skip = Saturating(0usize);572		for ele in self.iter_cores(core) {573			match ele.0.has_field_include_hidden_core(name.clone()) {574				HasFieldIncludeHidden::Exists => {575					if skip.0 == 0 {576						return true;577					}578				}579				HasFieldIncludeHidden::Omit(new_skip) => {580					// +1 including this core581					skip = skip.max(new_skip + Saturating(1));582				}583				HasFieldIncludeHidden::NotFound => {}584			}585			skip -= 1;586		}587		false588	}589	pub fn has_field(&self, name: IStr) -> bool {590		match self.field_visibility(name) {591			Some(Visibility::Unhide | Visibility::Normal) => true,592			Some(Visibility::Hidden) | None => false,593		}594	}595	pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {596		if include_hidden {597			self.has_field_include_hidden(name)598		} else {599			self.has_field(name)600		}601	}602	pub fn get(&self, key: IStr) -> Result<Option<Val>> {603		self.get_idx(604			key,605			CoreIdx {606				idx: self.0.cores.len(),607			},608		)609	}610611	fn get_idx(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {612		let cache_key = (key.clone(), core);613		{614			let mut cache = self.0.value_cache.borrow_mut();615			// entry_ref candidate?616			match cache.entry(cache_key.clone()) {617				Entry::Occupied(v) => match v.get() {618					CacheValue::Cached(v) => return v.clone(),619					CacheValue::Pending => {620						if !is_asserting(self) {621							bail!(InfiniteRecursionDetected);622						}623					}624				},625				Entry::Vacant(v) => {626					v.insert(CacheValue::Pending);627				}628			};629		}630		let result = self.get_idx_uncached(key, core);631		{632			let mut cache = self.0.value_cache.borrow_mut();633			cache.insert(cache_key, CacheValue::Cached(result.clone()));634		}635		result636	}637	fn get_idx_uncached(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {638		self.run_assertions()?;639		let mut first_add = None;640		let mut add_stack: Vec<Val> = Vec::new();641		let mut skip = Saturating(0);642		for (sup, core) in self.iter_cores_enumerate(core) {643			let sup_this = SupThis {644				sup,645				this: self.clone(),646			};647			match core.0.get_for_core(key.clone(), sup_this, skip.0 != 0)? {648				GetFor::Final(val) if first_add.is_none() => {649					if skip.0 == 0 {650						return Ok(Some(val));651					}652				}653				GetFor::Final(val) => {654					if skip.0 == 0 {655						add_stack.push(val);656						break;657					}658				}659				GetFor::SuperPlus(val) => {660					if skip.0 == 0 {661						if first_add.is_none() {662							first_add = Some(val);663						} else {664							add_stack.push(val);665						}666					}667				}668				GetFor::Omit(new_skip) => {669					skip = skip.max(new_skip + Saturating(1));670				}671				GetFor::NotFound => {}672			}673			skip -= 1;674		}675		let Some(first) = first_add else {676			if add_stack.is_empty() {677				return Ok(None);678			}679			return Ok(Some(add_stack.pop().expect("single element on stack")));680		};681		if add_stack.is_empty() {682			return Ok(Some(first));683		}684		add_stack.insert(0, first);685		let mut values = add_stack.into_iter().rev();686		let init = values.next().expect("at least 2 elements");687688		values689			.try_fold(init, |a, b| evaluate_add_op(&a, &b))690			.map(Some)691	}692693	pub fn get_or_bail(&self, key: IStr) -> Result<Val> {694		let Some(value) = self.get(key.clone())? else {695			let suggestions = suggest_object_fields(self, key.clone());696			bail!(NoSuchField(key, suggestions))697		};698		Ok(value)699	}700701	fn field_visibility(&self, field: IStr) -> Option<Visibility> {702		self.field_visibility_idx(703			field,704			CoreIdx {705				idx: self.0.cores.len(),706			},707		)708	}709	fn field_visibility_idx(&self, field: IStr, core: CoreIdx) -> Option<Visibility> {710		let mut exists = false;711		let mut skip = Saturating(0usize);712		for ele in self.iter_cores(core) {713			let vis = ele.0.field_visibility_core(field.clone());714			match vis {715				FieldVisibility::Found(vis @ (Visibility::Unhide | Visibility::Hidden)) => {716					if skip.0 == 0 {717						return Some(vis);718					}719				}720				FieldVisibility::Found(Visibility::Normal) => {721					if skip.0 == 0 {722						exists = true;723					}724				}725				FieldVisibility::NotFound => {}726				FieldVisibility::Omit(new_skip) => {727					// +1 including this core728					skip = skip.max(new_skip + Saturating(1));729				}730			}731			skip -= 1;732		}733		exists.then_some(Visibility::Normal)734	}735736	pub fn run_assertions(&self) -> Result<()> {737		if self.0.assertions_ran.get() {738			return Ok(());739		}740		if !start_asserting(self) {741			return Ok(());742		}743		for (idx, ele) in self.0.cores.iter().enumerate() {744			let sup_this = SupThis {745				sup: CoreIdx { idx },746				this: self.clone(),747			};748			ele.0.run_assertions_core(sup_this).inspect_err(|_e| {749				finish_asserting(self);750			})?;751		}752		finish_asserting(self);753		self.0.assertions_ran.set(true);754		Ok(())755	}756757	pub fn iter(758		&self,759		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,760	) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {761		let fields = self.fields(762			#[cfg(feature = "exp-preserve-order")]763			preserve_order,764		);765		fields.into_iter().map(|field| {766			(767				field.clone(),768				self.get(field)769					.map(|opt| opt.expect("iterating over keys, field exists")),770			)771		})772	}773	pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {774		#[derive(Trace)]775		struct ObjFieldThunk {776			obj: ObjValue,777			key: IStr,778		}779		impl ThunkValue for ObjFieldThunk {780			type Output = Val;781782			fn get(&self) -> Result<Self::Output> {783				self.obj784					.get(self.key.clone())785					.transpose()786					.expect("field existence checked")787			}788		}789790		if !self.has_field_ex(key.clone(), true) {791			return None;792		}793794		Some(Thunk::new(ObjFieldThunk {795			obj: self.clone(),796			key,797		}))798	}799	pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {800		#[derive(Trace)]801		struct ObjFieldThunk {802			obj: ObjValue,803			key: IStr,804		}805		impl ThunkValue for ObjFieldThunk {806			type Output = Val;807808			fn get(&self) -> Result<Self::Output> {809				self.obj.get_or_bail(self.key.clone())810			}811		}812813		Thunk::new(ObjFieldThunk {814			obj: self.clone(),815			key,816		})817	}818819	#[allow(dead_code, reason = "used in object ...rest destructuring")]820	pub(crate) fn as_standalone(&self) -> StandaloneSuperCore {821		StandaloneSuperCore {822			sup: CoreIdx {823				idx: self.0.cores.len(),824			},825			this: self.clone(),826		}827	}828	pub fn ptr_eq(a: &Self, b: &Self) -> bool {829		Cc::ptr_eq(&a.0, &b.0)830	}831	pub fn downgrade(self) -> WeakObjValue {832		WeakObjValue(self.0.downgrade())833	}834}835836#[derive(Debug)]837struct FieldVisibilityData {838	omitted_until: Saturating<usize>,839	exists_visible: Option<Visibility>,840	#[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]841	key: FieldSortKey,842}843impl FieldVisibilityData {844	fn visible(&self) -> bool {845		self.exists_visible846			.expect("non-existing fields shall be dropped at the end of fn fields_visibility()")847			.is_visible()848	}849	#[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]850	fn sort_key(&self) -> FieldSortKey {851		self.key852	}853}854855impl ObjValue {856	fn fields_visibility(&self) -> FxHashMap<IStr, FieldVisibilityData> {857		let mut out = FxHashMap::default();858859		let mut super_depth = SuperDepth::default();860		let mut omit_index = Saturating(0);861		for core in self.0.cores.iter().rev() {862			core.0863				.enum_fields_core(&mut super_depth, &mut |depth, index, name, visibility| {864					let entry = out.entry(name);865					let data = entry.or_insert_with(|| FieldVisibilityData {866						exists_visible: None,867						key: FieldSortKey::new(depth, index),868						omitted_until: omit_index,869					});870					match visibility {871						EnumFields::Omit(new_skip) => {872							// +1 including this core873							data.omitted_until = data874								.omitted_until875								.max(omit_index + new_skip + Saturating(1));876						}877						EnumFields::Normal(Visibility::Normal) => {878							if data.omitted_until <= omit_index && data.exists_visible.is_none() {879								data.exists_visible = Some(Visibility::Normal);880							}881						}882						EnumFields::Normal(Visibility::Hidden) => {883							if data.omitted_until <= omit_index {884								data.exists_visible = Some(match data.exists_visible {885									// We're iterating in reverse, later unhide is preserved886									Some(Visibility::Unhide) => Visibility::Unhide,887									_ => Visibility::Hidden,888								});889							}890						}891						EnumFields::Normal(Visibility::Unhide) => {892							if data.omitted_until <= omit_index {893								data.exists_visible = Some(match data.exists_visible {894									// We're iterating in reverse, later hide is preserved895									Some(Visibility::Hidden) => Visibility::Hidden,896									_ => Visibility::Unhide,897								});898							}899						}900					}901					ControlFlow::Continue(())902				});903904			super_depth.deepen();905			omit_index += 1;906		}907908		out.retain(|_, v| v.exists_visible.is_some());909910		out911	}912	pub fn fields_ex(913		&self,914		include_hidden: bool,915		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,916	) -> Vec<IStr> {917		#[cfg(feature = "exp-preserve-order")]918		if preserve_order {919			let (mut fields, mut keys): (Vec<_>, Vec<_>) = self920				.fields_visibility()921				.into_iter()922				.filter(|(_, d)| include_hidden || d.visible())923				.enumerate()924				.map(|(idx, (k, d))| (k, (d.sort_key(), idx)))925				.unzip();926			keys.sort_unstable_by_key(|v| v.0);927			// Reorder in-place by resulting indexes928			for i in 0..fields.len() {929				let x = fields[i].clone();930				let mut j = i;931				loop {932					let k = keys[j].1;933					keys[j].1 = j;934					if k == i {935						break;936					}937					fields[j] = fields[k].clone();938					j = k;939				}940				fields[j] = x;941			}942			return fields;943		}944945		let mut fields: Vec<_> = self946			.fields_visibility()947			.into_iter()948			.filter(|(_, d)| include_hidden || d.visible())949			.map(|(k, _)| k)950			.collect();951		fields.sort_unstable();952		fields953	}954	pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {955		self.fields_ex(956			false,957			#[cfg(feature = "exp-preserve-order")]958			preserve_order,959		)960	}961	pub fn values_ex(962		&self,963		include_hidden: bool,964		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,965	) -> ArrValue {966		ArrValue::new(PickObjectValues::new(967			self.clone(),968			self.fields_ex(969				include_hidden,970				#[cfg(feature = "exp-preserve-order")]971				preserve_order,972			),973		))974	}975	pub fn values(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> ArrValue {976		self.values_ex(977			false,978			#[cfg(feature = "exp-preserve-order")]979			preserve_order,980		)981	}982	pub fn key_values_ex(983		&self,984		include_hidden: bool,985		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,986	) -> ArrValue {987		ArrValue::new(PickObjectKeyValues::new(988			self.clone(),989			self.fields_ex(990				include_hidden,991				#[cfg(feature = "exp-preserve-order")]992				preserve_order,993			),994		))995	}996	pub fn key_values(997		&self,998		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,999	) -> ArrValue {1000		self.key_values_ex(1001			false,1002			#[cfg(feature = "exp-preserve-order")]1003			preserve_order,1004		)1005	}1006}10071008#[allow(clippy::module_name_repetitions)]1009#[must_use = "value not added unless binding() was called"]1010pub struct ObjMemberBuilder<Kind> {1011	kind: Kind,1012	name: IStr,1013	add: bool,1014	visibility: Visibility,1015	original_index: FieldIndex,1016	location: Option<Span>,1017}10181019#[allow(clippy::missing_const_for_fn)]1020impl<Kind> ObjMemberBuilder<Kind> {1021	pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {1022		Self {1023			kind,1024			name,1025			original_index,1026			add: false,1027			visibility: Visibility::Normal,1028			location: None,1029		}1030	}10311032	pub const fn with_add(mut self, add: bool) -> Self {1033		self.add = add;1034		self1035	}1036	pub fn add(self) -> Self {1037		self.with_add(true)1038	}1039	pub fn with_visibility(mut self, visibility: Visibility) -> Self {1040		self.visibility = visibility;1041		self1042	}1043	pub fn hide(self) -> Self {1044		self.with_visibility(Visibility::Hidden)1045	}1046	pub fn with_location(mut self, location: Span) -> Self {1047		self.location = Some(location);1048		self1049	}1050	fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, FieldIndex, ObjMember) {1051		(1052			self.kind,1053			self.name,1054			self.original_index,1055			ObjMember {1056				flags: ObjFieldFlags::new(self.add, self.visibility),1057				invoke: binding,1058				location: self.location,1059			},1060		)1061	}1062}10631064pub struct ExtendBuilder<'v>(&'v mut ObjValue);1065impl ObjMemberBuilder<ExtendBuilder<'_>> {1066	pub fn value(self, value: impl Into<Val>) {1067		self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));1068	}1069	pub fn bindable(self, bindable: impl Unbound<Bound = Val>) {1070		self.binding(MaybeUnbound::Unbound(CcUnbound::new(bindable)));1071	}1072	pub fn binding(self, binding: MaybeUnbound) {1073		let (receiver, name, _, member) = self.build_member(binding);1074		let new = receiver.0.clone();1075		*receiver.0 = new.extend_with_raw_member(name, member);1076	}1077}
modifiedcrates/jrsonnet-peg-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-peg-parser/src/lib.rs
+++ b/crates/jrsonnet-peg-parser/src/lib.rs
@@ -441,6 +441,7 @@
 	use crate::{ParserSettings, parse};
 
 	#[test]
+	#[cfg(not(feature = "exp-null-coaelse"))]
 	fn snapshots() {
 		glob!("tests/*.jsonnet", |path| {
 			let input = fs::read_to_string(path).expect("read test file");