git.delta.rocks / jrsonnet / refs/commits / 1bfba233fc03

difftreelog

refactor reenable clippy integer cast checks

slqpxoouYaroslav Bolyukin2026-04-25parent: #191649c.patch.diff
in: master

17 files changed

modifiedCargo.tomldiffbeforeafterboth
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -122,11 +122,6 @@
 wildcard_imports = "allow"
 enum_glob_use = "allow"
 module_name_repetitions = "allow"
-# TODO: fix individual issues, however this works as intended almost everywhere
-cast_precision_loss = "allow"
-cast_possible_wrap = "allow"
-cast_possible_truncation = "allow"
-cast_sign_loss = "allow"
 # False positives
 # https://github.com/rust-lang/rust-clippy/issues/6902
 use_self = "allow"
modifiedcrates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -128,7 +128,12 @@
 	#[must_use]
 	pub fn slice(self, index: Option<i32>, end: Option<i32>, step: Option<NonZeroU32>) -> Self {
 		let get_idx = |pos: Option<i32>, len: usize, default| match pos {
+			#[expect(
+				clippy::cast_sign_loss,
+				reason = "abs value is used, len is limited to u31"
+			)]
 			Some(v) if v < 0 => len.saturating_sub((-v) as usize),
+			#[expect(clippy::cast_sign_loss, reason = "abs value is used")]
 			Some(v) => (v as usize).min(len),
 			None => default,
 		};
@@ -142,7 +147,9 @@
 
 		Self::new(SliceArray {
 			inner: self,
+			#[expect(clippy::cast_possible_truncation, reason = "len is limited to u31")]
 			from: index as u32,
+			#[expect(clippy::cast_possible_truncation, reason = "len is limited to u31")]
 			to: end as u32,
 			step: step.get(),
 		})
modifiedcrates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -350,22 +350,26 @@
 	pub fn new_inclusive(start: i32, end: i32) -> Self {
 		Self { start, end }
 	}
+	#[expect(
+		clippy::cast_sign_loss,
+		reason = "the math is valid with wrapping, sign loss works as intended"
+	)]
+	fn size(&self) -> usize {
+		(self.end as usize)
+			.wrapping_sub(self.start as usize)
+			.wrapping_add(1)
+	}
 	fn range(&self) -> impl ExactSizeIterator<Item = i32> + DoubleEndedIterator {
-		WithExactSize(
-			self.start..=self.end,
-			(self.end as usize)
-				.wrapping_sub(self.start as usize)
-				.wrapping_add(1),
-		)
+		WithExactSize(self.start..=self.end, self.size())
 	}
 }
 
 impl ArrayLike for RangeArray {
 	fn len(&self) -> usize {
-		self.range().len()
+		self.size()
 	}
 	fn is_empty(&self) -> bool {
-		self.range().len() == 0
+		self.size() == 0
 	}
 
 	fn get(&self, index: usize) -> Result<Option<Val>> {
@@ -431,6 +435,10 @@
 	fn evaluate(&self, index: usize, value: Val) -> Result<Val> {
 		match &self.mapper {
 			ArrayMapper::Plain(f) => f.call(value),
+			#[expect(
+				clippy::cast_possible_truncation,
+				reason = "array len is limited to u31"
+			)]
 			ArrayMapper::WithIndex(f) => f.call(index as u32, value),
 		}
 	}
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -548,8 +548,18 @@
 							bail!(FractionalIndex)
 						}
 						if n < 0.0 {
-							bail!(ArrayBoundsError(n as isize, v.len()));
+							#[expect(
+								clippy::cast_possible_truncation,
+								reason = "it would be truncated anyway"
+							)]
+							let n = n as isize;
+							bail!(ArrayBoundsError(n, v.len()));
 						}
+						#[expect(
+							clippy::cast_possible_truncation,
+							clippy::cast_sign_loss,
+							reason = "n is checked postive"
+						)]
 						v.get(n as usize)?
 							.ok_or_else(|| ArrayBoundsError(n as isize, v.len()))?
 					}
@@ -568,18 +578,29 @@
 							bail!(FractionalIndex)
 						}
 						if n < 0.0 {
-							bail!(ArrayBoundsError(n as isize, s.into_flat().chars().count()));
+							#[expect(
+								clippy::cast_possible_truncation,
+								reason = "it would be truncated anyway"
+							)]
+							let n = n as isize;
+							bail!(ArrayBoundsError(n, s.into_flat().chars().count()));
 						}
+						#[expect(
+							clippy::cast_sign_loss,
+							clippy::cast_possible_truncation,
+							reason = "n is positive, overflow will truncate as expected"
+						)]
+						let n = n as usize;
 						let v: IStr = s
 							.clone()
 							.into_flat()
 							.chars()
-							.skip(n as usize)
+							.skip(n)
 							.take(1)
 							.collect::<String>()
 							.into();
 						if v.is_empty() {
-							bail!(StringBoundsError(n as usize, s.into_flat().chars().count()))
+							bail!(StringBoundsError(n, s.into_flat().chars().count()))
 						}
 						StrValue::Flat(v)
 					}),
modifiedcrates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -20,7 +20,8 @@
 		(Plus, Num(n)) => Val::Num(*n),
 		(Minus, Num(n)) => Val::try_num(-n.get())?,
 		(Not, Bool(v)) => Bool(!v),
-		(BitNot, Num(n)) => Val::try_num(!(n.get() as i64) as f64)?,
+		#[expect(clippy::cast_precision_loss, reason = "as spec")]
+		(BitNot, Num(n)) => Val::try_num(!n.truncate_for_bitwise()? as f64)?,
 		(op, o) => bail!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),
 	})
 }
@@ -73,7 +74,17 @@
 pub fn evaluate_mul_op(a: &Val, b: &Val) -> Result<Val> {
 	use Val::*;
 	Ok(match (a, b) {
+		#[expect(
+			clippy::cast_possible_truncation,
+			clippy::cast_sign_loss,
+			reason = "should not be used with values too large, negative == 0"
+		)]
 		(Str(s), Num(c)) => Val::string(s.to_string().repeat(c.get() as usize)),
+		#[expect(
+			clippy::cast_possible_truncation,
+			clippy::cast_sign_loss,
+			reason = "should not be used with values too large"
+		)]
 		(Num(c), Str(s)) => Val::string(s.to_string().repeat(c.get() as usize)),
 
 		(Num(v1), Num(v2)) => Val::try_num(v1.get() * v2.get())?,
@@ -218,13 +229,28 @@
 		(a, Div, b) => evaluate_div_op(a, b)?,
 		(a, Mod, b) => evaluate_mod_op(a, b)?,
 
-		(Num(v1), BitAnd, Num(v2)) => {
+		(Num(v1), BitAnd, Num(v2)) =>
+		{
+			#[expect(
+				clippy::cast_precision_loss,
+				reason = "values are within safe integer ranges"
+			)]
 			Val::try_num((v1.truncate_for_bitwise()? & v2.truncate_for_bitwise()?) as f64)?
 		}
-		(Num(v1), BitOr, Num(v2)) => {
+		(Num(v1), BitOr, Num(v2)) =>
+		{
+			#[expect(
+				clippy::cast_precision_loss,
+				reason = "values are within safe integer ranges"
+			)]
 			Val::try_num((v1.truncate_for_bitwise()? | v2.truncate_for_bitwise()?) as f64)?
 		}
-		(Num(v1), BitXor, Num(v2)) => {
+		(Num(v1), BitXor, Num(v2)) =>
+		{
+			#[expect(
+				clippy::cast_precision_loss,
+				reason = "values are within safe integer ranges"
+			)]
 			Val::try_num((v1.truncate_for_bitwise()? ^ v2.truncate_for_bitwise()?) as f64)?
 		}
 		(Num(v1), Lhs, Num(v2)) => {
@@ -234,16 +260,28 @@
 			let base = v1.truncate_for_bitwise()?;
 			let exp = v2.truncate_for_bitwise()? % 64;
 
+			#[expect(clippy::cast_sign_loss, reason = "exp is positive")]
 			if exp >= 1 && base >= (1i64 << (63 - exp as u32)) {
 				bail!("left shift would overflow")
 			}
+			#[expect(
+				clippy::cast_precision_loss,
+				clippy::cast_sign_loss,
+				reason = "checked as original impl"
+			)]
 			Val::try_num(base.wrapping_shl(exp as u32) as f64)?
 		}
 		(Num(v1), Rhs, Num(v2)) => {
 			if v2.get() < 0.0 {
 				bail!("shift by negative exponent")
 			}
+			#[expect(
+				clippy::cast_sign_loss,
+				clippy::cast_possible_truncation,
+				reason = "checked as original impl"
+			)]
 			let exp = ((v2.get() as i64) & 63) as u32;
+			#[expect(clippy::cast_precision_loss, reason = "checked as upstream impl")]
 			Val::try_num(v1.truncate_for_bitwise()?.wrapping_shr(exp) as f64)?
 		}
 
