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

difftreelog

perf static object shapes

ptmvzrqoYaroslav Bolyukin2026-05-08parent: #d0b9ff2.patch.diff
in: master

29 files changed

modifiedcrates/jrsonnet-evaluator/src/analyze.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/analyze.rs
1//! Static analysis of jsonnet IR.2//!3//! Walks the IR tree and produces a lowered IR (`LExpr`) with named locals resolved to numeric [`LocalId`] and4//! dependency analysis markers for every expression describing which objects and locals the expression depends on5//!6//! ```jsonnet7//! {8//!     a: $, // `a` is top-object-dependent.9//!     b: {10//!         // `b` is NOT object-dependent for the top object: it only references11//!         // things inside itself. `b` is built once per top-level object.12//!         a: $,13//!     },14//! }15//! ```1617use std::rc::Rc;1819use drop_bomb::DropBomb;20use jrsonnet_gcmodule::Acyclic;21use jrsonnet_interner::IStr;22use jrsonnet_ir::{23	ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BinaryOpType, BindSpec, CompSpec, Destruct, Expr,24	ExprParams, FieldName, ForSpecData, IfElse, IfSpecData, ImportKind, LiteralType, NumValue,25	ObjBody, ObjComp, ObjMembers, Slice, SliceDesc, Span, Spanned, UnaryOpType, Visibility,26	function::FunctionSignature,27};28use rustc_hash::FxHashMap;29use smallvec::SmallVec;3031use crate::{32	arr::arridx,33	error::{format_found, suggest_names},34};3536#[derive(Debug, Clone, Copy)]37#[must_use]38pub struct AnalysisResult {39	/// Highest object, on which identity the value is dependent. `u32::MAX` = not dependent at all40	pub object_dependent_depth: u32,41	/// Highest local frame, on which this value depends. `u32::MAX` = not dependent at all42	pub local_dependent_depth: u32,43}4445impl Default for AnalysisResult {46	fn default() -> Self {47		Self {48			object_dependent_depth: u32::MAX,49			local_dependent_depth: u32::MAX,50		}51	}52}5354impl AnalysisResult {55	fn depend_on_object(&mut self, depth: u32) {56		if depth < self.object_dependent_depth {57			self.object_dependent_depth = depth;58		}59	}60	fn depend_on_local(&mut self, depth: u32) {61		if depth < self.local_dependent_depth {62			self.local_dependent_depth = depth;63		}64	}65	fn taint_by(&mut self, other: AnalysisResult) {66		self.depend_on_object(other.object_dependent_depth);67		self.depend_on_local(other.local_dependent_depth);68	}69}7071#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Acyclic)]72pub struct LocalId(pub u32);7374impl LocalId {75	fn idx(self) -> usize {76		self.0 as usize77	}78	fn defined_before(self, other: Self) -> bool {79		self.0 < other.080	}81}8283#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Acyclic)]84pub enum LSlot {85	/// Enclosing frame locals (sibling letrec, params, etc.).86	Local(LocalSlot),87	/// Enclosing closure's capture pack.88	Capture(CaptureSlot),89}9091#[derive(Debug, Acyclic)]92pub struct ClosureShape {93	pub captures: Box<[LSlot]>,94	pub n_locals: u16,95}9697struct LocalDefinition {98	name: IStr,99	span: Option<Span>,100	/// At which frame depth this local was defined101	defined_at_depth: u32,102	/// Min frame depth, at which this local was used. `u32::MAX` = not used at all.103	/// This check won't catch unused argument closures, i.e:104	/// ```jsonnet105	/// local106	///     a = b,107	///     b = a,108	/// ; 2 + 2109	///110	/// ```111	/// Both `a` and `b` here are "used", but the whole closure was not used here.112	used_at_depth: u32,113	/// Used as part of the current frame closure114	used_by_sibling: bool,115	/// Analysys result for value of this local116	analysis: AnalysisResult,117	/// Has `analysis` been filled in?118	/// For sanity checking, locals are initialized in batchs, use `first_uninitialized_local`119	analyzed: bool,120	/// During walk over uninitialized vars, we can't refer to analysis results of other locals,121	/// but we need to. To make that work, for each variable in variable frame we capture its closure,122	/// by looking at referenced variables.123	scratch_referenced: bool,124}125126impl LocalDefinition {127	fn use_at(&mut self, depth: u32) {128		if depth == self.defined_at_depth {129			self.used_by_sibling = true;130			return;131		}132		if depth < self.used_at_depth {133			self.used_at_depth = depth;134		}135	}136}137138#[derive(Debug, Acyclic)]139pub enum LExpr {140	Slot(LSlot),141	Trivial(TrivialVal),142	Arr {143		shape: ClosureShape,144		items: Rc<Vec<LExpr>>,145	},146	ArrConst(Rc<Vec<TrivialVal>>),147	ArrComp(Box<LArrComp>),148	Obj(LObjBody),149	ObjExtend(Box<LExpr>, LObjBody),150	UnaryOp(UnaryOpType, Box<LExpr>),151	BinaryOp {152		lhs: Box<LExpr>,153		op: BinaryOpType,154		rhs: Box<LExpr>,155	},156	AssertExpr {157		assert: Rc<LAssertStmt>,158		rest: Box<LExpr>,159	},160	Error(Span, Box<LExpr>),161	LocalExpr(Box<LLocalExpr>),162	Import {163		kind: Spanned<ImportKind>,164		kind_span: Span,165		path: IStr,166	},167	Apply {168		applicable: Box<LExpr>,169		args: Spanned<LArgsDesc>,170		tailstrict: bool,171	},172	Index {173		indexable: Box<LExpr>,174		parts: Vec<LIndexPart>,175	},176	Function(Rc<LFunction>),177	IdentityFunction,178	IfElse {179		cond: Box<LExpr>,180		cond_then: Box<LExpr>,181		cond_else: Option<Box<LExpr>>,182	},183	Slice(Box<LSliceExpr>),184	Super,185186	/// Allows partial evaluation of broken expression tree,187	/// expressions with failed static analysis end up here188	BadLocal(&'static str),189}190191#[derive(Debug, Acyclic)]192pub struct LLocalExpr {193	pub frame_shape: ClosureShape,194	pub binds: Vec<LBind>,195	pub body: LExpr,196}197198#[derive(Debug, Acyclic)]199pub struct LFunction {200	pub name: Option<IStr>,201	pub params: Vec<LParam>,202	pub signature: FunctionSignature,203204	pub body_shape: ClosureShape,205	pub body: Rc<LExpr>,206}207208#[derive(Debug, Acyclic)]209pub struct LParam {210	pub name: Option<IStr>,211	pub destruct: LDestruct,212213	pub default: Option<(ClosureShape, Rc<LExpr>)>,214}215216#[derive(Debug, Acyclic)]217pub struct LBind {218	pub destruct: LDestruct,219	pub value_shape: ClosureShape,220	pub value: Rc<LExpr>,221}222223#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Acyclic)]224pub struct CaptureSlot(pub(crate) u16);225#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Acyclic)]226pub struct LocalSlot(pub(crate) u16);227228#[derive(Debug, Acyclic)]229pub enum LDestruct {230	Full(LocalSlot),231	#[cfg(feature = "exp-destruct")]232	Skip,233	#[cfg(feature = "exp-destruct")]234	Array {235		start: Vec<LDestruct>,236		rest: Option<LDestructRest>,237		end: Vec<LDestruct>,238	},239	#[cfg(feature = "exp-destruct")]240	Object {241		fields: Vec<LDestructField>,242		rest: Option<LDestructRest>,243	},244}245246#[derive(Debug, Clone, Copy, Acyclic)]247pub enum LDestructRest {248	Keep(LocalSlot),249	Drop,250}251252#[derive(Debug, Acyclic)]253pub struct LDestructField {254	pub name: IStr,255	pub into: Option<LDestruct>,256	pub default: Option<(ClosureShape, Rc<LExpr>)>,257}258259impl LDestruct {260	pub fn each_slot<F: FnMut(LocalSlot)>(&self, f: &mut F) {261		match self {262			Self::Full(s) => f(*s),263			#[cfg(feature = "exp-destruct")]264			Self::Skip => {}265			#[cfg(feature = "exp-destruct")]266			Self::Array { start, rest, end } => {267				for d in start {268					d.each_slot(f);269				}270				if let Some(LDestructRest::Keep(s)) = rest {271					f(*s);272				}273				for d in end {274					d.each_slot(f);275				}276			}277			#[cfg(feature = "exp-destruct")]278			Self::Object { fields, rest } => {279				for field in fields {280					if let Some(into) = &field.into {281						into.each_slot(f);282					} else {283						unreachable!("shorthand object destruct must store `into`");284					}285				}286				if let Some(LDestructRest::Keep(s)) = rest {287					f(*s);288				}289			}290		}291	}292293	pub fn slots(&self) -> SmallVec<[LocalSlot; 1]> {294		let mut out = SmallVec::new();295		self.each_slot(&mut |s| out.push(s));296		out297	}298}299300#[derive(Debug, Acyclic)]301pub struct LSliceExpr {302	pub value: LExpr,303	pub start: Option<LExpr>,304	pub end: Option<LExpr>,305	pub step: Option<LExpr>,306}307308#[derive(Debug, Acyclic)]309pub struct LArgsDesc {310	pub unnamed: Vec<Rc<LExpr>>,311	pub names: Vec<IStr>,312	pub values: Vec<Rc<LExpr>>,313}314315#[derive(Debug, Acyclic)]316pub struct LAssertStmt {317	pub cond: Spanned<LExpr>,318	pub message: Option<LExpr>,319}320321#[derive(Debug, Acyclic)]322pub struct LIndexPart {323	pub span: Span,324	pub value: LExpr,325	#[cfg(feature = "exp-null-coaelse")]326	pub null_coaelse: bool,327}328329#[derive(Debug, Acyclic)]330pub enum LObjBody {331	MemberList(LObjMembers),332	ObjComp(Box<LObjComp>),333}334335#[derive(Debug, Acyclic)]336pub struct LObjMembers {337	pub frame_shape: ClosureShape,338	/// If current object identity (`super`/`this`/`$`) is used, `this` should339	/// be saved to the specified local slot.340	pub this: Option<LocalSlot>,341	/// Set if dollar should also be assigned to object identity, `this` should also be set (TODO: proper type-level validation)342	pub set_dollar: bool,343	/// True iff `super` is referenced by this object's members.344	pub uses_super: bool,345346	pub locals: Rc<Vec<LBind>>,347	pub asserts: Option<Rc<LObjAsserts>>,348	pub fields: Vec<LFieldMember>,349}350351#[derive(Debug, Acyclic)]352pub struct LObjComp {353	pub frame_shape: Rc<ClosureShape>,354	pub this: Option<LocalSlot>,355	pub set_dollar: bool,356	pub uses_super: bool,357358	pub locals: Rc<Vec<LBind>>,359	pub field: LFieldMember,360	pub compspecs: Vec<LCompSpec>,361}362363#[derive(Debug, Acyclic)]364pub struct LFieldMember {365	pub name: LFieldName,366	pub plus: bool,367	pub visibility: Visibility,368	pub value: Rc<(ClosureShape, LExpr)>,369}370371#[derive(Debug, Acyclic)]372pub struct LClosure<T: Acyclic> {373	pub shape: ClosureShape,374	pub value: T,375}376377#[derive(Debug, Acyclic)]378pub struct LObjAsserts {379	pub shape: ClosureShape,380	pub asserts: Vec<LAssertStmt>,381}382383#[derive(Debug, Acyclic)]384pub enum LFieldName {385	Fixed(IStr),386	Dyn(LExpr),387}388impl LFieldName {389	fn function_name(&self) -> Option<IStr> {390		match self {391			LFieldName::Fixed(istr) => Some(istr.clone()),392			LFieldName::Dyn(_) => None,393		}394	}395}396397#[derive(Debug, Acyclic)]398pub struct LArrComp {399	pub value_shape: ClosureShape,400	pub value: Rc<LExpr>,401	pub compspecs: Vec<LCompSpec>,402}403404#[derive(Debug, Acyclic)]405pub enum LCompSpec {406	If(LExpr),407	For {408		frame_shape: ClosureShape,409		destruct: LDestruct,410		over: LExpr,411		/// Is `over` does not depend on any variable introduced by an earlier for-spec in this comprehension chain412		loop_invariant: bool,413	},414	#[cfg(feature = "exp-object-iteration")]415	ForObj {416		frame_shape: ClosureShape,417		key: LocalSlot,418		visibility: jrsonnet_ir::Visibility,419		value: LDestruct,420		over: LExpr,421		loop_invariant: bool,422	},423}424425struct FrameAlloc<'s> {426	first_in_frame: LocalId,427	stack: &'s mut AnalysisStack,428	bomb: DropBomb,429}430impl<'s> FrameAlloc<'s> {431	fn new(stack: &'s mut AnalysisStack) -> Self {432		FrameAlloc {433			first_in_frame: stack.next_local_id(),434			stack,435			bomb: DropBomb::new("binding frame state"),436		}437	}438439	fn push_locals_closure(&mut self) -> ClosureOnStack {440		self.stack.push_closure_a(self.first_in_frame)441	}442443	fn define_local(&mut self, name: IStr, span: Option<Span>) -> Option<(LocalId, LocalSlot)> {444		let id = self.stack.next_local_id();445		let stack = self.stack.local_by_name.entry(name.clone()).or_default();446		if let Some(&existing) = stack.last()447			&& !existing.defined_before(self.first_in_frame)448		{449			self.stack.report_error(450				format!("local is already defined in the current frame: {name}"),451				span,452			);453			return None;454		}455		stack.push(id);456		self.stack.local_defs.push(LocalDefinition {457			name,458			span,459			defined_at_depth: self.stack.depth,460			used_at_depth: u32::MAX,461			used_by_sibling: false,462			analysis: AnalysisResult::default(),463			analyzed: false,464			scratch_referenced: false,465		});466		let def = self.stack.defining_closure_mut();467		Some((id, def.define_local(id)))468	}469	fn alloc_bind(&mut self, bind: &BindSpec) -> Option<LDestruct> {470		match bind {471			BindSpec::Field { into, .. } => self.alloc_destruct(into),472			BindSpec::Function { name, .. } => {473				let (_, id) = self.define_local(name.value.clone(), Some(name.span.clone()))?;474				Some(LDestruct::Full(id))475			}476		}477	}478	fn alloc_destruct(&mut self, destruct: &Destruct) -> Option<LDestruct> {479		Some(match destruct {480			Destruct::Full(name) => {481				let (_, id) = self.define_local(name.value.clone(), Some(name.span.clone()))?;482				LDestruct::Full(id)483			}484			#[cfg(feature = "exp-destruct")]485			Destruct::Skip => LDestruct::Skip,486			#[cfg(feature = "exp-destruct")]487			Destruct::Array { start, rest, end } => {488				let start = start489					.iter()490					.map(|d| self.alloc_destruct(d))491					.collect::<Option<Vec<_>>>()?;492				let rest = match rest {493					Some(jrsonnet_ir::DestructRest::Keep(name)) => {494						let (_, id) = self.define_local(name.clone(), None)?;495						Some(LDestructRest::Keep(id))496					}497					Some(jrsonnet_ir::DestructRest::Drop) => Some(LDestructRest::Drop),498					None => None,499				};500				let end = end501					.iter()502					.map(|d| self.alloc_destruct(d))503					.collect::<Option<Vec<_>>>()?;504				LDestruct::Array { start, rest, end }505			}506			#[cfg(feature = "exp-destruct")]507			Destruct::Object { fields, rest } => {508				let mut l_fields: Vec<(IStr, LDestruct)> = Vec::with_capacity(fields.len());509				// Allocate destruct LocalIds, then analyse defaults510				for (name, into, _default) in fields {511					let into = if let Some(inner) = into {512						self.alloc_destruct(inner)?513					} else {514						let (_, id) = self.define_local(name.clone(), None)?;515						LDestruct::Full(id)516					};517					l_fields.push((name.clone(), into));518				}519				// All locals exist, so defaults can reference any sibling.520				let l_fields: Vec<LDestructField> = l_fields521					.into_iter()522					.zip(fields.iter())523					.map(|((name, into), (_n, _i, default))| {524						let default = match default {525							Some(e) => {526								let mut default_taint = AnalysisResult::default();527								Some(self.stack.in_using_closure(|stack| {528									Rc::new(analyze(&e.value, stack, &mut default_taint))529								}))530							}531							None => None,532						};533						LDestructField {534							name,535							into: Some(into),536							default,537						}538					})539					.collect();540				let rest = match rest {541					Some(jrsonnet_ir::DestructRest::Keep(name)) => {542						let (_, id) = self.define_local(name.clone(), None)?;543						Some(LDestructRest::Keep(id))544					}545					Some(jrsonnet_ir::DestructRest::Drop) => Some(LDestructRest::Drop),546					None => None,547				};548				LDestruct::Object {549					fields: l_fields,550					rest,551				}552			}553		})554	}555556	fn finish(self) -> PendingInit<'s> {557		let Self {558			first_in_frame,559			stack,560			bomb,561		} = self;562		let first_after_frame = stack.next_local_id();563		PendingInit {564			first_after_frame,565			stack,566			closures: Closures {567				referenced: vec![],568				spec_shapes: vec![],569				first_in_frame,570			},571			bomb,572		}573	}574}575576/// Frame state: `LocalIds` allocated, values not yet analysed.577struct PendingInit<'s> {578	first_after_frame: LocalId,579	stack: &'s mut AnalysisStack,580	closures: Closures,581	bomb: DropBomb,582}583584impl<'s> PendingInit<'s> {585	/// Record the analysis of a spec's value: stamp every id bound by the586	/// spec with `analysis`, collect the spec's same-frame references, and587	/// append them to `closures`.588	fn record_spec_init(&mut self, destruct: &LDestruct, analysis: AnalysisResult) {589		let mut refs: SmallVec<[LocalId; 4]> = SmallVec::new();590		for i in self.closures.first_in_frame.0..self.first_after_frame.0 {591			let def = &mut self.stack.local_defs[i as usize];592			if def.scratch_referenced {593				refs.push(LocalId(i));594				def.scratch_referenced = false;595			}596		}597598		let mut ids_count = 0;599		let first_local = self.stack.top_defining_local();600		destruct.each_slot(&mut |slot| {601			ids_count += 1;602			let id = LocalId(first_local.0 + u32::from(slot.0));603			let def = &mut self.stack.local_defs[id.idx()];604			debug_assert!(!def.analyzed, "sanity: local {:?} analysed twice", def.name);605			def.analysis = analysis;606			def.analyzed = true;607		});608		self.closures.push_spec(ids_count, &refs);609	}610	/// After all specs are analysed, propagate dependency information between611	/// siblings to a fix-point, then switch to "body" mode.612	fn finish(self) -> PendingBody<'s> {613		let Self {614			first_after_frame,615			closures,616			stack,617			bomb,618		} = self;619620		debug_assert_eq!(621			first_after_frame,622			stack.next_local_id(),623			"frame initialisation left unfinished locals"624		);625626		debug_assert_eq!(627			closures.spec_shapes.iter().map(|(_, d)| *d).sum::<usize>(),628			(first_after_frame.0 - closures.first_in_frame.0) as usize,629			"closures destruct-id counts must match frame local count"630		);631632		let mut changed = true;633		while changed {634			changed = false;635			for spec in closures.iter_specs() {636				for id_raw in spec.ids.clone() {637					let user = LocalId(id_raw);638					for &used in spec.references {639						changed |= stack.propagate_analysis(user, used);640					}641				}642			}643		}644645		stack.depth += 1;646		PendingBody {647			first_after_frame,648			closures,649			stack,650			bomb,651		}652	}653}654655/// Frame state: values analysed, body not yet walked.656struct PendingBody<'s> {657	first_after_frame: LocalId,658	closures: Closures,659	stack: &'s mut AnalysisStack,660	bomb: DropBomb,661}662impl PendingBody<'_> {663	/// After the body is processed, drop the frame's locals and emit any664	/// "unused local" warnings.665	fn finish(self) {666		let PendingBody {667			first_after_frame,668			closures,669			stack,670			mut bomb,671		} = self;672		bomb.defuse();673		stack.depth -= 1;674675		debug_assert_eq!(676			first_after_frame,677			stack.next_local_id(),678			"nested scopes must be popped before outer frames"679		);680681		let mut changed = true;682		while changed {683			changed = false;684			for spec in closures.iter_specs() {685				// Effective used_at_depth for the spec = min over its ids.686				let mut min_used_at = u32::MAX;687				for id_raw in spec.ids.clone() {688					min_used_at = min_used_at.min(stack.local_defs[id_raw as usize].used_at_depth);689				}690				if min_used_at == u32::MAX {691					continue;692				}693				for &used in spec.references {694					let used_def = &mut stack.local_defs[used.idx()];695					if min_used_at < used_def.used_at_depth {696						used_def.used_at_depth = min_used_at;697						changed = true;698					}699				}700			}701		}702703		let drained: Vec<LocalDefinition> = stack704			.local_defs705			.drain(closures.first_in_frame.idx()..)706			.collect();707		for (i, def) in drained.iter().enumerate().rev() {708			let id = LocalId(closures.first_in_frame.0 + arridx(i));709			let stack_locals = stack710				.local_by_name711				.get_mut(&def.name)712				.expect("local must be in name map");713			let popped = stack_locals.pop().expect("name stack should not be empty");714			debug_assert_eq!(popped, id, "name stack integrity");715			if stack_locals.is_empty() {716				stack.local_by_name.remove(&def.name);717			}718719			if def.used_at_depth == u32::MAX {720				if def.used_by_sibling {721					stack.report_warning(722						format!("local is only referenced by unused siblings: {}", def.name),723						def.span.clone(),724					);725				} else {726					stack.report_warning(format!("unused local: {}", def.name), def.span.clone());727				}728			} else if def.analysis.local_dependent_depth > def.defined_at_depth729				&& def.analysis.object_dependent_depth > def.defined_at_depth730				&& def.defined_at_depth != 0731			{732				// The value doesn't depend on anything defined at or inside733				// this local's scope - can be hoisted, unfortunately not automatically.734				stack.report_warning(735					format!("local could be hoisted to an outer scope: {}", def.name),736					def.span.clone(),737				);738			}739		}740	}741}742743struct Closures {744	/// All the referenced locals, maybe repeated multiple times745	/// It is recorded as continous vec of sets, I.e we have746	/// a = 1, 2, 3747	/// b = 3, 4, 5, 6748	/// And in `referenced` we have `[ 1, 2, 3, 3, 4, 5, 6 ]`. To actually get, which closure refers to which element, see `spec_shapes`...749	/// Flat concatenation of sibling-local references across all specs.750	referenced: Vec<LocalId>,751	/// Amount of elements per closure, for the above case it is a = 3, b = 4, so here752	/// lies `[ 3, 4 ]`753	/// ~~closures: Vec<usize>,~~754	/// Finally, we have destructs.755	/// Because single destruct references single closure, but destructs to multiple locals, we have even more complicated structure.756	/// Luckly, every destruct is not interleaved with each other, so here we can have full list...757	/// Imagine having (LocalId(20), LocalId(21)), we need to save it to the Map, but we know that the numbers are sequential, so here we store number of consequent elements758	/// for each destruct starting from `first_destruct_local`759	/// ~~destructs: Vec<usize>,~~760	///761	/// => two of those fields were merged, as there is currently no per-destruct tracking of closures.762	/// For each spec in order: `(references_count, destruct_ids_count)`.763	/// `references_count` tells us how many entries of `referenced` belong764	/// to this spec; `destruct_ids_count` tells us how many `LocalIds` it765	/// binds.766	spec_shapes: Vec<(usize, usize)>,767	/// This is not a related doccomment, just a continuation of docs for previous fields.768	/// Having769	/// ```jsonnet770	/// local771	///     [a, b, c] = [d, e, f],772	///     [d, e, f] = [a, b, c, h],773	///     h = 1,774	/// ;775	/// ```776	///777	/// We have total of 7 locals778	/// First local here is `a` => `first_destruct_local` = `a`779	/// For first closure `[a, b, c] = [d, e, f]` we have 3 referenced locals = [d, e, f] => `referenced += [d, e, f]`, `closures += 3`; 3 destructs = [a, b, c] => `destructs += 3`780	/// [d, e, f] = [a, b, c, h], => `referenced += [a, b, c, h]`, `closures += 4`, `destructs += 3` (Note that this destruct will fail at runtime,781	///                                                                                               this thing should not care about that, it only captures what the value are referencing)782	/// h = 1 => referenced += [], closures += 0, destructs += 1783	/// And the result is784	///785	/// ```rust,ignore786	/// Closures {787	///     referenced: vec![d, e, f, a, b, c, h]788	///     spec_shapes: vec![(3, 3), (4, 3), (0, 1)],789	///     first_destruct_label: a,790	/// }791	/// ```792	///793	/// Reconstruction of that:794	///795	/// We know that we start with a796	/// We get the first number from destructs: `destructs.shift() == 3` => `destructs = [3, 1]`797	/// 3 elements counting from a => [a, b, c]798	/// Then we take first number from closures: `closures.shift() == 3` => `closures = [4, 0]`799	/// Then we take 3 items from referenced: `referenced.shift()x3 == d, e, f` => `referenced = [a, b, c, h]`800	///801	/// Thus we have [a, b, c] = [d, e, f]802	first_in_frame: LocalId,803}804805struct Closure<'a> {806	references: &'a [LocalId],807	ids: std::ops::Range<u32>,808}809810impl Closures {811	fn push_spec(&mut self, destruct_ids_count: usize, refs: &[LocalId]) {812		self.referenced.extend_from_slice(refs);813		self.spec_shapes.push((refs.len(), destruct_ids_count));814	}815816	fn iter_specs(&self) -> impl Iterator<Item = Closure<'_>> {817		let mut refs = self.referenced.as_slice();818		let mut next_id = self.first_in_frame.0;819		self.spec_shapes.iter().map(move |(refs_len, dest_count)| {820			let (this_refs, rest) = refs.split_at(*refs_len);821			refs = rest;822			let start = next_id;823			next_id += arridx(*dest_count);824			Closure {825				references: this_refs,826				ids: start..next_id,827			}828		})829	}830}831832#[derive(Debug, Clone, Copy, PartialEq, Eq)]833pub enum DiagLevel {834	Error,835	Warning,836}837838#[derive(Debug, Clone, Acyclic)]839pub struct Diagnostic {840	pub level: DiagLevel,841	pub message: String,842	pub span: Option<Span>,843}844845struct DefiningClosure {846	first_local: LocalId,847	n_locals: u16,848}849850impl DefiningClosure {851	fn resolve(&self, target: LocalId) -> Option<LocalSlot> {852		let end = self.first_local.0 + u32::from(self.n_locals);853		if target.0 >= self.first_local.0 && target.0 < end {854			Some(LocalSlot(855				u16::try_from(target.0 - self.first_local.0).expect("local slots overflow"),856			))857		} else {858			None859		}860	}861	fn define_local(&mut self, local: LocalId) -> LocalSlot {862		let slot = self.n_locals;863		let id = self.first_local.0 + u32::from(slot);864		debug_assert_eq!(local.0, id);865		self.n_locals = self.n_locals.checked_add(1).expect("local slots overflow");866		LocalSlot(slot)867	}868}869870/// Per-closure capture computation state.871struct ClosureFrame {872	/// Closure may allocate locals873	defining: Option<DefiningClosure>,874	/// `LocalId` => capture index875	captures: FxHashMap<LocalId, CaptureSlot>,876	/// Capture sources in insertion order; consumed by `pop_closure_frame`.877	capture_sources: Vec<LSlot>,878}879880#[allow(clippy::struct_excessive_bools)]881pub struct AnalysisStack {882	local_defs: Vec<LocalDefinition>,883	/// Shadowing isn't used in jsonnet much, 2 because `SmallVec` allows to store 2 ptr-sized without overhead.884	/// TODO: Add test for this assumption (sizeof(SmallVec<[usize; 1]>) == sizeof(SmallVec<[usize; 2]>))885	local_by_name: FxHashMap<IStr, SmallVec<[LocalId; 2]>>,886887	/// Depth of the current locals frame.888	depth: u32,889	/// Last depth, at which object has appeared. `u32::MAX` = not appeared at all890	last_object_depth: u32,891	/// First depth, at which object has appeared. `u32::MAX` = not appeared at all892	/// $ refers to this object.893	first_object_depth: u32,894895	/// `LocalId` bound to the innermost object's `this`896	this_local: Option<LocalId>,897	/// Outermost object `this`, aka `$`898	dollar_alias: Option<LocalId>,899	/// True iff `self` has been referenced in the current object immediate members (not nested children).900	cur_self_used: bool,901	/// True iff `super` has been referenced in the current object immediate members.902	cur_super_used: bool,903	/// True iff `$` has been referenced anywhere since the outermost object's scope was entered.904	dollar_used: bool,905906	/// Stack of closure frames (innermost on top).907	closure_stack: Vec<ClosureFrame>,908909	diagnostics: Vec<Diagnostic>,910	/// Whenever analysis would be broken due to static analysis error.911	errored: bool,912}913914#[must_use]915struct ClosureOnStack {916	bomb: DropBomb,917}918919impl AnalysisStack {920	pub fn new() -> Self {921		Self {922			local_defs: Vec::new(),923			local_by_name: FxHashMap::default(),924			depth: 0,925			last_object_depth: u32::MAX,926			first_object_depth: u32::MAX,927			this_local: None,928			dollar_alias: None,929			cur_self_used: false,930			cur_super_used: false,931			dollar_used: false,932			closure_stack: Vec::new(),933			diagnostics: Vec::new(),934			errored: false,935		}936	}937938	fn push_root_closure(&mut self, externals: u16) -> ClosureOnStack {939		assert!(940			self.closure_stack.is_empty(),941			"root is only possible with empty stack"942		);943944		self.closure_stack.push(ClosureFrame {945			defining: Some(DefiningClosure {946				first_local: LocalId(0),947				n_locals: externals,948			}),949			captures: FxHashMap::default(),950			capture_sources: Vec::new(),951		});952953		ClosureOnStack {954			bomb: DropBomb::new("root closure"),955		}956	}957958	fn push_closure_a(&mut self, first_local: LocalId) -> ClosureOnStack {959		self.closure_stack.push(ClosureFrame {960			defining: Some(DefiningClosure {961				first_local,962				n_locals: 0,963			}),964			captures: FxHashMap::default(),965			capture_sources: Vec::new(),966		});967		ClosureOnStack {968			bomb: DropBomb::new("closure with locals"),969		}970	}971972	#[inline]973	fn in_using_closure<T>(974		&mut self,975		inner: impl FnOnce(&mut AnalysisStack) -> T,976	) -> (ClosureShape, T) {977		fn push_closure_b(stack: &mut AnalysisStack) -> ClosureOnStack {978			stack.closure_stack.push(ClosureFrame {979				defining: None,980				captures: FxHashMap::default(),981				capture_sources: Vec::new(),982			});983			ClosureOnStack {984				bomb: DropBomb::new("closure with locals"),985			}986		}987		let closure = push_closure_b(self);988		let v = inner(self);989		let shape = self.pop_closure(closure);990		(shape, v)991	}992993	fn pop_closure(&mut self, mut closure: ClosureOnStack) -> ClosureShape {994		closure.bomb.defuse();995		let frame = self.closure_stack.pop().expect("closure frame");996		ClosureShape {997			captures: frame.capture_sources.into_boxed_slice(),998			n_locals: frame.defining.map(|d| d.n_locals).unwrap_or_default(),999		}1000	}10011002	/// Resolve a `LocalId` reference to an `LSlot` against the innermost1003	/// closure frame. May insert capture entries up the closure stack as1004	/// needed.1005	fn resolve_to_slot(&mut self, target: LocalId) -> LSlot {1006		let top = self.closure_stack.len();1007		debug_assert!(top > 0, "resolve_to_slot called with no closure frame");1008		Self::resolve_at(&mut self.closure_stack, top - 1, target)1009	}10101011	fn resolve_at(stack: &mut [ClosureFrame], idx: usize, target: LocalId) -> LSlot {1012		if let Some(def) = &stack[idx].defining {1013			if let Some(resolved) = def.resolve(target) {1014				return LSlot::Local(resolved);1015			}1016		} else {1017			// A sibling letrec slot must never be packed as a capture, or1018			// it would read an empty `OnceCell`.1019			for j in (0..idx).rev() {1020				if let Some(def) = &stack[j].defining {1021					if let Some(resolved) = def.resolve(target) {1022						return LSlot::Local(resolved);1023					}1024					break;1025				}1026			}1027		}1028		if let Some(&cap_idx) = stack[idx].captures.get(&target) {1029			return LSlot::Capture(cap_idx);1030		}1031		debug_assert!(idx > 0, "no enclosing closure frame for target {target:?}");1032		let parent_slot = Self::resolve_at(stack, idx - 1, target);1033		let frame = &mut stack[idx];1034		let cap_idx = CaptureSlot(1035			frame1036				.capture_sources1037				.len()1038				.try_into()1039				.expect("frame has more than u16::MAX captures"),1040		);1041		frame.capture_sources.push(parent_slot);1042		frame.captures.insert(target, cap_idx);1043		LSlot::Capture(cap_idx)1044	}10451046	fn next_local_id(&self) -> LocalId {1047		LocalId(arridx(self.local_defs.len()))1048	}10491050	fn report_error(&mut self, msg: impl Into<String>, span: Option<Span>) {1051		self.errored = true;1052		self.diagnostics.push(Diagnostic {1053			level: DiagLevel::Error,1054			message: msg.into(),1055			span,1056		});1057	}1058	fn report_warning(&mut self, msg: impl Into<String>, span: Option<Span>) {1059		self.diagnostics.push(Diagnostic {1060			level: DiagLevel::Warning,1061			message: msg.into(),1062			span,1063		});1064	}10651066	fn use_local(&mut self, name: &IStr, span: Span, taint: &mut AnalysisResult) -> Option<LSlot> {1067		let Some(ids) = self.local_by_name.get(name) else {1068			let names = suggest_names(name, self.local_by_name.keys());1069			self.report_error(1070				format!("undefined local: {name}{}", format_found(&names, "local")),1071				Some(span),1072			);1073			return None;1074		};1075		let id = *ids.last().expect("empty stacks should be removed");1076		let depth = self.depth;1077		let def = &mut self.local_defs[id.idx()];1078		def.use_at(depth);1079		taint.depend_on_local(def.defined_at_depth);1080		if def.analyzed {1081			taint.taint_by(def.analysis);1082		} else {1083			def.scratch_referenced = true;1084		}1085		Some(self.resolve_to_slot(id))1086	}10871088	/// Assign name to the value provided externally, e.g `std`.1089	pub fn define_external_local(&mut self, name: IStr, id: LocalId) {1090		assert!(1091			self.local_defs.iter().all(|d| d.analyzed),1092			"external locals must be defined before the root expression is analysed"1093		);1094		assert_eq!(1095			id,1096			self.next_local_id(),1097			"external local id mismatch for {name} (externals must be defined in allocation order)"1098		);1099		self.local_defs.push(LocalDefinition {1100			name: name.clone(),1101			span: None,1102			defined_at_depth: 0,1103			used_at_depth: u32::MAX,1104			used_by_sibling: false,1105			analysis: AnalysisResult::default(),1106			analyzed: true,1107			scratch_referenced: false,1108		});1109		self.local_by_name.entry(name).or_default().push(id);1110	}11111112	fn defining_closure_mut(&mut self) -> &mut DefiningClosure {1113		self.closure_stack1114			.iter_mut()1115			.rev()1116			.find_map(|c| c.defining.as_mut())1117			.expect("no enclosing defining closure frame")1118	}1119	fn defining_closure(&self) -> &DefiningClosure {1120		self.closure_stack1121			.iter()1122			.rev()1123			.find_map(|c| c.defining.as_ref())1124			.expect("no enclosing defining closure frame")1125	}1126}11271128impl Default for AnalysisStack {1129	fn default() -> Self {1130		Self::new()1131	}1132}11331134impl AnalysisStack {1135	fn top_defining_local(&self) -> LocalId {1136		self.defining_closure().first_local1137	}11381139	/// Merge `used`'s analysis into `user`'s analysis and record that `user`1140	/// transitively depends on `used` (same-frame sibling reference).1141	/// Returns `true` if `user`'s analysis changed.1142	fn propagate_analysis(&mut self, user: LocalId, used: LocalId) -> bool {1143		let (used_analysis, used_defined_at_depth) = {1144			let u = &self.local_defs[used.idx()];1145			(u.analysis, u.defined_at_depth)1146		};1147		let user_def = &mut self.local_defs[user.idx()];1148		let before_obj = user_def.analysis.object_dependent_depth;1149		let before_loc = user_def.analysis.local_dependent_depth;1150		user_def.analysis.taint_by(used_analysis);1151		user_def.analysis.depend_on_local(used_defined_at_depth);1152		before_obj != user_def.analysis.object_dependent_depth1153			|| before_loc != user_def.analysis.local_dependent_depth1154	}1155}11561157mod names {1158	use crate::names;11591160	names! {1161		this: "this",1162	}1163}11641165// Object scope helpers1166impl AnalysisStack {1167	#[inline]1168	fn in_object_scope<T>(1169		&mut self,1170		inner: impl FnOnce(&mut AnalysisStack) -> T,1171	) -> (ObjectUsage, ClosureShape, T) {1172		fn enter_object_scope(stack: &mut AnalysisStack) -> ObjectScope {1173			let is_outermost = stack.first_object_depth == u32::MAX;1174			let this_id = stack.next_local_id();1175			let closure = stack.push_closure_a(this_id);1176			let pushed = stack.push_pseudo_local(names::this());1177			debug_assert_eq!(pushed, this_id, "this pseudo-local id");1178			let scope = ObjectScope {1179				this_id,1180				is_outermost,1181				prev_this_local: stack.this_local,1182				prev_dollar_alias: stack.dollar_alias,1183				prev_cur_self_used: stack.cur_self_used,1184				prev_cur_super_used: stack.cur_super_used,1185				prev_dollar_used: is_outermost.then_some(stack.dollar_used),1186				prev_last_object: stack.last_object_depth,1187				prev_first_object: stack.first_object_depth,1188				closure,1189			};11901191			stack.this_local = Some(scope.this_id);1192			if is_outermost {1193				stack.dollar_alias = Some(scope.this_id);1194				stack.first_object_depth = stack.depth;1195				stack.dollar_used = false;1196			}1197			stack.last_object_depth = stack.depth;1198			stack.cur_self_used = false;1199			stack.cur_super_used = false;1200			scope1201		}12021203		fn leave_object_scope(1204			stack: &mut AnalysisStack,1205			scope: ObjectScope,1206		) -> (ObjectUsage, ClosureShape) {1207			let ObjectScope {1208				this_id,1209				is_outermost,1210				prev_this_local,1211				prev_dollar_alias,1212				prev_cur_self_used,1213				prev_cur_super_used,1214				prev_dollar_used,1215				prev_last_object,1216				prev_first_object,1217				closure,1218			} = scope;1219			let _ = stack.local_defs.pop().expect("this pseudo-local exists");1220			debug_assert_eq!(stack.local_defs.len(), this_id.0 as usize);12211222			let set_dollar = is_outermost && stack.dollar_used;1223			let usage = ObjectUsage {1224				this_used: stack.cur_self_used || stack.cur_super_used || set_dollar,1225				uses_super: stack.cur_super_used,1226				set_dollar,1227			};12281229			stack.this_local = prev_this_local;1230			stack.dollar_alias = prev_dollar_alias;1231			stack.cur_self_used = prev_cur_self_used;1232			stack.cur_super_used = prev_cur_super_used;1233			if let Some(prev) = prev_dollar_used {1234				stack.dollar_used = prev;1235			}1236			stack.last_object_depth = prev_last_object;1237			stack.first_object_depth = prev_first_object;12381239			let frame_shape = stack.pop_closure(closure);1240			(usage, frame_shape)1241		}1242		let scope = enter_object_scope(self);1243		let v = inner(self);1244		let (usage, shape) = leave_object_scope(self, scope);1245		(usage, shape, v)1246	}12471248	fn push_pseudo_local(&mut self, name: IStr) -> LocalId {1249		let id = self.next_local_id();1250		self.local_defs.push(LocalDefinition {1251			name,1252			span: None,1253			defined_at_depth: self.depth,1254			used_at_depth: u32::MAX,1255			used_by_sibling: false,1256			analysis: AnalysisResult::default(),1257			analyzed: true,1258			scratch_referenced: false,1259		});1260		{1261			let def = self.defining_closure_mut();1262			let _ = def.define_local(id);1263		}1264		id1265	}12661267	fn use_this(&mut self, taint: &mut AnalysisResult) -> Option<LSlot> {1268		let id = self.this_local?;1269		self.cur_self_used = true;1270		self.use_pseudo_local(id, taint);1271		Some(self.resolve_to_slot(id))1272	}12731274	fn use_super(&mut self, taint: &mut AnalysisResult) -> Option<()> {1275		let id = self.this_local?;1276		self.cur_super_used = true;1277		self.use_pseudo_local(id, taint);1278		Some(())1279	}12801281	fn use_dollar(&mut self, taint: &mut AnalysisResult) -> Option<LSlot> {1282		let id = self.dollar_alias?;1283		self.dollar_used = true;1284		self.use_pseudo_local(id, taint);1285		Some(self.resolve_to_slot(id))1286	}12871288	// TODO: Dedicated type for object references instead of "pseudo local" BS, idk1289	fn use_pseudo_local(&mut self, id: LocalId, taint: &mut AnalysisResult) {1290		let depth = self.depth;1291		let def = &mut self.local_defs[id.idx()];1292		def.use_at(depth);1293		taint.depend_on_local(def.defined_at_depth);1294		taint.depend_on_object(def.defined_at_depth);1295	}1296}12971298#[must_use]1299struct ObjectScope {1300	this_id: LocalId,1301	is_outermost: bool,1302	prev_this_local: Option<LocalId>,1303	prev_dollar_alias: Option<LocalId>,1304	prev_cur_self_used: bool,1305	prev_cur_super_used: bool,1306	prev_dollar_used: Option<bool>,1307	prev_last_object: u32,1308	prev_first_object: u32,1309	closure: ClosureOnStack,1310}13111312struct ObjectUsage {1313	this_used: bool,1314	uses_super: bool,1315	set_dollar: bool,1316}13171318fn analyze_assert(1319	stmt: &AssertStmt,1320	stack: &mut AnalysisStack,1321	taint: &mut AnalysisResult,1322) -> LAssertStmt {1323	let cond = analyze(&stmt.assertion.value, stack, taint);1324	let message = stmt.message.as_ref().map(|m| analyze(m, stack, taint));1325	LAssertStmt {1326		cond: Spanned::new(cond, stmt.assertion.span.clone()),1327		message,1328	}1329}13301331#[allow(clippy::too_many_lines)]1332pub fn analyze_named(1333	name: IStr,1334	expr: &Expr,1335	stack: &mut AnalysisStack,1336	taint: &mut AnalysisResult,1337) -> LExpr {1338	if let Expr::Function(span, params, body) = expr {1339		return analyze_function(Some(name), &span, &params, body, stack, taint);1340	}1341	analyze(expr, stack, taint)1342}1343#[allow(clippy::too_many_lines)]1344pub fn analyze(expr: &Expr, stack: &mut AnalysisStack, taint: &mut AnalysisResult) -> LExpr {1345	match expr {1346		Expr::Identity(span, l) => match l {1347			IdentityKind::This => stack.use_this(taint).map_or_else(1348				|| {1349					stack.report_error("`self` used outside of object", Some(span.clone()));1350					LExpr::BadLocal("self")1351				},1352				LExpr::Slot,1353			),1354			IdentityKind::Super => {1355				if stack.use_super(taint).is_some() {1356					LExpr::Super1357				} else {1358					stack.report_error("`super` used outside of object", Some(span.clone()));1359					LExpr::BadLocal("super")1360				}1361			}1362			IdentityKind::Dollar => stack.use_dollar(taint).map_or_else(1363				|| {1364					stack.report_error("`$` used outside of object", Some(span.clone()));1365					LExpr::BadLocal("$")1366				},1367				LExpr::Slot,1368			),1369		},1370		Expr::Trivial(tv) => LExpr::Trivial(tv.clone()),1371		Expr::Var(v) => stack1372			.use_local(&v.value, v.span.clone(), taint)1373			.map_or_else(|| LExpr::BadLocal("ref"), LExpr::Slot),1374		Expr::Arr(a) => {1375			if a.iter().all(|i| matches!(i, Expr::Trivial(_))) {1376				let trivials: Vec<_> = a1377					.iter()1378					.map(|i| match i {1379						Expr::Trivial(tv) => tv.clone(),1380						_ => unreachable!("checked above"),1381					})1382					.collect();1383				return LExpr::ArrConst(Rc::new(trivials));1384			}1385			let (shape, items) = stack.in_using_closure(|stack| {1386				a.iter()1387					.map(|v| analyze(v, stack, taint))1388					.collect::<Vec<_>>()1389			});1390			LExpr::Arr {1391				shape,1392				items: Rc::new(items),1393			}1394		}1395		Expr::ArrComp(inner, comp) => analyze_arr_comp(inner, comp, stack, taint),1396		Expr::Obj(obj) => LExpr::Obj(analyze_obj_body(obj, stack, taint)),1397		Expr::ObjExtend(base, obj) => LExpr::ObjExtend(1398			Box::new(analyze(base, stack, taint)),1399			analyze_obj_body(obj, stack, taint),1400		),1401		Expr::UnaryOp(op, value) => LExpr::UnaryOp(*op, Box::new(analyze(value, stack, taint))),1402		Expr::BinaryOp(op) => {1403			let BinaryOp {1404				lhs,1405				op: optype,1406				rhs,1407			} = &**op;1408			LExpr::BinaryOp {1409				lhs: Box::new(analyze(lhs, stack, taint)),1410				op: *optype,1411				rhs: Box::new(analyze(rhs, stack, taint)),1412			}1413		}1414		Expr::AssertExpr(assert) => {1415			let AssertExpr { assert, rest } = &**assert;1416			let assert = Rc::new(analyze_assert(assert, stack, taint));1417			let rest = Box::new(analyze(rest, stack, taint));1418			LExpr::AssertExpr { assert, rest }1419		}1420		Expr::LocalExpr(binds, body) => analyze_local_expr(binds, body, stack, taint),1421		Expr::Import(kind, path_expr) => {1422			let Expr::Trivial(TrivialVal::Str(path)) = &**path_expr else {1423				stack.report_error(1424					"import path must be a string literal",1425					Some(kind.span.clone()),1426				);1427				return LExpr::BadLocal("bad import");1428			};1429			LExpr::Import {1430				kind: kind.clone(),1431				kind_span: kind.span.clone(),1432				path: path.clone(),1433			}1434		}1435		Expr::ErrorStmt(span, inner) => {1436			LExpr::Error(span.clone(), Box::new(analyze(inner, stack, taint)))1437		}1438		Expr::Apply(applicable, args, tailstrict) => {1439			let app = analyze(applicable, stack, taint);1440			let ArgsDesc {1441				unnamed,1442				names,1443				values,1444			} = &args.value;1445			let unnamed_l = unnamed1446				.iter()1447				.map(|a| Rc::new(analyze(a, stack, taint)))1448				.collect();1449			let values_l = values1450				.iter()1451				.map(|a| Rc::new(analyze(a, stack, taint)))1452				.collect();1453			LExpr::Apply {1454				applicable: Box::new(app),1455				args: Spanned::new(1456					LArgsDesc {1457						unnamed: unnamed_l,1458						names: names.clone(),1459						values: values_l,1460					},1461					args.span.clone(),1462				),1463				tailstrict: *tailstrict,1464			}1465		}1466		Expr::Index { indexable, parts } => {1467			let idx = analyze(indexable, stack, taint);1468			let parts_l = parts1469				.iter()1470				.map(|p| {1471					let value = analyze(&p.value, stack, taint);1472					LIndexPart {1473						span: p.span.clone(),1474						value,1475						#[cfg(feature = "exp-null-coaelse")]1476						null_coaelse: p.null_coaelse,1477					}1478				})1479				.collect();1480			LExpr::Index {1481				indexable: Box::new(idx),1482				parts: parts_l,1483			}1484		}1485		Expr::Function(span, params, body) => {1486			analyze_function(None, span, params, body, stack, taint)1487		}1488		Expr::IfElse(ifelse) => {1489			let IfElse {1490				cond,1491				cond_then,1492				cond_else,1493			} = &**ifelse;1494			let cond_l = analyze(&cond.cond, stack, taint);1495			let then_l = analyze(cond_then, stack, taint);1496			let else_l = cond_else1497				.as_ref()1498				.map(|e| Box::new(analyze(e, stack, taint)));1499			LExpr::IfElse {1500				cond: Box::new(cond_l),1501				cond_then: Box::new(then_l),1502				cond_else: else_l,1503			}1504		}1505		Expr::Slice(slice) => {1506			let Slice {1507				value,1508				slice: SliceDesc { start, end, step },1509			} = &**slice;1510			let value_l = analyze(value, stack, taint);1511			let start_l = start.as_ref().map(|e| analyze(&e.value, stack, taint));1512			let end_l = end.as_ref().map(|e| analyze(&e.value, stack, taint));1513			let step_l = step.as_ref().map(|e| analyze(&e.value, stack, taint));1514			LExpr::Slice(Box::new(LSliceExpr {1515				value: value_l,1516				start: start_l,1517				end: end_l,1518				step: step_l,1519			}))1520		}1521	}1522}15231524fn analyze_local_expr(1525	binds: &[BindSpec],1526	body: &Expr,1527	stack: &mut AnalysisStack,1528	taint: &mut AnalysisResult,1529) -> LExpr {1530	if binds.is_empty() {1531		return analyze(body, stack, taint);1532	}1533	let frame_start = stack.next_local_id();1534	let closure = stack.push_closure_a(frame_start);1535	let (l_binds, body_expr) = process_local_frame(binds, stack, taint, |stack, taint| {1536		analyze(body, stack, taint)1537	});1538	let frame_shape = stack.pop_closure(closure);1539	LExpr::LocalExpr(Box::new(LLocalExpr {1540		frame_shape,1541		binds: l_binds,1542		body: body_expr,1543	}))1544}15451546fn analyze_bind_value(1547	bind: &BindSpec,1548	stack: &mut AnalysisStack,1549	taint: &mut AnalysisResult,1550) -> LExpr {1551	match bind {1552		BindSpec::Field {1553			value: Expr::Function(span, params, value),1554			into: Destruct::Full(name),1555		} => analyze_function(Some(name.value.clone()), &span, params, value, stack, taint),1556		BindSpec::Field { value, .. } => analyze(value, stack, taint),1557		BindSpec::Function {1558			params,1559			value,1560			name,1561		} => analyze_function(1562			Some(name.value.clone()),1563			&name.span,1564			params,1565			value,1566			stack,1567			taint,1568		),1569	}1570}15711572fn process_local_frame<R>(1573	binds: &[BindSpec],1574	stack: &mut AnalysisStack,1575	taint: &mut AnalysisResult,1576	body_fn: impl FnOnce(&mut AnalysisStack, &mut AnalysisResult) -> R,1577) -> (Vec<LBind>, R) {1578	let mut alloc = FrameAlloc::new(stack);15791580	let mut destructs: Vec<Option<LDestruct>> = Vec::with_capacity(binds.len());1581	for bind in binds {1582		destructs.push(alloc.alloc_bind(bind));1583	}1584	let mut pending = alloc.finish();15851586	let mut l_binds: Vec<LBind> = Vec::with_capacity(binds.len());1587	for (bind, destruct) in binds.iter().zip(destructs) {1588		let mut value_taint = AnalysisResult::default();1589		let (value_shape, value) = pending1590			.stack1591			.in_using_closure(|stack| analyze_bind_value(bind, stack, &mut value_taint));1592		taint.taint_by(value_taint);1593		if let Some(destruct) = destruct {1594			pending.record_spec_init(&destruct, value_taint);1595			l_binds.push(LBind {1596				destruct,1597				value_shape,1598				value: Rc::new(value),1599			});1600		} else {1601			pending.closures.push_spec(0, &[]);1602		}1603	}16041605	let body_frame = pending.finish();1606	let result = body_fn(body_frame.stack, taint);1607	body_frame.finish();16081609	(l_binds, result)1610}16111612fn analyze_function(1613	name: Option<IStr>,1614	span: &Span,1615	params: &ExprParams,1616	body: &Expr,1617	stack: &mut AnalysisStack,1618	taint: &mut AnalysisResult,1619) -> LExpr {1620	let mut alloc = FrameAlloc::new(stack);1621	let closure = alloc.push_locals_closure();16221623	let mut param_destructs: Vec<Option<LDestruct>> = Vec::with_capacity(params.exprs.len());1624	for p in &params.exprs {1625		param_destructs.push(alloc.alloc_destruct(&p.destruct));1626	}16271628	let mut pending = alloc.finish();16291630	let mut l_params: Vec<LParam> = Vec::with_capacity(params.exprs.len());1631	for (p, destruct) in params.exprs.iter().zip(param_destructs) {1632		let mut value_taint = AnalysisResult::default();1633		let default = p.default.as_ref().map_or_else(1634			|| None,1635			|d| {1636				Some(1637					pending1638						.stack1639						.in_using_closure(|stack| Rc::new(analyze(d, stack, &mut value_taint))),1640				)1641			},1642		);1643		taint.taint_by(value_taint);1644		if let Some(destruct) = destruct {1645			let name = match &p.destruct {1646				Destruct::Full(n) => Some(n.value.clone()),1647				#[cfg(feature = "exp-destruct")]1648				_ => None,1649			};1650			pending.record_spec_init(&destruct, value_taint);1651			l_params.push(LParam {1652				name,1653				destruct,1654				default,1655			});1656		} else {1657			pending.closures.push_spec(0, &[]);1658		}1659	}16601661	let body_frame = pending.finish();1662	let body_expr = analyze(body, body_frame.stack, taint);1663	body_frame.finish();1664	let body_shape = stack.pop_closure(closure);16651666	// function(x) x is an identity function1667	if l_params.len() == 1 && l_params[0].default.is_none() {1668		#[allow(irrefutable_let_patterns, reason = "refutable with exp-destruct")]1669		if let LDestruct::Full(param_slot) = &l_params[0].destruct1670			&& let LExpr::Slot(LSlot::Local(s)) = &body_expr1671			&& s == param_slot1672		{1673			stack.report_warning(1674				"do not define identity functions manually, use std.id instead",1675				Some(span.clone()),1676			);1677			return LExpr::IdentityFunction {};1678		}1679	}16801681	LExpr::Function(Rc::new(LFunction {1682		name,1683		params: l_params,1684		signature: params.signature.clone(),1685		body_shape,1686		body: Rc::new(body_expr),1687	}))1688}16891690fn analyze_obj_body(1691	obj: &ObjBody,1692	stack: &mut AnalysisStack,1693	taint: &mut AnalysisResult,1694) -> LObjBody {1695	match obj {1696		ObjBody::MemberList(members) => {1697			LObjBody::MemberList(analyze_obj_members(members, stack, taint))1698		}1699		ObjBody::ObjComp(comp) => LObjBody::ObjComp(Box::new(analyze_obj_comp(comp, stack, taint))),1700	}1701}17021703fn analyze_obj_members(1704	members: &ObjMembers,1705	stack: &mut AnalysisStack,1706	taint: &mut AnalysisResult,1707) -> LObjMembers {1708	let ObjMembers {1709		locals,1710		asserts,1711		fields,1712	} = members;17131714	// Names are analyzed in enclosing scope, they can't depend on locals or self/super1715	let field_names: Vec<LFieldName> = fields1716		.iter()1717		.map(|f| match &f.name.value {1718			FieldName::Fixed(s) => LFieldName::Fixed(s.clone()),1719			FieldName::Dyn(e) => LFieldName::Dyn(analyze(e, stack, taint)),1720		})1721		.collect();17221723	let (usage, frame_shape, (l_binds, (l_asserts_opt, l_fields))) =1724		stack.in_object_scope(|stack| {1725			process_local_frame(locals, stack, taint, |stack, taint| {1726				let l_asserts_opt = if asserts.is_empty() {1727					None1728				} else {1729					let (shape, l_asserts) = stack.in_using_closure(|stack| {1730						let mut l_asserts = Vec::with_capacity(asserts.len());1731						for a in asserts {1732							let mut assert_taint = AnalysisResult::default();1733							l_asserts.push(analyze_assert(a, stack, &mut assert_taint));1734							taint.taint_by(assert_taint);1735						}1736						l_asserts1737					});1738					Some(Rc::new(LObjAsserts {1739						shape,1740						asserts: l_asserts,1741					}))1742				};1743				let mut l_fields = Vec::with_capacity(fields.len());1744				for (f, name) in fields.iter().zip(field_names) {1745					let value = stack.in_using_closure(|stack| {1746						if let Some(params) = &f.params {1747							analyze_function(1748								name.function_name(),1749								&f.name.span,1750								params,1751								&f.value,1752								stack,1753								taint,1754							)1755						} else {1756							analyze(&f.value, stack, taint)1757						}1758					});1759					l_fields.push(LFieldMember {1760						name,1761						plus: f.plus,1762						visibility: f.visibility,1763						value: Rc::new(value),1764					});1765				}1766				(l_asserts_opt, l_fields)1767			})1768		});1769	// `this` was allocated as the first local of the object's frame,1770	// so its slot is 0 within that frame.1771	let this_slot = usage.this_used.then_some(LocalSlot(0));1772	LObjMembers {1773		frame_shape,1774		this: this_slot,1775		set_dollar: usage.set_dollar,1776		uses_super: usage.uses_super,1777		locals: Rc::new(l_binds),1778		asserts: l_asserts_opt,1779		fields: l_fields,1780	}1781}17821783fn analyze_obj_comp(1784	comp: &ObjComp,1785	stack: &mut AnalysisStack,1786	taint: &mut AnalysisResult,1787) -> LObjComp {1788	let res = analyze_comp_specs(&comp.compspecs, stack, taint, |stack, taint| {1789		let field_name = match &comp.field.name.value {1790			FieldName::Fixed(s) => LFieldName::Fixed(s.clone()),1791			FieldName::Dyn(e) => LFieldName::Dyn(analyze(e, stack, taint)),1792		};17931794		let (usage, frame_shape, body) = stack.in_object_scope(|stack| {1795			process_local_frame(&comp.locals, stack, taint, |stack, taint| {1796				let value = stack.in_using_closure(|stack| {1797					if let Some(params) = &comp.field.params {1798						analyze_function(1799							None,1800							&comp.field.name.span,1801							params,1802							&comp.field.value,1803							stack,1804							taint,1805						)1806					} else {1807						analyze(&comp.field.value, stack, taint)1808					}1809				});1810				LFieldMember {1811					name: field_name,1812					plus: comp.field.plus,1813					visibility: comp.field.visibility,1814					value: Rc::new(value),1815				}1816			})1817		});1818		(usage, frame_shape, body)1819	});1820	let (usage, frame_shape, (locals, field)) = res.inner;1821	let this_slot = usage.this_used.then_some(LocalSlot(0));1822	LObjComp {1823		frame_shape: Rc::new(frame_shape),1824		this: this_slot,1825		set_dollar: usage.set_dollar,1826		uses_super: usage.uses_super,1827		locals: Rc::new(locals),1828		field,1829		compspecs: res.compspecs,1830	}1831}18321833fn analyze_arr_comp(1834	inner: &Expr,1835	specs: &[CompSpec],1836	stack: &mut AnalysisStack,1837	taint: &mut AnalysisResult,1838) -> LExpr {1839	let res = analyze_comp_specs(specs, stack, taint, |stack, taint| {1840		stack.in_using_closure(|stack| analyze(inner, stack, taint))1841	});1842	let (value_shape, value) = res.inner;1843	LExpr::ArrComp(Box::new(LArrComp {1844		value_shape,1845		value: Rc::new(value),1846		compspecs: res.compspecs,1847	}))1848}18491850fn analyze_comp_specs<R>(1851	specs: &[CompSpec],1852	stack: &mut AnalysisStack,1853	taint: &mut AnalysisResult,1854	inside: impl FnOnce(&mut AnalysisStack, &mut AnalysisResult) -> R,1855) -> CompSpecResult<R> {1856	fn go<R>(1857		idx: usize,1858		specs: &[CompSpec],1859		outer_depth: u32,1860		stack: &mut AnalysisStack,1861		taint: &mut AnalysisResult,1862		inside: impl FnOnce(&mut AnalysisStack, &mut AnalysisResult) -> R,1863	) -> (R, Vec<LCompSpec>) {1864		if idx >= specs.len() {1865			return (inside(stack, taint), Vec::new());1866		}1867		match &specs[idx] {1868			CompSpec::IfSpec(IfSpecData { cond, .. }) => {1869				let cond_l = analyze(cond, stack, taint);1870				let (r, mut rest) = go(idx + 1, specs, outer_depth, stack, taint, inside);1871				rest.insert(0, LCompSpec::If(cond_l));1872				(r, rest)1873			}1874			CompSpec::ForSpec(ForSpecData { destruct, over }) => {1875				let mut over_taint = AnalysisResult::default();1876				let over_l = analyze(over, stack, &mut over_taint);1877				let loop_invariant = over_taint.local_dependent_depth > outer_depth;1878				taint.taint_by(over_taint);18791880				let mut alloc = FrameAlloc::new(stack);1881				let closure = alloc.push_locals_closure();1882				let Some(l_destruct) = alloc.alloc_destruct(destruct) else {1883					stack.pop_closure(closure);1884					return go(idx + 1, specs, outer_depth, stack, taint, inside);1885				};1886				let mut pending = alloc.finish();18871888				let var_analysis = AnalysisResult::default();1889				pending.record_spec_init(&l_destruct, var_analysis);18901891				let body_frame = pending.finish();1892				let (r, mut rest) =1893					go(idx + 1, specs, outer_depth, body_frame.stack, taint, inside);1894				body_frame.finish();1895				let frame_shape = stack.pop_closure(closure);18961897				rest.insert(1898					0,1899					LCompSpec::For {1900						frame_shape,1901						destruct: l_destruct,1902						over: over_l,1903						loop_invariant,1904					},1905				);1906				(r, rest)1907			}1908			#[cfg(feature = "exp-object-iteration")]1909			CompSpec::ForObjSpec(data) => {1910				let mut over_taint = AnalysisResult::default();1911				let over_l = analyze(&data.over, stack, &mut over_taint);1912				let loop_invariant = over_taint.local_dependent_depth > outer_depth;1913				taint.taint_by(over_taint);19141915				let mut alloc = FrameAlloc::new(stack);1916				let closure = alloc.push_locals_closure();1917				let Some((_, key_slot)) = alloc.define_local(data.key.clone(), None) else {1918					stack.pop_closure(closure);1919					return go(idx + 1, specs, outer_depth, stack, taint, inside);1920				};1921				let Some(l_value) = alloc.alloc_destruct(&data.value) else {1922					stack.pop_closure(closure);1923					return go(idx + 1, specs, outer_depth, stack, taint, inside);1924				};1925				let mut pending = alloc.finish();19261927				let var_analysis = AnalysisResult::default();1928				pending.record_spec_init(&LDestruct::Full(key_slot), var_analysis);1929				pending.record_spec_init(&l_value, var_analysis);19301931				let body_frame = pending.finish();1932				let (r, mut rest) =1933					go(idx + 1, specs, outer_depth, body_frame.stack, taint, inside);1934				body_frame.finish();1935				let frame_shape = stack.pop_closure(closure);19361937				rest.insert(1938					0,1939					LCompSpec::ForObj {1940						frame_shape,1941						key: key_slot,1942						visibility: data.visibility,1943						value: l_value,1944						over: over_l,1945						loop_invariant,1946					},1947				);1948				(r, rest)1949			}1950		}1951	}1952	let outer_depth = stack.depth;1953	let (r, compspecs) = go(0, specs, outer_depth, stack, taint, inside);1954	CompSpecResult {1955		inner: r,1956		compspecs,1957	}1958}19591960struct CompSpecResult<R> {1961	inner: R,1962	compspecs: Vec<LCompSpec>,1963}19641965pub fn analyze_root(expr: &Expr, ctx: Vec<(IStr, LocalId)>) -> AnalysisReport {1966	let mut stack = AnalysisStack::new();1967	for (name, id) in ctx {1968		stack.define_external_local(name, id);1969	}19701971	let externals_count: u16 = stack1972		.local_defs1973		.len()1974		.try_into()1975		.expect("more than u16::MAX externals");1976	let closure = stack.push_root_closure(externals_count);19771978	let mut taint = AnalysisResult::default();1979	let lir = analyze(expr, &mut stack, &mut taint);19801981	let root_shape = stack.pop_closure(closure);1982	debug_assert!(1983		stack.closure_stack.is_empty(),1984		"closure stack imbalance after analyze"1985	);19861987	AnalysisReport {1988		lir,1989		root_shape,1990		root_analysis: taint,1991		diagnostics_list: stack.diagnostics,1992		errored: stack.errored,1993	}1994}19951996pub struct AnalysisReport {1997	pub lir: LExpr,1998	pub root_shape: ClosureShape,1999	pub root_analysis: AnalysisResult,2000	pub diagnostics_list: Vec<Diagnostic>,2001	pub errored: bool,2002}20032004#[cfg(test)]2005mod tests {2006	#[test]2007	#[cfg(not(feature = "exp-null-coaelse"))]2008	fn snapshots() {2009		use std::fs;20102011		use insta::{assert_snapshot, glob};2012		use jrsonnet_ir::Source;20132014		use super::*;20152016		fn render_diagnostics(src: &str, diags: &[Diagnostic]) -> String {2017			use std::fmt::Write;20182019			use hi_doc::{Formatting, SnippetBuilder, Text};20202021			let mut out = String::new();2022			let mut unspanned = Vec::new();2023			let mut spanned: Vec<&Diagnostic> = Vec::new();2024			for d in diags {2025				if d.span.is_some() {2026					spanned.push(d);2027				} else {2028					unspanned.push(d);2029				}2030			}2031			if !spanned.is_empty() {2032				let mut builder = SnippetBuilder::new(src);2033				for d in spanned {2034					let span = d.span.as_ref().expect("spanned");2035					let ab = match d.level {2036						DiagLevel::Error => {2037							builder.error(Text::fragment(d.message.clone(), Formatting::default()))2038						}2039						DiagLevel::Warning => builder2040							.warning(Text::fragment(d.message.clone(), Formatting::default())),2041					};2042					ab.range(span.range()).build();2043				}2044				out.push_str(&hi_doc::source_to_ansi(&builder.build()));2045			}2046			for d in unspanned {2047				let prefix = match d.level {2048					DiagLevel::Error => "error",2049					DiagLevel::Warning => "warning",2050				};2051				writeln!(out, "{prefix}: {}", d.message).expect("fmt");2052			}2053			out2054		}2055		fn fmt_depth(d: u32) -> String {2056			if d == u32::MAX {2057				"none".into()2058			} else {2059				d.to_string()2060			}2061		}20622063		glob!("analysis_tests/*.jsonnet", |path| {2064			let code = fs::read_to_string(path).expect("read test file");2065			let src = Source::new_virtual("<test>".into(), code.clone().into());2066			let expr = crate::parse_jsonnet(&code, src.clone()).expect("parse");2067			let report = analyze_root(&expr, Vec::new());20682069			let diagnostics = render_diagnostics(src.code(), &report.diagnostics_list);2070			// Strip ANSI escapes from diagnostics so snapshots are readable.2071			let diagnostics = strip_ansi_escapes::strip_str(&diagnostics);2072			let rendered = format!(2073				"--- source ---\n{}\n--- root analysis ---\nobject_dependent_depth: {}\nlocal_dependent_depth: {}\nerrored: {}\n--- diagnostics ---\n{}--- lir ---\n{:#?}\n",2074				code.trim_end(),2075				fmt_depth(report.root_analysis.object_dependent_depth),2076				fmt_depth(report.root_analysis.local_dependent_depth),2077				report.errored,2078				diagnostics,2079				report.lir,2080			);2081			assert_snapshot!(rendered);2082		});2083	}2084}
after · crates/jrsonnet-evaluator/src/analyze.rs
1//! Static analysis of jsonnet IR.2//!3//! Walks the IR tree and produces a lowered IR (`LExpr`) with named locals resolved to numeric [`LocalId`] and4//! dependency analysis markers for every expression describing which objects and locals the expression depends on5//!6//! ```jsonnet7//! {8//!     a: $, // `a` is top-object-dependent.9//!     b: {10//!         // `b` is NOT object-dependent for the top object: it only references11//!         // things inside itself. `b` is built once per top-level object.12//!         a: $,13//!     },14//! }15//! ```1617use std::rc::Rc;1819use drop_bomb::DropBomb;20use jrsonnet_gcmodule::Acyclic;21use jrsonnet_interner::IStr;22use jrsonnet_ir::{23	ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BinaryOpType, BindSpec, CompSpec, Destruct, Expr,24	ExprParams, FieldName, ForSpecData, IdentityKind, IfElse, IfSpecData, ImportKind, ObjBody,25	ObjComp, ObjMembers, Slice, SliceDesc, Span, Spanned, TrivialVal, UnaryOpType, Visibility,26	function::FunctionSignature,27};28use rustc_hash::{FxHashMap, FxHashSet};29use smallvec::SmallVec;3031use crate::{32	arr::arridx,33	error::{format_found, suggest_names},34	gc::WithCapacityExt as _,35	obj::{ObjFieldFlags, ObjShape, ShapeField, ordering::FieldIndex},36};3738#[derive(Debug, Clone, Copy)]39#[must_use]40pub struct AnalysisResult {41	/// Highest object, on which identity the value is dependent. `u32::MAX` = not dependent at all42	pub object_dependent_depth: u32,43	/// Highest local frame, on which this value depends. `u32::MAX` = not dependent at all44	pub local_dependent_depth: u32,45}4647impl Default for AnalysisResult {48	fn default() -> Self {49		Self {50			object_dependent_depth: u32::MAX,51			local_dependent_depth: u32::MAX,52		}53	}54}5556impl AnalysisResult {57	fn depend_on_object(&mut self, depth: u32) {58		if depth < self.object_dependent_depth {59			self.object_dependent_depth = depth;60		}61	}62	fn depend_on_local(&mut self, depth: u32) {63		if depth < self.local_dependent_depth {64			self.local_dependent_depth = depth;65		}66	}67	fn taint_by(&mut self, other: AnalysisResult) {68		self.depend_on_object(other.object_dependent_depth);69		self.depend_on_local(other.local_dependent_depth);70	}71}7273#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Acyclic)]74pub struct LocalId(pub u32);7576impl LocalId {77	fn idx(self) -> usize {78		self.0 as usize79	}80	fn defined_before(self, other: Self) -> bool {81		self.0 < other.082	}83}8485#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Acyclic)]86pub enum LSlot {87	/// Enclosing frame locals (sibling letrec, params, etc.).88	Local(LocalSlot),89	/// Enclosing closure's capture pack.90	Capture(CaptureSlot),91}9293#[derive(Debug, Acyclic)]94pub struct ClosureShape {95	pub captures: Box<[LSlot]>,96	pub n_locals: u16,97}9899struct LocalDefinition {100	name: IStr,101	span: Option<Span>,102	/// At which frame depth this local was defined103	defined_at_depth: u32,104	/// Min frame depth, at which this local was used. `u32::MAX` = not used at all.105	/// This check won't catch unused argument closures, i.e:106	/// ```jsonnet107	/// local108	///     a = b,109	///     b = a,110	/// ; 2 + 2111	///112	/// ```113	/// Both `a` and `b` here are "used", but the whole closure was not used here.114	used_at_depth: u32,115	/// Used as part of the current frame closure116	used_by_sibling: bool,117	/// Analysys result for value of this local118	analysis: AnalysisResult,119	/// Has `analysis` been filled in?120	/// For sanity checking, locals are initialized in batchs, use `first_uninitialized_local`121	analyzed: bool,122	/// During walk over uninitialized vars, we can't refer to analysis results of other locals,123	/// but we need to. To make that work, for each variable in variable frame we capture its closure,124	/// by looking at referenced variables.125	scratch_referenced: bool,126}127128impl LocalDefinition {129	fn use_at(&mut self, depth: u32) {130		if depth == self.defined_at_depth {131			self.used_by_sibling = true;132			return;133		}134		if depth < self.used_at_depth {135			self.used_at_depth = depth;136		}137	}138}139140#[derive(Debug, Acyclic)]141pub enum LExpr {142	Slot(LSlot),143	Trivial(TrivialVal),144	Arr {145		shape: ClosureShape,146		items: Rc<Vec<LExpr>>,147	},148	ArrConst(Rc<Vec<TrivialVal>>),149	ArrComp(Box<LArrComp>),150	Obj(LObjBody),151	ObjExtend(Box<LExpr>, LObjBody),152	UnaryOp(UnaryOpType, Box<LExpr>),153	BinaryOp {154		lhs: Box<LExpr>,155		op: BinaryOpType,156		rhs: Box<LExpr>,157	},158	AssertExpr {159		assert: Rc<LAssertStmt>,160		rest: Box<LExpr>,161	},162	Error(Span, Box<LExpr>),163	LocalExpr(Box<LLocalExpr>),164	Import {165		kind: Spanned<ImportKind>,166		kind_span: Span,167		path: IStr,168	},169	Apply {170		applicable: Box<LExpr>,171		args: Spanned<LArgsDesc>,172		tailstrict: bool,173	},174	Index {175		indexable: Box<LExpr>,176		parts: Vec<LIndexPart>,177	},178	Function(Rc<LFunction>),179	IdentityFunction,180	IfElse {181		cond: Box<LExpr>,182		cond_then: Box<LExpr>,183		cond_else: Option<Box<LExpr>>,184	},185	Slice(Box<LSliceExpr>),186	Super,187188	/// Allows partial evaluation of broken expression tree,189	/// expressions with failed static analysis end up here190	BadLocal(&'static str),191}192193#[derive(Debug, Acyclic)]194pub struct LLocalExpr {195	pub frame_shape: ClosureShape,196	pub binds: Vec<LBind>,197	pub body: LExpr,198}199200#[derive(Debug, Acyclic)]201pub struct LFunction {202	pub name: Option<IStr>,203	pub params: Vec<LParam>,204	pub signature: FunctionSignature,205206	pub body_shape: ClosureShape,207	pub body: Rc<LExpr>,208}209210#[derive(Debug, Acyclic)]211pub struct LParam {212	pub name: Option<IStr>,213	pub destruct: LDestruct,214215	pub default: Option<(ClosureShape, Rc<LExpr>)>,216}217218#[derive(Debug, Acyclic)]219pub struct LBind {220	pub destruct: LDestruct,221	pub value_shape: ClosureShape,222	pub value: Rc<LExpr>,223}224225#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Acyclic)]226pub struct CaptureSlot(pub(crate) u16);227#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Acyclic)]228pub struct LocalSlot(pub(crate) u16);229230#[derive(Debug, Acyclic)]231pub enum LDestruct {232	Full(LocalSlot),233	#[cfg(feature = "exp-destruct")]234	Skip,235	#[cfg(feature = "exp-destruct")]236	Array {237		start: Vec<LDestruct>,238		rest: Option<LDestructRest>,239		end: Vec<LDestruct>,240	},241	#[cfg(feature = "exp-destruct")]242	Object {243		fields: Vec<LDestructField>,244		rest: Option<LDestructRest>,245	},246}247248#[derive(Debug, Clone, Copy, Acyclic)]249pub enum LDestructRest {250	Keep(LocalSlot),251	Drop,252}253254#[derive(Debug, Acyclic)]255pub struct LDestructField {256	pub name: IStr,257	pub into: Option<LDestruct>,258	pub default: Option<(ClosureShape, Rc<LExpr>)>,259}260261impl LDestruct {262	pub fn each_slot<F: FnMut(LocalSlot)>(&self, f: &mut F) {263		match self {264			Self::Full(s) => f(*s),265			#[cfg(feature = "exp-destruct")]266			Self::Skip => {}267			#[cfg(feature = "exp-destruct")]268			Self::Array { start, rest, end } => {269				for d in start {270					d.each_slot(f);271				}272				if let Some(LDestructRest::Keep(s)) = rest {273					f(*s);274				}275				for d in end {276					d.each_slot(f);277				}278			}279			#[cfg(feature = "exp-destruct")]280			Self::Object { fields, rest } => {281				for field in fields {282					if let Some(into) = &field.into {283						into.each_slot(f);284					} else {285						unreachable!("shorthand object destruct must store `into`");286					}287				}288				if let Some(LDestructRest::Keep(s)) = rest {289					f(*s);290				}291			}292		}293	}294295	pub fn slots(&self) -> SmallVec<[LocalSlot; 1]> {296		let mut out = SmallVec::new();297		self.each_slot(&mut |s| out.push(s));298		out299	}300}301302#[derive(Debug, Acyclic)]303pub struct LSliceExpr {304	pub value: LExpr,305	pub start: Option<LExpr>,306	pub end: Option<LExpr>,307	pub step: Option<LExpr>,308}309310#[derive(Debug, Acyclic)]311pub struct LArgsDesc {312	pub unnamed: Vec<Rc<LExpr>>,313	pub names: Vec<IStr>,314	pub values: Vec<Rc<LExpr>>,315}316317#[derive(Debug, Acyclic)]318pub struct LAssertStmt {319	pub cond: Spanned<LExpr>,320	pub message: Option<LExpr>,321}322323#[derive(Debug, Acyclic)]324pub struct LIndexPart {325	pub span: Span,326	pub value: LExpr,327	#[cfg(feature = "exp-null-coaelse")]328	pub null_coaelse: bool,329}330331#[derive(Debug, Acyclic)]332pub enum LObjBody {333	MemberList(LObjMembers),334	StaticMembers(Box<LObjStaticMembers>),335	ObjComp(Box<LObjComp>),336}337338#[derive(Debug, Acyclic)]339pub struct LObjMembers {340	pub frame_shape: ClosureShape,341	/// If current object identity (`super`/`this`/`$`) is used, `this` should342	/// be saved to the specified local slot.343	pub this: Option<LocalSlot>,344	/// Set if dollar should also be assigned to object identity, `this` should also be set (TODO: proper type-level validation)345	pub set_dollar: bool,346	/// True iff `super` is referenced by this object's members.347	pub uses_super: bool,348349	pub locals: Rc<Vec<LBind>>,350	pub asserts: Option<Rc<LObjAsserts>>,351	pub fields: Vec<LFieldMember>,352}353354#[derive(Debug, Acyclic)]355pub struct LObjStaticMembers {356	pub frame_shape: ClosureShape,357	pub this: Option<LocalSlot>,358	pub set_dollar: bool,359	pub uses_super: bool,360361	pub locals: Rc<Vec<LBind>>,362	pub asserts: Option<Rc<LObjAsserts>>,363364	pub shape: Rc<ObjShape>,365	pub bindings: Vec<Rc<(ClosureShape, LExpr)>>,366}367368#[derive(Debug, Acyclic)]369pub struct LObjComp {370	pub frame_shape: Rc<ClosureShape>,371	pub this: Option<LocalSlot>,372	pub set_dollar: bool,373	pub uses_super: bool,374375	pub locals: Rc<Vec<LBind>>,376	pub field: LFieldMember,377	pub compspecs: Vec<LCompSpec>,378}379380#[derive(Debug, Acyclic)]381pub struct LFieldMember {382	pub name: LFieldName,383	pub plus: bool,384	pub visibility: Visibility,385	pub value: Rc<(ClosureShape, LExpr)>,386}387388#[derive(Debug, Acyclic)]389pub struct LClosure<T: Acyclic> {390	pub shape: ClosureShape,391	pub value: T,392}393394#[derive(Debug, Acyclic)]395pub struct LObjAsserts {396	pub shape: ClosureShape,397	pub asserts: Vec<LAssertStmt>,398}399400#[derive(Debug, Acyclic)]401pub enum LFieldName {402	Fixed(IStr),403	Dyn(LExpr),404}405impl LFieldName {406	fn function_name(&self) -> Option<IStr> {407		match self {408			LFieldName::Fixed(istr) => Some(istr.clone()),409			LFieldName::Dyn(_) => None,410		}411	}412}413414#[derive(Debug, Acyclic)]415pub struct LArrComp {416	pub value_shape: ClosureShape,417	pub value: Rc<LExpr>,418	pub compspecs: Vec<LCompSpec>,419}420421#[derive(Debug, Acyclic)]422pub enum LCompSpec {423	If(LExpr),424	For {425		frame_shape: ClosureShape,426		destruct: LDestruct,427		over: LExpr,428		/// Is `over` does not depend on any variable introduced by an earlier for-spec in this comprehension chain429		loop_invariant: bool,430	},431	#[cfg(feature = "exp-object-iteration")]432	ForObj {433		frame_shape: ClosureShape,434		key: LocalSlot,435		visibility: jrsonnet_ir::Visibility,436		value: LDestruct,437		over: LExpr,438		loop_invariant: bool,439	},440}441442struct FrameAlloc<'s> {443	first_in_frame: LocalId,444	stack: &'s mut AnalysisStack,445	bomb: DropBomb,446}447impl<'s> FrameAlloc<'s> {448	fn new(stack: &'s mut AnalysisStack) -> Self {449		FrameAlloc {450			first_in_frame: stack.next_local_id(),451			stack,452			bomb: DropBomb::new("binding frame state"),453		}454	}455456	fn push_locals_closure(&mut self) -> ClosureOnStack {457		self.stack.push_closure_a(self.first_in_frame)458	}459460	fn define_local(&mut self, name: IStr, span: Option<Span>) -> Option<(LocalId, LocalSlot)> {461		let id = self.stack.next_local_id();462		let stack = self.stack.local_by_name.entry(name.clone()).or_default();463		if let Some(&existing) = stack.last()464			&& !existing.defined_before(self.first_in_frame)465		{466			self.stack.report_error(467				format!("local is already defined in the current frame: {name}"),468				span,469			);470			return None;471		}472		stack.push(id);473		self.stack.local_defs.push(LocalDefinition {474			name,475			span,476			defined_at_depth: self.stack.depth,477			used_at_depth: u32::MAX,478			used_by_sibling: false,479			analysis: AnalysisResult::default(),480			analyzed: false,481			scratch_referenced: false,482		});483		let def = self.stack.defining_closure_mut();484		Some((id, def.define_local(id)))485	}486	fn alloc_bind(&mut self, bind: &BindSpec) -> Option<LDestruct> {487		match bind {488			BindSpec::Field { into, .. } => self.alloc_destruct(into),489			BindSpec::Function { name, .. } => {490				let (_, id) = self.define_local(name.value.clone(), Some(name.span.clone()))?;491				Some(LDestruct::Full(id))492			}493		}494	}495	fn alloc_destruct(&mut self, destruct: &Destruct) -> Option<LDestruct> {496		Some(match destruct {497			Destruct::Full(name) => {498				let (_, id) = self.define_local(name.value.clone(), Some(name.span.clone()))?;499				LDestruct::Full(id)500			}501			#[cfg(feature = "exp-destruct")]502			Destruct::Skip => LDestruct::Skip,503			#[cfg(feature = "exp-destruct")]504			Destruct::Array { start, rest, end } => {505				let start = start506					.iter()507					.map(|d| self.alloc_destruct(d))508					.collect::<Option<Vec<_>>>()?;509				let rest = match rest {510					Some(jrsonnet_ir::DestructRest::Keep(name)) => {511						let (_, id) = self.define_local(name.clone(), None)?;512						Some(LDestructRest::Keep(id))513					}514					Some(jrsonnet_ir::DestructRest::Drop) => Some(LDestructRest::Drop),515					None => None,516				};517				let end = end518					.iter()519					.map(|d| self.alloc_destruct(d))520					.collect::<Option<Vec<_>>>()?;521				LDestruct::Array { start, rest, end }522			}523			#[cfg(feature = "exp-destruct")]524			Destruct::Object { fields, rest } => {525				let mut l_fields: Vec<(IStr, LDestruct)> = Vec::with_capacity(fields.len());526				// Allocate destruct LocalIds, then analyse defaults527				for (name, into, _default) in fields {528					let into = if let Some(inner) = into {529						self.alloc_destruct(inner)?530					} else {531						let (_, id) = self.define_local(name.clone(), None)?;532						LDestruct::Full(id)533					};534					l_fields.push((name.clone(), into));535				}536				// All locals exist, so defaults can reference any sibling.537				let l_fields: Vec<LDestructField> = l_fields538					.into_iter()539					.zip(fields.iter())540					.map(|((name, into), (_n, _i, default))| {541						let default = match default {542							Some(e) => {543								let mut default_taint = AnalysisResult::default();544								Some(self.stack.in_using_closure(|stack| {545									Rc::new(analyze(&e.value, stack, &mut default_taint))546								}))547							}548							None => None,549						};550						LDestructField {551							name,552							into: Some(into),553							default,554						}555					})556					.collect();557				let rest = match rest {558					Some(jrsonnet_ir::DestructRest::Keep(name)) => {559						let (_, id) = self.define_local(name.clone(), None)?;560						Some(LDestructRest::Keep(id))561					}562					Some(jrsonnet_ir::DestructRest::Drop) => Some(LDestructRest::Drop),563					None => None,564				};565				LDestruct::Object {566					fields: l_fields,567					rest,568				}569			}570		})571	}572573	fn finish(self) -> PendingInit<'s> {574		let Self {575			first_in_frame,576			stack,577			bomb,578		} = self;579		let first_after_frame = stack.next_local_id();580		PendingInit {581			first_after_frame,582			stack,583			closures: Closures {584				referenced: vec![],585				spec_shapes: vec![],586				first_in_frame,587			},588			bomb,589		}590	}591}592593/// Frame state: `LocalIds` allocated, values not yet analysed.594struct PendingInit<'s> {595	first_after_frame: LocalId,596	stack: &'s mut AnalysisStack,597	closures: Closures,598	bomb: DropBomb,599}600601impl<'s> PendingInit<'s> {602	/// Record the analysis of a spec's value: stamp every id bound by the603	/// spec with `analysis`, collect the spec's same-frame references, and604	/// append them to `closures`.605	fn record_spec_init(&mut self, destruct: &LDestruct, analysis: AnalysisResult) {606		let mut refs: SmallVec<[LocalId; 4]> = SmallVec::new();607		for i in self.closures.first_in_frame.0..self.first_after_frame.0 {608			let def = &mut self.stack.local_defs[i as usize];609			if def.scratch_referenced {610				refs.push(LocalId(i));611				def.scratch_referenced = false;612			}613		}614615		let mut ids_count = 0;616		let first_local = self.stack.top_defining_local();617		destruct.each_slot(&mut |slot| {618			ids_count += 1;619			let id = LocalId(first_local.0 + u32::from(slot.0));620			let def = &mut self.stack.local_defs[id.idx()];621			debug_assert!(!def.analyzed, "sanity: local {:?} analysed twice", def.name);622			def.analysis = analysis;623			def.analyzed = true;624		});625		self.closures.push_spec(ids_count, &refs);626	}627	/// After all specs are analysed, propagate dependency information between628	/// siblings to a fix-point, then switch to "body" mode.629	fn finish(self) -> PendingBody<'s> {630		let Self {631			first_after_frame,632			closures,633			stack,634			bomb,635		} = self;636637		debug_assert_eq!(638			first_after_frame,639			stack.next_local_id(),640			"frame initialisation left unfinished locals"641		);642643		debug_assert_eq!(644			closures.spec_shapes.iter().map(|(_, d)| *d).sum::<usize>(),645			(first_after_frame.0 - closures.first_in_frame.0) as usize,646			"closures destruct-id counts must match frame local count"647		);648649		let mut changed = true;650		while changed {651			changed = false;652			for spec in closures.iter_specs() {653				for id_raw in spec.ids.clone() {654					let user = LocalId(id_raw);655					for &used in spec.references {656						changed |= stack.propagate_analysis(user, used);657					}658				}659			}660		}661662		stack.depth += 1;663		PendingBody {664			first_after_frame,665			closures,666			stack,667			bomb,668		}669	}670}671672/// Frame state: values analysed, body not yet walked.673struct PendingBody<'s> {674	first_after_frame: LocalId,675	closures: Closures,676	stack: &'s mut AnalysisStack,677	bomb: DropBomb,678}679impl PendingBody<'_> {680	/// After the body is processed, drop the frame's locals and emit any681	/// "unused local" warnings.682	fn finish(self) {683		let PendingBody {684			first_after_frame,685			closures,686			stack,687			mut bomb,688		} = self;689		bomb.defuse();690		stack.depth -= 1;691692		debug_assert_eq!(693			first_after_frame,694			stack.next_local_id(),695			"nested scopes must be popped before outer frames"696		);697698		let mut changed = true;699		while changed {700			changed = false;701			for spec in closures.iter_specs() {702				// Effective used_at_depth for the spec = min over its ids.703				let mut min_used_at = u32::MAX;704				for id_raw in spec.ids.clone() {705					min_used_at = min_used_at.min(stack.local_defs[id_raw as usize].used_at_depth);706				}707				if min_used_at == u32::MAX {708					continue;709				}710				for &used in spec.references {711					let used_def = &mut stack.local_defs[used.idx()];712					if min_used_at < used_def.used_at_depth {713						used_def.used_at_depth = min_used_at;714						changed = true;715					}716				}717			}718		}719720		let drained: Vec<LocalDefinition> = stack721			.local_defs722			.drain(closures.first_in_frame.idx()..)723			.collect();724		for (i, def) in drained.iter().enumerate().rev() {725			let id = LocalId(closures.first_in_frame.0 + arridx(i));726			let stack_locals = stack727				.local_by_name728				.get_mut(&def.name)729				.expect("local must be in name map");730			let popped = stack_locals.pop().expect("name stack should not be empty");731			debug_assert_eq!(popped, id, "name stack integrity");732			if stack_locals.is_empty() {733				stack.local_by_name.remove(&def.name);734			}735736			if def.used_at_depth == u32::MAX {737				if def.used_by_sibling {738					stack.report_warning(739						format!("local is only referenced by unused siblings: {}", def.name),740						def.span.clone(),741					);742				} else {743					stack.report_warning(format!("unused local: {}", def.name), def.span.clone());744				}745			} else if def.analysis.local_dependent_depth > def.defined_at_depth746				&& def.analysis.object_dependent_depth > def.defined_at_depth747				&& def.defined_at_depth != 0748			{749				// The value doesn't depend on anything defined at or inside750				// this local's scope - can be hoisted, unfortunately not automatically.751				stack.report_warning(752					format!("local could be hoisted to an outer scope: {}", def.name),753					def.span.clone(),754				);755			}756		}757	}758}759760struct Closures {761	/// All the referenced locals, maybe repeated multiple times762	/// It is recorded as continous vec of sets, I.e we have763	/// a = 1, 2, 3764	/// b = 3, 4, 5, 6765	/// And in `referenced` we have `[ 1, 2, 3, 3, 4, 5, 6 ]`. To actually get, which closure refers to which element, see `spec_shapes`...766	/// Flat concatenation of sibling-local references across all specs.767	referenced: Vec<LocalId>,768	/// Amount of elements per closure, for the above case it is a = 3, b = 4, so here769	/// lies `[ 3, 4 ]`770	/// ~~closures: Vec<usize>,~~771	/// Finally, we have destructs.772	/// Because single destruct references single closure, but destructs to multiple locals, we have even more complicated structure.773	/// Luckly, every destruct is not interleaved with each other, so here we can have full list...774	/// Imagine having (LocalId(20), LocalId(21)), we need to save it to the Map, but we know that the numbers are sequential, so here we store number of consequent elements775	/// for each destruct starting from `first_destruct_local`776	/// ~~destructs: Vec<usize>,~~777	///778	/// => two of those fields were merged, as there is currently no per-destruct tracking of closures.779	/// For each spec in order: `(references_count, destruct_ids_count)`.780	/// `references_count` tells us how many entries of `referenced` belong781	/// to this spec; `destruct_ids_count` tells us how many `LocalIds` it782	/// binds.783	spec_shapes: Vec<(usize, usize)>,784	/// This is not a related doccomment, just a continuation of docs for previous fields.785	/// Having786	/// ```jsonnet787	/// local788	///     [a, b, c] = [d, e, f],789	///     [d, e, f] = [a, b, c, h],790	///     h = 1,791	/// ;792	/// ```793	///794	/// We have total of 7 locals795	/// First local here is `a` => `first_destruct_local` = `a`796	/// For first closure `[a, b, c] = [d, e, f]` we have 3 referenced locals = [d, e, f] => `referenced += [d, e, f]`, `closures += 3`; 3 destructs = [a, b, c] => `destructs += 3`797	/// [d, e, f] = [a, b, c, h], => `referenced += [a, b, c, h]`, `closures += 4`, `destructs += 3` (Note that this destruct will fail at runtime,798	///                                                                                               this thing should not care about that, it only captures what the value are referencing)799	/// h = 1 => referenced += [], closures += 0, destructs += 1800	/// And the result is801	///802	/// ```rust,ignore803	/// Closures {804	///     referenced: vec![d, e, f, a, b, c, h]805	///     spec_shapes: vec![(3, 3), (4, 3), (0, 1)],806	///     first_destruct_label: a,807	/// }808	/// ```809	///810	/// Reconstruction of that:811	///812	/// We know that we start with a813	/// We get the first number from destructs: `destructs.shift() == 3` => `destructs = [3, 1]`814	/// 3 elements counting from a => [a, b, c]815	/// Then we take first number from closures: `closures.shift() == 3` => `closures = [4, 0]`816	/// Then we take 3 items from referenced: `referenced.shift()x3 == d, e, f` => `referenced = [a, b, c, h]`817	///818	/// Thus we have [a, b, c] = [d, e, f]819	first_in_frame: LocalId,820}821822struct Closure<'a> {823	references: &'a [LocalId],824	ids: std::ops::Range<u32>,825}826827impl Closures {828	fn push_spec(&mut self, destruct_ids_count: usize, refs: &[LocalId]) {829		self.referenced.extend_from_slice(refs);830		self.spec_shapes.push((refs.len(), destruct_ids_count));831	}832833	fn iter_specs(&self) -> impl Iterator<Item = Closure<'_>> {834		let mut refs = self.referenced.as_slice();835		let mut next_id = self.first_in_frame.0;836		self.spec_shapes.iter().map(move |(refs_len, dest_count)| {837			let (this_refs, rest) = refs.split_at(*refs_len);838			refs = rest;839			let start = next_id;840			next_id += arridx(*dest_count);841			Closure {842				references: this_refs,843				ids: start..next_id,844			}845		})846	}847}848849#[derive(Debug, Clone, Copy, PartialEq, Eq)]850pub enum DiagLevel {851	Error,852	Warning,853}854855#[derive(Debug, Clone, Acyclic)]856pub struct Diagnostic {857	pub level: DiagLevel,858	pub message: String,859	pub span: Option<Span>,860}861862struct DefiningClosure {863	first_local: LocalId,864	n_locals: u16,865}866867impl DefiningClosure {868	fn resolve(&self, target: LocalId) -> Option<LocalSlot> {869		let end = self.first_local.0 + u32::from(self.n_locals);870		if target.0 >= self.first_local.0 && target.0 < end {871			Some(LocalSlot(872				u16::try_from(target.0 - self.first_local.0).expect("local slots overflow"),873			))874		} else {875			None876		}877	}878	fn define_local(&mut self, local: LocalId) -> LocalSlot {879		let slot = self.n_locals;880		let id = self.first_local.0 + u32::from(slot);881		debug_assert_eq!(local.0, id);882		self.n_locals = self.n_locals.checked_add(1).expect("local slots overflow");883		LocalSlot(slot)884	}885}886887/// Per-closure capture computation state.888struct ClosureFrame {889	/// Closure may allocate locals890	defining: Option<DefiningClosure>,891	/// `LocalId` => capture index892	captures: FxHashMap<LocalId, CaptureSlot>,893	/// Capture sources in insertion order; consumed by `pop_closure_frame`.894	capture_sources: Vec<LSlot>,895}896897#[allow(clippy::struct_excessive_bools)]898pub struct AnalysisStack {899	local_defs: Vec<LocalDefinition>,900	/// Shadowing isn't used in jsonnet much, 2 because `SmallVec` allows to store 2 ptr-sized without overhead.901	/// TODO: Add test for this assumption (sizeof(SmallVec<[usize; 1]>) == sizeof(SmallVec<[usize; 2]>))902	local_by_name: FxHashMap<IStr, SmallVec<[LocalId; 2]>>,903904	/// Depth of the current locals frame.905	depth: u32,906	/// Last depth, at which object has appeared. `u32::MAX` = not appeared at all907	last_object_depth: u32,908	/// First depth, at which object has appeared. `u32::MAX` = not appeared at all909	/// $ refers to this object.910	first_object_depth: u32,911912	/// `LocalId` bound to the innermost object's `this`913	this_local: Option<LocalId>,914	/// Outermost object `this`, aka `$`915	dollar_alias: Option<LocalId>,916	/// True iff `self` has been referenced in the current object immediate members (not nested children).917	cur_self_used: bool,918	/// True iff `super` has been referenced in the current object immediate members.919	cur_super_used: bool,920	/// True iff `$` has been referenced anywhere since the outermost object's scope was entered.921	dollar_used: bool,922923	/// Stack of closure frames (innermost on top).924	closure_stack: Vec<ClosureFrame>,925926	diagnostics: Vec<Diagnostic>,927	/// Whenever analysis would be broken due to static analysis error.928	errored: bool,929}930931#[must_use]932struct ClosureOnStack {933	bomb: DropBomb,934}935936impl AnalysisStack {937	pub fn new() -> Self {938		Self {939			local_defs: Vec::new(),940			local_by_name: FxHashMap::default(),941			depth: 0,942			last_object_depth: u32::MAX,943			first_object_depth: u32::MAX,944			this_local: None,945			dollar_alias: None,946			cur_self_used: false,947			cur_super_used: false,948			dollar_used: false,949			closure_stack: Vec::new(),950			diagnostics: Vec::new(),951			errored: false,952		}953	}954955	fn push_root_closure(&mut self, externals: u16) -> ClosureOnStack {956		assert!(957			self.closure_stack.is_empty(),958			"root is only possible with empty stack"959		);960961		self.closure_stack.push(ClosureFrame {962			defining: Some(DefiningClosure {963				first_local: LocalId(0),964				n_locals: externals,965			}),966			captures: FxHashMap::default(),967			capture_sources: Vec::new(),968		});969970		ClosureOnStack {971			bomb: DropBomb::new("root closure"),972		}973	}974975	fn push_closure_a(&mut self, first_local: LocalId) -> ClosureOnStack {976		self.closure_stack.push(ClosureFrame {977			defining: Some(DefiningClosure {978				first_local,979				n_locals: 0,980			}),981			captures: FxHashMap::default(),982			capture_sources: Vec::new(),983		});984		ClosureOnStack {985			bomb: DropBomb::new("closure with locals"),986		}987	}988989	#[inline]990	fn in_using_closure<T>(991		&mut self,992		inner: impl FnOnce(&mut AnalysisStack) -> T,993	) -> (ClosureShape, T) {994		fn push_closure_b(stack: &mut AnalysisStack) -> ClosureOnStack {995			stack.closure_stack.push(ClosureFrame {996				defining: None,997				captures: FxHashMap::default(),998				capture_sources: Vec::new(),999			});1000			ClosureOnStack {1001				bomb: DropBomb::new("closure with locals"),1002			}1003		}1004		let closure = push_closure_b(self);1005		let v = inner(self);1006		let shape = self.pop_closure(closure);1007		(shape, v)1008	}10091010	fn pop_closure(&mut self, mut closure: ClosureOnStack) -> ClosureShape {1011		closure.bomb.defuse();1012		let frame = self.closure_stack.pop().expect("closure frame");1013		ClosureShape {1014			captures: frame.capture_sources.into_boxed_slice(),1015			n_locals: frame.defining.map(|d| d.n_locals).unwrap_or_default(),1016		}1017	}10181019	/// Resolve a `LocalId` reference to an `LSlot` against the innermost1020	/// closure frame. May insert capture entries up the closure stack as1021	/// needed.1022	fn resolve_to_slot(&mut self, target: LocalId) -> LSlot {1023		let top = self.closure_stack.len();1024		debug_assert!(top > 0, "resolve_to_slot called with no closure frame");1025		Self::resolve_at(&mut self.closure_stack, top - 1, target)1026	}10271028	fn resolve_at(stack: &mut [ClosureFrame], idx: usize, target: LocalId) -> LSlot {1029		if let Some(def) = &stack[idx].defining {1030			if let Some(resolved) = def.resolve(target) {1031				return LSlot::Local(resolved);1032			}1033		} else {1034			// A sibling letrec slot must never be packed as a capture, or1035			// it would read an empty `OnceCell`.1036			for j in (0..idx).rev() {1037				if let Some(def) = &stack[j].defining {1038					if let Some(resolved) = def.resolve(target) {1039						return LSlot::Local(resolved);1040					}1041					break;1042				}1043			}1044		}1045		if let Some(&cap_idx) = stack[idx].captures.get(&target) {1046			return LSlot::Capture(cap_idx);1047		}1048		debug_assert!(idx > 0, "no enclosing closure frame for target {target:?}");1049		let parent_slot = Self::resolve_at(stack, idx - 1, target);1050		let frame = &mut stack[idx];1051		let cap_idx = CaptureSlot(1052			frame1053				.capture_sources1054				.len()1055				.try_into()1056				.expect("frame has more than u16::MAX captures"),1057		);1058		frame.capture_sources.push(parent_slot);1059		frame.captures.insert(target, cap_idx);1060		LSlot::Capture(cap_idx)1061	}10621063	fn next_local_id(&self) -> LocalId {1064		LocalId(arridx(self.local_defs.len()))1065	}10661067	fn report_error(&mut self, msg: impl Into<String>, span: Option<Span>) {1068		self.errored = true;1069		self.diagnostics.push(Diagnostic {1070			level: DiagLevel::Error,1071			message: msg.into(),1072			span,1073		});1074	}1075	fn report_warning(&mut self, msg: impl Into<String>, span: Option<Span>) {1076		self.diagnostics.push(Diagnostic {1077			level: DiagLevel::Warning,1078			message: msg.into(),1079			span,1080		});1081	}10821083	fn use_local(&mut self, name: &IStr, span: Span, taint: &mut AnalysisResult) -> Option<LSlot> {1084		let Some(ids) = self.local_by_name.get(name) else {1085			let names = suggest_names(name, self.local_by_name.keys());1086			self.report_error(1087				format!("undefined local: {name}{}", format_found(&names, "local")),1088				Some(span),1089			);1090			return None;1091		};1092		let id = *ids.last().expect("empty stacks should be removed");1093		let depth = self.depth;1094		let def = &mut self.local_defs[id.idx()];1095		def.use_at(depth);1096		taint.depend_on_local(def.defined_at_depth);1097		if def.analyzed {1098			taint.taint_by(def.analysis);1099		} else {1100			def.scratch_referenced = true;1101		}1102		Some(self.resolve_to_slot(id))1103	}11041105	/// Assign name to the value provided externally, e.g `std`.1106	pub fn define_external_local(&mut self, name: IStr, id: LocalId) {1107		assert!(1108			self.local_defs.iter().all(|d| d.analyzed),1109			"external locals must be defined before the root expression is analysed"1110		);1111		assert_eq!(1112			id,1113			self.next_local_id(),1114			"external local id mismatch for {name} (externals must be defined in allocation order)"1115		);1116		self.local_defs.push(LocalDefinition {1117			name: name.clone(),1118			span: None,1119			defined_at_depth: 0,1120			used_at_depth: u32::MAX,1121			used_by_sibling: false,1122			analysis: AnalysisResult::default(),1123			analyzed: true,1124			scratch_referenced: false,1125		});1126		self.local_by_name.entry(name).or_default().push(id);1127	}11281129	fn defining_closure_mut(&mut self) -> &mut DefiningClosure {1130		self.closure_stack1131			.iter_mut()1132			.rev()1133			.find_map(|c| c.defining.as_mut())1134			.expect("no enclosing defining closure frame")1135	}1136	fn defining_closure(&self) -> &DefiningClosure {1137		self.closure_stack1138			.iter()1139			.rev()1140			.find_map(|c| c.defining.as_ref())1141			.expect("no enclosing defining closure frame")1142	}1143}11441145impl Default for AnalysisStack {1146	fn default() -> Self {1147		Self::new()1148	}1149}11501151impl AnalysisStack {1152	fn top_defining_local(&self) -> LocalId {1153		self.defining_closure().first_local1154	}11551156	/// Merge `used`'s analysis into `user`'s analysis and record that `user`1157	/// transitively depends on `used` (same-frame sibling reference).1158	/// Returns `true` if `user`'s analysis changed.1159	fn propagate_analysis(&mut self, user: LocalId, used: LocalId) -> bool {1160		let (used_analysis, used_defined_at_depth) = {1161			let u = &self.local_defs[used.idx()];1162			(u.analysis, u.defined_at_depth)1163		};1164		let user_def = &mut self.local_defs[user.idx()];1165		let before_obj = user_def.analysis.object_dependent_depth;1166		let before_loc = user_def.analysis.local_dependent_depth;1167		user_def.analysis.taint_by(used_analysis);1168		user_def.analysis.depend_on_local(used_defined_at_depth);1169		before_obj != user_def.analysis.object_dependent_depth1170			|| before_loc != user_def.analysis.local_dependent_depth1171	}1172}11731174mod names {1175	use crate::names;11761177	names! {1178		this: "this",1179	}1180}11811182// Object scope helpers1183impl AnalysisStack {1184	#[inline]1185	fn in_object_scope<T>(1186		&mut self,1187		inner: impl FnOnce(&mut AnalysisStack) -> T,1188	) -> (ObjectUsage, ClosureShape, T) {1189		fn enter_object_scope(stack: &mut AnalysisStack) -> ObjectScope {1190			let is_outermost = stack.first_object_depth == u32::MAX;1191			let this_id = stack.next_local_id();1192			let closure = stack.push_closure_a(this_id);1193			let pushed = stack.push_pseudo_local(names::this());1194			debug_assert_eq!(pushed, this_id, "this pseudo-local id");1195			let scope = ObjectScope {1196				this_id,1197				is_outermost,1198				prev_this_local: stack.this_local,1199				prev_dollar_alias: stack.dollar_alias,1200				prev_cur_self_used: stack.cur_self_used,1201				prev_cur_super_used: stack.cur_super_used,1202				prev_dollar_used: is_outermost.then_some(stack.dollar_used),1203				prev_last_object: stack.last_object_depth,1204				prev_first_object: stack.first_object_depth,1205				closure,1206			};12071208			stack.this_local = Some(scope.this_id);1209			if is_outermost {1210				stack.dollar_alias = Some(scope.this_id);1211				stack.first_object_depth = stack.depth;1212				stack.dollar_used = false;1213			}1214			stack.last_object_depth = stack.depth;1215			stack.cur_self_used = false;1216			stack.cur_super_used = false;1217			scope1218		}12191220		fn leave_object_scope(1221			stack: &mut AnalysisStack,1222			scope: ObjectScope,1223		) -> (ObjectUsage, ClosureShape) {1224			let ObjectScope {1225				this_id,1226				is_outermost,1227				prev_this_local,1228				prev_dollar_alias,1229				prev_cur_self_used,1230				prev_cur_super_used,1231				prev_dollar_used,1232				prev_last_object,1233				prev_first_object,1234				closure,1235			} = scope;1236			let _ = stack.local_defs.pop().expect("this pseudo-local exists");1237			debug_assert_eq!(stack.local_defs.len(), this_id.0 as usize);12381239			let set_dollar = is_outermost && stack.dollar_used;1240			let usage = ObjectUsage {1241				this_used: stack.cur_self_used || stack.cur_super_used || set_dollar,1242				uses_super: stack.cur_super_used,1243				set_dollar,1244			};12451246			stack.this_local = prev_this_local;1247			stack.dollar_alias = prev_dollar_alias;1248			stack.cur_self_used = prev_cur_self_used;1249			stack.cur_super_used = prev_cur_super_used;1250			if let Some(prev) = prev_dollar_used {1251				stack.dollar_used = prev;1252			}1253			stack.last_object_depth = prev_last_object;1254			stack.first_object_depth = prev_first_object;12551256			let frame_shape = stack.pop_closure(closure);1257			(usage, frame_shape)1258		}1259		let scope = enter_object_scope(self);1260		let v = inner(self);1261		let (usage, shape) = leave_object_scope(self, scope);1262		(usage, shape, v)1263	}12641265	fn push_pseudo_local(&mut self, name: IStr) -> LocalId {1266		let id = self.next_local_id();1267		self.local_defs.push(LocalDefinition {1268			name,1269			span: None,1270			defined_at_depth: self.depth,1271			used_at_depth: u32::MAX,1272			used_by_sibling: false,1273			analysis: AnalysisResult::default(),1274			analyzed: true,1275			scratch_referenced: false,1276		});1277		{1278			let def = self.defining_closure_mut();1279			let _ = def.define_local(id);1280		}1281		id1282	}12831284	fn use_this(&mut self, taint: &mut AnalysisResult) -> Option<LSlot> {1285		let id = self.this_local?;1286		self.cur_self_used = true;1287		self.use_pseudo_local(id, taint);1288		Some(self.resolve_to_slot(id))1289	}12901291	fn use_super(&mut self, taint: &mut AnalysisResult) -> Option<()> {1292		let id = self.this_local?;1293		self.cur_super_used = true;1294		self.use_pseudo_local(id, taint);1295		Some(())1296	}12971298	fn use_dollar(&mut self, taint: &mut AnalysisResult) -> Option<LSlot> {1299		let id = self.dollar_alias?;1300		self.dollar_used = true;1301		self.use_pseudo_local(id, taint);1302		Some(self.resolve_to_slot(id))1303	}13041305	// TODO: Dedicated type for object references instead of "pseudo local" BS, idk1306	fn use_pseudo_local(&mut self, id: LocalId, taint: &mut AnalysisResult) {1307		let depth = self.depth;1308		let def = &mut self.local_defs[id.idx()];1309		def.use_at(depth);1310		taint.depend_on_local(def.defined_at_depth);1311		taint.depend_on_object(def.defined_at_depth);1312	}1313}13141315#[must_use]1316struct ObjectScope {1317	this_id: LocalId,1318	is_outermost: bool,1319	prev_this_local: Option<LocalId>,1320	prev_dollar_alias: Option<LocalId>,1321	prev_cur_self_used: bool,1322	prev_cur_super_used: bool,1323	prev_dollar_used: Option<bool>,1324	prev_last_object: u32,1325	prev_first_object: u32,1326	closure: ClosureOnStack,1327}13281329struct ObjectUsage {1330	this_used: bool,1331	uses_super: bool,1332	set_dollar: bool,1333}13341335fn analyze_assert(1336	stmt: &AssertStmt,1337	stack: &mut AnalysisStack,1338	taint: &mut AnalysisResult,1339) -> LAssertStmt {1340	let cond = analyze(&stmt.assertion.value, stack, taint);1341	let message = stmt.message.as_ref().map(|m| analyze(m, stack, taint));1342	LAssertStmt {1343		cond: Spanned::new(cond, stmt.assertion.span.clone()),1344		message,1345	}1346}13471348#[allow(clippy::too_many_lines)]1349pub fn analyze_named(1350	name: IStr,1351	expr: &Expr,1352	stack: &mut AnalysisStack,1353	taint: &mut AnalysisResult,1354) -> LExpr {1355	if let Expr::Function(span, params, body) = expr {1356		return analyze_function(Some(name), span, params, body, stack, taint);1357	}1358	analyze(expr, stack, taint)1359}1360#[allow(clippy::too_many_lines)]1361pub fn analyze(expr: &Expr, stack: &mut AnalysisStack, taint: &mut AnalysisResult) -> LExpr {1362	match expr {1363		Expr::Identity(span, l) => match l {1364			IdentityKind::This => stack.use_this(taint).map_or_else(1365				|| {1366					stack.report_error("`self` used outside of object", Some(span.clone()));1367					LExpr::BadLocal("self")1368				},1369				LExpr::Slot,1370			),1371			IdentityKind::Super => {1372				if stack.use_super(taint).is_some() {1373					LExpr::Super1374				} else {1375					stack.report_error("`super` used outside of object", Some(span.clone()));1376					LExpr::BadLocal("super")1377				}1378			}1379			IdentityKind::Dollar => stack.use_dollar(taint).map_or_else(1380				|| {1381					stack.report_error("`$` used outside of object", Some(span.clone()));1382					LExpr::BadLocal("$")1383				},1384				LExpr::Slot,1385			),1386		},1387		Expr::Trivial(tv) => LExpr::Trivial(tv.clone()),1388		Expr::Var(v) => stack1389			.use_local(&v.value, v.span.clone(), taint)1390			.map_or_else(|| LExpr::BadLocal("ref"), LExpr::Slot),1391		Expr::Arr(a) => {1392			if a.iter().all(|i| matches!(i, Expr::Trivial(_))) {1393				let trivials: Vec<_> = a1394					.iter()1395					.map(|i| match i {1396						Expr::Trivial(tv) => tv.clone(),1397						_ => unreachable!("checked above"),1398					})1399					.collect();1400				return LExpr::ArrConst(Rc::new(trivials));1401			}1402			let (shape, items) = stack.in_using_closure(|stack| {1403				a.iter()1404					.map(|v| analyze(v, stack, taint))1405					.collect::<Vec<_>>()1406			});1407			LExpr::Arr {1408				shape,1409				items: Rc::new(items),1410			}1411		}1412		Expr::ArrComp(inner, comp) => analyze_arr_comp(inner, comp, stack, taint),1413		Expr::Obj(obj) => LExpr::Obj(analyze_obj_body(obj, stack, taint)),1414		Expr::ObjExtend(base, obj) => LExpr::ObjExtend(1415			Box::new(analyze(base, stack, taint)),1416			analyze_obj_body(obj, stack, taint),1417		),1418		Expr::UnaryOp(op, value) => LExpr::UnaryOp(*op, Box::new(analyze(value, stack, taint))),1419		Expr::BinaryOp(op) => {1420			let BinaryOp {1421				lhs,1422				op: optype,1423				rhs,1424			} = &**op;1425			LExpr::BinaryOp {1426				lhs: Box::new(analyze(lhs, stack, taint)),1427				op: *optype,1428				rhs: Box::new(analyze(rhs, stack, taint)),1429			}1430		}1431		Expr::AssertExpr(assert) => {1432			let AssertExpr { assert, rest } = &**assert;1433			let assert = Rc::new(analyze_assert(assert, stack, taint));1434			let rest = Box::new(analyze(rest, stack, taint));1435			LExpr::AssertExpr { assert, rest }1436		}1437		Expr::LocalExpr(binds, body) => analyze_local_expr(binds, body, stack, taint),1438		Expr::Import(kind, path_expr) => {1439			let Expr::Trivial(TrivialVal::Str(path)) = &**path_expr else {1440				stack.report_error(1441					"import path must be a string literal",1442					Some(kind.span.clone()),1443				);1444				return LExpr::BadLocal("bad import");1445			};1446			LExpr::Import {1447				kind: kind.clone(),1448				kind_span: kind.span.clone(),1449				path: path.clone(),1450			}1451		}1452		Expr::ErrorStmt(span, inner) => {1453			LExpr::Error(span.clone(), Box::new(analyze(inner, stack, taint)))1454		}1455		Expr::Apply(applicable, args, tailstrict) => {1456			let app = analyze(applicable, stack, taint);1457			let ArgsDesc {1458				unnamed,1459				names,1460				values,1461			} = &args.value;1462			let unnamed_l = unnamed1463				.iter()1464				.map(|a| Rc::new(analyze(a, stack, taint)))1465				.collect();1466			let values_l = values1467				.iter()1468				.map(|a| Rc::new(analyze(a, stack, taint)))1469				.collect();1470			LExpr::Apply {1471				applicable: Box::new(app),1472				args: Spanned::new(1473					LArgsDesc {1474						unnamed: unnamed_l,1475						names: names.clone(),1476						values: values_l,1477					},1478					args.span.clone(),1479				),1480				tailstrict: *tailstrict,1481			}1482		}1483		Expr::Index { indexable, parts } => {1484			let idx = analyze(indexable, stack, taint);1485			let parts_l = parts1486				.iter()1487				.map(|p| {1488					let value = analyze(&p.value, stack, taint);1489					LIndexPart {1490						span: p.span.clone(),1491						value,1492						#[cfg(feature = "exp-null-coaelse")]1493						null_coaelse: p.null_coaelse,1494					}1495				})1496				.collect();1497			LExpr::Index {1498				indexable: Box::new(idx),1499				parts: parts_l,1500			}1501		}1502		Expr::Function(span, params, body) => {1503			analyze_function(None, span, params, body, stack, taint)1504		}1505		Expr::IfElse(ifelse) => {1506			let IfElse {1507				cond,1508				cond_then,1509				cond_else,1510			} = &**ifelse;1511			let cond_l = analyze(&cond.cond, stack, taint);1512			let then_l = analyze(cond_then, stack, taint);1513			let else_l = cond_else1514				.as_ref()1515				.map(|e| Box::new(analyze(e, stack, taint)));1516			LExpr::IfElse {1517				cond: Box::new(cond_l),1518				cond_then: Box::new(then_l),1519				cond_else: else_l,1520			}1521		}1522		Expr::Slice(slice) => {1523			let Slice {1524				value,1525				slice: SliceDesc { start, end, step },1526			} = &**slice;1527			let value_l = analyze(value, stack, taint);1528			let start_l = start.as_ref().map(|e| analyze(&e.value, stack, taint));1529			let end_l = end.as_ref().map(|e| analyze(&e.value, stack, taint));1530			let step_l = step.as_ref().map(|e| analyze(&e.value, stack, taint));1531			LExpr::Slice(Box::new(LSliceExpr {1532				value: value_l,1533				start: start_l,1534				end: end_l,1535				step: step_l,1536			}))1537		}1538	}1539}15401541fn analyze_local_expr(1542	binds: &[BindSpec],1543	body: &Expr,1544	stack: &mut AnalysisStack,1545	taint: &mut AnalysisResult,1546) -> LExpr {1547	if binds.is_empty() {1548		return analyze(body, stack, taint);1549	}1550	let frame_start = stack.next_local_id();1551	let closure = stack.push_closure_a(frame_start);1552	let (l_binds, body_expr) = process_local_frame(binds, stack, taint, |stack, taint| {1553		analyze(body, stack, taint)1554	});1555	let frame_shape = stack.pop_closure(closure);1556	LExpr::LocalExpr(Box::new(LLocalExpr {1557		frame_shape,1558		binds: l_binds,1559		body: body_expr,1560	}))1561}15621563fn analyze_bind_value(1564	bind: &BindSpec,1565	stack: &mut AnalysisStack,1566	taint: &mut AnalysisResult,1567) -> LExpr {1568	match bind {1569		BindSpec::Field {1570			value: Expr::Function(span, params, value),1571			into: Destruct::Full(name),1572		} => analyze_function(Some(name.value.clone()), span, params, value, stack, taint),1573		BindSpec::Field { value, .. } => analyze(value, stack, taint),1574		BindSpec::Function {1575			params,1576			value,1577			name,1578		} => analyze_function(1579			Some(name.value.clone()),1580			&name.span,1581			params,1582			value,1583			stack,1584			taint,1585		),1586	}1587}15881589fn process_local_frame<R>(1590	binds: &[BindSpec],1591	stack: &mut AnalysisStack,1592	taint: &mut AnalysisResult,1593	body_fn: impl FnOnce(&mut AnalysisStack, &mut AnalysisResult) -> R,1594) -> (Vec<LBind>, R) {1595	let mut alloc = FrameAlloc::new(stack);15961597	let mut destructs: Vec<Option<LDestruct>> = Vec::with_capacity(binds.len());1598	for bind in binds {1599		destructs.push(alloc.alloc_bind(bind));1600	}1601	let mut pending = alloc.finish();16021603	let mut l_binds: Vec<LBind> = Vec::with_capacity(binds.len());1604	for (bind, destruct) in binds.iter().zip(destructs) {1605		let mut value_taint = AnalysisResult::default();1606		let (value_shape, value) = pending1607			.stack1608			.in_using_closure(|stack| analyze_bind_value(bind, stack, &mut value_taint));1609		taint.taint_by(value_taint);1610		if let Some(destruct) = destruct {1611			pending.record_spec_init(&destruct, value_taint);1612			l_binds.push(LBind {1613				destruct,1614				value_shape,1615				value: Rc::new(value),1616			});1617		} else {1618			pending.closures.push_spec(0, &[]);1619		}1620	}16211622	let body_frame = pending.finish();1623	let result = body_fn(body_frame.stack, taint);1624	body_frame.finish();16251626	(l_binds, result)1627}16281629fn analyze_function(1630	name: Option<IStr>,1631	span: &Span,1632	params: &ExprParams,1633	body: &Expr,1634	stack: &mut AnalysisStack,1635	taint: &mut AnalysisResult,1636) -> LExpr {1637	let mut alloc = FrameAlloc::new(stack);1638	let closure = alloc.push_locals_closure();16391640	let mut param_destructs: Vec<Option<LDestruct>> = Vec::with_capacity(params.exprs.len());1641	for p in &params.exprs {1642		param_destructs.push(alloc.alloc_destruct(&p.destruct));1643	}16441645	let mut pending = alloc.finish();16461647	let mut l_params: Vec<LParam> = Vec::with_capacity(params.exprs.len());1648	for (p, destruct) in params.exprs.iter().zip(param_destructs) {1649		let mut value_taint = AnalysisResult::default();1650		let default = p.default.as_ref().map_or_else(1651			|| None,1652			|d| {1653				Some(1654					pending1655						.stack1656						.in_using_closure(|stack| Rc::new(analyze(d, stack, &mut value_taint))),1657				)1658			},1659		);1660		taint.taint_by(value_taint);1661		if let Some(destruct) = destruct {1662			let name = match &p.destruct {1663				Destruct::Full(n) => Some(n.value.clone()),1664				#[cfg(feature = "exp-destruct")]1665				_ => None,1666			};1667			pending.record_spec_init(&destruct, value_taint);1668			l_params.push(LParam {1669				name,1670				destruct,1671				default,1672			});1673		} else {1674			pending.closures.push_spec(0, &[]);1675		}1676	}16771678	let body_frame = pending.finish();1679	let body_expr = analyze(body, body_frame.stack, taint);1680	body_frame.finish();1681	let body_shape = stack.pop_closure(closure);16821683	// function(x) x is an identity function1684	if l_params.len() == 1 && l_params[0].default.is_none() {1685		#[allow(irrefutable_let_patterns, reason = "refutable with exp-destruct")]1686		if let LDestruct::Full(param_slot) = &l_params[0].destruct1687			&& let LExpr::Slot(LSlot::Local(s)) = &body_expr1688			&& s == param_slot1689		{1690			stack.report_warning(1691				"do not define identity functions manually, use std.id instead",1692				Some(span.clone()),1693			);1694			return LExpr::IdentityFunction {};1695		}1696	}16971698	LExpr::Function(Rc::new(LFunction {1699		name,1700		params: l_params,1701		signature: params.signature.clone(),1702		body_shape,1703		body: Rc::new(body_expr),1704	}))1705}17061707fn analyze_obj_body(1708	obj: &ObjBody,1709	stack: &mut AnalysisStack,1710	taint: &mut AnalysisResult,1711) -> LObjBody {1712	match obj {1713		ObjBody::MemberList(members) => {1714			let lowered = analyze_obj_members(members, stack, taint);1715			match try_lower_static(lowered) {1716				Ok(static_members) => LObjBody::StaticMembers(Box::new(static_members)),1717				Err(member_list) => LObjBody::MemberList(member_list),1718			}1719		}1720		ObjBody::ObjComp(comp) => LObjBody::ObjComp(Box::new(analyze_obj_comp(comp, stack, taint))),1721	}1722}17231724fn try_lower_static(members: LObjMembers) -> Result<LObjStaticMembers, LObjMembers> {1725	let mut seen: FxHashSet<IStr> = FxHashSet::with_capacity(members.fields.len());1726	for f in &members.fields {1727		match &f.name {1728			LFieldName::Fixed(name) => {1729				if !seen.insert(name.clone()) {1730					return Err(members);1731				}1732			}1733			LFieldName::Dyn(_) => return Err(members),1734		}1735	}17361737	let LObjMembers {1738		frame_shape,1739		this,1740		set_dollar,1741		uses_super,1742		locals,1743		asserts,1744		fields,1745	} = members;17461747	let mut shape_fields = Vec::with_capacity(fields.len());1748	let mut bindings = Vec::with_capacity(fields.len());1749	let mut next_index = FieldIndex::default();1750	for f in fields {1751		let LFieldName::Fixed(name) = f.name else {1752			unreachable!("checked above");1753		};1754		let index = next_index;1755		next_index = next_index.next();1756		shape_fields.push(ShapeField {1757			name,1758			flags: ObjFieldFlags::new(f.plus, f.visibility),1759			location: None,1760			index,1761		});1762		bindings.push(f.value);1763	}17641765	Ok(LObjStaticMembers {1766		frame_shape,1767		this,1768		set_dollar,1769		uses_super,1770		locals,1771		asserts,1772		shape: Rc::new(ObjShape::new(shape_fields)),1773		bindings,1774	})1775}17761777fn analyze_obj_members(1778	members: &ObjMembers,1779	stack: &mut AnalysisStack,1780	taint: &mut AnalysisResult,1781) -> LObjMembers {1782	let ObjMembers {1783		locals,1784		asserts,1785		fields,1786	} = members;17871788	// Names are analyzed in enclosing scope, they can't depend on locals or self/super1789	let field_names: Vec<LFieldName> = fields1790		.iter()1791		.map(|f| match &f.name.value {1792			FieldName::Fixed(s) => LFieldName::Fixed(s.clone()),1793			FieldName::Dyn(e) => LFieldName::Dyn(analyze(e, stack, taint)),1794		})1795		.collect();17961797	let (usage, frame_shape, (l_binds, (l_asserts_opt, l_fields))) =1798		stack.in_object_scope(|stack| {1799			process_local_frame(locals, stack, taint, |stack, taint| {1800				let l_asserts_opt = if asserts.is_empty() {1801					None1802				} else {1803					let (shape, l_asserts) = stack.in_using_closure(|stack| {1804						let mut l_asserts = Vec::with_capacity(asserts.len());1805						for a in asserts {1806							let mut assert_taint = AnalysisResult::default();1807							l_asserts.push(analyze_assert(a, stack, &mut assert_taint));1808							taint.taint_by(assert_taint);1809						}1810						l_asserts1811					});1812					Some(Rc::new(LObjAsserts {1813						shape,1814						asserts: l_asserts,1815					}))1816				};1817				let mut l_fields = Vec::with_capacity(fields.len());1818				for (f, name) in fields.iter().zip(field_names) {1819					let value = stack.in_using_closure(|stack| {1820						if let Some(params) = &f.params {1821							analyze_function(1822								name.function_name(),1823								&f.name.span,1824								params,1825								&f.value,1826								stack,1827								taint,1828							)1829						} else {1830							analyze(&f.value, stack, taint)1831						}1832					});1833					l_fields.push(LFieldMember {1834						name,1835						plus: f.plus,1836						visibility: f.visibility,1837						value: Rc::new(value),1838					});1839				}1840				(l_asserts_opt, l_fields)1841			})1842		});1843	// `this` was allocated as the first local of the object's frame,1844	// so its slot is 0 within that frame.1845	let this_slot = usage.this_used.then_some(LocalSlot(0));1846	LObjMembers {1847		frame_shape,1848		this: this_slot,1849		set_dollar: usage.set_dollar,1850		uses_super: usage.uses_super,1851		locals: Rc::new(l_binds),1852		asserts: l_asserts_opt,1853		fields: l_fields,1854	}1855}18561857fn analyze_obj_comp(1858	comp: &ObjComp,1859	stack: &mut AnalysisStack,1860	taint: &mut AnalysisResult,1861) -> LObjComp {1862	let res = analyze_comp_specs(&comp.compspecs, stack, taint, |stack, taint| {1863		let field_name = match &comp.field.name.value {1864			FieldName::Fixed(s) => LFieldName::Fixed(s.clone()),1865			FieldName::Dyn(e) => LFieldName::Dyn(analyze(e, stack, taint)),1866		};18671868		let (usage, frame_shape, body) = stack.in_object_scope(|stack| {1869			process_local_frame(&comp.locals, stack, taint, |stack, taint| {1870				let value = stack.in_using_closure(|stack| {1871					if let Some(params) = &comp.field.params {1872						analyze_function(1873							None,1874							&comp.field.name.span,1875							params,1876							&comp.field.value,1877							stack,1878							taint,1879						)1880					} else {1881						analyze(&comp.field.value, stack, taint)1882					}1883				});1884				LFieldMember {1885					name: field_name,1886					plus: comp.field.plus,1887					visibility: comp.field.visibility,1888					value: Rc::new(value),1889				}1890			})1891		});1892		(usage, frame_shape, body)1893	});1894	let (usage, frame_shape, (locals, field)) = res.inner;1895	let this_slot = usage.this_used.then_some(LocalSlot(0));1896	LObjComp {1897		frame_shape: Rc::new(frame_shape),1898		this: this_slot,1899		set_dollar: usage.set_dollar,1900		uses_super: usage.uses_super,1901		locals: Rc::new(locals),1902		field,1903		compspecs: res.compspecs,1904	}1905}19061907fn analyze_arr_comp(1908	inner: &Expr,1909	specs: &[CompSpec],1910	stack: &mut AnalysisStack,1911	taint: &mut AnalysisResult,1912) -> LExpr {1913	let res = analyze_comp_specs(specs, stack, taint, |stack, taint| {1914		stack.in_using_closure(|stack| analyze(inner, stack, taint))1915	});1916	let (value_shape, value) = res.inner;1917	LExpr::ArrComp(Box::new(LArrComp {1918		value_shape,1919		value: Rc::new(value),1920		compspecs: res.compspecs,1921	}))1922}19231924fn analyze_comp_specs<R>(1925	specs: &[CompSpec],1926	stack: &mut AnalysisStack,1927	taint: &mut AnalysisResult,1928	inside: impl FnOnce(&mut AnalysisStack, &mut AnalysisResult) -> R,1929) -> CompSpecResult<R> {1930	fn go<R>(1931		idx: usize,1932		specs: &[CompSpec],1933		outer_depth: u32,1934		stack: &mut AnalysisStack,1935		taint: &mut AnalysisResult,1936		inside: impl FnOnce(&mut AnalysisStack, &mut AnalysisResult) -> R,1937	) -> (R, Vec<LCompSpec>) {1938		if idx >= specs.len() {1939			return (inside(stack, taint), Vec::new());1940		}1941		match &specs[idx] {1942			CompSpec::IfSpec(IfSpecData { cond, .. }) => {1943				let cond_l = analyze(cond, stack, taint);1944				let (r, mut rest) = go(idx + 1, specs, outer_depth, stack, taint, inside);1945				rest.insert(0, LCompSpec::If(cond_l));1946				(r, rest)1947			}1948			CompSpec::ForSpec(ForSpecData { destruct, over }) => {1949				let mut over_taint = AnalysisResult::default();1950				let over_l = analyze(over, stack, &mut over_taint);1951				let loop_invariant = over_taint.local_dependent_depth > outer_depth;1952				taint.taint_by(over_taint);19531954				let mut alloc = FrameAlloc::new(stack);1955				let closure = alloc.push_locals_closure();1956				let Some(l_destruct) = alloc.alloc_destruct(destruct) else {1957					stack.pop_closure(closure);1958					return go(idx + 1, specs, outer_depth, stack, taint, inside);1959				};1960				let mut pending = alloc.finish();19611962				let var_analysis = AnalysisResult::default();1963				pending.record_spec_init(&l_destruct, var_analysis);19641965				let body_frame = pending.finish();1966				let (r, mut rest) =1967					go(idx + 1, specs, outer_depth, body_frame.stack, taint, inside);1968				body_frame.finish();1969				let frame_shape = stack.pop_closure(closure);19701971				rest.insert(1972					0,1973					LCompSpec::For {1974						frame_shape,1975						destruct: l_destruct,1976						over: over_l,1977						loop_invariant,1978					},1979				);1980				(r, rest)1981			}1982			#[cfg(feature = "exp-object-iteration")]1983			CompSpec::ForObjSpec(data) => {1984				let mut over_taint = AnalysisResult::default();1985				let over_l = analyze(&data.over, stack, &mut over_taint);1986				let loop_invariant = over_taint.local_dependent_depth > outer_depth;1987				taint.taint_by(over_taint);19881989				let mut alloc = FrameAlloc::new(stack);1990				let closure = alloc.push_locals_closure();1991				let Some((_, key_slot)) = alloc.define_local(data.key.clone(), None) else {1992					stack.pop_closure(closure);1993					return go(idx + 1, specs, outer_depth, stack, taint, inside);1994				};1995				let Some(l_value) = alloc.alloc_destruct(&data.value) else {1996					stack.pop_closure(closure);1997					return go(idx + 1, specs, outer_depth, stack, taint, inside);1998				};1999				let mut pending = alloc.finish();20002001				let var_analysis = AnalysisResult::default();2002				pending.record_spec_init(&LDestruct::Full(key_slot), var_analysis);2003				pending.record_spec_init(&l_value, var_analysis);20042005				let body_frame = pending.finish();2006				let (r, mut rest) =2007					go(idx + 1, specs, outer_depth, body_frame.stack, taint, inside);2008				body_frame.finish();2009				let frame_shape = stack.pop_closure(closure);20102011				rest.insert(2012					0,2013					LCompSpec::ForObj {2014						frame_shape,2015						key: key_slot,2016						visibility: data.visibility,2017						value: l_value,2018						over: over_l,2019						loop_invariant,2020					},2021				);2022				(r, rest)2023			}2024		}2025	}2026	let outer_depth = stack.depth;2027	let (r, compspecs) = go(0, specs, outer_depth, stack, taint, inside);2028	CompSpecResult {2029		inner: r,2030		compspecs,2031	}2032}20332034struct CompSpecResult<R> {2035	inner: R,2036	compspecs: Vec<LCompSpec>,2037}20382039pub fn analyze_root(expr: &Expr, ctx: Vec<(IStr, LocalId)>) -> AnalysisReport {2040	let mut stack = AnalysisStack::new();2041	for (name, id) in ctx {2042		stack.define_external_local(name, id);2043	}20442045	let externals_count: u16 = stack2046		.local_defs2047		.len()2048		.try_into()2049		.expect("more than u16::MAX externals");2050	let closure = stack.push_root_closure(externals_count);20512052	let mut taint = AnalysisResult::default();2053	let lir = analyze(expr, &mut stack, &mut taint);20542055	let root_shape = stack.pop_closure(closure);2056	debug_assert!(2057		stack.closure_stack.is_empty(),2058		"closure stack imbalance after analyze"2059	);20602061	AnalysisReport {2062		lir,2063		root_shape,2064		root_analysis: taint,2065		diagnostics_list: stack.diagnostics,2066		errored: stack.errored,2067	}2068}20692070pub struct AnalysisReport {2071	pub lir: LExpr,2072	pub root_shape: ClosureShape,2073	pub root_analysis: AnalysisResult,2074	pub diagnostics_list: Vec<Diagnostic>,2075	pub errored: bool,2076}20772078#[cfg(test)]2079mod tests {2080	#[test]2081	#[cfg(not(feature = "exp-null-coaelse"))]2082	fn snapshots() {2083		use std::fs;20842085		use insta::{assert_snapshot, glob};2086		use jrsonnet_ir::Source;20872088		use super::*;20892090		fn render_diagnostics(src: &str, diags: &[Diagnostic]) -> String {2091			use std::fmt::Write;20922093			use hi_doc::{Formatting, SnippetBuilder, Text};20942095			let mut out = String::new();2096			let mut unspanned = Vec::new();2097			let mut spanned: Vec<&Diagnostic> = Vec::new();2098			for d in diags {2099				if d.span.is_some() {2100					spanned.push(d);2101				} else {2102					unspanned.push(d);2103				}2104			}2105			if !spanned.is_empty() {2106				let mut builder = SnippetBuilder::new(src);2107				for d in spanned {2108					let span = d.span.as_ref().expect("spanned");2109					let ab = match d.level {2110						DiagLevel::Error => {2111							builder.error(Text::fragment(d.message.clone(), Formatting::default()))2112						}2113						DiagLevel::Warning => builder2114							.warning(Text::fragment(d.message.clone(), Formatting::default())),2115					};2116					ab.range(span.range()).build();2117				}2118				out.push_str(&hi_doc::source_to_ansi(&builder.build()));2119			}2120			for d in unspanned {2121				let prefix = match d.level {2122					DiagLevel::Error => "error",2123					DiagLevel::Warning => "warning",2124				};2125				writeln!(out, "{prefix}: {}", d.message).expect("fmt");2126			}2127			out2128		}2129		fn fmt_depth(d: u32) -> String {2130			if d == u32::MAX {2131				"none".into()2132			} else {2133				d.to_string()2134			}2135		}21362137		glob!("analysis_tests/*.jsonnet", |path| {2138			let code = fs::read_to_string(path).expect("read test file");2139			let src = Source::new_virtual("<test>".into(), code.clone().into());2140			let expr = crate::parse_jsonnet(&code, src.clone()).expect("parse");2141			let report = analyze_root(&expr, Vec::new());21422143			let diagnostics = render_diagnostics(src.code(), &report.diagnostics_list);2144			// Strip ANSI escapes from diagnostics so snapshots are readable.2145			let diagnostics = strip_ansi_escapes::strip_str(&diagnostics);2146			let rendered = format!(2147				"--- source ---\n{}\n--- root analysis ---\nobject_dependent_depth: {}\nlocal_dependent_depth: {}\nerrored: {}\n--- diagnostics ---\n{}--- lir ---\n{:#?}\n",2148				code.trim_end(),2149				fmt_depth(report.root_analysis.object_dependent_depth),2150				fmt_depth(report.root_analysis.local_dependent_depth),2151				report.errored,2152				diagnostics,2153				report.lir,2154			);2155			assert_snapshot!(rendered);2156		});2157	}2158}
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -11,11 +11,11 @@
 	operator::evaluate_binary_op_special,
 };
 use crate::{
-	Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Result, ResultExt as _, SupThis,
-	Unbound, Val,
+	CcObjectAssertion, CcUnbound, Context, Error, MaybeUnbound, ObjValue, ObjValueBuilder,
+	ObjectAssertion, Result, ResultExt as _, StaticShapeOopObject, SupThis, Unbound, Val,
 	analyze::{
 		ClosureShape, LArgsDesc, LAssertStmt, LExpr, LFieldMember, LFieldName, LFunction,
-		LIndexPart, LObjAsserts, LObjBody, LObjMembers, LSlot,
+		LIndexPart, LObjAsserts, LObjBody, LObjMembers, LObjStaticMembers, LSlot,
 	},
 	arr::ArrValue,
 	bail,
@@ -469,6 +469,9 @@
 fn evaluate_obj_body(super_obj: Option<ObjValue>, ctx: Context, body: &LObjBody) -> Result<Val> {
 	match body {
 		LObjBody::MemberList(members) => evaluate_obj_members(super_obj, ctx, members),
+		LObjBody::StaticMembers(members) => {
+			Ok(evaluate_static_obj_members(super_obj, ctx, members))
+		}
 		LObjBody::ObjComp(comp) => evaluate_obj_comp(super_obj, ctx, comp),
 	}
 }
@@ -540,6 +543,84 @@
 	Ok(())
 }
 
