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

difftreelog

refactor reenable clippy integer cast checks

slqpxoouYaroslav Bolyukin2026-04-04parent: #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
--- a/crates/jrsonnet-evaluator/src/obj/mod.rs
+++ b/crates/jrsonnet-evaluator/src/obj/mod.rs
@@ -792,6 +792,8 @@
 			key,
 		})
 	}
+
+	#[allow(dead_code, reason = "used in object ...rest destructuring")]
 	pub(crate) fn as_standalone(&self) -> StandaloneSuperCore {
 		StandaloneSuperCore {
 			sup: CoreIdx {
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
before · crates/jrsonnet-evaluator/src/val.rs
1use std::{2	cell::RefCell,3	cmp::Ordering,4	fmt::{self, Debug, Display},5	marker::PhantomData,6	mem::replace,7	num::NonZeroU32,8	ops::Deref,9	rc::Rc,10};1112use jrsonnet_gcmodule::{Acyclic, Cc, Trace, cc_dyn};13use jrsonnet_interner::IStr;14pub use jrsonnet_macros::Thunk;15use jrsonnet_types::ValType;16use rustc_hash::FxHashMap;17use thiserror::Error;1819pub use crate::arr::{ArrValue, ArrayLike};20use crate::{21	ObjValue, Result, SupThis, Unbound, WeakSupThis, bail,22	error::{Error, ErrorKind::*},23	function::FuncVal,24	gc::WithCapacityExt as _,25	manifest::{ManifestFormat, ToStringFormat},26	typed::{BoundedUsize, MAX_SAFE_INTEGER, MIN_SAFE_INTEGER},27};2829pub trait ThunkValue: Trace {30	type Output;31	fn get(&self) -> Result<Self::Output>;32}3334#[derive(Trace)]35enum MemoizedClusureThunkInner<D: Trace, T: Trace> {36	Computed(T),37	Errored(Error),38	Waiting {39		env: D,40		// Carries no data, as it is not a real closure, all the41		// captured environment is stored in `env` field.42		#[trace(skip)]43		closure: fn(D) -> Result<T>,44	},45	Pending,46}47#[derive(Trace)]48pub struct MemoizedClosureThunk<D: Trace, T: Trace>(RefCell<MemoizedClusureThunkInner<D, T>>);49impl<D: Trace, T: Trace> MemoizedClosureThunk<D, T> {50	pub fn new(env: D, closure: fn(D) -> Result<T>) -> Self {51		Self(RefCell::new(MemoizedClusureThunkInner::Waiting {52			env,53			closure,54		}))55	}56}5758impl<D: Trace, T: Trace + Clone> ThunkValue for MemoizedClosureThunk<D, T> {59	type Output = T;6061	fn get(&self) -> Result<Self::Output> {62		match &*self.0.borrow() {63			MemoizedClusureThunkInner::Computed(v) => return Ok(v.clone()),64			MemoizedClusureThunkInner::Errored(e) => return Err(e.clone()),65			MemoizedClusureThunkInner::Pending => return Err(InfiniteRecursionDetected.into()),66			MemoizedClusureThunkInner::Waiting { .. } => (),67		}68		let MemoizedClusureThunkInner::Waiting { env, closure } = replace(69			&mut *self.0.borrow_mut(),70			MemoizedClusureThunkInner::Pending,71		) else {72			unreachable!();73		};74		let new_value = match closure(env) {75			Ok(v) => v,76			Err(e) => {77				*self.0.borrow_mut() = MemoizedClusureThunkInner::Errored(e.clone());78				return Err(e);79			}80		};81		*self.0.borrow_mut() = MemoizedClusureThunkInner::Computed(new_value.clone());82		Ok(new_value)83	}84}8586cc_dyn!(87	/// Lazily evaluated value88	#[derive(Clone)] Thunk<V: Trace>,89	ThunkValue<Output = V>,90	pub fn new() {...}91);9293impl<T: Trace> Thunk<T> {94	pub fn evaluated(val: T) -> Self95	where96		T: Clone,97	{98		#[derive(Trace)]99		struct EvaluatedThunk<T: Trace>(T);100		impl<T> ThunkValue for EvaluatedThunk<T>101		where102			T: Clone + Trace,103		{104			type Output = T;105106			fn get(&self) -> Result<Self::Output> {107				Ok(self.0.clone())108			}109		}110		Self::new(EvaluatedThunk(val))111	}112	pub fn errored(e: Error) -> Self {113		#[derive(Trace)]114		struct ErroredThunk<T: Trace>(Error, PhantomData<T>);115		impl<T> ThunkValue for ErroredThunk<T>116		where117			T: Trace,118		{119			type Output = T;120121			fn get(&self) -> Result<Self::Output> {122				Err(self.0.clone())123			}124		}125		Self::new(ErroredThunk(e, PhantomData))126	}127	pub fn result(res: Result<T, Error>) -> Self128	where129		T: Clone,130	{131		match res {132			Ok(o) => Self::evaluated(o),133			Err(e) => Self::errored(e),134		}135	}136}137138impl<T> Thunk<T>139where140	T: Clone + Trace,141{142	pub fn force(&self) -> Result<()> {143		self.evaluate()?;144		Ok(())145	}146147	/// Evaluate thunk, or return cached value148	///149	/// # Errors150	///151	/// - Lazy value evaluation returned error152	/// - This method was called during inner value evaluation153	pub fn evaluate(&self) -> Result<T> {154		self.0.get()155	}156}157158pub trait ThunkMapper<Input>: Trace {159	type Output;160	fn map(self, from: Input) -> Result<Self::Output>;161}162impl<Input> Thunk<Input>163where164	Input: Trace + Clone,165{166	pub fn map<M>(self, mapper: M) -> Thunk<M::Output>167	where168		M: ThunkMapper<Input>,169		M::Output: Trace + Clone,170	{171		let inner = self;172		Thunk!(move || {173			let value = inner.evaluate()?;174			let mapped = mapper.map(value)?;175			Ok(mapped)176		})177	}178}179180impl<T: Trace + Clone> From<Result<T>> for Thunk<T> {181	fn from(value: Result<T>) -> Self {182		match value {183			Ok(o) => Self::evaluated(o),184			Err(e) => Self::errored(e),185		}186	}187}188impl<T, V: Trace> From<T> for Thunk<V>189where190	T: ThunkValue<Output = V>,191{192	fn from(value: T) -> Self {193		Self::new(value)194	}195}196197impl<T: Trace + Default + Clone> Default for Thunk<T> {198	fn default() -> Self {199		Self::evaluated(T::default())200	}201}202203#[derive(Trace, Clone)]204pub struct CachedUnbound<I, T>205where206	I: Unbound<Bound = T>,207	T: Trace,208{209	cache: Cc<RefCell<FxHashMap<WeakSupThis, T>>>,210	value: I,211}212impl<I: Unbound<Bound = T>, T: Trace> CachedUnbound<I, T> {213	pub fn new(value: I) -> Self {214		Self {215			cache: Cc::new(RefCell::new(FxHashMap::new())),216			value,217		}218	}219}220impl<I: Unbound<Bound = T>, T: Clone + Trace> Unbound for CachedUnbound<I, T> {221	type Bound = T;222	fn bind(&self, sup_this: SupThis) -> Result<T> {223		let cache_key = sup_this.clone().downgrade();224		{225			if let Some(t) = self.cache.borrow().get(&cache_key) {226				return Ok(t.clone());227			}228		}229		let bound = self.value.bind(sup_this)?;230231		{232			let mut cache = self.cache.borrow_mut();233			cache.insert(cache_key, bound.clone());234		}235236		Ok(bound)237	}238}239240impl<T: Debug + Trace> Debug for Thunk<T> {241	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {242		write!(f, "Lazy")243	}244}245impl<T: Trace> PartialEq for Thunk<T> {246	fn eq(&self, other: &Self) -> bool {247		Cc::ptr_eq(&self.0, &other.0)248	}249}250251/// Represents a Jsonnet value, which can be sliced or indexed (string or array).252#[allow(clippy::module_name_repetitions)]253pub enum IndexableVal {254	/// String.255	Str(IStr),256	/// Array.257	Arr(ArrValue),258}259impl IndexableVal {260	pub fn is_empty(&self) -> bool {261		match self {262			Self::Str(s) => s.is_empty(),263			Self::Arr(s) => s.is_empty(),264		}265	}266267	pub fn to_array(self) -> ArrValue {268		match self {269			Self::Str(s) => ArrValue::chars(s.chars()),270			Self::Arr(arr) => arr,271		}272	}273	/// Slice the value.274	///275	/// # Implementation276	///277	/// For strings, will create a copy of specified interval.278	///279	/// For arrays, nothing will be copied on this call, instead [`ArrValue::Slice`] view will be returned.280	pub fn slice(281		self,282		index: Option<i32>,283		end: Option<i32>,284		step: Option<BoundedUsize<1, { i32::MAX as usize }>>,285	) -> Result<Self> {286		match &self {287			Self::Str(s) => {288				let mut computed_len = None;289				let mut get_len = || {290					computed_len.unwrap_or_else(|| {291						let len = s.chars().count();292						let _ = computed_len.insert(len);293						len294					})295				};296				let mut get_idx = |pos: Option<i32>, default| {297					match pos {298						Some(v) if v < 0 => get_len().saturating_sub((-v) as usize),299						// No need to clamp, as iterator interface is used300						Some(v) => v as usize,301						None => default,302					}303				};304305				let index = get_idx(index, 0);306				let end = get_idx(end, usize::MAX);307				let step = step.as_deref().copied().unwrap_or(1);308309				if index >= end {310					return Ok(Self::Str("".into()));311				}312313				Ok(Self::Str(314					(s.chars()315						.skip(index)316						.take(end - index)317						.step_by(step)318						.collect::<String>())319					.into(),320				))321			}322			Self::Arr(arr) => Ok(Self::Arr(arr.clone().slice(323				index,324				end,325				step.map(|v| NonZeroU32::new(v.value() as u32).expect("bounded != 0")),326			))),327		}328	}329}330331#[derive(Debug, Clone, Acyclic)]332pub enum StrValue {333	Flat(IStr),334	Tree(Rc<(StrValue, StrValue, usize)>),335}336impl StrValue {337	pub fn concat(a: Self, b: Self) -> Self {338		// TODO: benchmark for an optimal value, currently just a arbitrary choice339		const STRING_EXTEND_THRESHOLD: usize = 100;340341		if a.is_empty() {342			b343		} else if b.is_empty() {344			a345		} else if a.len() + b.len() < STRING_EXTEND_THRESHOLD {346			Self::Flat(format!("{a}{b}").into())347		} else {348			let len = a.len() + b.len();349			Self::Tree(Rc::new((a, b, len)))350		}351	}352	pub fn into_flat(self) -> IStr {353		#[cold]354		fn write_buf(s: &StrValue, out: &mut String) {355			match s {356				StrValue::Flat(f) => out.push_str(f),357				StrValue::Tree(t) => {358					write_buf(&t.0, out);359					write_buf(&t.1, out);360				}361			}362		}363		match self {364			Self::Flat(f) => f,365			Self::Tree(_) => {366				let mut buf = String::with_capacity(self.len());367				write_buf(&self, &mut buf);368				buf.into()369			}370		}371	}372	pub fn len(&self) -> usize {373		match self {374			Self::Flat(v) => v.len(),375			Self::Tree(t) => t.2,376		}377	}378	pub fn is_empty(&self) -> bool {379		match self {380			Self::Flat(v) => v.is_empty(),381			// Can't create non-flat empty string382			Self::Tree(_) => false,383		}384	}385}386impl<T> From<T> for StrValue387where388	IStr: From<T>,389{390	fn from(value: T) -> Self {391		Self::Flat(IStr::from(value))392	}393}394impl Display for StrValue {395	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {396		match self {397			Self::Flat(v) => write!(f, "{v}"),398			Self::Tree(t) => {399				write!(f, "{}", t.0)?;400				write!(f, "{}", t.1)401			}402		}403	}404}405impl PartialEq for StrValue {406	// False positive, into_flat returns not StrValue, but IStr, thus no infinite recursion here.407	#[allow(clippy::unconditional_recursion)]408	fn eq(&self, other: &Self) -> bool {409		let a = self.clone().into_flat();410		let b = other.clone().into_flat();411		a == b412	}413}414impl Eq for StrValue {}415impl PartialOrd for StrValue {416	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {417		Some(self.cmp(other))418	}419}420impl Ord for StrValue {421	fn cmp(&self, other: &Self) -> Ordering {422		let a = self.clone().into_flat();423		let b = other.clone().into_flat();424		a.cmp(&b)425	}426}427428/// Represents jsonnet number429/// Jsonnet numbers are finite f64, with NaNs disallowed430#[derive(Trace, Clone, Copy)]431#[repr(transparent)]432pub struct NumValue(f64);433impl NumValue {434	/// Creates a [`NumValue`], if value is finite and not NaN435	pub fn new(v: f64) -> Option<Self> {436		if !v.is_finite() {437			return None;438		}439		Some(Self(v))440	}441	#[inline]442	pub const fn get(&self) -> f64 {443		self.0444	}445	pub(crate) fn truncate_for_bitwise(self) -> Result<i64> {446		if self.0 < MIN_SAFE_INTEGER || self.0 > MAX_SAFE_INTEGER {447			bail!("numberic value outside of safe integer range for bitwise operation");448		}449		Ok(self.0 as i64)450	}451}452impl PartialEq for NumValue {453	fn eq(&self, other: &Self) -> bool {454		self.0 == other.0455	}456}457impl Eq for NumValue {}458impl Ord for NumValue {459	#[inline]460	fn cmp(&self, other: &Self) -> Ordering {461		// Can't use `total_cmp`: its behavior for `-0` and `0`462		// is not following wanted.463		unsafe { self.0.partial_cmp(&other.0).unwrap_unchecked() }464	}465}466impl PartialOrd for NumValue {467	#[inline]468	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {469		Some(self.cmp(other))470	}471}472impl Debug for NumValue {473	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {474		Debug::fmt(&self.0, f)475	}476}477impl Display for NumValue {478	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {479		Display::fmt(&self.0, f)480	}481}482impl Deref for NumValue {483	type Target = f64;484485	#[inline]486	fn deref(&self) -> &Self::Target {487		&self.0488	}489}490macro_rules! impl_num {491	($($ty:ty),+) => {$(492		impl From<$ty> for NumValue {493			#[inline]494			fn from(value: $ty) -> Self {495				Self(value.into())496			}497		}498	)+};499}500impl_num!(i8, u8, i16, u16, i32, u32);501502#[derive(Clone, Copy, Debug, Error, Trace)]503pub enum ConvertNumValueError {504	#[error("overflow")]505	Overflow,506	#[error("underflow")]507	Underflow,508	#[error("non-finite")]509	NonFinite,510}511impl From<ConvertNumValueError> for Error {512	fn from(e: ConvertNumValueError) -> Self {513		Self::new(e.into())514	}515}516517macro_rules! impl_try_num {518	($($ty:ty),+) => {$(519		impl TryFrom<$ty> for NumValue {520			type Error = ConvertNumValueError;521			#[inline]522			fn try_from(value: $ty) -> Result<Self, ConvertNumValueError> {523				let value = value as f64;524				if value < MIN_SAFE_INTEGER {525					return Err(ConvertNumValueError::Underflow)526				} else if value > MAX_SAFE_INTEGER {527					return Err(ConvertNumValueError::Overflow)528				}529				// Number is finite.530				Ok(Self(value))531			}532		}533	)+};534}535impl_try_num!(usize, isize, i64, u64);536537impl TryFrom<f64> for NumValue {538	type Error = ConvertNumValueError;539540	#[inline]541	fn try_from(value: f64) -> Result<Self, Self::Error> {542		Self::new(value).ok_or(ConvertNumValueError::NonFinite)543	}544}545impl TryFrom<f32> for NumValue {546	type Error = ConvertNumValueError;547548	#[inline]549	fn try_from(value: f32) -> Result<Self, Self::Error> {550		Self::new(f64::from(value)).ok_or(ConvertNumValueError::NonFinite)551	}552}553554/// Represents any valid Jsonnet value.555#[derive(Debug, Clone, Trace, Default)]556pub enum Val {557	/// Represents a Jsonnet boolean.558	Bool(bool),559	/// Represents a Jsonnet null value.560	#[default]561	Null,562	/// Represents a Jsonnet string.563	Str(StrValue),564	/// Represents a Jsonnet number.565	/// Should be finite, and not NaN566	/// This restriction isn't enforced by enum, as enum field can't be marked as private567	Num(NumValue),568	/// Experimental bigint569	#[cfg(feature = "exp-bigint")]570	BigInt(#[trace(skip)] Box<num_bigint::BigInt>),571	/// Represents a Jsonnet array.572	Arr(ArrValue),573	/// Represents a Jsonnet object.574	Obj(ObjValue),575	/// Represents a Jsonnet function.576	Func(FuncVal),577}578579#[cfg(target_pointer_width = "64")]580static_assertions::assert_eq_size!(Val, [u8; 24]);581582impl From<IndexableVal> for Val {583	fn from(v: IndexableVal) -> Self {584		match v {585			IndexableVal::Str(s) => Self::string(s),586			IndexableVal::Arr(a) => Self::Arr(a),587		}588	}589}590591impl Val {592	pub const fn as_bool(&self) -> Option<bool> {593		match self {594			Self::Bool(v) => Some(*v),595			_ => None,596		}597	}598	pub const fn as_null(&self) -> Option<()> {599		match self {600			Self::Null => Some(()),601			_ => None,602		}603	}604	pub fn as_str(&self) -> Option<IStr> {605		match self {606			Self::Str(s) => Some(s.clone().into_flat()),607			_ => None,608		}609	}610	pub const fn as_num(&self) -> Option<f64> {611		match self {612			Self::Num(n) => Some(n.get()),613			_ => None,614		}615	}616	#[cfg(feature = "exp-bigint")]617	pub fn as_bigint(&self) -> Option<num_bigint::BigInt> {618		match self {619			Self::BigInt(n) => Some(*n.clone()),620			_ => None,621		}622	}623	pub fn as_arr(&self) -> Option<ArrValue> {624		match self {625			Self::Arr(a) => Some(a.clone()),626			_ => None,627		}628	}629	pub fn as_obj(&self) -> Option<ObjValue> {630		match self {631			Self::Obj(o) => Some(o.clone()),632			_ => None,633		}634	}635	pub fn as_func(&self) -> Option<FuncVal> {636		match self {637			Self::Func(f) => Some(f.clone()),638			_ => None,639		}640	}641642	pub const fn value_type(&self) -> ValType {643		match self {644			Self::Str(..) => ValType::Str,645			Self::Num(..) => ValType::Num,646			#[cfg(feature = "exp-bigint")]647			Self::BigInt(..) => ValType::BigInt,648			Self::Arr(..) => ValType::Arr,649			Self::Obj(..) => ValType::Obj,650			Self::Bool(_) => ValType::Bool,651			Self::Null => ValType::Null,652			Self::Func(..) => ValType::Func,653		}654	}655656	pub fn manifest(&self, format: impl ManifestFormat) -> Result<String> {657		fn manifest_dyn(val: &Val, manifest: &dyn ManifestFormat) -> Result<String> {658			manifest.manifest(val.clone())659		}660		manifest_dyn(self, &format)661	}662663	pub fn to_string(&self) -> Result<IStr> {664		Ok(match self {665			Self::Bool(true) => "true".into(),666			Self::Bool(false) => "false".into(),667			Self::Null => "null".into(),668			Self::Str(s) => s.clone().into_flat(),669			_ => self.manifest(ToStringFormat).map(IStr::from)?,670		})671	}672673	pub fn into_indexable(self) -> Result<IndexableVal> {674		Ok(match self {675			Self::Str(s) => IndexableVal::Str(s.into_flat()),676			Self::Arr(arr) => IndexableVal::Arr(arr),677			_ => bail!(ValueIsNotIndexable(self.value_type())),678		})679	}680681	pub fn function(function: impl Into<FuncVal>) -> Self {682		Self::Func(function.into())683	}684	pub fn string(string: impl Into<StrValue>) -> Self {685		Self::Str(string.into())686	}687	pub fn num(num: impl Into<NumValue>) -> Self {688		Self::Num(num.into())689	}690	pub fn try_num<V, E>(num: V) -> Result<Self, E>691	where692		NumValue: TryFrom<V, Error = E>,693	{694		Ok(Self::Num(num.try_into()?))695	}696}697698impl From<IStr> for Val {699	fn from(value: IStr) -> Self {700		Self::string(value)701	}702}703impl From<String> for Val {704	fn from(value: String) -> Self {705		Self::string(value)706	}707}708impl From<&str> for Val {709	fn from(value: &str) -> Self {710		Self::string(value)711	}712}713impl From<ObjValue> for Val {714	fn from(value: ObjValue) -> Self {715		Self::Obj(value)716	}717}718719const fn is_function_like(val: &Val) -> bool {720	matches!(val, Val::Func(_))721}722723/// Native implementation of `std.primitiveEquals`724pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {725	Ok(match (val_a, val_b) {726		(Val::Bool(a), Val::Bool(b)) => a == b,727		(Val::Null, Val::Null) => true,728		(Val::Str(a), Val::Str(b)) => a == b,729		(Val::Num(a), Val::Num(b)) => (a.get() - b.get()).abs() <= f64::EPSILON,730		#[cfg(feature = "exp-bigint")]731		(Val::BigInt(a), Val::BigInt(b)) => a == b,732		(Val::Arr(_), Val::Arr(_)) => {733			bail!("primitiveEquals operates on primitive types, got array")734		}735		(Val::Obj(_), Val::Obj(_)) => {736			bail!("primitiveEquals operates on primitive types, got object")737		}738		(a, b) if is_function_like(a) && is_function_like(b) => {739			bail!("cannot test equality of functions")740		}741		(_, _) => false,742	})743}744745/// Native implementation of `std.equals`746pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {747	if val_a.value_type() != val_b.value_type() {748		return Ok(false);749	}750	match (val_a, val_b) {751		(Val::Arr(a), Val::Arr(b)) => {752			if ArrValue::ptr_eq(a, b) {753				return Ok(true);754			}755			if a.len() != b.len() {756				return Ok(false);757			}758			for (a, b) in a.iter().zip(b.iter()) {759				if !equals(&a?, &b?)? {760					return Ok(false);761				}762			}763			Ok(true)764		}765		(Val::Obj(a), Val::Obj(b)) => {766			if ObjValue::ptr_eq(a, b) {767				return Ok(true);768			}769			let fields = a.fields(770				#[cfg(feature = "exp-preserve-order")]771				false,772			);773			if fields774				!= b.fields(775					#[cfg(feature = "exp-preserve-order")]776					false,777				) {778				return Ok(false);779			}780			for field in fields {781				if !equals(782					&a.get(field.clone())?.expect("field exists"),783					&b.get(field)?.expect("field exists"),784				)? {785					return Ok(false);786				}787			}788			Ok(true)789		}790		(a, b) => Ok(primitive_equals(a, b)?),791	}792}
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
           ];