modifiedcrates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -69,12 +69,20 @@
 			where
 				E: de::Error,
 			{
+				#[expect(
+					clippy::cast_precision_loss,
+					reason = "this is how it works with stdlib functions"
+				)]
 				Ok(Val::Num(NumValue::new(v as f64).expect("no overflow")))
 			}
 			fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
 			where
 				E: de::Error,
 			{
+				#[expect(
+					clippy::cast_precision_loss,
+					reason = "this is how it works with stdlib functions"
+				)]
 				Ok(Val::Num(NumValue::new(v as f64).expect("no overflow")))
 			}
 
@@ -161,6 +169,10 @@
 			Self::Num(n) => {
 				let n = n.get();
 				if n.fract() == 0.0 {
+					#[expect(
+						clippy::cast_possible_truncation,
+						reason = "no correct implementation is possible here; expected"
+					)]
 					let n = n as i64;
 					serializer.serialize_i64(n)
 				} else {
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	identity_hash,30	operator::evaluate_add_op,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)]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	#[trace(skip)]139	flags: ObjFieldFlags,140	original_index: FieldIndex,141	pub invoke: MaybeUnbound,142	pub location: Option<Span>,143}144145cc_dyn!(CcObjectAssertion, ObjectAssertion);146pub trait ObjectAssertion: Trace {147	fn run(&self, sup_this: SupThis) -> Result<()>;148}149150// Field => This151152#[derive(Trace, Debug)]153enum CacheValue {154	Cached(Result<Option<Val>>),155	Pending,156}157158pub type EnumFieldsHandler<'a> =159	dyn FnMut(SuperDepth, FieldIndex, IStr, EnumFields) -> ControlFlow<()> + 'a;160161#[derive(Debug)]162pub enum EnumFields {163	Normal(Visibility),164	Omit(Skip),165}166167#[derive(Trace, Clone)]168pub enum GetFor {169	// Return value170	Final(Val),171	// Continue iterating over cores, add current value to sum stack172	SuperPlus(Val),173	// Ignore the field value, stop at this layer instead174	Omit(#[trace(skip)] Skip),175	NotFound,176}177178#[derive(Acyclic, Clone)]179pub enum FieldVisibility {180	Found(Visibility),181	Omit(Skip),182	NotFound,183}184185#[derive(Acyclic, Clone)]186pub enum HasFieldIncludeHidden {187	Exists,188	NotFound,189	Omit(Skip),190}191192type Skip = Saturating<usize>;193194pub trait ObjectCore: Trace + Any + Debug {195	// If callback returns false, iteration stops, and this call returns false.196	fn enum_fields_core(197		&self,198		super_depth: &mut SuperDepth,199		handler: &mut EnumFieldsHandler<'_>,200	) -> bool;201202	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden;203204	fn get_for_core(&self, key: IStr, sup_this: SupThis, omit_only: bool) -> Result<GetFor>;205	fn field_visibility_core(&self, field: IStr) -> FieldVisibility;206207	fn run_assertions_core(&self, sup_this: SupThis) -> Result<()>;208}209210#[derive(Clone, Trace)]211pub struct WeakObjValue(#[trace(skip)] Weak<ObjValueInner>);212impl Debug for WeakObjValue {213	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {214		f.debug_tuple("WeakObjValue").finish()215	}216}217218impl PartialEq for WeakObjValue {219	fn eq(&self, other: &Self) -> bool {220		Weak::ptr_eq(&self.0, &other.0)221	}222}223224impl Eq for WeakObjValue {}225impl Hash for WeakObjValue {226	fn hash<H: Hasher>(&self, hasher: &mut H) {227		// Safety: usize is POD228		let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };229		hasher.write_usize(addr);230	}231}232233cc_dyn!(234	#[derive(Clone, Debug)]235	CcObjectCore, ObjectCore,236	pub fn new() {...}237);238#[derive(Trace, Educe)]239#[educe(Debug)]240struct ObjValueInner {241	cores: Vec<CcObjectCore>,242	assertions_ran: Cell<bool>,243	has_assertions: bool,244	value_cache: RefCell<FxHashMap<(IStr, CoreIdx), CacheValue>>,245}246247thread_local! {248	static RUNNING_ASSERTIONS: RefCell<FxHashSet<ObjValue>> = RefCell::default();249}250fn is_asserting(obj: &ObjValue) -> bool {251	RUNNING_ASSERTIONS.with_borrow(|v| v.contains(obj))252}253/// Returns false if already asserting254fn start_asserting(obj: &ObjValue) -> bool {255	RUNNING_ASSERTIONS.with_borrow_mut(|v| v.insert(obj.clone()))256}257fn finish_asserting(obj: &ObjValue) {258	RUNNING_ASSERTIONS.with_borrow_mut(|v| {259		let r = v.remove(obj);260		debug_assert!(261			r,262			"finish_asserting was called before start_asserting or twice"263		);264	});265}266267thread_local! {268	static EMPTY_OBJ: ObjValue = ObjValue(Cc::new(ObjValueInner {269		cores: vec![],270		assertions_ran: Cell::new(true),271		has_assertions: false,272		value_cache: RefCell::default(),273	}))274}275276#[allow(clippy::module_name_repetitions)]277#[derive(Clone, Trace, Debug, Educe)]278#[educe(PartialEq, Hash, Eq)]279pub struct ObjValue(280	#[educe(PartialEq(method(Cc::ptr_eq)), Hash(method(identity_hash)))] Cc<ObjValueInner>,281);282283impl ObjValue {284	pub fn empty() -> Self {285		EMPTY_OBJ.with(Clone::clone)286	}287	pub fn is_empty(&self) -> bool {288		self.0.cores.is_empty() || self.len() == 0289	}290}291292#[derive(Trace, Debug)]293pub(crate) struct StandaloneSuperCore {294	sup: CoreIdx,295	this: ObjValue,296}297impl ObjectCore for StandaloneSuperCore {298	fn enum_fields_core(299		&self,300		super_depth: &mut SuperDepth,301		handler: &mut EnumFieldsHandler<'_>,302	) -> bool {303		self.this.enum_fields_idx(super_depth, handler, self.sup)304	}305306	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {307		if self.this.has_field_include_hidden_idx(name, self.sup) {308			HasFieldIncludeHidden::Exists309		} else {310			HasFieldIncludeHidden::NotFound311		}312	}313314	fn get_for_core(&self, key: IStr, _sup_this: SupThis, omit_only: bool) -> Result<GetFor> {315		if omit_only {316			return Ok(GetFor::NotFound);317		}318		let v = self.this.get_idx(key, self.sup)?;319		Ok(v.map_or(GetFor::NotFound, GetFor::Final))320	}321322	fn field_visibility_core(&self, field: IStr) -> FieldVisibility {323		self.this324			.field_visibility_idx(field, self.sup)325			.map_or(FieldVisibility::NotFound, FieldVisibility::Found)326	}327328	fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {329		self.this.run_assertions()330	}331}332333#[derive(Debug, Acyclic)]334struct OmitFieldsCore {335	omit: FxHashSet<IStr>,336	prev_layers: usize,337}338impl ObjectCore for OmitFieldsCore {339	fn enum_fields_core(340		&self,341		super_depth: &mut SuperDepth,342		handler: &mut EnumFieldsHandler<'_>,343	) -> bool {344		let mut fi = FieldIndex::default();345		for f in &self.omit {346			if handler(347				*super_depth,348				fi,349				f.clone(),350				EnumFields::Omit(Saturating(self.prev_layers)),351			) == ControlFlow::Break(())352			{353				return false;354			}355			fi = fi.next();356		}357		true358	}359360	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {361		if self.omit.contains(&name) {362			return HasFieldIncludeHidden::Omit(Saturating(self.prev_layers));363		}364		HasFieldIncludeHidden::NotFound365	}366367	fn get_for_core(&self, key: IStr, _sup_this: SupThis, _omit_only: bool) -> Result<GetFor> {368		if self.omit.contains(&key) {369			return Ok(GetFor::Omit(Saturating(self.prev_layers)));370		}371		Ok(GetFor::NotFound)372	}373374	fn field_visibility_core(&self, field: IStr) -> FieldVisibility {375		if self.omit.contains(&field) {376			return FieldVisibility::Omit(Saturating(self.prev_layers));377		}378		FieldVisibility::NotFound379	}380381	fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {382		Ok(())383	}384}385386#[derive(Hash, PartialEq, Eq, Trace, Clone, Copy, Debug)]387struct CoreIdx {388	idx: usize,389}390impl CoreIdx {391	fn super_exists(self) -> bool {392		self.idx != 0393	}394}395#[derive(Trace, Clone, PartialEq, Eq, Hash, Debug)]396pub struct SupThis {397	sup: CoreIdx,398	this: ObjValue,399}400impl SupThis {401	pub fn has_super(&self) -> bool {402		self.sup.super_exists()403	}404	/// Implementation of `"field" in super` operation,405	/// works faster than standalone super path.406	///407	/// In case of no `super` existence, returns false.408	pub fn field_in_super(&self, field: IStr) -> bool {409		self.this.has_field_include_hidden_idx(field, self.sup)410	}411	/// Implementation of `super.field` operation,412	/// works faster than standalone super path.413	///414	/// In case of no `super` existence, returns `NoSuperFound`415	pub fn get_super(&self, field: IStr) -> Result<Option<Val>> {416		if !self.sup.super_exists() {417			bail!(NoSuperFound);418		}419		self.this.get_idx(field, self.sup)420	}421	/// `super` with `self` overriden for top-level lookups.422	/// Exists when super appears outside of `super.field`/`"field" in super` expressions423	/// Exclusive to jrsonnet.424	///425	/// Might return `NoSuperFound` error.426	pub fn standalone_super(&self) -> Result<ObjValue> {427		if !self.sup.super_exists() {428			bail!(NoSuperFound)429		}430		let mut out = ObjValue::builder();431		out.reserve_cores(1).extend_with_core(StandaloneSuperCore {432			sup: self.sup,433			this: self.this.clone(),434		});435		Ok(out.build())436	}437	pub fn this(&self) -> &ObjValue {438		&self.this439	}440	pub fn downgrade(self) -> WeakSupThis {441		WeakSupThis {442			sup: self.sup,443			this: self.this.downgrade(),444		}445	}446}447#[derive(Trace, PartialEq, Eq, Hash, Debug)]448pub struct WeakSupThis {449	sup: CoreIdx,450	this: WeakObjValue,451}452453impl ObjValue {454	pub fn builder() -> ObjValueBuilder {455		ObjValueBuilder::new()456	}457	pub fn builder_with_capacity(capacity: usize) -> ObjValueBuilder {458		ObjValueBuilder::with_capacity(capacity)459	}460	pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {461		let mut out = ObjValueBuilder::with_capacity(1);462		out.with_super(self);463		let mut member = out.field(key);464		if value.flags.add() {465			member = member.add();466		}467		if let Some(loc) = value.location {468			member = member.with_location(loc);469		}470		let _ = member471			.with_visibility(value.flags.visibility())472			.binding(value.invoke);473		out.build()474	}475	pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {476		ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())477	}478479	pub fn extend(&mut self) -> ObjValueBuilder {480		let mut out = ObjValueBuilder::new();481		out.with_super(self.clone());482		out483	}484485	#[must_use]486	pub fn extend_from(&self, sup: Self) -> Self {487		let mut cores = Vec::with_capacity(sup.0.cores.len() + self.0.cores.len());488		cores.extend(sup.0.cores.iter().cloned());489		cores.extend(self.0.cores.iter().cloned());490		let has_assertions = sup.0.has_assertions || self.0.has_assertions;491		ObjValue(Cc::new(ObjValueInner {492			cores,493			value_cache: RefCell::default(),494			assertions_ran: Cell::new(!has_assertions),495			has_assertions,496		}))497	}498	// #[must_use]499	// pub fn with_this(&self, this: Self) -> Self {500	// 	self.0.with_this(self.clone(), this)501	// }502	/// Returns amount of visible object fields503	/// If object only contains hidden fields - may return zero.504	pub fn len(&self) -> usize {505		self.fields_visibility()506			.values()507			.filter(|d| d.visible())508			.count()509	}510	/// For each field, calls callback.511	/// If callback returns false - ends iteration prematurely.512	///513	/// Returns false if ended prematurely514	pub fn enum_fields(&self, handler: &mut EnumFieldsHandler<'_>) -> bool {515		let mut super_depth = SuperDepth::default();516		self.enum_fields_idx(517			&mut super_depth,518			handler,519			CoreIdx {520				idx: self.0.cores.len(),521			},522		)523	}524	fn enum_fields_idx(525		&self,526		super_depth: &mut SuperDepth,527		handler: &mut EnumFieldsHandler<'_>,528		idx: CoreIdx,529	) -> bool {530		for core in self.0.cores[..idx.idx].iter().rev() {531			if !core.0.enum_fields_core(super_depth, handler) {532				return false;533			}534			super_depth.deepen();535		}536		true537	}538539	pub fn has_field_include_hidden(&self, name: IStr) -> bool {540		self.has_field_include_hidden_idx(541			name,542			CoreIdx {543				idx: self.0.cores.len(),544			},545		)546	}547	fn has_field_include_hidden_idx(&self, name: IStr, core: CoreIdx) -> bool {548		let mut skip = Saturating(0usize);549		for ele in self.0.cores[..core.idx].iter().rev() {550			match ele.0.has_field_include_hidden_core(name.clone()) {551				HasFieldIncludeHidden::Exists => {552					if skip.0 == 0 {553						return true;554					}555				}556				HasFieldIncludeHidden::Omit(new_skip) => {557					// +1 including this core558					skip = skip.max(new_skip + Saturating(1));559				}560				HasFieldIncludeHidden::NotFound => {}561			}562			skip -= 1;563		}564		false565	}566	pub fn has_field(&self, name: IStr) -> bool {567		match self.field_visibility(name) {568			Some(Visibility::Unhide | Visibility::Normal) => true,569			Some(Visibility::Hidden) | None => false,570		}571	}572	pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {573		if include_hidden {574			self.has_field_include_hidden(name)575		} else {576			self.has_field(name)577		}578	}579	pub fn get(&self, key: IStr) -> Result<Option<Val>> {580		self.get_idx(581			key,582			CoreIdx {583				idx: self.0.cores.len(),584			},585		)586	}587588	fn get_idx(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {589		let cache_key = (key.clone(), core);590		{591			let mut cache = self.0.value_cache.borrow_mut();592			// entry_ref candidate?593			match cache.entry(cache_key.clone()) {594				Entry::Occupied(v) => match v.get() {595					CacheValue::Cached(v) => return v.clone(),596					CacheValue::Pending => {597						if !is_asserting(self) {598							bail!(InfiniteRecursionDetected);599						}600					}601				},602				Entry::Vacant(v) => {603					v.insert(CacheValue::Pending);604				}605			};606		}607		let result = self.get_idx_uncached(key, core);608		{609			let mut cache = self.0.value_cache.borrow_mut();610			cache.insert(cache_key, CacheValue::Cached(result.clone()));611		}612		result613	}614	fn get_idx_uncached(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {615		self.run_assertions()?;616		let mut first_add = None;617		let mut add_stack: Vec<Val> = Vec::new();618		let mut skip = Saturating(0);619		for (sup, core) in self.0.cores[..core.idx].iter().enumerate().rev() {620			let sup_this = SupThis {621				sup: CoreIdx { idx: sup },622				this: self.clone(),623			};624			match core.0.get_for_core(key.clone(), sup_this, skip.0 != 0)? {625				GetFor::Final(val) if first_add.is_none() => {626					if skip.0 == 0 {627						return Ok(Some(val));628					}629				}630				GetFor::Final(val) => {631					if skip.0 == 0 {632						add_stack.push(val);633						break;634					}635				}636				GetFor::SuperPlus(val) => {637					if skip.0 == 0 {638						if first_add.is_none() {639							first_add = Some(val);640						} else {641							add_stack.push(val);642						}643					}644				}645				GetFor::Omit(new_skip) => {646					skip = skip.max(new_skip + Saturating(1));647				}648				GetFor::NotFound => {}649			}650			skip -= 1;651		}652		let Some(first) = first_add else {653			if add_stack.is_empty() {654				return Ok(None);655			}656			return Ok(Some(add_stack.pop().expect("single element on stack")));657		};658		if add_stack.is_empty() {659			return Ok(Some(first));660		}661		add_stack.insert(0, first);662		let mut values = add_stack.into_iter().rev();663		let init = values.next().expect("at least 2 elements");664665		values666			.try_fold(init, |a, b| evaluate_add_op(&a, &b))667			.map(Some)668	}669670	pub fn get_or_bail(&self, key: IStr) -> Result<Val> {671		let Some(value) = self.get(key.clone())? else {672			let suggestions = suggest_object_fields(self, key.clone());673			bail!(NoSuchField(key, suggestions))674		};675		Ok(value)676	}677678	fn field_visibility(&self, field: IStr) -> Option<Visibility> {679		self.field_visibility_idx(680			field,681			CoreIdx {682				idx: self.0.cores.len(),683			},684		)685	}686	fn field_visibility_idx(&self, field: IStr, core: CoreIdx) -> Option<Visibility> {687		let mut exists = false;688		let mut skip = Saturating(0usize);689		for ele in self.0.cores[..core.idx].iter().rev() {690			let vis = ele.0.field_visibility_core(field.clone());691			match vis {692				FieldVisibility::Found(vis @ (Visibility::Unhide | Visibility::Hidden)) => {693					if skip.0 == 0 {694						return Some(vis);695					}696				}697				FieldVisibility::Found(Visibility::Normal) => {698					if skip.0 == 0 {699						exists = true;700					}701				}702				FieldVisibility::NotFound => {}703				FieldVisibility::Omit(new_skip) => {704					// +1 including this core705					skip = skip.max(new_skip + Saturating(1));706				}707			}708			skip -= 1;709		}710		exists.then_some(Visibility::Normal)711	}712713	pub fn run_assertions(&self) -> Result<()> {714		if self.0.assertions_ran.get() {715			return Ok(());716		}717		if !start_asserting(self) {718			return Ok(());719		}720		for (idx, ele) in self.0.cores.iter().enumerate() {721			let sup_this = SupThis {722				sup: CoreIdx { idx },723				this: self.clone(),724			};725			ele.0.run_assertions_core(sup_this).inspect_err(|_e| {726				finish_asserting(self);727			})?;728		}729		finish_asserting(self);730		self.0.assertions_ran.set(true);731		Ok(())732	}733734	pub fn iter(735		&self,736		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,737	) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {738		let fields = self.fields(739			#[cfg(feature = "exp-preserve-order")]740			preserve_order,741		);742		fields.into_iter().map(|field| {743			(744				field.clone(),745				self.get(field)746					.map(|opt| opt.expect("iterating over keys, field exists")),747			)748		})749	}750	pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {751		#[derive(Trace)]752		struct ObjFieldThunk {753			obj: ObjValue,754			key: IStr,755		}756		impl ThunkValue for ObjFieldThunk {757			type Output = Val;758759			fn get(&self) -> Result<Self::Output> {760				self.obj761					.get(self.key.clone())762					.transpose()763					.expect("field existence checked")764			}765		}766767		if !self.has_field_ex(key.clone(), true) {768			return None;769		}770771		Some(Thunk::new(ObjFieldThunk {772			obj: self.clone(),773			key,774		}))775	}776	pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {777		#[derive(Trace)]778		struct ObjFieldThunk {779			obj: ObjValue,780			key: IStr,781		}782		impl ThunkValue for ObjFieldThunk {783			type Output = Val;784785			fn get(&self) -> Result<Self::Output> {786				self.obj.get_or_bail(self.key.clone())787			}788		}789790		Thunk::new(ObjFieldThunk {791			obj: self.clone(),792			key,793		})794	}795	pub(crate) fn as_standalone(&self) -> StandaloneSuperCore {796		StandaloneSuperCore {797			sup: CoreIdx {798				idx: self.0.cores.len(),799			},800			this: self.clone(),801		}802	}803	pub fn ptr_eq(a: &Self, b: &Self) -> bool {804		Cc::ptr_eq(&a.0, &b.0)805	}806	pub fn downgrade(self) -> WeakObjValue {807		WeakObjValue(self.0.downgrade())808	}809}810811#[derive(Debug)]812struct FieldVisibilityData {813	omitted_until: Saturating<usize>,814	exists_visible: Option<Visibility>,815	#[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]816	key: FieldSortKey,817}818impl FieldVisibilityData {819	fn visible(&self) -> bool {820		self.exists_visible821			.expect("non-existing fields shall be dropped at the end of fn fields_visibility()")822			.is_visible()823	}824	#[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]825	fn sort_key(&self) -> FieldSortKey {826		self.key827	}828}829830impl ObjValue {831	fn fields_visibility(&self) -> FxHashMap<IStr, FieldVisibilityData> {832		let mut out = FxHashMap::default();833834		let mut super_depth = SuperDepth::default();835		let mut omit_index = Saturating(0);836		for core in self.0.cores.iter().rev() {837			core.0838				.enum_fields_core(&mut super_depth, &mut |depth, index, name, visibility| {839					let entry = out.entry(name);840					let data = entry.or_insert_with(|| FieldVisibilityData {841						exists_visible: None,842						key: FieldSortKey::new(depth, index),843						omitted_until: omit_index,844					});845					match visibility {846						EnumFields::Omit(new_skip) => {847							// +1 including this core848							data.omitted_until = data849								.omitted_until850								.max(omit_index + new_skip + Saturating(1));851						}852						EnumFields::Normal(Visibility::Normal) => {853							if data.omitted_until <= omit_index && data.exists_visible.is_none() {854								data.exists_visible = Some(Visibility::Normal);855							}856						}857						EnumFields::Normal(Visibility::Hidden) => {858							if data.omitted_until <= omit_index {859								data.exists_visible = Some(match data.exists_visible {860									// We're iterating in reverse, later unhide is preserved861									Some(Visibility::Unhide) => Visibility::Unhide,862									_ => Visibility::Hidden,863								});864							}865						}866						EnumFields::Normal(Visibility::Unhide) => {867							if data.omitted_until <= omit_index {868								data.exists_visible = Some(match data.exists_visible {869									// We're iterating in reverse, later hide is preserved870									Some(Visibility::Hidden) => Visibility::Hidden,871									_ => Visibility::Unhide,872								});873							}874						}875					}876					ControlFlow::Continue(())877				});878879			super_depth.deepen();880			omit_index += 1;881		}882883		out.retain(|_, v| v.exists_visible.is_some());884885		out886	}887	pub fn fields_ex(888		&self,889		include_hidden: bool,890		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,891	) -> Vec<IStr> {892		#[cfg(feature = "exp-preserve-order")]893		if preserve_order {894			let (mut fields, mut keys): (Vec<_>, Vec<_>) = self895				.fields_visibility()896				.into_iter()897				.filter(|(_, d)| include_hidden || d.visible())898				.enumerate()899				.map(|(idx, (k, d))| (k, (d.sort_key(), idx)))900				.unzip();901			keys.sort_unstable_by_key(|v| v.0);902			// Reorder in-place by resulting indexes903			for i in 0..fields.len() {904				let x = fields[i].clone();905				let mut j = i;906				loop {907					let k = keys[j].1;908					keys[j].1 = j;909					if k == i {910						break;911					}912					fields[j] = fields[k].clone();913					j = k;914				}915				fields[j] = x;916			}917			return fields;918		}919920		let mut fields: Vec<_> = self921			.fields_visibility()922			.into_iter()923			.filter(|(_, d)| include_hidden || d.visible())924			.map(|(k, _)| k)925			.collect();926		fields.sort_unstable();927		fields928	}929	pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {930		self.fields_ex(931			false,932			#[cfg(feature = "exp-preserve-order")]933			preserve_order,934		)935	}936	pub fn values_ex(937		&self,938		include_hidden: bool,939		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,940	) -> ArrValue {941		ArrValue::new(PickObjectValues::new(942			self.clone(),943			self.fields_ex(944				include_hidden,945				#[cfg(feature = "exp-preserve-order")]946				preserve_order,947			),948		))949	}950	pub fn values(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> ArrValue {951		self.values_ex(952			false,953			#[cfg(feature = "exp-preserve-order")]954			preserve_order,955		)956	}957	pub fn key_values_ex(958		&self,959		include_hidden: bool,960		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,961	) -> ArrValue {962		ArrValue::new(PickObjectKeyValues::new(963			self.clone(),964			self.fields_ex(965				include_hidden,966				#[cfg(feature = "exp-preserve-order")]967				preserve_order,968			),969		))970	}971	pub fn key_values(972		&self,973		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,974	) -> ArrValue {975		self.key_values_ex(976			false,977			#[cfg(feature = "exp-preserve-order")]978			preserve_order,979		)980	}981}982983#[allow(clippy::module_name_repetitions)]984#[must_use = "value not added unless binding() was called"]985pub struct ObjMemberBuilder<Kind> {986	kind: Kind,987	name: IStr,988	add: bool,989	visibility: Visibility,990	original_index: FieldIndex,991	location: Option<Span>,992}993994#[allow(clippy::missing_const_for_fn)]995impl<Kind> ObjMemberBuilder<Kind> {996	pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {997		Self {998			kind,999			name,1000			original_index,1001			add: false,1002			visibility: Visibility::Normal,1003			location: None,1004		}1005	}10061007	pub const fn with_add(mut self, add: bool) -> Self {1008		self.add = add;1009		self1010	}1011	pub fn add(self) -> Self {1012		self.with_add(true)1013	}1014	pub fn with_visibility(mut self, visibility: Visibility) -> Self {1015		self.visibility = visibility;1016		self1017	}1018	pub fn hide(self) -> Self {1019		self.with_visibility(Visibility::Hidden)1020	}1021	pub fn with_location(mut self, location: Span) -> Self {1022		self.location = Some(location);1023		self1024	}1025	fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {1026		(1027			self.kind,1028			self.name,1029			ObjMember {1030				flags: ObjFieldFlags::new(self.add, self.visibility),1031				original_index: self.original_index,1032				invoke: binding,1033				location: self.location,1034			},1035		)1036	}1037}10381039pub struct ExtendBuilder<'v>(&'v mut ObjValue);1040impl ObjMemberBuilder<ExtendBuilder<'_>> {1041	pub fn value(self, value: impl Into<Val>) {1042		self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));1043	}1044	pub fn bindable(self, bindable: impl Unbound<Bound = Val>) {1045		self.binding(MaybeUnbound::Unbound(CcUnbound::new(bindable)));1046	}1047	pub fn binding(self, binding: MaybeUnbound) {1048		let (receiver, name, member) = self.build_member(binding);1049		let new = receiver.0.clone();1050		*receiver.0 = new.extend_with_raw_member(name, member);1051	}1052}
after · 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	identity_hash,30	operator::evaluate_add_op,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)]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	#[trace(skip)]139	flags: ObjFieldFlags,140	original_index: FieldIndex,141	pub invoke: MaybeUnbound,142	pub location: Option<Span>,143}144145cc_dyn!(CcObjectAssertion, ObjectAssertion);146pub trait ObjectAssertion: Trace {147	fn run(&self, sup_this: SupThis) -> Result<()>;148}149150// Field => This151152#[derive(Trace, Debug)]153enum CacheValue {154	Cached(Result<Option<Val>>),155	Pending,156}157158pub type EnumFieldsHandler<'a> =159	dyn FnMut(SuperDepth, FieldIndex, IStr, EnumFields) -> ControlFlow<()> + 'a;160161#[derive(Debug)]162pub enum EnumFields {163	Normal(Visibility),164	Omit(Skip),165}166167#[derive(Trace, Clone)]168pub enum GetFor {169	// Return value170	Final(Val),171	// Continue iterating over cores, add current value to sum stack172	SuperPlus(Val),173	// Ignore the field value, stop at this layer instead174	Omit(#[trace(skip)] Skip),175	NotFound,176}177178#[derive(Acyclic, Clone)]179pub enum FieldVisibility {180	Found(Visibility),181	Omit(Skip),182	NotFound,183}184185#[derive(Acyclic, Clone)]186pub enum HasFieldIncludeHidden {187	Exists,188	NotFound,189	Omit(Skip),190}191192type Skip = Saturating<usize>;193194pub trait ObjectCore: Trace + Any + Debug {195	// If callback returns false, iteration stops, and this call returns false.196	fn enum_fields_core(197		&self,198		super_depth: &mut SuperDepth,199		handler: &mut EnumFieldsHandler<'_>,200	) -> bool;201202	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden;203204	fn get_for_core(&self, key: IStr, sup_this: SupThis, omit_only: bool) -> Result<GetFor>;205	fn field_visibility_core(&self, field: IStr) -> FieldVisibility;206207	fn run_assertions_core(&self, sup_this: SupThis) -> Result<()>;208}209210#[derive(Clone, Trace)]211pub struct WeakObjValue(#[trace(skip)] Weak<ObjValueInner>);212impl Debug for WeakObjValue {213	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {214		f.debug_tuple("WeakObjValue").finish()215	}216}217218impl PartialEq for WeakObjValue {219	fn eq(&self, other: &Self) -> bool {220		Weak::ptr_eq(&self.0, &other.0)221	}222}223224impl Eq for WeakObjValue {}225impl Hash for WeakObjValue {226	fn hash<H: Hasher>(&self, hasher: &mut H) {227		// Safety: usize is POD228		let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };229		hasher.write_usize(addr);230	}231}232233cc_dyn!(234	#[derive(Clone, Debug)]235	CcObjectCore, ObjectCore,236	pub fn new() {...}237);238#[derive(Trace, Educe)]239#[educe(Debug)]240struct ObjValueInner {241	cores: Vec<CcObjectCore>,242	assertions_ran: Cell<bool>,243	has_assertions: bool,244	value_cache: RefCell<FxHashMap<(IStr, CoreIdx), CacheValue>>,245}246247thread_local! {248	static RUNNING_ASSERTIONS: RefCell<FxHashSet<ObjValue>> = RefCell::default();249}250fn is_asserting(obj: &ObjValue) -> bool {251	RUNNING_ASSERTIONS.with_borrow(|v| v.contains(obj))252}253/// Returns false if already asserting254fn start_asserting(obj: &ObjValue) -> bool {255	RUNNING_ASSERTIONS.with_borrow_mut(|v| v.insert(obj.clone()))256}257fn finish_asserting(obj: &ObjValue) {258	RUNNING_ASSERTIONS.with_borrow_mut(|v| {259		let r = v.remove(obj);260		debug_assert!(261			r,262			"finish_asserting was called before start_asserting or twice"263		);264	});265}266267thread_local! {268	static EMPTY_OBJ: ObjValue = ObjValue(Cc::new(ObjValueInner {269		cores: vec![],270		assertions_ran: Cell::new(true),271		has_assertions: false,272		value_cache: RefCell::default(),273	}))274}275276#[allow(clippy::module_name_repetitions)]277#[derive(Clone, Trace, Debug, Educe)]278#[educe(PartialEq, Hash, Eq)]279pub struct ObjValue(280	#[educe(PartialEq(method(Cc::ptr_eq)), Hash(method(identity_hash)))] Cc<ObjValueInner>,281);282283impl ObjValue {284	pub fn empty() -> Self {285		EMPTY_OBJ.with(Clone::clone)286	}287	pub fn is_empty(&self) -> bool {288		self.0.cores.is_empty() || self.len() == 0289	}290}291292#[derive(Trace, Debug)]293pub(crate) struct StandaloneSuperCore {294	sup: CoreIdx,295	this: ObjValue,296}297impl ObjectCore for StandaloneSuperCore {298	fn enum_fields_core(299		&self,300		super_depth: &mut SuperDepth,301		handler: &mut EnumFieldsHandler<'_>,302	) -> bool {303		self.this.enum_fields_idx(super_depth, handler, self.sup)304	}305306	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {307		if self.this.has_field_include_hidden_idx(name, self.sup) {308			HasFieldIncludeHidden::Exists309		} else {310			HasFieldIncludeHidden::NotFound311		}312	}313314	fn get_for_core(&self, key: IStr, _sup_this: SupThis, omit_only: bool) -> Result<GetFor> {315		if omit_only {316			return Ok(GetFor::NotFound);317		}318		let v = self.this.get_idx(key, self.sup)?;319		Ok(v.map_or(GetFor::NotFound, GetFor::Final))320	}321322	fn field_visibility_core(&self, field: IStr) -> FieldVisibility {323		self.this324			.field_visibility_idx(field, self.sup)325			.map_or(FieldVisibility::NotFound, FieldVisibility::Found)326	}327328	fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {329		self.this.run_assertions()330	}331}332333#[derive(Debug, Acyclic)]334struct OmitFieldsCore {335	omit: FxHashSet<IStr>,336	prev_layers: usize,337}338impl ObjectCore for OmitFieldsCore {339	fn enum_fields_core(340		&self,341		super_depth: &mut SuperDepth,342		handler: &mut EnumFieldsHandler<'_>,343	) -> bool {344		let mut fi = FieldIndex::default();345		for f in &self.omit {346			if handler(347				*super_depth,348				fi,349				f.clone(),350				EnumFields::Omit(Saturating(self.prev_layers)),351			) == ControlFlow::Break(())352			{353				return false;354			}355			fi = fi.next();356		}357		true358	}359360	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {361		if self.omit.contains(&name) {362			return HasFieldIncludeHidden::Omit(Saturating(self.prev_layers));363		}364		HasFieldIncludeHidden::NotFound365	}366367	fn get_for_core(&self, key: IStr, _sup_this: SupThis, _omit_only: bool) -> Result<GetFor> {368		if self.omit.contains(&key) {369			return Ok(GetFor::Omit(Saturating(self.prev_layers)));370		}371		Ok(GetFor::NotFound)372	}373374	fn field_visibility_core(&self, field: IStr) -> FieldVisibility {375		if self.omit.contains(&field) {376			return FieldVisibility::Omit(Saturating(self.prev_layers));377		}378		FieldVisibility::NotFound379	}380381	fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {382		Ok(())383	}384}385386#[derive(Hash, PartialEq, Eq, Trace, Clone, Copy, Debug)]387struct CoreIdx {388	idx: usize,389}390impl CoreIdx {391	fn super_exists(self) -> bool {392		self.idx != 0393	}394}395#[derive(Trace, Clone, PartialEq, Eq, Hash, Debug)]396pub struct SupThis {397	sup: CoreIdx,398	this: ObjValue,399}400impl SupThis {401	pub fn has_super(&self) -> bool {402		self.sup.super_exists()403	}404	/// Implementation of `"field" in super` operation,405	/// works faster than standalone super path.406	///407	/// In case of no `super` existence, returns false.408	pub fn field_in_super(&self, field: IStr) -> bool {409		self.this.has_field_include_hidden_idx(field, self.sup)410	}411	/// Implementation of `super.field` operation,412	/// works faster than standalone super path.413	///414	/// In case of no `super` existence, returns `NoSuperFound`415	pub fn get_super(&self, field: IStr) -> Result<Option<Val>> {416		if !self.sup.super_exists() {417			bail!(NoSuperFound);418		}419		self.this.get_idx(field, self.sup)420	}421	/// `super` with `self` overriden for top-level lookups.422	/// Exists when super appears outside of `super.field`/`"field" in super` expressions423	/// Exclusive to jrsonnet.424	///425	/// Might return `NoSuperFound` error.426	pub fn standalone_super(&self) -> Result<ObjValue> {427		if !self.sup.super_exists() {428			bail!(NoSuperFound)429		}430		let mut out = ObjValue::builder();431		out.reserve_cores(1).extend_with_core(StandaloneSuperCore {432			sup: self.sup,433			this: self.this.clone(),434		});435		Ok(out.build())436	}437	pub fn this(&self) -> &ObjValue {438		&self.this439	}440	pub fn downgrade(self) -> WeakSupThis {441		WeakSupThis {442			sup: self.sup,443			this: self.this.downgrade(),444		}445	}446}447#[derive(Trace, PartialEq, Eq, Hash, Debug)]448pub struct WeakSupThis {449	sup: CoreIdx,450	this: WeakObjValue,451}452453impl ObjValue {454	pub fn builder() -> ObjValueBuilder {455		ObjValueBuilder::new()456	}457	pub fn builder_with_capacity(capacity: usize) -> ObjValueBuilder {458		ObjValueBuilder::with_capacity(capacity)459	}460	pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {461		let mut out = ObjValueBuilder::with_capacity(1);462		out.with_super(self);463		let mut member = out.field(key);464		if value.flags.add() {465			member = member.add();466		}467		if let Some(loc) = value.location {468			member = member.with_location(loc);469		}470		let _ = member471			.with_visibility(value.flags.visibility())472			.binding(value.invoke);473		out.build()474	}475	pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {476		ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())477	}478479	pub fn extend(&mut self) -> ObjValueBuilder {480		let mut out = ObjValueBuilder::new();481		out.with_super(self.clone());482		out483	}484485	#[must_use]486	pub fn extend_from(&self, sup: Self) -> Self {487		let mut cores = Vec::with_capacity(sup.0.cores.len() + self.0.cores.len());488		cores.extend(sup.0.cores.iter().cloned());489		cores.extend(self.0.cores.iter().cloned());490		let has_assertions = sup.0.has_assertions || self.0.has_assertions;491		ObjValue(Cc::new(ObjValueInner {492			cores,493			value_cache: RefCell::default(),494			assertions_ran: Cell::new(!has_assertions),495			has_assertions,496		}))497	}498	// #[must_use]499	// pub fn with_this(&self, this: Self) -> Self {500	// 	self.0.with_this(self.clone(), this)501	// }502	/// Returns amount of visible object fields503	/// If object only contains hidden fields - may return zero.504	pub fn len(&self) -> usize {505		self.fields_visibility()506			.values()507			.filter(|d| d.visible())508			.count()509	}510	/// For each field, calls callback.511	/// If callback returns false - ends iteration prematurely.512	///513	/// Returns false if ended prematurely514	pub fn enum_fields(&self, handler: &mut EnumFieldsHandler<'_>) -> bool {515		let mut super_depth = SuperDepth::default();516		self.enum_fields_idx(517			&mut super_depth,518			handler,519			CoreIdx {520				idx: self.0.cores.len(),521			},522		)523	}524	fn enum_fields_idx(525		&self,526		super_depth: &mut SuperDepth,527		handler: &mut EnumFieldsHandler<'_>,528		idx: CoreIdx,529	) -> bool {530		for core in self.0.cores[..idx.idx].iter().rev() {531			if !core.0.enum_fields_core(super_depth, handler) {532				return false;533			}534			super_depth.deepen();535		}536		true537	}538539	pub fn has_field_include_hidden(&self, name: IStr) -> bool {540		self.has_field_include_hidden_idx(541			name,542			CoreIdx {543				idx: self.0.cores.len(),544			},545		)546	}547	fn has_field_include_hidden_idx(&self, name: IStr, core: CoreIdx) -> bool {548		let mut skip = Saturating(0usize);549		for ele in self.0.cores[..core.idx].iter().rev() {550			match ele.0.has_field_include_hidden_core(name.clone()) {551				HasFieldIncludeHidden::Exists => {552					if skip.0 == 0 {553						return true;554					}555				}556				HasFieldIncludeHidden::Omit(new_skip) => {557					// +1 including this core558					skip = skip.max(new_skip + Saturating(1));559				}560				HasFieldIncludeHidden::NotFound => {}561			}562			skip -= 1;563		}564		false565	}566	pub fn has_field(&self, name: IStr) -> bool {567		match self.field_visibility(name) {568			Some(Visibility::Unhide | Visibility::Normal) => true,569			Some(Visibility::Hidden) | None => false,570		}571	}572	pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {573		if include_hidden {574			self.has_field_include_hidden(name)575		} else {576			self.has_field(name)577		}578	}579	pub fn get(&self, key: IStr) -> Result<Option<Val>> {580		self.get_idx(581			key,582			CoreIdx {583				idx: self.0.cores.len(),584			},585		)586	}587588	fn get_idx(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {589		let cache_key = (key.clone(), core);590		{591			let mut cache = self.0.value_cache.borrow_mut();592			// entry_ref candidate?593			match cache.entry(cache_key.clone()) {594				Entry::Occupied(v) => match v.get() {595					CacheValue::Cached(v) => return v.clone(),596					CacheValue::Pending => {597						if !is_asserting(self) {598							bail!(InfiniteRecursionDetected);599						}600					}601				},602				Entry::Vacant(v) => {603					v.insert(CacheValue::Pending);604				}605			};606		}607		let result = self.get_idx_uncached(key, core);608		{609			let mut cache = self.0.value_cache.borrow_mut();610			cache.insert(cache_key, CacheValue::Cached(result.clone()));611		}612		result613	}614	fn get_idx_uncached(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {615		self.run_assertions()?;616		let mut first_add = None;617		let mut add_stack: Vec<Val> = Vec::new();618		let mut skip = Saturating(0);619		for (sup, core) in self.0.cores[..core.idx].iter().enumerate().rev() {620			let sup_this = SupThis {621				sup: CoreIdx { idx: sup },622				this: self.clone(),623			};624			match core.0.get_for_core(key.clone(), sup_this, skip.0 != 0)? {625				GetFor::Final(val) if first_add.is_none() => {626					if skip.0 == 0 {627						return Ok(Some(val));628					}629				}630				GetFor::Final(val) => {631					if skip.0 == 0 {632						add_stack.push(val);633						break;634					}635				}636				GetFor::SuperPlus(val) => {637					if skip.0 == 0 {638						if first_add.is_none() {639							first_add = Some(val);640						} else {641							add_stack.push(val);642						}643					}644				}645				GetFor::Omit(new_skip) => {646					skip = skip.max(new_skip + Saturating(1));647				}648				GetFor::NotFound => {}649			}650			skip -= 1;651		}652		let Some(first) = first_add else {653			if add_stack.is_empty() {654				return Ok(None);655			}656			return Ok(Some(add_stack.pop().expect("single element on stack")));657		};658		if add_stack.is_empty() {659			return Ok(Some(first));660		}661		add_stack.insert(0, first);662		let mut values = add_stack.into_iter().rev();663		let init = values.next().expect("at least 2 elements");664665		values666			.try_fold(init, |a, b| evaluate_add_op(&a, &b))667			.map(Some)668	}669670	pub fn get_or_bail(&self, key: IStr) -> Result<Val> {671		let Some(value) = self.get(key.clone())? else {672			let suggestions = suggest_object_fields(self, key.clone());673			bail!(NoSuchField(key, suggestions))674		};675		Ok(value)676	}677678	fn field_visibility(&self, field: IStr) -> Option<Visibility> {679		self.field_visibility_idx(680			field,681			CoreIdx {682				idx: self.0.cores.len(),683			},684		)685	}686	fn field_visibility_idx(&self, field: IStr, core: CoreIdx) -> Option<Visibility> {687		let mut exists = false;688		let mut skip = Saturating(0usize);689		for ele in self.0.cores[..core.idx].iter().rev() {690			let vis = ele.0.field_visibility_core(field.clone());691			match vis {692				FieldVisibility::Found(vis @ (Visibility::Unhide | Visibility::Hidden)) => {693					if skip.0 == 0 {694						return Some(vis);695					}696				}697				FieldVisibility::Found(Visibility::Normal) => {698					if skip.0 == 0 {699						exists = true;700					}701				}702				FieldVisibility::NotFound => {}703				FieldVisibility::Omit(new_skip) => {704					// +1 including this core705					skip = skip.max(new_skip + Saturating(1));706				}707			}708			skip -= 1;709		}710		exists.then_some(Visibility::Normal)711	}712713	pub fn run_assertions(&self) -> Result<()> {714		if self.0.assertions_ran.get() {715			return Ok(());716		}717		if !start_asserting(self) {718			return Ok(());719		}720		for (idx, ele) in self.0.cores.iter().enumerate() {721			let sup_this = SupThis {722				sup: CoreIdx { idx },723				this: self.clone(),724			};725			ele.0.run_assertions_core(sup_this).inspect_err(|_e| {726				finish_asserting(self);727			})?;728		}729		finish_asserting(self);730		self.0.assertions_ran.set(true);731		Ok(())732	}733734	pub fn iter(735		&self,736		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,737	) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {738		let fields = self.fields(739			#[cfg(feature = "exp-preserve-order")]740			preserve_order,741		);742		fields.into_iter().map(|field| {743			(744				field.clone(),745				self.get(field)746					.map(|opt| opt.expect("iterating over keys, field exists")),747			)748		})749	}750	pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {751		#[derive(Trace)]752		struct ObjFieldThunk {753			obj: ObjValue,754			key: IStr,755		}756		impl ThunkValue for ObjFieldThunk {757			type Output = Val;758759			fn get(&self) -> Result<Self::Output> {760				self.obj761					.get(self.key.clone())762					.transpose()763					.expect("field existence checked")764			}765		}766767		if !self.has_field_ex(key.clone(), true) {768			return None;769		}770771		Some(Thunk::new(ObjFieldThunk {772			obj: self.clone(),773			key,774		}))775	}776	pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {777		#[derive(Trace)]778		struct ObjFieldThunk {779			obj: ObjValue,780			key: IStr,781		}782		impl ThunkValue for ObjFieldThunk {783			type Output = Val;784785			fn get(&self) -> Result<Self::Output> {786				self.obj.get_or_bail(self.key.clone())787			}788		}789790		Thunk::new(ObjFieldThunk {791			obj: self.clone(),792			key,793		})794	}795796	#[allow(dead_code, reason = "used in object ...rest destructuring")]797	pub(crate) fn as_standalone(&self) -> StandaloneSuperCore {798		StandaloneSuperCore {799			sup: CoreIdx {800				idx: self.0.cores.len(),801			},802			this: self.clone(),803		}804	}805	pub fn ptr_eq(a: &Self, b: &Self) -> bool {806		Cc::ptr_eq(&a.0, &b.0)807	}808	pub fn downgrade(self) -> WeakObjValue {809		WeakObjValue(self.0.downgrade())810	}811}812813#[derive(Debug)]814struct FieldVisibilityData {815	omitted_until: Saturating<usize>,816	exists_visible: Option<Visibility>,817	#[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]818	key: FieldSortKey,819}820impl FieldVisibilityData {821	fn visible(&self) -> bool {822		self.exists_visible823			.expect("non-existing fields shall be dropped at the end of fn fields_visibility()")824			.is_visible()825	}826	#[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]827	fn sort_key(&self) -> FieldSortKey {828		self.key829	}830}831832impl ObjValue {833	fn fields_visibility(&self) -> FxHashMap<IStr, FieldVisibilityData> {834		let mut out = FxHashMap::default();835836		let mut super_depth = SuperDepth::default();837		let mut omit_index = Saturating(0);838		for core in self.0.cores.iter().rev() {839			core.0840				.enum_fields_core(&mut super_depth, &mut |depth, index, name, visibility| {841					let entry = out.entry(name);842					let data = entry.or_insert_with(|| FieldVisibilityData {843						exists_visible: None,844						key: FieldSortKey::new(depth, index),845						omitted_until: omit_index,846					});847					match visibility {848						EnumFields::Omit(new_skip) => {849							// +1 including this core850							data.omitted_until = data851								.omitted_until852								.max(omit_index + new_skip + Saturating(1));853						}854						EnumFields::Normal(Visibility::Normal) => {855							if data.omitted_until <= omit_index && data.exists_visible.is_none() {856								data.exists_visible = Some(Visibility::Normal);857							}858						}859						EnumFields::Normal(Visibility::Hidden) => {860							if data.omitted_until <= omit_index {861								data.exists_visible = Some(match data.exists_visible {862									// We're iterating in reverse, later unhide is preserved863									Some(Visibility::Unhide) => Visibility::Unhide,864									_ => Visibility::Hidden,865								});866							}867						}868						EnumFields::Normal(Visibility::Unhide) => {869							if data.omitted_until <= omit_index {870								data.exists_visible = Some(match data.exists_visible {871									// We're iterating in reverse, later hide is preserved872									Some(Visibility::Hidden) => Visibility::Hidden,873									_ => Visibility::Unhide,874								});875							}876						}877					}878					ControlFlow::Continue(())879				});880881			super_depth.deepen();882			omit_index += 1;883		}884885		out.retain(|_, v| v.exists_visible.is_some());886887		out888	}889	pub fn fields_ex(890		&self,891		include_hidden: bool,892		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,893	) -> Vec<IStr> {894		#[cfg(feature = "exp-preserve-order")]895		if preserve_order {896			let (mut fields, mut keys): (Vec<_>, Vec<_>) = self897				.fields_visibility()898				.into_iter()899				.filter(|(_, d)| include_hidden || d.visible())900				.enumerate()901				.map(|(idx, (k, d))| (k, (d.sort_key(), idx)))902				.unzip();903			keys.sort_unstable_by_key(|v| v.0);904			// Reorder in-place by resulting indexes905			for i in 0..fields.len() {906				let x = fields[i].clone();907				let mut j = i;908				loop {909					let k = keys[j].1;910					keys[j].1 = j;911					if k == i {912						break;913					}914					fields[j] = fields[k].clone();915					j = k;916				}917				fields[j] = x;918			}919			return fields;920		}921922		let mut fields: Vec<_> = self923			.fields_visibility()924			.into_iter()925			.filter(|(_, d)| include_hidden || d.visible())926			.map(|(k, _)| k)927			.collect();928		fields.sort_unstable();929		fields930	}931	pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {932		self.fields_ex(933			false,934			#[cfg(feature = "exp-preserve-order")]935			preserve_order,936		)937	}938	pub fn values_ex(939		&self,940		include_hidden: bool,941		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,942	) -> ArrValue {943		ArrValue::new(PickObjectValues::new(944			self.clone(),945			self.fields_ex(946				include_hidden,947				#[cfg(feature = "exp-preserve-order")]948				preserve_order,949			),950		))951	}952	pub fn values(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> ArrValue {953		self.values_ex(954			false,955			#[cfg(feature = "exp-preserve-order")]956			preserve_order,957		)958	}959	pub fn key_values_ex(960		&self,961		include_hidden: bool,962		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,963	) -> ArrValue {964		ArrValue::new(PickObjectKeyValues::new(965			self.clone(),966			self.fields_ex(967				include_hidden,968				#[cfg(feature = "exp-preserve-order")]969				preserve_order,970			),971		))972	}973	pub fn key_values(974		&self,975		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,976	) -> ArrValue {977		self.key_values_ex(978			false,979			#[cfg(feature = "exp-preserve-order")]980			preserve_order,981		)982	}983}984985#[allow(clippy::module_name_repetitions)]986#[must_use = "value not added unless binding() was called"]987pub struct ObjMemberBuilder<Kind> {988	kind: Kind,989	name: IStr,990	add: bool,991	visibility: Visibility,992	original_index: FieldIndex,993	location: Option<Span>,994}995996#[allow(clippy::missing_const_for_fn)]997impl<Kind> ObjMemberBuilder<Kind> {998	pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {999		Self {1000			kind,1001			name,1002			original_index,1003			add: false,1004			visibility: Visibility::Normal,1005			location: None,1006		}1007	}10081009	pub const fn with_add(mut self, add: bool) -> Self {1010		self.add = add;1011		self1012	}1013	pub fn add(self) -> Self {1014		self.with_add(true)1015	}1016	pub fn with_visibility(mut self, visibility: Visibility) -> Self {1017		self.visibility = visibility;1018		self1019	}1020	pub fn hide(self) -> Self {1021		self.with_visibility(Visibility::Hidden)1022	}1023	pub fn with_location(mut self, location: Span) -> Self {1024		self.location = Some(location);1025		self1026	}1027	fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {1028		(1029			self.kind,1030			self.name,1031			ObjMember {1032				flags: ObjFieldFlags::new(self.add, self.visibility),1033				original_index: self.original_index,1034				invoke: binding,1035				location: self.location,1036			},1037		)1038	}1039}10401041pub struct ExtendBuilder<'v>(&'v mut ObjValue);1042impl ObjMemberBuilder<ExtendBuilder<'_>> {1043	pub fn value(self, value: impl Into<Val>) {1044		self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));1045	}1046	pub fn bindable(self, bindable: impl Unbound<Bound = Val>) {1047		self.binding(MaybeUnbound::Unbound(CcUnbound::new(bindable)));1048	}1049	pub fn binding(self, binding: MaybeUnbound) {1050		let (receiver, name, member) = self.build_member(binding);1051		let new = receiver.0.clone();1052		*receiver.0 = new.extend_with_raw_member(name, member);1053	}1054}
modifiedcrates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/format.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/format.rs
@@ -1,5 +1,10 @@
 //! faster std.format impl
 #![allow(clippy::too_many_arguments)]