+fn evaluate_static_obj_members(
+	super_obj: Option<ObjValue>,
+	ctx: Context,
+	members: &LObjStaticMembers,
+) -> Val {
+	#[derive(Trace)]
+	struct UnboundField<B: Trace> {
+		uctx: B,
+		value: Rc<(ClosureShape, LExpr)>,
+		name: IStr,
+	}
+	impl<B: Unbound<Bound = Context>> Unbound for UnboundField<B> {
+		type Bound = Val;
+		fn bind(&self, sup_this: SupThis) -> Result<Val> {
+			let a_ctx = self.uctx.bind(sup_this)?;
+			let b_ctx = Context::enter_using(&a_ctx, &self.value.0);
+			evaluate(b_ctx, &self.value.1)
+		}
+	}
+
+	let needs_unbound = members.this.is_some() || members.uses_super;
+
+	let mut bindings: Vec<MaybeUnbound> = Vec::with_capacity(members.bindings.len());
+
+	let assertion = if needs_unbound {
+		let uctx = CachedUnbound::new(evaluate_locals_unbound(
+			&ctx,
+			&members.frame_shape,
+			members.this,
+			members.locals.clone(),
+		));
+		for (binding, field) in members.bindings.iter().zip(members.shape.fields()) {
+			if let Some(v) = evaluate_trivial(&binding.1) {
+				bindings.push(MaybeUnbound::Const(v));
+				continue;
+			}
+			bindings.push(MaybeUnbound::Unbound(CcUnbound::new(UnboundField {
+				uctx: uctx.clone(),
+				value: binding.clone(),
+				name: field.name.clone(),
+			})));
+		}
+		members
+			.asserts
+			.as_ref()
+			.map(|a| CcObjectAssertion::new(evaluate_object_assertions_unbound(uctx, a.clone())))
+	} else {
+		let a_ctx = ctx
+			.pack_captures_sup_this(&members.frame_shape)
+			.enter(|fill, ctx| {
+				fill_letrec_binds(fill, ctx, &members.locals);
+			});
+		for binding in &members.bindings {
+			if let Some(v) = evaluate_trivial(&binding.1) {
+				bindings.push(MaybeUnbound::Const(v));
+				continue;
+			}
+			let env = Context::enter_using(&a_ctx, &binding.0);
+			let value = binding.clone();
+			bindings.push(MaybeUnbound::Bound(Thunk!(move || evaluate(env, &value.1))));
+		}
+		members.asserts.as_ref().map(|a| {
+			CcObjectAssertion::new(evaluate_object_assertions_static(a_ctx.clone(), a.clone()))
+		})
+	};
+
+	let mut builder = ObjValueBuilder::with_capacity(0);
+	if let Some(sup) = super_obj {
+		builder.with_super(sup);
+	}
+	builder.extend_with_core(StaticShapeOopObject::new(
+		members.shape.clone(),
+		bindings,
+		assertion,
+	));
+	Val::Obj(builder.build())
+}
+
 fn evaluate_obj_members(
 	super_obj: Option<ObjValue>,
 	ctx: Context,
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -144,6 +144,7 @@
 	Unbound(CcUnbound<Val>),
 	/// Value is object-independent
 	Bound(Thunk<Val>),
+	Const(Val),
 }
 
 impl Debug for MaybeUnbound {
@@ -157,6 +158,7 @@
 		match self {
 			Self::Unbound(v) => v.0.bind(sup_this),
 			Self::Bound(v) => Ok(v.evaluate()?),
+			Self::Const(v) => Ok(v.clone()),
 		}
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/obj/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj/mod.rs
+++ b/crates/jrsonnet-evaluator/src/obj/mod.rs
@@ -17,9 +17,11 @@
 use rustc_hash::{FxHashMap, FxHashSet};
 
 mod oop;
+mod static_shape;
 
 pub use jrsonnet_ir::Visibility;
 pub use oop::ObjValueBuilder;
+pub use static_shape::{ObjShape, ObjShapeBuilder, ShapeField, StaticShapeOopObject};
 
 use crate::{
 	CcUnbound, MaybeUnbound, Result, Thunk, Unbound, Val,
@@ -99,7 +101,7 @@
 #[derive(Clone, Copy, Acyclic)]
 pub struct ObjFieldFlags(u8);
 impl ObjFieldFlags {
-	fn new(add: bool, visibility: Visibility) -> Self {
+	pub fn new(add: bool, visibility: Visibility) -> Self {
 		let mut v = 0;
 		if add {
 			v |= 1;
@@ -140,7 +142,7 @@
 	pub location: Option<Span>,
 }
 
-cc_dyn!(CcObjectAssertion, ObjectAssertion);
+cc_dyn!(CcObjectAssertion, ObjectAssertion, pub fn new() {...});
 pub trait ObjectAssertion: Trace {
 	fn run(&self, sup_this: SupThis) -> Result<()>;
 }
@@ -203,6 +205,10 @@
 	fn field_visibility_core(&self, field: IStr) -> FieldVisibility;
 
 	fn run_assertions_core(&self, sup_this: SupThis) -> Result<()>;
+
+	fn has_assertion(&self) -> bool {
+		false
+	}
 }
 
 #[derive(Clone, Trace)]
@@ -1117,7 +1123,7 @@
 pub struct ExtendBuilder<'v>(&'v mut ObjValue);
 impl ObjMemberBuilder<ExtendBuilder<'_>> {
 	pub fn value(self, value: impl Into<Val>) {
-		self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));
+		self.binding(MaybeUnbound::Const(value.into()));
 	}
 	pub fn bindable(self, bindable: impl Unbound<Bound = Val>) {
 		self.binding(MaybeUnbound::Unbound(CcUnbound::new(bindable)));
modifiedcrates/jrsonnet-evaluator/src/obj/oop.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj/oop.rs
+++ b/crates/jrsonnet-evaluator/src/obj/oop.rs
@@ -113,6 +113,10 @@
 		}
 		Ok(())
 	}
+
+	fn has_assertion(&self) -> bool {
+		self.assertion.is_some()
+	}
 }
 
 #[allow(clippy::module_name_repetitions)]
@@ -177,6 +181,7 @@
 
 	pub fn extend_with_core(&mut self, core: impl ObjectCore) {
 		self.commit();
+		self.has_assertions |= core.has_assertion();
 		self.sup.push(CcObjectCore::new(core));
 	}
 
@@ -219,8 +224,7 @@
 impl ObjMemberBuilder<ValueBuilder<'_>> {
 	/// Inserts value, replacing if it is already defined
 	pub fn value(self, value: impl Into<Val>) {
-		let (receiver, name, idx, member) =
-			self.build_member(MaybeUnbound::Bound(Thunk::evaluated(value.into())));
+		let (receiver, name, idx, member) = self.build_member(MaybeUnbound::Const(value.into()));
 		let entry = receiver.0.new.this_entries.entry(name);
 		entry.insert_entry((member, idx));
 	}
@@ -233,7 +237,7 @@
 
 	/// Tries to insert value, returns an error if it was already defined
 	pub fn try_value(self, value: impl Into<Val>) -> Result<()> {
-		self.try_thunk(Thunk::evaluated(value.into()))
+		self.binding(MaybeUnbound::Const(value.into()))
 	}
 	pub fn try_thunk(self, value: impl Into<Thunk<Val>>) -> Result<()> {
 		self.binding(MaybeUnbound::Bound(value.into()))
addedcrates/jrsonnet-evaluator/src/obj/static_shape.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/obj/static_shape.rs
@@ -0,0 +1,213 @@
+use std::{fmt, ops::ControlFlow, rc::Rc};
+
+use jrsonnet_gcmodule::{Acyclic, Trace, TraceBox};
+use jrsonnet_interner::IStr;
+use jrsonnet_ir::Span;
+
+use super::{
+	CcObjectAssertion, EnumFields, EnumFieldsHandler, FieldVisibility, GetFor,
+	HasFieldIncludeHidden, ObjFieldFlags, ObjectCore, SupThis, Visibility,
+	ordering::{FieldIndex, SuperDepth},
+};
+use crate::{MaybeUnbound, Result};
+
+#[derive(Acyclic, Debug)]
+pub struct ShapeField {
+	pub name: IStr,
+	pub flags: ObjFieldFlags,
+	pub location: Option<Span>,
+	pub index: FieldIndex,
+}
+
+#[derive(Acyclic, Debug)]
+pub struct ObjShape {
+	fields: Vec<ShapeField>,
+}
+
+impl ObjShape {
+	#[must_use]
+	pub fn new(fields: Vec<ShapeField>) -> Self {
+		Self { fields }
+	}
+
+	#[inline]
+	pub fn fields(&self) -> &[ShapeField] {
+		&self.fields
+	}
+
+	#[inline]
+	#[must_use]
+	pub fn len(&self) -> usize {
+		self.fields.len()
+	}
+
+	#[inline]
+	#[must_use]
+	pub fn is_empty(&self) -> bool {
+		self.fields.is_empty()
+	}
+
+	#[inline]
+	pub fn find_index(&self, name: &IStr) -> Option<usize> {
+		self.fields.iter().position(|f| &f.name == name)
+	}
+}
+
+#[derive(Trace)]
+pub struct StaticShapeOopObject {
+	shape: Rc<ObjShape>,
+	bindings: TraceBox<[MaybeUnbound]>,
+	assertion: Option<CcObjectAssertion>,
+}
+
+impl fmt::Debug for StaticShapeOopObject {
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+		f.debug_struct("StaticShapeOopObject")
+			.field("shape", &self.shape)
+			.field("has_assertion", &self.assertion.is_some())
+			.finish_non_exhaustive()
+	}
+}
+
+impl StaticShapeOopObject {
+	pub fn new(
+		shape: Rc<ObjShape>,
+		bindings: Vec<MaybeUnbound>,
+		assertion: Option<CcObjectAssertion>,
+	) -> Self {
+		debug_assert_eq!(
+			shape.fields.len(),
+			bindings.len(),
+			"shape arity must match bindings"
+		);
+		Self {
+			shape,
+			bindings: TraceBox(bindings.into_boxed_slice()),
+			assertion,
+		}
+	}
+
+	#[inline]
+	#[must_use]
+	pub const fn shape(&self) -> &Rc<ObjShape> {
+		&self.shape
+	}
+}
+
+impl ObjectCore for StaticShapeOopObject {
+	fn enum_fields_core(
+		&self,
+		super_depth: &mut SuperDepth,
+		handler: &mut EnumFieldsHandler<'_>,
+	) -> bool {
+		for field in &self.shape.fields {
+			if matches!(
+				handler(
+					*super_depth,
+					field.index,
+					field.name.clone(),
+					EnumFields::Normal(field.flags.visibility()),
+				),
+				ControlFlow::Break(())
+			) {
+				return false;
+			}
+		}
+		true
+	}
+
+	fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {
+		if self.shape.find_index(&name).is_some() {
+			HasFieldIncludeHidden::Exists
+		} else {
+			HasFieldIncludeHidden::NotFound
+		}
+	}
+
+	fn get_for_core(&self, key: IStr, sup_this: SupThis, omit_only: bool) -> Result<GetFor> {
+		if omit_only {
+			return Ok(GetFor::NotFound);
+		}
+		let Some(i) = self.shape.find_index(&key) else {
+			return Ok(GetFor::NotFound);
+		};
+		let field = &self.shape.fields[i];
+		let v = self.bindings[i].evaluate(sup_this)?;
+		Ok(if field.flags.add() {
+			GetFor::SuperPlus(v)
+		} else {
+			GetFor::Final(v)
+		})
+	}
+
+	fn field_visibility_core(&self, field: IStr) -> FieldVisibility {
+		self.shape
+			.find_index(&field)
+			.map_or(FieldVisibility::NotFound, |i| {
+				FieldVisibility::Found(self.shape.fields[i].flags.visibility())
+			})
+	}
+
+	fn run_assertions_core(&self, sup_this: SupThis) -> Result<()> {
+		if let Some(assertion) = &self.assertion {
+			assertion.0.run(sup_this)?;
+		}
+		Ok(())
+	}
+
+	fn has_assertion(&self) -> bool {
+		self.assertion.is_some()
+	}
+}
+
+pub struct ObjShapeBuilder {
+	fields: Vec<ShapeField>,
+	next_index: FieldIndex,
+}
+
+impl ObjShapeBuilder {
+	#[must_use]
+	pub fn new() -> Self {
+		Self {
+			fields: Vec::new(),
+			next_index: FieldIndex::default(),
+		}
+	}
+
+	#[must_use]
+	pub fn with_capacity(cap: usize) -> Self {
+		Self {
+			fields: Vec::with_capacity(cap),
+			next_index: FieldIndex::default(),
+		}
+	}
+
+	pub fn field(
+		&mut self,
+		name: impl Into<IStr>,
+		visibility: Visibility,
+		add: bool,
+		location: Option<Span>,
+	) -> &mut Self {
+		let index = self.next_index;
+		self.next_index = self.next_index.next();
+		self.fields.push(ShapeField {
+			name: name.into(),
+			flags: ObjFieldFlags::new(add, visibility),
+			location,
+			index,
+		});
+		self
+	}
+
+	#[must_use]
+	pub fn build(self) -> ObjShape {
+		ObjShape::new(self.fields)
+	}
+}
+
+impl Default for ObjShapeBuilder {
+	fn default() -> Self {
+		Self::new()
+	}
+}
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@array_comp.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@array_comp.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@array_comp.jsonnet.snap
@@ -1,5 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/array_comp.jsonnet
 ---