+#![expect(
+	clippy::cast_possible_truncation,
+	clippy::cast_sign_loss,
+	reason = "many safe integer casts, behavior on overflow is not specified"
+)]
 
 use jrsonnet_gcmodule::Trace;
 use jrsonnet_interner::IStr;
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -129,6 +129,7 @@
 			} else {
 				false
 			};
+			#[expect(clippy::cast_possible_truncation, reason = "code is limited by 4gb")]
 			let mut location = path
 				.map_source_locations(&[offset as u32])
 				.into_iter()
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -157,7 +157,9 @@
 	}
 }
 
+#[expect(clippy::cast_precision_loss, reason = "checked to not overflow")]
 pub const MAX_SAFE_INTEGER: f64 = ((1u64 << (f64::MANTISSA_DIGITS)) - 1) as f64;
+#[expect(clippy::cast_precision_loss, reason = "checked to not overflow")]
 pub const MIN_SAFE_INTEGER: f64 = (-((1i64 << (f64::MANTISSA_DIGITS)) - 1)) as f64;
 
 macro_rules! impl_int {
@@ -179,6 +181,7 @@
 								stringify!($ty)
 							)
 						}
+						#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation, reason = "checked by TYPE")]
 						Ok(n as Self)
 					}
 					_ => unreachable!(),
@@ -198,6 +201,7 @@
 macro_rules! impl_bounded_int {
 	($($name:ident = $ty:ty)*) => {$(
 		#[derive(Clone, Copy)]
+		#[allow(clippy::cast_possible_truncation, reason = "overflow is api misuse")]
 		pub struct $name<const MIN: $ty, const MAX: $ty>($ty);
 		impl<const MIN: $ty, const MAX: $ty> $name<MIN, MAX> {
 			pub const fn new(value: $ty) -> Option<$name<MIN, MAX>> {
@@ -219,6 +223,7 @@
 		}
 
 		impl<const MIN: $ty, const MAX: $ty> Typed for $name<MIN, MAX> {
+			#[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss, reason = "overflow is api misuse")]
 			const TYPE: &'static ComplexValType =
 				&ComplexValType::BoundedNumber(
 					Some(MIN as f64),
@@ -239,6 +244,7 @@
 								stringify!($ty)
 							)
 						}
+						#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss, reason = "overflow is api misuse, the range is checked by TYPE")]
 						Ok(Self(n as $ty))
 					}
 					_ => unreachable!(),
@@ -318,6 +324,11 @@
 				if n.trunc() != n {
 					bail!("cannot convert number with fractional part to usize")
 				}
+				#[allow(
+					clippy::cast_possible_truncation,
+					clippy::cast_sign_loss,
+					reason = "the range is checked by TYPE"
+				)]
 				Ok(n as Self)
 			}
 			_ => unreachable!(),
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -295,8 +295,10 @@
 				};
 				let mut get_idx = |pos: Option<i32>, default| {
 					match pos {
-						Some(v) if v < 0 => get_len().saturating_sub((-v) as usize),
+						#[expect(clippy::cast_sign_loss, reason = "abs value is used")]
+						Some(v) if v < 0 => get_len().saturating_sub((-v as isize) as usize),
 						// No need to clamp, as iterator interface is used
+						#[expect(clippy::cast_sign_loss, reason = "abs value is used")]
 						Some(v) => v as usize,
 						None => default,
 					}
@@ -322,6 +324,10 @@
 			Self::Arr(arr) => Ok(Self::Arr(arr.clone().slice(
 				index,
 				end,
+				#[expect(
+					clippy::cast_possible_truncation,
+					reason = "overflow will result with skip too large which would be equivalent"
+				)]
 				step.map(|v| NonZeroU32::new(v.value() as u32).expect("bounded != 0")),
 			))),
 		}