@@ -26,8 +27,10 @@
                 ),
             ),
             op: Mul,
-            rhs: Num(
-                2.0,
+            rhs: Trivial(
+                Num(
+                    2.0,
+                ),
             ),
         },
         compspecs: [
@@ -41,12 +44,8 @@
                         0,
                     ),
                 ),
-                over: Arr {
-                    shape: ClosureShape {
-                        captures: [],
-                        n_locals: 0,
-                    },
-                    items: [
+                over: ArrConst(
+                    [
                         Num(
                             1.0,
                         ),
@@ -57,7 +56,7 @@
                             3.0,
                         ),
                     ],
-                },
+                ),
                 loop_invariant: true,
             },
             If(
@@ -70,8 +69,10 @@
                         ),
                     ),
                     op: Gt,
-                    rhs: Num(
-                        1.0,
+                    rhs: Trivial(
+                        Num(
+                            1.0,
+                        ),
                     ),
                 },
             ),
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@dollar_deeply_nested.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@dollar_deeply_nested.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@dollar_deeply_nested.jsonnet.snap
@@ -20,8 +20,8 @@
 --- diagnostics ---
 --- lir ---
 Obj(
-    MemberList(
-        LObjMembers {
+    StaticMembers(
+        LObjStaticMembers {
             frame_shape: ClosureShape {
                 captures: [],
                 n_locals: 1,
@@ -35,74 +35,145 @@
             uses_super: false,
             locals: [],
             asserts: None,
-            fields: [
-                LFieldMember {
-                    name: Fixed(
-                        "top",
-                    ),
-                    plus: false,
-                    visibility: Normal,
-                    value: (
-                        ClosureShape {
-                            captures: [],
-                            n_locals: 0,
+            shape: ObjShape {
+                fields: [
+                    ShapeField {
+                        name: "top",
+                        flags: ObjFieldFlags {
+                            add: false,
+                            visibility: Normal,
+                        },
+                        location: None,
+                        index: FieldIndex(
+                            (),
+                        ),
+                    },
+                    ShapeField {
+                        name: "a",
+                        flags: ObjFieldFlags {
+                            add: false,
+                            visibility: Normal,
                         },
+                        location: None,
+                        index: FieldIndex(
+                            (),
+                        ),
+                    },
+                ],
+            },
+            bindings: [
+                (
+                    ClosureShape {
+                        captures: [],
+                        n_locals: 0,
+                    },
+                    Trivial(
                         Str(
                             "outer",
                         ),
                     ),
-                },
-                LFieldMember {
-                    name: Fixed(
-                        "a",
-                    ),
-                    plus: false,
-                    visibility: Normal,
-                    value: (
-                        ClosureShape {
-                            captures: [],
-                            n_locals: 0,
-                        },
-                        Obj(
-                            MemberList(
-                                LObjMembers {
-                                    frame_shape: ClosureShape {
-                                        captures: [
-                                            Local(
-                                                LocalSlot(
-                                                    0,
-                                                ),
+                ),
+                (
+                    ClosureShape {
+                        captures: [],
+                        n_locals: 0,
+                    },
+                    Obj(
+                        StaticMembers(
+                            LObjStaticMembers {
+                                frame_shape: ClosureShape {
+                                    captures: [
+                                        Local(
+                                            LocalSlot(
+                                                0,
                                             ),
-                                        ],
-                                        n_locals: 1,
-                                    },
-                                    this: None,
-                                    set_dollar: false,
-                                    uses_super: false,
-                                    locals: [],
-                                    asserts: None,
+                                        ),
+                                    ],
+                                    n_locals: 1,
+                                },
+                                this: None,
+                                set_dollar: false,
+                                uses_super: false,
+                                locals: [],
+                                asserts: None,
+                                shape: ObjShape {
                                     fields: [
-                                        LFieldMember {
-                                            name: Fixed(
-                                                "b",
+                                        ShapeField {
+                                            name: "b",
+                                            flags: ObjFieldFlags {
+                                                add: false,
+                                                visibility: Normal,
+                                            },
+                                            location: None,
+                                            index: FieldIndex(
+                                                (),
                                             ),
-                                            plus: false,
-                                            visibility: Normal,
-                                            value: (
-                                                ClosureShape {
-                                                    captures: [
-                                                        Capture(
-                                                            CaptureSlot(
-                                                                0,
+                                        },
+                                    ],
+                                },
+                                bindings: [
+                                    (
+                                        ClosureShape {
+                                            captures: [
+                                                Capture(
+                                                    CaptureSlot(
+                                                        0,
+                                                    ),
+                                                ),
+                                            ],
+                                            n_locals: 0,
+                                        },
+                                        Obj(
+                                            StaticMembers(
+                                                LObjStaticMembers {
+                                                    frame_shape: ClosureShape {
+                                                        captures: [
+                                                            Capture(
+                                                                CaptureSlot(
+                                                                    0,
+                                                                ),
                                                             ),
+                                                        ],
+                                                        n_locals: 1,
+                                                    },
+                                                    this: Some(
+                                                        LocalSlot(
+                                                            0,
                                                         ),
-                                                    ],
-                                                    n_locals: 0,
-                                                },
-                                                Obj(
-                                                    MemberList(
-                                                        LObjMembers {
-                                                            frame_shape: ClosureShape {
+                                                    ),
+                                                    set_dollar: false,
+                                                    uses_super: false,
+                                                    locals: [],
+                                                    asserts: None,
+                                                    shape: ObjShape {
+                                                        fields: [
+                                                            ShapeField {
+                                                                name: "c",
+                                                                flags: ObjFieldFlags {
+                                                                    add: false,
+                                                                    visibility: Normal,
+                                                                },
+                                                                location: None,
+                                                                index: FieldIndex(
+                                                                    (),
+                                                                ),
+                                                            },
+                                                            ShapeField {
+                                                                name: "d",
+                                                                flags: ObjFieldFlags {
+                                                                    add: false,
+                                                                    visibility: Normal,
+                                                                },
+                                                                location: None,
+                                                                index: FieldIndex(
+                                                                    (),
+                                                                ),
+                                                            },
+                                                        ],
+                                                    },
+                                                    bindings: [
+                                                        (
+                                                            ClosureShape {
                                                                 captures: [
                                                                     Capture(
                                                                         CaptureSlot(
@@ -110,86 +181,51 @@
                                                                         ),
                                                                     ),
                                                                 ],
-                                                                n_locals: 1,
+                                                                n_locals: 0,
                                                             },
-                                                            this: Some(
-                                                                LocalSlot(
-                                                                    0,
-                                                                ),
-                                                            ),
-                                                            set_dollar: false,
-                                                            uses_super: false,
-                                                            locals: [],
-                                                            asserts: None,
-                                                            fields: [
-                                                                LFieldMember {
-                                                                    name: Fixed(
-                                                                        "c",
+                                                            Index {
+                                                                indexable: Slot(
+                                                                    Capture(
+                                                                        CaptureSlot(
+                                                                            0,
+                                                                        ),
                                                                     ),
-                                                                    plus: false,
-                                                                    visibility: Normal,
-                                                                    value: (
-                                                                        ClosureShape {
-                                                                            captures: [
-                                                                                Capture(
-                                                                                    CaptureSlot(
-                                                                                        0,
-                                                                                    ),
-                                                                                ),
-                                                                            ],
-                                                                            n_locals: 0,
-                                                                        },
-                                                                        Index {
-                                                                            indexable: Slot(
-                                                                                Capture(
-                                                                                    CaptureSlot(
-                                                                                        0,
-                                                                                    ),
-                                                                                ),
+                                                                ),
+                                                                parts: [
+                                                                    LIndexPart {
+                                                                        span: virtual:<test>:45-48,
+                                                                        value: Trivial(
+                                                                            Str(
+                                                                                "top",
                                                                             ),
-                                                                            parts: [
-                                                                                LIndexPart {
-                                                                                    span: virtual:<test>:45-48,
-                                                                                    value: Str(
-                                                                                        "top",
-                                                                                    ),
-                                                                                },
-                                                                            ],
-                                                                        },
-                                                                    ),
-                                                                },
-                                                                LFieldMember {
-                                                                    name: Fixed(
-                                                                        "d",
-                                                                    ),
-                                                                    plus: false,
-                                                                    visibility: Normal,
-                                                                    value: (
-                                                                        ClosureShape {
-                                                                            captures: [],
-                                                                            n_locals: 0,
-                                                                        },
-                                                                        Slot(
-                                                                            Local(
-                                                                                LocalSlot(
-                                                                                    0,
-                                                                                ),
-                                                                            ),
                                                                         ),
+                                                                    },
+                                                                ],
+                                                            },
+                                                        ),
+                                                        (
+                                                            ClosureShape {
+                                                                captures: [],
+                                                                n_locals: 0,
+                                                            },
+                                                            Slot(
+                                                                Local(
+                                                                    LocalSlot(
+                                                                        0,
                                                                     ),
-                                                                },
-                                                            ],
-                                                        },
-                                                    ),
-                                                ),
+                                                                ),
+                                                            ),
+                                                        ),
+                                                    ],
+                                                },
                                             ),
-                                        },
-                                    ],
-                                },
-                            ),
+                                        ),
+                                    ),
+                                ],
+                            },
                         ),
                     ),
-                },
+                ),
             ],
         },
     ),
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@dollar_outside_object.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@dollar_outside_object.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@dollar_outside_object.jsonnet.snap
@@ -1,5 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/dollar_outside_object.jsonnet
 ---
@@ -21,8 +22,10 @@
     parts: [
         LIndexPart {
             span: virtual:<test>:2-3,
-            value: Str(
-                "a",
+            value: Trivial(
+                Str(
+                    "a",
+                ),
             ),
         },
     ],
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@function_def.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@function_def.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@function_def.jsonnet.snap
@@ -1,5 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/function_def.jsonnet
 ---
@@ -108,11 +109,15 @@
             ),
             args: LArgsDesc {
                 unnamed: [
-                    Num(
-                        1.0,
+                    Trivial(
+                        Num(
+                            1.0,
+                        ),
                     ),
-                    Num(
-                        2.0,
+                    Trivial(
+                        Num(
+                            2.0,
+                        ),
                     ),
                 ],
                 names: [],
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@hoistable_local.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@hoistable_local.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@hoistable_local.jsonnet.snap
@@ -1,5 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/hoistable_local.jsonnet
 ---
@@ -31,8 +32,10 @@
                     captures: [],
                     n_locals: 0,
                 },
-                value: Num(
-                    1.0,
+                value: Trivial(
+                    Num(
+                        1.0,
+                    ),
                 ),
             },
         ],
@@ -60,12 +63,16 @@
                             n_locals: 0,
                         },
                         value: BinaryOp {
-                            lhs: Num(
-                                10.0,
+                            lhs: Trivial(
+                                Num(
+                                    10.0,
+                                ),
                             ),
                             op: Add,
-                            rhs: Num(
-                                20.0,
+                            rhs: Trivial(
+                                Num(
+                                    20.0,
+                                ),
                             ),
                         },
                     },
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@ifelse.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@ifelse.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@ifelse.jsonnet.snap
@@ -1,7 +1,8 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
-input_file: crates/jrsonnet-evaluator/src/analyze_tests/ifelse.jsonnet
+input_file: crates/jrsonnet-evaluator/src/analysis_tests/ifelse.jsonnet
 ---
 --- source ---
 if true then 1 else 2
@@ -12,15 +13,21 @@
 --- diagnostics ---
 --- lir ---
 IfElse {
-    cond: Bool(
-        true,
+    cond: Trivial(
+        Bool(
+            true,
+        ),
     ),
-    cond_then: Num(
-        1.0,
+    cond_then: Trivial(
+        Num(
+            1.0,
+        ),
     ),
     cond_else: Some(
-        Num(
-            2.0,
+        Trivial(
+            Num(
+                2.0,
+            ),
         ),
     ),
 }
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@literal.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@literal.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@literal.jsonnet.snap
@@ -1,7 +1,8 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
-input_file: crates/jrsonnet-evaluator/src/analyze_tests/literal.jsonnet
+input_file: crates/jrsonnet-evaluator/src/analysis_tests/literal.jsonnet
 ---
 --- source ---
 42
@@ -11,6 +12,8 @@
 errored: false
 --- diagnostics ---
 --- lir ---
-Num(
-    42.0,
+Trivial(
+    Num(
+        42.0,
+    ),
 )
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@loop_invariant.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@loop_invariant.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@loop_invariant.jsonnet.snap
@@ -1,5 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/loop_invariant.jsonnet
 ---
@@ -64,19 +65,25 @@
                         parts: [
                             LIndexPart {
                                 span: virtual:<test>:21-26,
-                                value: Str(
-                                    "range",
+                                value: Trivial(
+                                    Str(
+                                        "range",
+                                    ),
                                 ),
                             },
                         ],
                     },
                     args: LArgsDesc {
                         unnamed: [
-                            Num(
-                                1.0,
+                            Trivial(
+                                Num(
+                                    1.0,
+                                ),
                             ),
-                            Num(
-                                1000.0,
+                            Trivial(
+                                Num(
+                                    1000.0,
+                                ),
                             ),
                         ],
                         names: [],
@@ -110,19 +117,25 @@
                         parts: [
                             LIndexPart {
                                 span: virtual:<test>:49-54,
-                                value: Str(
-                                    "range",
+                                value: Trivial(
+                                    Str(
+                                        "range",
+                                    ),
                                 ),
                             },
                         ],
                     },
                     args: LArgsDesc {
                         unnamed: [
-                            Num(
-                                1.0,
+                            Trivial(
+                                Num(
+                                    1.0,
+                                ),
                             ),
-                            Num(
-                                1000.0,
+                            Trivial(
+                                Num(
+                                    1000.0,
+                                ),
                             ),
                         ],
                         names: [],
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@mutual_recursion.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@mutual_recursion.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@mutual_recursion.jsonnet.snap
@@ -1,5 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/mutual_recursion.jsonnet
 ---
@@ -46,8 +47,10 @@
                     captures: [],
                     n_locals: 0,
                 },
-                value: Num(
-                    1.0,
+                value: Trivial(
+                    Num(
+                        1.0,
+                    ),
                 ),
             },
         ],
@@ -60,8 +63,10 @@
                 ),
             ),
             op: Add,
-            rhs: Num(
-                2.0,
+            rhs: Trivial(
+                Num(
+                    2.0,
+                ),
             ),
         },
     },
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@nested_object_independent.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@nested_object_independent.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@nested_object_independent.jsonnet.snap
@@ -17,8 +17,8 @@
 --- diagnostics ---
 --- lir ---
 Obj(
-    MemberList(
-        LObjMembers {
+    StaticMembers(
+        LObjStaticMembers {
             frame_shape: ClosureShape {
                 captures: [],
                 n_locals: 1,
@@ -28,103 +28,133 @@
             uses_super: false,
             locals: [],
             asserts: None,
-            fields: [
-                LFieldMember {
-                    name: Fixed(
-                        "a",
-                    ),
-                    plus: false,
-                    visibility: Normal,
-                    value: (
-                        ClosureShape {
-                            captures: [],
-                            n_locals: 0,
+            shape: ObjShape {
+                fields: [
+                    ShapeField {
+                        name: "a",
+                        flags: ObjFieldFlags {
+                            add: false,
+                            visibility: Normal,
                         },
+                        location: None,
+                        index: FieldIndex(
+                            (),
+                        ),
+                    },
+                    ShapeField {
+                        name: "b",
+                        flags: ObjFieldFlags {
+                            add: false,
+                            visibility: Normal,
+                        },
+                        location: None,
+                        index: FieldIndex(
+                            (),
+                        ),
+                    },
+                ],
+            },
+            bindings: [
+                (
+                    ClosureShape {
+                        captures: [],
+                        n_locals: 0,
+                    },
+                    Trivial(
                         Num(
                             1.0,
                         ),
                     ),
-                },
-                LFieldMember {
-                    name: Fixed(
-                        "b",
-                    ),
-                    plus: false,
-                    visibility: Normal,
-                    value: (
-                        ClosureShape {
-                            captures: [],
-                            n_locals: 0,
-                        },
-                        Obj(
-                            MemberList(
-                                LObjMembers {
-                                    frame_shape: ClosureShape {
-                                        captures: [],
-                                        n_locals: 1,
-                                    },
-                                    this: Some(
-                                        LocalSlot(
-                                            0,
-                                        ),
+                ),
+                (
+                    ClosureShape {
+                        captures: [],
+                        n_locals: 0,
+                    },
+                    Obj(
+                        StaticMembers(
+                            LObjStaticMembers {
+                                frame_shape: ClosureShape {
+                                    captures: [],
+                                    n_locals: 1,
+                                },
+                                this: Some(
+                                    LocalSlot(
+                                        0,
                                     ),
-                                    set_dollar: false,
-                                    uses_super: false,
-                                    locals: [],
-                                    asserts: None,
+                                ),
+                                set_dollar: false,
+                                uses_super: false,
+                                locals: [],
+                                asserts: None,
+                                shape: ObjShape {
                                     fields: [
-                                        LFieldMember {
-                                            name: Fixed(
-                                                "c",
+                                        ShapeField {
+                                            name: "c",
+                                            flags: ObjFieldFlags {
+                                                add: false,
+                                                visibility: Normal,
+                                            },
+                                            location: None,
+                                            index: FieldIndex(
+                                                (),
                                             ),
-                                            plus: false,
-                                            visibility: Normal,
-                                            value: (
-                                                ClosureShape {
-                                                    captures: [],
-                                                    n_locals: 0,
-                                                },
-                                                Num(
-                                                    2.0,
-                                                ),
+                                        },
+                                        ShapeField {
+                                            name: "d",
+                                            flags: ObjFieldFlags {
+                                                add: false,
+                                                visibility: Normal,
+                                            },
+                                            location: None,
+                                            index: FieldIndex(
+                                                (),
                                             ),
                                         },
-                                        LFieldMember {
-                                            name: Fixed(
-                                                "d",
+                                    ],
+                                },
+                                bindings: [
+                                    (
+                                        ClosureShape {
+                                            captures: [],
+                                            n_locals: 0,
+                                        },
+                                        Trivial(
+                                            Num(
+                                                2.0,
                                             ),
-                                            plus: false,
-                                            visibility: Normal,
-                                            value: (
-                                                ClosureShape {
-                                                    captures: [],
-                                                    n_locals: 0,
-                                                },
-                                                Index {
-                                                    indexable: Slot(
-                                                        Local(
-                                                            LocalSlot(
-                                                                0,
-                                                            ),
+                                        ),
+                                    ),
+                                    (
+                                        ClosureShape {
+                                            captures: [],
+                                            n_locals: 0,
+                                        },
+                                        Index {
+                                            indexable: Slot(
+                                                Local(
+                                                    LocalSlot(
+                                                        0,
+                                                    ),
+                                                ),
+                                            ),
+                                            parts: [
+                                                LIndexPart {
+                                                    span: virtual:<test>:35-36,
+                                                    value: Trivial(
+                                                        Str(
+                                                            "c",
                                                         ),
                                                     ),
-                                                    parts: [
-                                                        LIndexPart {
-                                                            span: virtual:<test>:35-36,
-                                                            value: Str(
-                                                                "c",
-                                                            ),
-                                                        },
-                                                    ],
                                                 },
-                                            ),
+                                            ],
                                         },
-                                    ],
-                                },
-                            ),
+                                    ),
+                                ],
+                            },
                         ),
                     ),
-                },
+                ),
             ],
         },
     ),
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@object_comp.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@object_comp.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@object_comp.jsonnet.snap
@@ -1,5 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/object_comp.jsonnet
 ---
@@ -71,12 +72,8 @@
                             0,
                         ),
                     ),
-                    over: Arr {
-                        shape: ClosureShape {
-                            captures: [],
-                            n_locals: 0,
-                        },
-                        items: [
+                    over: ArrConst(
+                        [
                             Str(
                                 "a",
                             ),
@@ -84,7 +81,7 @@
                                 "b",
                             ),
                         ],
-                    },
+                    ),
                     loop_invariant: true,
                 },
             ],
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@object_dollar.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@object_dollar.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@object_dollar.jsonnet.snap
@@ -12,8 +12,8 @@
 --- diagnostics ---
 --- lir ---
 Obj(
-    MemberList(
-        LObjMembers {
+    StaticMembers(
+        LObjStaticMembers {
             frame_shape: ClosureShape {
                 captures: [],
                 n_locals: 1,
@@ -27,95 +27,119 @@
             uses_super: false,
             locals: [],
             asserts: None,
-            fields: [
-                LFieldMember {
-                    name: Fixed(
-                        "a",
-                    ),
-                    plus: false,
-                    visibility: Normal,
-                    value: (
-                        ClosureShape {
-                            captures: [],
-                            n_locals: 0,
+            shape: ObjShape {
+                fields: [
+                    ShapeField {
+                        name: "a",
+                        flags: ObjFieldFlags {
+                            add: false,
+                            visibility: Normal,
                         },
+                        location: None,
+                        index: FieldIndex(
+                            (),
+                        ),
+                    },
+                    ShapeField {
+                        name: "b",
+                        flags: ObjFieldFlags {
+                            add: false,
+                            visibility: Normal,
+                        },
+                        location: None,
+                        index: FieldIndex(
+                            (),
+                        ),
+                    },
+                ],
+            },
+            bindings: [
+                (
+                    ClosureShape {
+                        captures: [],
+                        n_locals: 0,
+                    },
+                    Trivial(
                         Num(
                             1.0,
                         ),
                     ),
-                },
-                LFieldMember {
-                    name: Fixed(
-                        "b",
-                    ),
-                    plus: false,
-                    visibility: Normal,
-                    value: (
-                        ClosureShape {
-                            captures: [],
-                            n_locals: 0,
-                        },
-                        Obj(
-                            MemberList(
-                                LObjMembers {
-                                    frame_shape: ClosureShape {
-                                        captures: [
-                                            Local(
-                                                LocalSlot(
-                                                    0,
-                                                ),
+                ),
+                (
+                    ClosureShape {
+                        captures: [],
+                        n_locals: 0,
+                    },
+                    Obj(
+                        StaticMembers(
+                            LObjStaticMembers {
+                                frame_shape: ClosureShape {
+                                    captures: [
+                                        Local(
+                                            LocalSlot(
+                                                0,
                                             ),
-                                        ],
-                                        n_locals: 1,
-                                    },
-                                    this: None,
-                                    set_dollar: false,
-                                    uses_super: false,
-                                    locals: [],
-                                    asserts: None,
+                                        ),
+                                    ],
+                                    n_locals: 1,
+                                },
+                                this: None,
+                                set_dollar: false,
+                                uses_super: false,
+                                locals: [],
+                                asserts: None,
+                                shape: ObjShape {
                                     fields: [
-                                        LFieldMember {
-                                            name: Fixed(
-                                                "c",
+                                        ShapeField {
+                                            name: "c",
+                                            flags: ObjFieldFlags {
+                                                add: false,
+                                                visibility: Normal,
+                                            },
+                                            location: None,
+                                            index: FieldIndex(
+                                                (),
                                             ),
-                                            plus: false,
-                                            visibility: Normal,
-                                            value: (
-                                                ClosureShape {
-                                                    captures: [
-                                                        Capture(
-                                                            CaptureSlot(
-                                                                0,
-                                                            ),
-                                                        ),
-                                                    ],
-                                                    n_locals: 0,
-                                                },
-                                                Index {
-                                                    indexable: Slot(
-                                                        Capture(
-                                                            CaptureSlot(
-                                                                0,
-                                                            ),
+                                        },
+                                    ],
+                                },
+                                bindings: [
+                                    (
+                                        ClosureShape {
+                                            captures: [
+                                                Capture(
+                                                    CaptureSlot(
+                                                        0,
+                                                    ),
+                                                ),
+                                            ],
+                                            n_locals: 0,
+                                        },
+                                        Index {
+                                            indexable: Slot(
+                                                Capture(
+                                                    CaptureSlot(
+                                                        0,
+                                                    ),
+                                                ),
+                                            ),
+                                            parts: [
+                                                LIndexPart {
+                                                    span: virtual:<test>:18-19,
+                                                    value: Trivial(
+                                                        Str(
+                                                            "a",
                                                         ),
                                                     ),
-                                                    parts: [
-                                                        LIndexPart {
-                                                            span: virtual:<test>:18-19,
-                                                            value: Str(
-                                                                "a",
-                                                            ),
-                                                        },
-                                                    ],
                                                 },
-                                            ),
+                                            ],
                                         },
-                                    ],
-                                },
-                            ),
+                                    ),
+                                ],
+                            },
                         ),
                     ),
-                },
+                ),
             ],
         },
     ),
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@object_self.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@object_self.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@object_self.jsonnet.snap
@@ -12,8 +12,8 @@
 --- diagnostics ---
 --- lir ---
 Obj(
-    MemberList(
-        LObjMembers {
+    StaticMembers(
+        LObjStaticMembers {
             frame_shape: ClosureShape {
                 captures: [],
                 n_locals: 1,
@@ -27,53 +27,69 @@
             uses_super: false,
             locals: [],
             asserts: None,
-            fields: [
-                LFieldMember {
-                    name: Fixed(
-                        "a",
-                    ),
-                    plus: false,
-                    visibility: Normal,
-                    value: (
-                        ClosureShape {
-                            captures: [],
-                            n_locals: 0,
+            shape: ObjShape {
+                fields: [
+                    ShapeField {
+                        name: "a",
+                        flags: ObjFieldFlags {
+                            add: false,
+                            visibility: Normal,
+                        },
+                        location: None,
+                        index: FieldIndex(
+                            (),
+                        ),
+                    },
+                    ShapeField {
+                        name: "b",
+                        flags: ObjFieldFlags {
+                            add: false,
+                            visibility: Normal,
                         },
+                        location: None,
+                        index: FieldIndex(
+                            (),
+                        ),
+                    },
+                ],
+            },
+            bindings: [
+                (
+                    ClosureShape {
+                        captures: [],
+                        n_locals: 0,
+                    },
+                    Trivial(
                         Num(
                             1.0,
                         ),
                     ),
-                },
-                LFieldMember {
-                    name: Fixed(
-                        "b",
-                    ),
-                    plus: false,
-                    visibility: Normal,
-                    value: (
-                        ClosureShape {
-                            captures: [],
-                            n_locals: 0,
-                        },
-                        Index {
-                            indexable: Slot(
-                                Local(
-                                    LocalSlot(
-                                        0,
-                                    ),
+                ),
+                (
+                    ClosureShape {
+                        captures: [],
+                        n_locals: 0,
+                    },
+                    Index {
+                        indexable: Slot(
+                            Local(
+                                LocalSlot(
+                                    0,
                                 ),
                             ),
-                            parts: [
-                                LIndexPart {
-                                    span: virtual:<test>:16-17,
-                                    value: Str(
+                        ),
+                        parts: [
+                            LIndexPart {
+                                span: virtual:<test>:16-17,
+                                value: Trivial(
+                                    Str(
                                         "a",
                                     ),
-                                },
-                            ],
-                        },
-                    ),
-                },
+                                ),
+                            },
+                        ],
+                    },
+                ),
             ],
         },
     ),
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@object_with_locals.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@object_with_locals.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@object_with_locals.jsonnet.snap
@@ -16,8 +16,8 @@
 --- diagnostics ---
 --- lir ---
 Obj(
-    MemberList(
-        LObjMembers {
+    StaticMembers(
+        LObjStaticMembers {
             frame_shape: ClosureShape {
                 captures: [],
                 n_locals: 2,
@@ -36,59 +36,75 @@
                         captures: [],
                         n_locals: 0,
                     },
-                    value: Num(
-                        10.0,
+                    value: Trivial(
+                        Num(
+                            10.0,
+                        ),
                     ),
                 },
             ],
             asserts: None,
-            fields: [
-                LFieldMember {
-                    name: Fixed(
-                        "a",
+            shape: ObjShape {
+                fields: [
+                    ShapeField {
+                        name: "a",
+                        flags: ObjFieldFlags {
+                            add: false,
+                            visibility: Normal,
+                        },
+                        location: None,
+                        index: FieldIndex(
+                            (),
+                        ),
+                    },
+                    ShapeField {
+                        name: "b",
+                        flags: ObjFieldFlags {
+                            add: false,
+                            visibility: Normal,
+                        },
+                        location: None,
+                        index: FieldIndex(
+                            (),
+                        ),
+                    },
+                ],
+            },
+            bindings: [
+                (
+                    ClosureShape {
+                        captures: [],
+                        n_locals: 0,
+                    },
+                    Slot(
+                        Local(
+                            LocalSlot(
+                                1,
+                            ),
+                        ),
                     ),
-                    plus: false,
-                    visibility: Normal,
-                    value: (
-                        ClosureShape {
-                            captures: [],
-                            n_locals: 0,
-                        },
-                        Slot(
+                ),
+                (
+                    ClosureShape {
+                        captures: [],
+                        n_locals: 0,
+                    },
+                    BinaryOp {
+                        lhs: Slot(
                             Local(
                                 LocalSlot(
                                     1,
                                 ),
                             ),
                         ),
-                    ),
-                },
-                LFieldMember {
-                    name: Fixed(
-                        "b",
-                    ),
-                    plus: false,
-                    visibility: Normal,
-                    value: (
-                        ClosureShape {
-                            captures: [],
-                            n_locals: 0,
-                        },
-                        BinaryOp {
-                            lhs: Slot(
-                                Local(
-                                    LocalSlot(
-                                        1,
-                                    ),
-                                ),
-                            ),
-                            op: Mul,
-                            rhs: Num(
+                        op: Mul,
+                        rhs: Trivial(
+                            Num(
                                 2.0,
                             ),
-                        },
-                    ),
-                },
+                        ),
+                    },
+                ),
             ],
         },
     ),
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@redeclared_local.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@redeclared_local.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@redeclared_local.jsonnet.snap
@@ -1,5 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/redeclared_local.jsonnet
 ---
@@ -31,8 +32,10 @@
                     captures: [],
                     n_locals: 0,
                 },
-                value: Num(
-                    1.0,
+                value: Trivial(
+                    Num(
+                        1.0,
+                    ),
                 ),
             },
         ],
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@shadowing.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@shadowing.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@shadowing.jsonnet.snap
@@ -1,5 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/shadowing.jsonnet
 ---
@@ -32,8 +33,10 @@
                     captures: [],
                     n_locals: 0,
                 },
-                value: Num(
-                    1.0,
+                value: Trivial(
+                    Num(
+                        1.0,
+                    ),
                 ),
             },
         ],
@@ -54,8 +57,10 @@
                             captures: [],
                             n_locals: 0,
                         },
-                        value: Num(
-                            2.0,
+                        value: Trivial(
+                            Num(
+                                2.0,
+                            ),
                         ),
                     },
                 ],
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@simple_local.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@simple_local.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@simple_local.jsonnet.snap
@@ -1,5 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/simple_local.jsonnet
 ---
@@ -28,8 +29,10 @@
                     captures: [],
                     n_locals: 0,
                 },
-                value: Num(
-                    1.0,
+                value: Trivial(
+                    Num(
+                        1.0,
+                    ),
                 ),
             },
         ],
@@ -42,8 +45,10 @@
                 ),
             ),
             op: Add,
-            rhs: Num(
-                2.0,
+            rhs: Trivial(
+                Num(
+                    2.0,
+                ),
             ),
         },
     },
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@slice.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@slice.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@slice.jsonnet.snap
@@ -1,6 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
-assertion_line: 2017
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/slice.jsonnet
 ---
@@ -14,12 +14,8 @@
 --- lir ---
 Slice(
     LSliceExpr {
-        value: Arr {
-            shape: ClosureShape {
-                captures: [],
-                n_locals: 0,
-            },
-            items: [
+        value: ArrConst(
+            [
                 Num(
                     1.0,
                 ),
@@ -36,15 +32,19 @@
                     5.0,
                 ),
             ],
-        },
+        ),
         start: Some(
-            Num(
-                1.0,
+            Trivial(
+                Num(
+                    1.0,
+                ),
             ),
         ),
         end: Some(
-            Num(
-                3.0,
+            Trivial(
+                Num(
+                    3.0,
+                ),
             ),
         ),
         step: None,
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@super_outside_object.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@super_outside_object.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@super_outside_object.jsonnet.snap
@@ -1,5 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/super_outside_object.jsonnet
 ---
@@ -21,8 +22,10 @@
     parts: [
         LIndexPart {
             span: virtual:<test>:6-7,
-            value: Str(
-                "a",
+            value: Trivial(
+                Str(
+                    "a",
+                ),
             ),
         },
     ],
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@super_usage.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@super_usage.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@super_usage.jsonnet.snap
@@ -13,8 +13,8 @@
 --- lir ---
 BinaryOp {
     lhs: Obj(
-        MemberList(
-            LObjMembers {
+        StaticMembers(
+            LObjStaticMembers {
                 frame_shape: ClosureShape {
                     captures: [],
                     n_locals: 1,
@@ -24,47 +24,63 @@
                 uses_super: false,
                 locals: [],
                 asserts: None,
-                fields: [
-                    LFieldMember {
-                        name: Fixed(
-                            "a",
-                        ),
-                        plus: false,
-                        visibility: Normal,
-                        value: (
-                            ClosureShape {
-                                captures: [],
-                                n_locals: 0,
+                shape: ObjShape {
+                    fields: [
+                        ShapeField {
+                            name: "a",
+                            flags: ObjFieldFlags {
+                                add: false,
+                                visibility: Normal,
                             },
+                            location: None,
+                            index: FieldIndex(
+                                (),
+                            ),
+                        },
+                        ShapeField {
+                            name: "b",
+                            flags: ObjFieldFlags {
+                                add: false,
+                                visibility: Normal,
+                            },
+                            location: None,
+                            index: FieldIndex(
+                                (),
+                            ),
+                        },
+                    ],
+                },
+                bindings: [
+                    (
+                        ClosureShape {
+                            captures: [],
+                            n_locals: 0,
+                        },
+                        Trivial(
                             Num(
                                 1.0,
                             ),
                         ),
-                    },
-                    LFieldMember {
-                        name: Fixed(
-                            "b",
-                        ),
-                        plus: false,
-                        visibility: Normal,
-                        value: (
-                            ClosureShape {
-                                captures: [],
-                                n_locals: 0,
-                            },
+                    ),
+                    (
+                        ClosureShape {
+                            captures: [],
+                            n_locals: 0,
+                        },
+                        Trivial(
                             Num(
                                 2.0,
                             ),
                         ),
-                    },
+                    ),
                 ],
             },
         ),
     ),
     op: Add,
     rhs: Obj(
-        MemberList(
-            LObjMembers {
+        StaticMembers(
+            LObjStaticMembers {
                 frame_shape: ClosureShape {
                     captures: [],
                     n_locals: 1,
@@ -78,67 +94,85 @@
                 uses_super: true,
                 locals: [],
                 asserts: None,
-                fields: [
-                    LFieldMember {
-                        name: Fixed(
-                            "a",
-                        ),
-                        plus: false,
-                        visibility: Normal,
-                        value: (
-                            ClosureShape {
-                                captures: [],
-                                n_locals: 0,
+                shape: ObjShape {
+                    fields: [
+                        ShapeField {
+                            name: "a",
+                            flags: ObjFieldFlags {
+                                add: false,
+                                visibility: Normal,
+                            },
+                            location: None,
+                            index: FieldIndex(
+                                (),
+                            ),
+                        },
+                        ShapeField {
+                            name: "c",
+                            flags: ObjFieldFlags {
+                                add: false,
+                                visibility: Normal,
                             },
-                            BinaryOp {
-                                lhs: Index {
-                                    indexable: Super,
-                                    parts: [
-                                        LIndexPart {
-                                            span: virtual:<test>:28-29,
-                                            value: Str(
+                            location: None,
+                            index: FieldIndex(
+                                (),
+                            ),
+                        },
+                    ],
+                },
+                bindings: [
+                    (
+                        ClosureShape {
+                            captures: [],
+                            n_locals: 0,
+                        },
+                        BinaryOp {
+                            lhs: Index {
+                                indexable: Super,
+                                parts: [
+                                    LIndexPart {
+                                        span: virtual:<test>:28-29,
+                                        value: Trivial(
+                                            Str(
                                                 "a",
                                             ),
-                                        },
-                                    ],
-                                },
-                                op: Add,
-                                rhs: Num(
+                                        ),
+                                    },
+                                ],
+                            },
+                            op: Add,
+                            rhs: Trivial(
+                                Num(
                                     10.0,
                                 ),
-                            },
-                        ),
-                    },
-                    LFieldMember {
-                        name: Fixed(
-                            "c",
-                        ),
-                        plus: false,
-                        visibility: Normal,
-                        value: (
-                            ClosureShape {
-                                captures: [],
-                                n_locals: 0,
-                            },
-                            Index {
-                                indexable: Slot(
-                                    Local(
-                                        LocalSlot(
-                                            0,
-                                        ),
+                            ),
+                        },
+                    ),
+                    (
+                        ClosureShape {
+                            captures: [],
+                            n_locals: 0,
+                        },
+                        Index {
+                            indexable: Slot(
+                                Local(
+                                    LocalSlot(
+                                        0,
                                     ),
                                 ),
-                                parts: [
-                                    LIndexPart {
-                                        span: virtual:<test>:44-45,
-                                        value: Str(
+                            ),
+                            parts: [
+                                LIndexPart {
+                                    span: virtual:<test>:44-45,
+                                    value: Trivial(
+                                        Str(
                                             "b",
                                         ),
-                                    },
-                                ],
-                            },
-                        ),
-                    },
+                                    ),
+                                },
+                            ],
+                        },
+                    ),
                 ],
             },
         ),
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@undefined_var.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@undefined_var.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@undefined_var.jsonnet.snap
@@ -1,5 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/undefined_var.jsonnet
 ---
@@ -19,7 +20,9 @@
         "ref",
     ),
     op: Add,
-    rhs: Num(
-        1.0,
+    rhs: Trivial(
+        Num(
+            1.0,
+        ),
     ),
 }
modifiedcrates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@unused_local.jsonnet.snapdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@unused_local.jsonnet.snap
+++ b/crates/jrsonnet-evaluator/src/snapshots/jrsonnet_evaluator__analyze__tests__snapshots@unused_local.jsonnet.snap
@@ -1,5 +1,6 @@
 ---
 source: crates/jrsonnet-evaluator/src/analyze.rs
+assertion_line: 2175
 expression: rendered
 input_file: crates/jrsonnet-evaluator/src/analysis_tests/unused_local.jsonnet
 ---
@@ -31,13 +32,17 @@
                     captures: [],
                     n_locals: 0,
                 },
-                value: Num(
-                    1.0,
+                value: Trivial(
+                    Num(
+                        1.0,
+                    ),
                 ),
             },
         ],
-        body: Num(
-            2.0,
+        body: Trivial(
+            Num(
+                2.0,
+            ),
         ),
     },
 )
modifiedcrates/jrsonnet-evaluator/src/tla.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/tla.rs
+++ b/crates/jrsonnet-evaluator/src/tla.rs
@@ -5,7 +5,7 @@
 use jrsonnet_ir::{SourceFifo, SourcePath};
 
 use crate::{
-	Result, Thunk, Val,
+	Result, Thunk, Val, ensure_sufficient_stack,
 	function::{CallLocation, PreparedFuncVal},
 	in_description_frame, with_state,
 };
@@ -21,7 +21,7 @@
 }
 impl TlaArg {
 	pub fn evaluate_tailstrict(&self) -> Result<Val> {
-		match self {
+		ensure_sufficient_stack(|| match self {
 			Self::String(s) => Ok(Val::string(s.clone())),
 			Self::Val(val) => Ok(val.clone()),
 			Self::Lazy(lazy) => Ok(lazy.evaluate()?),
@@ -38,7 +38,7 @@
 					SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));
 				s.import_resolved(resolved)
 			}),
-		}
+		})
 	}
 	pub fn evaluate(&self) -> Result<Thunk<Val>> {
 		match self {