@@ -446,6 +452,7 @@
 		if self.0 < MIN_SAFE_INTEGER || self.0 > MAX_SAFE_INTEGER {
 			bail!("numberic value outside of safe integer range for bitwise operation");
 		}
+		#[expect(clippy::cast_possible_truncation, reason = "intended")]
 		Ok(self.0 as i64)
 	}
 }
@@ -520,6 +527,7 @@
 			type Error = ConvertNumValueError;
 			#[inline]
 			fn try_from(value: $ty) -> Result<Self, ConvertNumValueError> {
+				#[expect(clippy::cast_precision_loss, reason = "precision loss is explicitly handled")]
 				let value = value as f64;
 				if value < MIN_SAFE_INTEGER {
 					return Err(ConvertNumValueError::Underflow)
modifiedcrates/jrsonnet-interner/src/inner.rsdiffbeforeafterboth
--- a/crates/jrsonnet-interner/src/inner.rs
+++ b/crates/jrsonnet-interner/src/inner.rs
@@ -67,7 +67,7 @@
 			.cast();
 			assert!(!data.is_null());
 			*data = InnerHeader::new(bytes.len().try_into().expect("bytes > 4GB"), is_utf8);
-			ptr::copy_nonoverlapping(bytes.as_ptr(), data.offset(1).cast::<u8>(), bytes.len());
+			ptr::copy_nonoverlapping(bytes.as_ptr(), data.add(1).cast::<u8>(), bytes.len());
 			Self(UnsafeCell::new(NonNull::new_unchecked(data)))
 		}
 	}
@@ -89,10 +89,7 @@
 		let size = unsafe { (*header).size };
 		// SAFETY: bytes after data is allocated to be exactly data.size in length
 		unsafe {
-			slice::from_raw_parts(
-				(*self.0.get()).as_ptr().offset(1).cast::<u8>(),
-				size as usize,
-			)
+			slice::from_raw_parts((*self.0.get()).as_ptr().add(1).cast::<u8>(), size as usize)
 		}
 	}
 
@@ -156,7 +153,7 @@
 	}
 	pub fn as_ptr(this: &Self) -> *const u8 {
 		// SAFETY: data is initialized
-		unsafe { (*this.0.get()).as_ptr().offset(1).cast() }
+		unsafe { (*this.0.get()).as_ptr().add(1).cast() }
 	}
 
 	pub fn strong_count(this: &Self) -> u32 {
modifiedcrates/jrsonnet-ir-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-ir-parser/src/lib.rs
+++ b/crates/jrsonnet-ir-parser/src/lib.rs
@@ -638,6 +638,7 @@
 	}
 }
 
+#[allow(clippy::too_many_lines)]
 fn expr_basic(p: &mut Parser<'_>) -> Result<Expr> {
 	if let Some(lit) = literal(p) {
 		return Ok(Expr::Literal(lit));
@@ -764,7 +765,6 @@
 		}
 
 		SyntaxKind::IDENT => {
-			let text = p.text();
 			let n = spanned(p, |p| {
 				let s: IStr = p.text().into();
 				p.eat_any();
@@ -1005,8 +1005,9 @@
 }
 
 pub fn string_to_expr(s: IStr, settings: &ParserSettings) -> Spanned<Expr> {
-	let len = s.len();
-	Spanned::new(Expr::Str(s), Span(settings.source.clone(), 0, len as u32))
+	let len = u32::try_from(s.len()).expect("code size is limited by 4gb");
+
+	Spanned::new(Expr::Str(s), Span(settings.source.clone(), 0, len))
 }
 
 #[cfg(test)]
modifiedcrates/jrsonnet-lexer/src/lex.rsdiffbeforeafterboth
--- a/crates/jrsonnet-lexer/src/lex.rs
+++ b/crates/jrsonnet-lexer/src/lex.rs
@@ -60,7 +60,10 @@
 			range: {
 				let Range { start, end } = self.inner.span();
 
-				Span(start as u32, end as u32)
+				Span(
+					u32::try_from(start).expect("code size is limited by 4gb"),
+					u32::try_from(end).expect("code size is limited by 4gb"),
+				)
 			},
 		})
 	}
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -17,7 +17,11 @@
 }
 
 #[builtin]
-pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {
+pub fn builtin_make_array(
+	// Can't use usize because range_exclusive is over i32
+	sz: BoundedI32<0, { i32::MAX }>,
+	func: FuncVal,
+) -> Result<ArrValue> {
 	if *sz == 0 {
 		return Ok(ArrValue::empty());
 	}
@@ -25,6 +29,7 @@
 		// TODO: Different mapped array impl avoiding allocating unnecessary vals
 		|| Ok(ArrValue::range_exclusive(0, *sz).map(FromUntyped::from_untyped(Val::Func(func))?)),
 		|trivial| {
+			#[expect(clippy::cast_sign_loss, reason = "sz is bounded to be larger than 0")]
 			let mut out = Vec::with_capacity(*sz as usize);
 			for _ in 0..*sz {
 				out.push(trivial.clone());
@@ -363,6 +368,10 @@
 	if arr.is_empty() {
 		return eval_on_empty(onEmpty);
 	}
+	#[expect(
+		clippy::cast_precision_loss,
+		reason = "array sizes are bounded to i32 len"
+	)]
 	Ok(Val::try_num(arr.iter().sum::<f64>() / (arr.len() as f64))?)
 }
 
@@ -378,6 +387,11 @@
 pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {
 	for (index, item) in arr.iter().enumerate() {
 		if equals(&item?, &elem)? {
+			#[expect(
+				clippy::cast_possible_truncation,
+				clippy::cast_possible_wrap,
+				reason = "array sizes are bounded to i32 len"
+			)]
 			return builtin_remove_at(arr.clone(), index as i32);
 		}
 	}
modifiedcrates/jrsonnet-stdlib/src/math.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/math.rs
+++ b/crates/jrsonnet-stdlib/src/math.rs
@@ -120,6 +120,7 @@
 		let lg = s.abs().log2();
 		let x = (lg - lg.floor() - 1.0).exp2();
 		let exp = lg.floor() + 1.0;
+		#[expect(clippy::cast_possible_truncation, reason = "exponent can fit in i16")]
 		(s.signum() * x, exp as i16)
 	}
 }
modifiedflake.nixdiffbeforeafterboth
--- a/flake.nix
+++ b/flake.nix
@@ -66,6 +66,7 @@
               "clippy"
               "rustc"
               "rust-src"
+              "rust-analyzer"
             ])
             rustfmt
           ];