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

difftreelog

feat forObj evaluation

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

4 files changed

modifiedcrates/jrsonnet-evaluator/src/analyze.rsdiffbeforeafterboth
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::error::{format_found, suggest_names};3233#[derive(Debug, Clone, Copy)]34#[must_use]35pub struct AnalysisResult {36	/// Highest object, on which identity the value is dependent. `u32::MAX` = not dependent at all37	pub object_dependent_depth: u32,38	/// Highest local frame, on which this value depends. `u32::MAX` = not dependent at all39	pub local_dependent_depth: u32,40}4142impl Default for AnalysisResult {43	fn default() -> Self {44		Self {45			object_dependent_depth: u32::MAX,46			local_dependent_depth: u32::MAX,47		}48	}49}5051impl AnalysisResult {52	fn depend_on_object(&mut self, depth: u32) {53		if depth < self.object_dependent_depth {54			self.object_dependent_depth = depth;55		}56	}57	fn depend_on_local(&mut self, depth: u32) {58		if depth < self.local_dependent_depth {59			self.local_dependent_depth = depth;60		}61	}62	fn taint_by(&mut self, other: AnalysisResult) {63		self.depend_on_object(other.object_dependent_depth);64		self.depend_on_local(other.local_dependent_depth);65	}66}6768#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Acyclic)]69pub struct LocalId(pub u32);7071impl LocalId {72	fn idx(self) -> usize {73		self.0 as usize74	}75	fn defined_before(self, other: Self) -> bool {76		self.0 < other.077	}78}7980#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Acyclic)]81pub enum LSlot {82	/// Enclosing frame locals (sibling letrec, params, etc.).83	Local(LocalSlot),84	/// Enclosing closure's capture pack.85	Capture(CaptureSlot),86}8788#[derive(Debug, Acyclic)]89pub struct ClosureShape {90	pub captures: Box<[LSlot]>,91	pub n_locals: u16,92}9394struct LocalDefinition {95	name: IStr,96	span: Option<Span>,97	/// At which frame depth this local was defined98	defined_at_depth: u32,99	/// Min frame depth, at which this local was used. `u32::MAX` = not used at all.100	/// This check won't catch unused argument closures, i.e:101	/// ```jsonnet102	/// local103	///     a = b,104	///     b = a,105	/// ; 2 + 2106	///107	/// ```108	/// Both `a` and `b` here are "used", but the whole closure was not used here.109	used_at_depth: u32,110	/// Used as part of the current frame closure111	used_by_sibling: bool,112	/// Analysys result for value of this local113	analysis: AnalysisResult,114	/// Has `analysis` been filled in?115	/// For sanity checking, locals are initialized in batchs, use `first_uninitialized_local`116	analyzed: bool,117	/// During walk over uninitialized vars, we can't refer to analysis results of other locals,118	/// but we need to. To make that work, for each variable in variable frame we capture its closure,119	/// by looking at referenced variables.120	scratch_referenced: bool,121}122123impl LocalDefinition {124	fn use_at(&mut self, depth: u32) {125		if depth == self.defined_at_depth {126			self.used_by_sibling = true;127			return;128		}129		if depth < self.used_at_depth {130			self.used_at_depth = depth;131		}132	}133}134135#[derive(Debug, Acyclic)]136pub enum LExpr {137	Slot(LSlot),138	Null,139	Bool(bool),140	Str(IStr),141	Num(NumValue),142	Arr {143		shape: ClosureShape,144		items: Rc<Vec<LExpr>>,145	},146	ArrComp(Box<LArrComp>),147	Obj(LObjBody),148	ObjExtend(Box<LExpr>, LObjBody),149	UnaryOp(UnaryOpType, Box<LExpr>),150	BinaryOp {151		lhs: Box<LExpr>,152		op: BinaryOpType,153		rhs: Box<LExpr>,154	},155	AssertExpr {156		assert: Rc<LAssertStmt>,157		rest: Box<LExpr>,158	},159	Error(Span, Box<LExpr>),160	LocalExpr(Box<LLocalExpr>),161	Import {162		kind: Spanned<ImportKind>,163		kind_span: Span,164		path: IStr,165	},166	Apply {167		applicable: Box<LExpr>,168		args: Spanned<LArgsDesc>,169		tailstrict: bool,170	},171	Index {172		indexable: Box<LExpr>,173		parts: Vec<LIndexPart>,174	},175	Function(Rc<LFunction>),176	IdentityFunction,177	IfElse {178		cond: Box<LExpr>,179		cond_then: Box<LExpr>,180		cond_else: Option<Box<LExpr>>,181	},182	Slice(Box<LSliceExpr>),183	Super,184185	/// Allows partial evaluation of broken expression tree,186	/// expressions with failed static analysis end up here187	BadLocal(&'static str),188}189190#[derive(Debug, Acyclic)]191pub struct LLocalExpr {192	pub frame_shape: ClosureShape,193	pub binds: Vec<LBind>,194	pub body: LExpr,195}196197#[derive(Debug, Acyclic)]198pub struct LFunction {199	pub name: Option<IStr>,200	pub params: Vec<LParam>,201	pub signature: FunctionSignature,202203	pub body_shape: ClosureShape,204	pub body: Rc<LExpr>,205}206207#[derive(Debug, Acyclic)]208pub struct LParam {209	pub name: Option<IStr>,210	pub destruct: LDestruct,211212	pub default: Option<(ClosureShape, Rc<LExpr>)>,213}214215#[derive(Debug, Acyclic)]216pub struct LBind {217	pub destruct: LDestruct,218	pub value_shape: ClosureShape,219	pub value: Rc<LExpr>,220}221222#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Acyclic)]223pub struct CaptureSlot(pub(crate) u16);224#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Acyclic)]225pub struct LocalSlot(pub(crate) u16);226227#[derive(Debug, Acyclic)]228pub enum LDestruct {229	Full(LocalSlot),230	#[cfg(feature = "exp-destruct")]231	Skip,232	#[cfg(feature = "exp-destruct")]233	Array {234		start: Vec<LDestruct>,235		rest: Option<LDestructRest>,236		end: Vec<LDestruct>,237	},238	#[cfg(feature = "exp-destruct")]239	Object {240		fields: Vec<LDestructField>,241		rest: Option<LDestructRest>,242	},243}244245#[derive(Debug, Clone, Copy, Acyclic)]246pub enum LDestructRest {247	Keep(LocalSlot),248	Drop,249}250251#[derive(Debug, Acyclic)]252pub struct LDestructField {253	pub name: IStr,254	pub into: Option<LDestruct>,255	pub default: Option<(ClosureShape, Rc<LExpr>)>,256}257258impl LDestruct {259	pub fn each_slot<F: FnMut(LocalSlot)>(&self, f: &mut F) {260		match self {261			Self::Full(s) => f(*s),262			#[cfg(feature = "exp-destruct")]263			Self::Skip => {}264			#[cfg(feature = "exp-destruct")]265			Self::Array { start, rest, end } => {266				for d in start {267					d.each_slot(f);268				}269				if let Some(LDestructRest::Keep(s)) = rest {270					f(*s);271				}272				for d in end {273					d.each_slot(f);274				}275			}276			#[cfg(feature = "exp-destruct")]277			Self::Object { fields, rest } => {278				for field in fields {279					if let Some(into) = &field.into {280						into.each_slot(f);281					} else {282						unreachable!("shorthand object destruct must store `into`");283					}284				}285				if let Some(LDestructRest::Keep(s)) = rest {286					f(*s);287				}288			}289		}290	}291292	pub fn slots(&self) -> SmallVec<[LocalSlot; 1]> {293		let mut out = SmallVec::new();294		self.each_slot(&mut |s| out.push(s));295		out296	}297}298299#[derive(Debug, Acyclic)]300pub struct LSliceExpr {301	pub value: LExpr,302	pub start: Option<LExpr>,303	pub end: Option<LExpr>,304	pub step: Option<LExpr>,305}306307#[derive(Debug, Acyclic)]308pub struct LArgsDesc {309	pub unnamed: Vec<Rc<LExpr>>,310	pub names: Vec<IStr>,311	pub values: Vec<Rc<LExpr>>,312}313314#[derive(Debug, Acyclic)]315pub struct LAssertStmt {316	pub cond: Spanned<LExpr>,317	pub message: Option<LExpr>,318}319320#[derive(Debug, Acyclic)]321pub struct LIndexPart {322	pub span: Span,323	pub value: LExpr,324	#[cfg(feature = "exp-null-coaelse")]325	pub null_coaelse: bool,326}327328#[derive(Debug, Acyclic)]329pub enum LObjBody {330	MemberList(LObjMembers),331	ObjComp(Box<LObjComp>),332}333334#[derive(Debug, Acyclic)]335pub struct LObjMembers {336	pub frame_shape: ClosureShape,337	/// If current object identity (`super`/`this`/`$`) is used, `this` should338	/// be saved to the specified local slot.339	pub this: Option<LocalSlot>,340	/// Set if dollar should also be assigned to object identity, `this` should also be set (TODO: proper type-level validation)341	pub set_dollar: bool,342	/// True iff `super` is referenced by this object's members.343	pub uses_super: bool,344345	pub locals: Rc<Vec<LBind>>,346	pub asserts: Option<Rc<LObjAsserts>>,347	pub fields: Vec<LFieldMember>,348}349350#[derive(Debug, Acyclic)]351pub struct LObjComp {352	pub frame_shape: Rc<ClosureShape>,353	pub this: Option<LocalSlot>,354	pub set_dollar: bool,355	pub uses_super: bool,356357	pub locals: Rc<Vec<LBind>>,358	pub field: LFieldMember,359	pub compspecs: Vec<LCompSpec>,360}361362#[derive(Debug, Acyclic)]363pub struct LFieldMember {364	pub name: LFieldName,365	pub plus: bool,366	pub visibility: Visibility,367	pub value: Rc<(ClosureShape, LExpr)>,368}369370#[derive(Debug, Acyclic)]371pub struct LClosure<T: Acyclic> {372	pub shape: ClosureShape,373	pub value: T,374}375376#[derive(Debug, Acyclic)]377pub struct LObjAsserts {378	pub shape: ClosureShape,379	pub asserts: Vec<LAssertStmt>,380}381382#[derive(Debug, Acyclic)]383pub enum LFieldName {384	Fixed(IStr),385	Dyn(LExpr),386}387impl LFieldName {388	fn function_name(&self) -> Option<IStr> {389		match self {390			LFieldName::Fixed(istr) => Some(istr.clone()),391			LFieldName::Dyn(_) => None,392		}393	}394}395396#[derive(Debug, Acyclic)]397pub struct LArrComp {398	pub value_shape: ClosureShape,399	pub value: Rc<LExpr>,400	pub compspecs: Vec<LCompSpec>,401}402403#[derive(Debug, Acyclic)]404pub enum LCompSpec {405	If(LExpr),406	For {407		frame_shape: ClosureShape,408		destruct: LDestruct,409		over: LExpr,410		/// Is `over` does not depend on any variable introduced by an earlier for-spec in this comprehension chain411		loop_invariant: bool,412	},413}414415struct FrameAlloc<'s> {416	first_in_frame: LocalId,417	stack: &'s mut AnalysisStack,418	bomb: DropBomb,419}420impl<'s> FrameAlloc<'s> {421	fn new(stack: &'s mut AnalysisStack) -> Self {422		FrameAlloc {423			first_in_frame: stack.next_local_id(),424			stack,425			bomb: DropBomb::new("binding frame state"),426		}427	}428429	fn push_locals_closure(&mut self) -> ClosureOnStack {430		self.stack.push_closure_a(self.first_in_frame)431	}432433	fn define_local(&mut self, name: IStr, span: Option<Span>) -> Option<(LocalId, LocalSlot)> {434		let id = self.stack.next_local_id();435		let stack = self.stack.local_by_name.entry(name.clone()).or_default();436		if let Some(&existing) = stack.last()437			&& !existing.defined_before(self.first_in_frame)438		{439			self.stack.report_error(440				format!("local is already defined in the current frame: {name}"),441				span,442			);443			return None;444		}445		stack.push(id);446		self.stack.local_defs.push(LocalDefinition {447			name,448			span,449			defined_at_depth: self.stack.depth,450			used_at_depth: u32::MAX,451			used_by_sibling: false,452			analysis: AnalysisResult::default(),453			analyzed: false,454			scratch_referenced: false,455		});456		let def = self.stack.defining_closure_mut();457		Some((id, def.define_local(id)))458	}459	fn alloc_bind(&mut self, bind: &BindSpec) -> Option<LDestruct> {460		match bind {461			BindSpec::Field { into, .. } => self.alloc_destruct(into),462			BindSpec::Function { name, .. } => {463				let (_, id) = self.define_local(name.clone(), None)?;464				Some(LDestruct::Full(id))465			}466		}467	}468	fn alloc_destruct(&mut self, destruct: &Destruct) -> Option<LDestruct> {469		Some(match destruct {470			Destruct::Full(name) => {471				let (_, id) = self.define_local(name.value.clone(), Some(name.span.clone()))?;472				LDestruct::Full(id)473			}474			#[cfg(feature = "exp-destruct")]475			Destruct::Skip => LDestruct::Skip,476			#[cfg(feature = "exp-destruct")]477			Destruct::Array { start, rest, end } => {478				let start = start479					.iter()480					.map(|d| self.alloc_destruct(d))481					.collect::<Option<Vec<_>>>()?;482				let rest = match rest {483					Some(jrsonnet_ir::DestructRest::Keep(name)) => {484						let (_, id) = self.define_local(name.clone(), None)?;485						Some(LDestructRest::Keep(id))486					}487					Some(jrsonnet_ir::DestructRest::Drop) => Some(LDestructRest::Drop),488					None => None,489				};490				let end = end491					.iter()492					.map(|d| self.alloc_destruct(d))493					.collect::<Option<Vec<_>>>()?;494				LDestruct::Array { start, rest, end }495			}496			#[cfg(feature = "exp-destruct")]497			Destruct::Object { fields, rest } => {498				let mut l_fields: Vec<(IStr, LDestruct)> = Vec::with_capacity(fields.len());499				// Allocate destruct LocalIds, then analyse defaults500				for (name, into, _default) in fields {501					let into = if let Some(inner) = into {502						self.alloc_destruct(inner)?503					} else {504						let (_, id) = self.define_local(name.clone(), None)?;505						LDestruct::Full(id)506					};507					l_fields.push((name.clone(), into));508				}509				// All locals exist, so defaults can reference any sibling.510				let l_fields: Vec<LDestructField> = l_fields511					.into_iter()512					.zip(fields.iter())513					.map(|((name, into), (_n, _i, default))| {514						let default = match default {515							Some(e) => {516								let mut default_taint = AnalysisResult::default();517								Some(self.stack.in_using_closure(|stack| {518									Rc::new(analyze(&e.value, stack, &mut default_taint))519								}))520							}521							None => None,522						};523						LDestructField {524							name,525							into: Some(into),526							default,527						}528					})529					.collect();530				let rest = match rest {531					Some(jrsonnet_ir::DestructRest::Keep(name)) => {532						let (_, id) = self.define_local(name.clone(), None)?;533						Some(LDestructRest::Keep(id))534					}535					Some(jrsonnet_ir::DestructRest::Drop) => Some(LDestructRest::Drop),536					None => None,537				};538				LDestruct::Object {539					fields: l_fields,540					rest,541				}542			}543		})544	}545546	fn finish(self) -> PendingInit<'s> {547		let Self {548			first_in_frame,549			stack,550			bomb,551		} = self;552		let first_after_frame = stack.next_local_id();553		PendingInit {554			first_after_frame,555			stack,556			closures: Closures {557				referenced: vec![],558				spec_shapes: vec![],559				first_in_frame,560			},561			bomb,562		}563	}564}565566/// Frame state: `LocalIds` allocated, values not yet analysed.567struct PendingInit<'s> {568	first_after_frame: LocalId,569	stack: &'s mut AnalysisStack,570	closures: Closures,571	bomb: DropBomb,572}573574impl<'s> PendingInit<'s> {575	/// Record the analysis of a spec's value: stamp every id bound by the576	/// spec with `analysis`, collect the spec's same-frame references, and577	/// append them to `closures`.578	fn record_spec_init(&mut self, destruct: &LDestruct, analysis: AnalysisResult) {579		let mut refs: SmallVec<[LocalId; 4]> = SmallVec::new();580		for i in self.closures.first_in_frame.0..self.first_after_frame.0 {581			let def = &mut self.stack.local_defs[i as usize];582			if def.scratch_referenced {583				refs.push(LocalId(i));584				def.scratch_referenced = false;585			}586		}587588		let mut ids_count = 0;589		let first_local = self.stack.top_defining_local();590		destruct.each_slot(&mut |slot| {591			ids_count += 1;592			let id = LocalId(first_local.0 + u32::from(slot.0));593			let def = &mut self.stack.local_defs[id.idx()];594			debug_assert!(!def.analyzed, "sanity: local {:?} analysed twice", def.name);595			def.analysis = analysis;596			def.analyzed = true;597		});598		self.closures.push_spec(ids_count, &refs);599	}600	/// After all specs are analysed, propagate dependency information between601	/// siblings to a fix-point, then switch to "body" mode.602	fn finish(self) -> PendingBody<'s> {603		let Self {604			first_after_frame,605			closures,606			stack,607			bomb,608		} = self;609610		debug_assert_eq!(611			first_after_frame,612			stack.next_local_id(),613			"frame initialisation left unfinished locals"614		);615616		debug_assert_eq!(617			closures.spec_shapes.iter().map(|(_, d)| *d).sum::<usize>(),618			(first_after_frame.0 - closures.first_in_frame.0) as usize,619			"closures destruct-id counts must match frame local count"620		);621622		let mut changed = true;623		while changed {624			changed = false;625			for spec in closures.iter_specs() {626				for id_raw in spec.ids.clone() {627					let user = LocalId(id_raw);628					for &used in spec.references {629						changed |= stack.propagate_analysis(user, used);630					}631				}632			}633		}634635		stack.depth += 1;636		PendingBody {637			first_after_frame,638			closures,639			stack,640			bomb,641		}642	}643}644645/// Frame state: values analysed, body not yet walked.646struct PendingBody<'s> {647	first_after_frame: LocalId,648	closures: Closures,649	stack: &'s mut AnalysisStack,650	bomb: DropBomb,651}652impl<'s> PendingBody<'s> {653	/// After the body is processed, drop the frame's locals and emit any654	/// "unused local" warnings.655	fn finish(self) {656		let PendingBody {657			first_after_frame,658			closures,659			stack,660			mut bomb,661		} = self;662		bomb.defuse();663		stack.depth -= 1;664665		debug_assert_eq!(666			first_after_frame,667			stack.next_local_id(),668			"nested scopes must be popped before outer frames"669		);670671		let mut changed = true;672		while changed {673			changed = false;674			for spec in closures.iter_specs() {675				// Effective used_at_depth for the spec = min over its ids.676				let mut min_used_at = u32::MAX;677				for id_raw in spec.ids.clone() {678					min_used_at = min_used_at.min(stack.local_defs[id_raw as usize].used_at_depth);679				}680				if min_used_at == u32::MAX {681					continue;682				}683				for &used in spec.references {684					let used_def = &mut stack.local_defs[used.idx()];685					if min_used_at < used_def.used_at_depth {686						used_def.used_at_depth = min_used_at;687						changed = true;688					}689				}690			}691		}692693		let drained: Vec<LocalDefinition> = stack694			.local_defs695			.drain(closures.first_in_frame.idx()..)696			.collect();697		for (i, def) in drained.iter().enumerate().rev() {698			let id = LocalId(closures.first_in_frame.0 + i as u32);699			let stack_locals = stack700				.local_by_name701				.get_mut(&def.name)702				.expect("local must be in name map");703			let popped = stack_locals.pop().expect("name stack should not be empty");704			debug_assert_eq!(popped, id, "name stack integrity");705			if stack_locals.is_empty() {706				stack.local_by_name.remove(&def.name);707			}708709			if def.used_at_depth == u32::MAX {710				if def.used_by_sibling {711					stack.report_warning(712						format!("local is only referenced by unused siblings: {}", def.name),713						def.span.clone(),714					);715				} else {716					stack.report_warning(format!("unused local: {}", def.name), def.span.clone());717				}718			} else if def.analysis.local_dependent_depth > def.defined_at_depth719				&& def.analysis.object_dependent_depth > def.defined_at_depth720				&& def.defined_at_depth != 0721			{722				// The value doesn't depend on anything defined at or inside723				// this local's scope - can be hoisted, unfortunately not automatically.724				stack.report_warning(725					format!("local could be hoisted to an outer scope: {}", def.name),726					def.span.clone(),727				);728			}729		}730	}731}732733struct Closures {734	/// All the referenced locals, maybe repeated multiple times735	/// It is recorded as continous vec of sets, I.e we have736	/// a = 1, 2, 3737	/// b = 3, 4, 5, 6738	/// And in `referenced` we have `[ 1, 2, 3, 3, 4, 5, 6 ]`. To actually get, which closure refers to which element, see `spec_shapes`...739	/// Flat concatenation of sibling-local references across all specs.740	referenced: Vec<LocalId>,741	/// Amount of elements per closure, for the above case it is a = 3, b = 4, so here742	/// lies `[ 3, 4 ]`743	/// ~~closures: Vec<usize>,~~744	/// Finally, we have destructs.745	/// Because single destruct references single closure, but destructs to multiple locals, we have even more complicated structure.746	/// Luckly, every destruct is not interleaved with each other, so here we can have full list...747	/// 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 elements748	/// for each destruct starting from `first_destruct_local`749	/// ~~destructs: Vec<usize>,~~750	///751	/// => two of those fields were merged, as there is currently no per-destruct tracking of closures.752	/// For each spec in order: `(references_count, destruct_ids_count)`.753	/// `references_count` tells us how many entries of `referenced` belong754	/// to this spec; `destruct_ids_count` tells us how many `LocalIds` it755	/// binds.756	spec_shapes: Vec<(usize, usize)>,757	/// This is not a related doccomment, just a continuation of docs for previous fields.758	/// Having759	/// ```jsonnet760	/// local761	///     [a, b, c] = [d, e, f],762	///     [d, e, f] = [a, b, c, h],763	///     h = 1,764	/// ;765	/// ```766	///767	/// We have total of 7 locals768	/// First local here is `a` => `first_destruct_local` = `a`769	/// 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`770	/// [d, e, f] = [a, b, c, h], => `referenced += [a, b, c, h]`, `closures += 4`, `destructs += 3` (Note that this destruct will fail at runtime,771	///                                                                                               this thing should not care about that, it only captures what the value are referencing)772	/// h = 1 => referenced += [], closures += 0, destructs += 1773	/// And the result is774	///775	/// ```rust,ignore776	/// Closures {777	///     referenced: vec![d, e, f, a, b, c, h]778	///     spec_shapes: vec![(3, 3), (4, 3), (0, 1)],779	///     first_destruct_label: a,780	/// }781	/// ```782	///783	/// Reconstruction of that:784	///785	/// We know that we start with a786	/// We get the first number from destructs: `destructs.shift() == 3` => `destructs = [3, 1]`787	/// 3 elements counting from a => [a, b, c]788	/// Then we take first number from closures: `closures.shift() == 3` => `closures = [4, 0]`789	/// Then we take 3 items from referenced: `referenced.shift()x3 == d, e, f` => `referenced = [a, b, c, h]`790	///791	/// Thus we have [a, b, c] = [d, e, f]792	first_in_frame: LocalId,793}794795struct Closure<'a> {796	references: &'a [LocalId],797	ids: std::ops::Range<u32>,798}799800impl Closures {801	fn push_spec(&mut self, destruct_ids_count: usize, refs: &[LocalId]) {802		self.referenced.extend_from_slice(refs);803		self.spec_shapes.push((refs.len(), destruct_ids_count));804	}805806	fn iter_specs(&self) -> impl Iterator<Item = Closure<'_>> {807		let mut refs = self.referenced.as_slice();808		let mut next_id = self.first_in_frame.0;809		self.spec_shapes.iter().map(move |(refs_len, dest_count)| {810			let (this_refs, rest) = refs.split_at(*refs_len);811			refs = rest;812			let start = next_id;813			next_id += *dest_count as u32;814			Closure {815				references: this_refs,816				ids: start..next_id,817			}818		})819	}820}821822#[derive(Debug, Clone, Copy, PartialEq, Eq)]823pub enum DiagLevel {824	Error,825	Warning,826}827828#[derive(Debug, Clone, Acyclic)]829pub struct Diagnostic {830	pub level: DiagLevel,831	pub message: String,832	pub span: Option<Span>,833}834835struct DefiningClosure {836	first_local: LocalId,837	n_locals: u16,838}839840impl DefiningClosure {841	fn resolve(&self, target: LocalId) -> Option<LocalSlot> {842		let end = self.first_local.0 + u32::from(self.n_locals);843		if target.0 >= self.first_local.0 && target.0 < end {844			Some(LocalSlot(845				u16::try_from(target.0 - self.first_local.0).expect("local slots overflow"),846			))847		} else {848			None849		}850	}851	fn define_local(&mut self, local: LocalId) -> LocalSlot {852		let slot = self.n_locals;853		let id = self.first_local.0 + u32::from(slot);854		debug_assert_eq!(local.0, id);855		self.n_locals = self.n_locals.checked_add(1).expect("local slots overflow");856		LocalSlot(slot)857	}858}859860/// Per-closure capture computation state.861struct ClosureFrame {862	/// Closure may allocate locals863	defining: Option<DefiningClosure>,864	/// `LocalId` => capture index865	captures: FxHashMap<LocalId, CaptureSlot>,866	/// Capture sources in insertion order; consumed by `pop_closure_frame`.867	capture_sources: Vec<LSlot>,868}869870#[allow(clippy::struct_excessive_bools)]871pub struct AnalysisStack {872	local_defs: Vec<LocalDefinition>,873	/// Shadowing isn't used in jsonnet much, 2 because `SmallVec` allows to store 2 ptr-sized without overhead.874	/// TODO: Add test for this assumption (sizeof(SmallVec<[usize; 1]>) == sizeof(SmallVec<[usize; 2]>))875	local_by_name: FxHashMap<IStr, SmallVec<[LocalId; 2]>>,876877	/// Depth of the current locals frame.878	depth: u32,879	/// Last depth, at which object has appeared. `u32::MAX` = not appeared at all880	last_object_depth: u32,881	/// First depth, at which object has appeared. `u32::MAX` = not appeared at all882	/// $ refers to this object.883	first_object_depth: u32,884885	/// `LocalId` bound to the innermost object's `this`886	this_local: Option<LocalId>,887	/// Outermost object `this`, aka `$`888	dollar_alias: Option<LocalId>,889	/// True iff `self` has been referenced in the current object immediate members (not nested children).890	cur_self_used: bool,891	/// True iff `super` has been referenced in the current object immediate members.892	cur_super_used: bool,893	/// True iff `$` has been referenced anywhere since the outermost object's scope was entered.894	dollar_used: bool,895896	/// Stack of closure frames (innermost on top).897	closure_stack: Vec<ClosureFrame>,898899	diagnostics: Vec<Diagnostic>,900	/// Whenever analysis would be broken due to static analysis error.901	errored: bool,902}903904#[must_use]905struct ClosureOnStack {906	bomb: DropBomb,907}908909impl AnalysisStack {910	pub fn new() -> Self {911		Self {912			local_defs: Vec::new(),913			local_by_name: FxHashMap::default(),914			depth: 0,915			last_object_depth: u32::MAX,916			first_object_depth: u32::MAX,917			this_local: None,918			dollar_alias: None,919			cur_self_used: false,920			cur_super_used: false,921			dollar_used: false,922			closure_stack: Vec::new(),923			diagnostics: Vec::new(),924			errored: false,925		}926	}927928	fn push_root_closure(&mut self, externals: u16) -> ClosureOnStack {929		assert!(930			self.closure_stack.is_empty(),931			"root is only possible with empty stack"932		);933934		self.closure_stack.push(ClosureFrame {935			defining: Some(DefiningClosure {936				first_local: LocalId(0),937				n_locals: externals,938			}),939			captures: FxHashMap::default(),940			capture_sources: Vec::new(),941		});942943		ClosureOnStack {944			bomb: DropBomb::new("root closure"),945		}946	}947948	fn push_closure_a(&mut self, first_local: LocalId) -> ClosureOnStack {949		self.closure_stack.push(ClosureFrame {950			defining: Some(DefiningClosure {951				first_local,952				n_locals: 0,953			}),954			captures: FxHashMap::default(),955			capture_sources: Vec::new(),956		});957		ClosureOnStack {958			bomb: DropBomb::new("closure with locals"),959		}960	}961962	#[inline]963	fn in_using_closure<T>(964		&mut self,965		inner: impl FnOnce(&mut AnalysisStack) -> T,966	) -> (ClosureShape, T) {967		fn push_closure_b(stack: &mut AnalysisStack) -> ClosureOnStack {968			stack.closure_stack.push(ClosureFrame {969				defining: None,970				captures: FxHashMap::default(),971				capture_sources: Vec::new(),972			});973			ClosureOnStack {974				bomb: DropBomb::new("closure with locals"),975			}976		}977		let closure = push_closure_b(self);978		let v = inner(self);979		let shape = self.pop_closure(closure);980		(shape, v)981	}982983	fn pop_closure(&mut self, mut closure: ClosureOnStack) -> ClosureShape {984		closure.bomb.defuse();985		let frame = self.closure_stack.pop().expect("closure frame");986		ClosureShape {987			captures: frame.capture_sources.into_boxed_slice(),988			n_locals: frame.defining.map(|d| d.n_locals).unwrap_or_default(),989		}990	}991992	/// Resolve a `LocalId` reference to an `LSlot` against the innermost993	/// closure frame. May insert capture entries up the closure stack as994	/// needed.995	fn resolve_to_slot(&mut self, target: LocalId) -> LSlot {996		let top = self.closure_stack.len();997		debug_assert!(top > 0, "resolve_to_slot called with no closure frame");998		Self::resolve_at(&mut self.closure_stack, top - 1, target)999	}10001001	fn resolve_at(stack: &mut [ClosureFrame], idx: usize, target: LocalId) -> LSlot {1002		if let Some(def) = &stack[idx].defining {1003			if let Some(resolved) = def.resolve(target) {1004				return LSlot::Local(resolved);1005			}1006		} else {1007			// A sibling letrec slot must never be packed as a capture, or1008			// it would read an empty `OnceCell`.1009			for j in (0..idx).rev() {1010				if let Some(def) = &stack[j].defining {1011					if let Some(resolved) = def.resolve(target) {1012						return LSlot::Local(resolved);1013					}1014					break;1015				}1016			}1017		}1018		if let Some(&cap_idx) = stack[idx].captures.get(&target) {1019			return LSlot::Capture(cap_idx);1020		}1021		debug_assert!(idx > 0, "no enclosing closure frame for target {target:?}");1022		let parent_slot = Self::resolve_at(stack, idx - 1, target);1023		let frame = &mut stack[idx];1024		let cap_idx = CaptureSlot(1025			frame1026				.capture_sources1027				.len()1028				.try_into()1029				.expect("frame has more than u16::MAX captures"),1030		);1031		frame.capture_sources.push(parent_slot);1032		frame.captures.insert(target, cap_idx);1033		LSlot::Capture(cap_idx)1034	}10351036	fn next_local_id(&self) -> LocalId {1037		LocalId(self.local_defs.len() as u32)1038	}10391040	fn report_error(&mut self, msg: impl Into<String>, span: Option<Span>) {1041		self.errored = true;1042		self.diagnostics.push(Diagnostic {1043			level: DiagLevel::Error,1044			message: msg.into(),1045			span,1046		});1047	}1048	fn report_warning(&mut self, msg: impl Into<String>, span: Option<Span>) {1049		self.diagnostics.push(Diagnostic {1050			level: DiagLevel::Warning,1051			message: msg.into(),1052			span,1053		});1054	}10551056	fn use_local(&mut self, name: &IStr, span: Span, taint: &mut AnalysisResult) -> Option<LSlot> {1057		let Some(ids) = self.local_by_name.get(name) else {1058			let names = suggest_names(name, self.local_by_name.keys());1059			self.report_error(1060				format!("undefined local: {name}{}", format_found(&names, "local")),1061				Some(span),1062			);1063			return None;1064		};1065		let id = *ids.last().expect("empty stacks should be removed");1066		let depth = self.depth;1067		let def = &mut self.local_defs[id.idx()];1068		def.use_at(depth);1069		taint.depend_on_local(def.defined_at_depth);1070		if def.analyzed {1071			taint.taint_by(def.analysis);1072		} else {1073			def.scratch_referenced = true;1074		}1075		Some(self.resolve_to_slot(id))1076	}10771078	/// Assign name to the value provided externally, e.g `std`.1079	pub fn define_external_local(&mut self, name: IStr, id: LocalId) {1080		assert!(1081			self.local_defs.iter().all(|d| d.analyzed),1082			"external locals must be defined before the root expression is analysed"1083		);1084		assert_eq!(1085			id,1086			self.next_local_id(),1087			"external local id mismatch for {name} (externals must be defined in allocation order)"1088		);1089		self.local_defs.push(LocalDefinition {1090			name: name.clone(),1091			span: None,1092			defined_at_depth: 0,1093			used_at_depth: u32::MAX,1094			used_by_sibling: false,1095			analysis: AnalysisResult::default(),1096			analyzed: true,1097			scratch_referenced: false,1098		});1099		self.local_by_name.entry(name).or_default().push(id);1100	}11011102	fn defining_closure_mut(&mut self) -> &mut DefiningClosure {1103		self.closure_stack1104			.iter_mut()1105			.rev()1106			.find_map(|c| c.defining.as_mut())1107			.expect("no enclosing defining closure frame")1108	}1109	fn defining_closure(&self) -> &DefiningClosure {1110		self.closure_stack1111			.iter()1112			.rev()1113			.find_map(|c| c.defining.as_ref())1114			.expect("no enclosing defining closure frame")1115	}1116}11171118impl Default for AnalysisStack {1119	fn default() -> Self {1120		Self::new()1121	}1122}11231124impl AnalysisStack {1125	fn top_defining_local(&self) -> LocalId {1126		self.defining_closure().first_local1127	}11281129	/// Merge `used`'s analysis into `user`'s analysis and record that `user`1130	/// transitively depends on `used` (same-frame sibling reference).1131	/// Returns `true` if `user`'s analysis changed.1132	fn propagate_analysis(&mut self, user: LocalId, used: LocalId) -> bool {1133		let (used_analysis, used_defined_at_depth) = {1134			let u = &self.local_defs[used.idx()];1135			(u.analysis, u.defined_at_depth)1136		};1137		let user_def = &mut self.local_defs[user.idx()];1138		let before_obj = user_def.analysis.object_dependent_depth;1139		let before_loc = user_def.analysis.local_dependent_depth;1140		user_def.analysis.taint_by(used_analysis);1141		user_def.analysis.depend_on_local(used_defined_at_depth);1142		before_obj != user_def.analysis.object_dependent_depth1143			|| before_loc != user_def.analysis.local_dependent_depth1144	}1145}11461147mod names {1148	use crate::names;11491150	names! {1151		this: "this",1152	}1153}11541155// Object scope helpers1156impl AnalysisStack {1157	#[inline]1158	fn in_object_scope<T>(1159		&mut self,1160		inner: impl FnOnce(&mut AnalysisStack) -> T,1161	) -> (ObjectUsage, ClosureShape, T) {1162		fn enter_object_scope(stack: &mut AnalysisStack) -> ObjectScope {1163			let is_outermost = stack.first_object_depth == u32::MAX;1164			let this_id = stack.next_local_id();1165			let closure = stack.push_closure_a(this_id);1166			let pushed = stack.push_pseudo_local(names::this());1167			debug_assert_eq!(pushed, this_id, "this pseudo-local id");1168			let scope = ObjectScope {1169				this_id,1170				is_outermost,1171				prev_this_local: stack.this_local,1172				prev_dollar_alias: stack.dollar_alias,1173				prev_cur_self_used: stack.cur_self_used,1174				prev_cur_super_used: stack.cur_super_used,1175				prev_dollar_used: is_outermost.then_some(stack.dollar_used),1176				prev_last_object: stack.last_object_depth,1177				prev_first_object: stack.first_object_depth,1178				closure,1179			};11801181			stack.this_local = Some(scope.this_id);1182			if is_outermost {1183				stack.dollar_alias = Some(scope.this_id);1184				stack.first_object_depth = stack.depth;1185				stack.dollar_used = false;1186			}1187			stack.last_object_depth = stack.depth;1188			stack.cur_self_used = false;1189			stack.cur_super_used = false;1190			scope1191		}11921193		fn leave_object_scope(1194			stack: &mut AnalysisStack,1195			scope: ObjectScope,1196		) -> (ObjectUsage, ClosureShape) {1197			let ObjectScope {1198				this_id,1199				is_outermost,1200				prev_this_local,1201				prev_dollar_alias,1202				prev_cur_self_used,1203				prev_cur_super_used,1204				prev_dollar_used,1205				prev_last_object,1206				prev_first_object,1207				closure,1208			} = scope;1209			let _ = stack.local_defs.pop().expect("this pseudo-local exists");1210			debug_assert_eq!(stack.local_defs.len(), this_id.0 as usize);12111212			let set_dollar = is_outermost && stack.dollar_used;1213			let usage = ObjectUsage {1214				this_used: stack.cur_self_used || stack.cur_super_used || set_dollar,1215				uses_super: stack.cur_super_used,1216				set_dollar,1217			};12181219			stack.this_local = prev_this_local;1220			stack.dollar_alias = prev_dollar_alias;1221			stack.cur_self_used = prev_cur_self_used;1222			stack.cur_super_used = prev_cur_super_used;1223			if let Some(prev) = prev_dollar_used {1224				stack.dollar_used = prev;1225			}1226			stack.last_object_depth = prev_last_object;1227			stack.first_object_depth = prev_first_object;12281229			let frame_shape = stack.pop_closure(closure);1230			(usage, frame_shape)1231		}1232		let scope = enter_object_scope(self);1233		let v = inner(self);1234		let (usage, shape) = leave_object_scope(self, scope);1235		(usage, shape, v)1236	}12371238	fn push_pseudo_local(&mut self, name: IStr) -> LocalId {1239		let id = self.next_local_id();1240		self.local_defs.push(LocalDefinition {1241			name,1242			span: None,1243			defined_at_depth: self.depth,1244			used_at_depth: u32::MAX,1245			used_by_sibling: false,1246			analysis: AnalysisResult::default(),1247			analyzed: true,1248			scratch_referenced: false,1249		});1250		{1251			let def = self.defining_closure_mut();1252			let _ = def.define_local(id);1253		}1254		id1255	}12561257	fn use_this(&mut self, taint: &mut AnalysisResult) -> Option<LSlot> {1258		let id = self.this_local?;1259		self.cur_self_used = true;1260		self.use_pseudo_local(id, taint);1261		Some(self.resolve_to_slot(id))1262	}12631264	fn use_super(&mut self, taint: &mut AnalysisResult) -> Option<()> {1265		let id = self.this_local?;1266		self.cur_super_used = true;1267		self.use_pseudo_local(id, taint);1268		Some(())1269	}12701271	fn use_dollar(&mut self, taint: &mut AnalysisResult) -> Option<LSlot> {1272		let id = self.dollar_alias?;1273		self.dollar_used = true;1274		self.use_pseudo_local(id, taint);1275		Some(self.resolve_to_slot(id))1276	}12771278	// TODO: Dedicated type for object references instead of "pseudo local" BS, idk1279	fn use_pseudo_local(&mut self, id: LocalId, taint: &mut AnalysisResult) {1280		let depth = self.depth;1281		let def = &mut self.local_defs[id.idx()];1282		def.use_at(depth);1283		taint.depend_on_local(def.defined_at_depth);1284		taint.depend_on_object(def.defined_at_depth);1285	}1286}12871288#[must_use]1289struct ObjectScope {1290	this_id: LocalId,1291	is_outermost: bool,1292	prev_this_local: Option<LocalId>,1293	prev_dollar_alias: Option<LocalId>,1294	prev_cur_self_used: bool,1295	prev_cur_super_used: bool,1296	prev_dollar_used: Option<bool>,1297	prev_last_object: u32,1298	prev_first_object: u32,1299	closure: ClosureOnStack,1300}13011302struct ObjectUsage {1303	this_used: bool,1304	uses_super: bool,1305	set_dollar: bool,1306}13071308fn analyze_assert(1309	stmt: &AssertStmt,1310	stack: &mut AnalysisStack,1311	taint: &mut AnalysisResult,1312) -> LAssertStmt {1313	let cond = analyze(&stmt.assertion.value, stack, taint);1314	let message = stmt.message.as_ref().map(|m| analyze(m, stack, taint));1315	LAssertStmt {1316		cond: Spanned::new(cond, stmt.assertion.span.clone()),1317		message,1318	}1319}13201321#[allow(clippy::too_many_lines)]1322pub fn analyze_named(1323	name: IStr,1324	expr: &Expr,1325	stack: &mut AnalysisStack,1326	taint: &mut AnalysisResult,1327) -> LExpr {1328	if let Expr::Function(params, body) = expr {1329		return analyze_function(Some(name), params, body, stack, taint);1330	}1331	analyze(expr, stack, taint)1332}1333#[allow(clippy::too_many_lines)]1334pub fn analyze(expr: &Expr, stack: &mut AnalysisStack, taint: &mut AnalysisResult) -> LExpr {1335	match expr {1336		Expr::Literal(l) => match l {1337			LiteralType::This => stack.use_this(taint).map_or_else(1338				|| {1339					stack.report_error("`self` used outside of object", None);1340					LExpr::BadLocal("self")1341				},1342				LExpr::Slot,1343			),1344			LiteralType::Super => {1345				if stack.use_super(taint).is_some() {1346					LExpr::Super1347				} else {1348					stack.report_error("`super` used outside of object", None);1349					LExpr::BadLocal("super")1350				}1351			}1352			LiteralType::Dollar => stack.use_dollar(taint).map_or_else(1353				|| {1354					stack.report_error("`$` used outside of object", None);1355					LExpr::BadLocal("$")1356				},1357				LExpr::Slot,1358			),1359			LiteralType::Null => LExpr::Null,1360			LiteralType::True => LExpr::Bool(true),1361			LiteralType::False => LExpr::Bool(false),1362		},1363		Expr::Str(s) => LExpr::Str(s.clone()),1364		Expr::Num(n) => LExpr::Num(*n),1365		Expr::Var(v) => stack1366			.use_local(&v.value, v.span.clone(), taint)1367			.map_or_else(|| LExpr::BadLocal("ref"), LExpr::Slot),1368		Expr::Arr(a) => {1369			let (shape, items) = stack1370				.in_using_closure(|stack| a.iter().map(|v| analyze(v, stack, taint)).collect());1371			LExpr::Arr {1372				shape,1373				items: Rc::new(items),1374			}1375		}1376		Expr::ArrComp(inner, comp) => analyze_arr_comp(inner, comp, stack, taint),1377		Expr::Obj(obj) => LExpr::Obj(analyze_obj_body(obj, stack, taint)),1378		Expr::ObjExtend(base, obj) => LExpr::ObjExtend(1379			Box::new(analyze(base, stack, taint)),1380			analyze_obj_body(obj, stack, taint),1381		),1382		Expr::UnaryOp(op, value) => LExpr::UnaryOp(*op, Box::new(analyze(value, stack, taint))),1383		Expr::BinaryOp(op) => {1384			let BinaryOp {1385				lhs,1386				op: optype,1387				rhs,1388			} = &**op;1389			LExpr::BinaryOp {1390				lhs: Box::new(analyze(lhs, stack, taint)),1391				op: *optype,1392				rhs: Box::new(analyze(rhs, stack, taint)),1393			}1394		}1395		Expr::AssertExpr(assert) => {1396			let AssertExpr { assert, rest } = &**assert;1397			let assert = Rc::new(analyze_assert(assert, stack, taint));1398			let rest = Box::new(analyze(rest, stack, taint));1399			LExpr::AssertExpr { assert, rest }1400		}1401		Expr::LocalExpr(binds, body) => analyze_local_expr(binds, body, stack, taint),1402		Expr::Import(kind, path_expr) => {1403			let Expr::Str(path) = &**path_expr else {1404				stack.report_error(1405					"import path must be a string literal",1406					Some(kind.span.clone()),1407				);1408				return LExpr::BadLocal("bad import");1409			};1410			LExpr::Import {1411				kind: kind.clone(),1412				kind_span: kind.span.clone(),1413				path: path.clone(),1414			}1415		}1416		Expr::ErrorStmt(span, inner) => {1417			LExpr::Error(span.clone(), Box::new(analyze(inner, stack, taint)))1418		}1419		Expr::Apply(applicable, args, tailstrict) => {1420			let app = analyze(applicable, stack, taint);1421			let ArgsDesc {1422				unnamed,1423				names,1424				values,1425			} = &args.value;1426			let unnamed_l = unnamed1427				.iter()1428				.map(|a| Rc::new(analyze(a, stack, taint)))1429				.collect();1430			let values_l = values1431				.iter()1432				.map(|a| Rc::new(analyze(a, stack, taint)))1433				.collect();1434			LExpr::Apply {1435				applicable: Box::new(app),1436				args: Spanned::new(1437					LArgsDesc {1438						unnamed: unnamed_l,1439						names: names.clone(),1440						values: values_l,1441					},1442					args.span.clone(),1443				),1444				tailstrict: *tailstrict,1445			}1446		}1447		Expr::Index { indexable, parts } => {1448			let idx = analyze(indexable, stack, taint);1449			let parts_l = parts1450				.iter()1451				.map(|p| {1452					let value = analyze(&p.value, stack, taint);1453					LIndexPart {1454						span: p.span.clone(),1455						value,1456						#[cfg(feature = "exp-null-coaelse")]1457						null_coaelse: p.null_coaelse,1458					}1459				})1460				.collect();1461			LExpr::Index {1462				indexable: Box::new(idx),1463				parts: parts_l,1464			}1465		}1466		Expr::Function(params, body) => analyze_function(None, params, body, stack, taint),1467		Expr::IfElse(ifelse) => {1468			let IfElse {1469				cond,1470				cond_then,1471				cond_else,1472			} = &**ifelse;1473			let cond_l = analyze(&cond.cond, stack, taint);1474			let then_l = analyze(cond_then, stack, taint);1475			let else_l = cond_else1476				.as_ref()1477				.map(|e| Box::new(analyze(e, stack, taint)));1478			LExpr::IfElse {1479				cond: Box::new(cond_l),1480				cond_then: Box::new(then_l),1481				cond_else: else_l,1482			}1483		}1484		Expr::Slice(slice) => {1485			let Slice {1486				value,1487				slice: SliceDesc { start, end, step },1488			} = &**slice;1489			let value_l = analyze(value, stack, taint);1490			let start_l = start.as_ref().map(|e| analyze(&e.value, stack, taint));1491			let end_l = end.as_ref().map(|e| analyze(&e.value, stack, taint));1492			let step_l = step.as_ref().map(|e| analyze(&e.value, stack, taint));1493			LExpr::Slice(Box::new(LSliceExpr {1494				value: value_l,1495				start: start_l,1496				end: end_l,1497				step: step_l,1498			}))1499		}1500	}1501}15021503fn analyze_local_expr(1504	binds: &[BindSpec],1505	body: &Expr,1506	stack: &mut AnalysisStack,1507	taint: &mut AnalysisResult,1508) -> LExpr {1509	if binds.is_empty() {1510		return analyze(body, stack, taint);1511	}1512	let frame_start = stack.next_local_id();1513	let closure = stack.push_closure_a(frame_start);1514	let (l_binds, body_expr) = process_local_frame(binds, stack, taint, |stack, taint| {1515		analyze(body, stack, taint)1516	});1517	let frame_shape = stack.pop_closure(closure);1518	LExpr::LocalExpr(Box::new(LLocalExpr {1519		frame_shape,1520		binds: l_binds,1521		body: body_expr,1522	}))1523}15241525fn analyze_bind_value(1526	bind: &BindSpec,1527	stack: &mut AnalysisStack,1528	taint: &mut AnalysisResult,1529) -> LExpr {1530	match bind {1531		BindSpec::Field {1532			value: Expr::Function(params, value),1533			into: Destruct::Full(name),1534		} => analyze_function(Some(name.value.clone()), params, value, stack, taint),1535		BindSpec::Field { value, .. } => analyze(value, stack, taint),1536		BindSpec::Function {1537			params,1538			value,1539			name,1540		} => analyze_function(Some(name.clone()), params, value, stack, taint),1541	}1542}15431544fn process_local_frame<R>(1545	binds: &[BindSpec],1546	stack: &mut AnalysisStack,1547	taint: &mut AnalysisResult,1548	body_fn: impl FnOnce(&mut AnalysisStack, &mut AnalysisResult) -> R,1549) -> (Vec<LBind>, R) {1550	let mut alloc = FrameAlloc::new(stack);15511552	let mut destructs: Vec<Option<LDestruct>> = Vec::with_capacity(binds.len());1553	for bind in binds {1554		destructs.push(alloc.alloc_bind(bind));1555	}1556	let mut pending = alloc.finish();15571558	let mut l_binds: Vec<LBind> = Vec::with_capacity(binds.len());1559	for (bind, destruct) in binds.iter().zip(destructs.into_iter()) {1560		let mut value_taint = AnalysisResult::default();1561		let (value_shape, value) = pending1562			.stack1563			.in_using_closure(|stack| analyze_bind_value(bind, stack, &mut value_taint));1564		taint.taint_by(value_taint);1565		if let Some(destruct) = destruct {1566			pending.record_spec_init(&destruct, value_taint);1567			l_binds.push(LBind {1568				destruct,1569				value_shape,1570				value: Rc::new(value),1571			});1572		} else {1573			pending.closures.push_spec(0, &[]);1574		}1575	}15761577	let body_frame = pending.finish();1578	let result = body_fn(body_frame.stack, taint);1579	body_frame.finish();15801581	(l_binds, result)1582}15831584fn analyze_function(1585	name: Option<IStr>,1586	params: &ExprParams,1587	body: &Expr,1588	stack: &mut AnalysisStack,1589	taint: &mut AnalysisResult,1590) -> LExpr {1591	let mut alloc = FrameAlloc::new(stack);1592	let closure = alloc.push_locals_closure();15931594	let mut param_destructs: Vec<Option<LDestruct>> = Vec::with_capacity(params.exprs.len());1595	for p in &params.exprs {1596		param_destructs.push(alloc.alloc_destruct(&p.destruct));1597	}15981599	let mut pending = alloc.finish();16001601	let mut l_params: Vec<LParam> = Vec::with_capacity(params.exprs.len());1602	for (p, destruct) in params.exprs.iter().zip(param_destructs.into_iter()) {1603		let mut value_taint = AnalysisResult::default();1604		let default = p.default.as_ref().map_or_else(1605			|| None,1606			|d| {1607				Some(1608					pending1609						.stack1610						.in_using_closure(|stack| Rc::new(analyze(d, stack, &mut value_taint))),1611				)1612			},1613		);1614		taint.taint_by(value_taint);1615		if let Some(destruct) = destruct {1616			let name = match &p.destruct {1617				Destruct::Full(n) => Some(n.value.clone()),1618				#[cfg(feature = "exp-destruct")]1619				_ => None,1620			};1621			pending.record_spec_init(&destruct, value_taint);1622			l_params.push(LParam {1623				name,1624				destruct,1625				default,1626			});1627		} else {1628			pending.closures.push_spec(0, &[]);1629		}1630	}16311632	let body_frame = pending.finish();1633	let body_expr = analyze(body, body_frame.stack, taint);1634	body_frame.finish();1635	let body_shape = stack.pop_closure(closure);16361637	// function(x) x is an identity function1638	if l_params.len() == 1 && l_params[0].default.is_none() {1639		stack.report_warning(1640			"do not define identity functions manually, use std.id instead",1641			None,1642		);1643		#[allow(irrefutable_let_patterns, reason = "refutable with exp-destruct")]1644		if let LDestruct::Full(param_slot) = &l_params[0].destruct1645			&& let LExpr::Slot(LSlot::Local(s)) = &body_expr1646			&& s == param_slot1647		{1648			return LExpr::IdentityFunction {};1649		}1650	}16511652	LExpr::Function(Rc::new(LFunction {1653		name,1654		params: l_params,1655		signature: params.signature.clone(),1656		body_shape,1657		body: Rc::new(body_expr),1658	}))1659}16601661fn analyze_obj_body(1662	obj: &ObjBody,1663	stack: &mut AnalysisStack,1664	taint: &mut AnalysisResult,1665) -> LObjBody {1666	match obj {1667		ObjBody::MemberList(members) => {1668			LObjBody::MemberList(analyze_obj_members(members, stack, taint))1669		}1670		ObjBody::ObjComp(comp) => LObjBody::ObjComp(Box::new(analyze_obj_comp(comp, stack, taint))),1671	}1672}16731674fn analyze_obj_members(1675	members: &ObjMembers,1676	stack: &mut AnalysisStack,1677	taint: &mut AnalysisResult,1678) -> LObjMembers {1679	let ObjMembers {1680		locals,1681		asserts,1682		fields,1683	} = members;16841685	// Names are analyzed in enclosing scope, they can't depend on locals or self/super1686	let field_names: Vec<LFieldName> = fields1687		.iter()1688		.map(|f| match &f.name.value {1689			FieldName::Fixed(s) => LFieldName::Fixed(s.clone()),1690			FieldName::Dyn(e) => LFieldName::Dyn(analyze(e, stack, taint)),1691		})1692		.collect();16931694	let (usage, frame_shape, (l_binds, (l_asserts_opt, l_fields))) =1695		stack.in_object_scope(|stack| {1696			process_local_frame(locals, stack, taint, |stack, taint| {1697				let l_asserts_opt = if asserts.is_empty() {1698					None1699				} else {1700					let (shape, l_asserts) = stack.in_using_closure(|stack| {1701						let mut l_asserts = Vec::with_capacity(asserts.len());1702						for a in asserts {1703							let mut assert_taint = AnalysisResult::default();1704							l_asserts.push(analyze_assert(a, stack, &mut assert_taint));1705							taint.taint_by(assert_taint);1706						}1707						l_asserts1708					});1709					Some(Rc::new(LObjAsserts {1710						shape,1711						asserts: l_asserts,1712					}))1713				};1714				let mut l_fields = Vec::with_capacity(fields.len());1715				for (f, name) in fields.iter().zip(field_names) {1716					let value = stack.in_using_closure(|stack| {1717						if let Some(params) = &f.params {1718							analyze_function(name.function_name(), params, &f.value, stack, taint)1719						} else {1720							analyze(&f.value, stack, taint)1721						}1722					});1723					l_fields.push(LFieldMember {1724						name,1725						plus: f.plus,1726						visibility: f.visibility,1727						value: Rc::new(value),1728					});1729				}1730				(l_asserts_opt, l_fields)1731			})1732		});1733	// `this` was allocated as the first local of the object's frame,1734	// so its slot is 0 within that frame.1735	let this_slot = usage.this_used.then_some(LocalSlot(0));1736	LObjMembers {1737		frame_shape,1738		this: this_slot,1739		set_dollar: usage.set_dollar,1740		uses_super: usage.uses_super,1741		locals: Rc::new(l_binds),1742		asserts: l_asserts_opt,1743		fields: l_fields,1744	}1745}17461747fn analyze_obj_comp(1748	comp: &ObjComp,1749	stack: &mut AnalysisStack,1750	taint: &mut AnalysisResult,1751) -> LObjComp {1752	let res = analyze_comp_specs(&comp.compspecs, stack, taint, |stack, taint| {1753		let field_name = match &comp.field.name.value {1754			FieldName::Fixed(s) => LFieldName::Fixed(s.clone()),1755			FieldName::Dyn(e) => LFieldName::Dyn(analyze(e, stack, taint)),1756		};17571758		let (usage, frame_shape, body) = stack.in_object_scope(|stack| {1759			process_local_frame(&comp.locals, stack, taint, |stack, taint| {1760				let value = stack.in_using_closure(|stack| {1761					if let Some(params) = &comp.field.params {1762						analyze_function(None, params, &comp.field.value, stack, taint)1763					} else {1764						analyze(&comp.field.value, stack, taint)1765					}1766				});1767				LFieldMember {1768					name: field_name,1769					plus: comp.field.plus,1770					visibility: comp.field.visibility,1771					value: Rc::new(value),1772				}1773			})1774		});1775		(usage, frame_shape, body)1776	});1777	let (usage, frame_shape, (locals, field)) = res.inner;1778	let this_slot = usage.this_used.then_some(LocalSlot(0));1779	LObjComp {1780		frame_shape: Rc::new(frame_shape),1781		this: this_slot,1782		set_dollar: usage.set_dollar,1783		uses_super: usage.uses_super,1784		locals: Rc::new(locals),1785		field,1786		compspecs: res.compspecs,1787	}1788}17891790fn analyze_arr_comp(1791	inner: &Expr,1792	specs: &[CompSpec],1793	stack: &mut AnalysisStack,1794	taint: &mut AnalysisResult,1795) -> LExpr {1796	let res = analyze_comp_specs(specs, stack, taint, |stack, taint| {1797		stack.in_using_closure(|stack| analyze(inner, stack, taint))1798	});1799	let (value_shape, value) = res.inner;1800	LExpr::ArrComp(Box::new(LArrComp {1801		value_shape,1802		value: Rc::new(value),1803		compspecs: res.compspecs,1804	}))1805}18061807fn analyze_comp_specs<R>(1808	specs: &[CompSpec],1809	stack: &mut AnalysisStack,1810	taint: &mut AnalysisResult,1811	inside: impl FnOnce(&mut AnalysisStack, &mut AnalysisResult) -> R,1812) -> CompSpecResult<R> {1813	fn go<R>(1814		idx: usize,1815		specs: &[CompSpec],1816		outer_depth: u32,1817		stack: &mut AnalysisStack,1818		taint: &mut AnalysisResult,1819		inside: impl FnOnce(&mut AnalysisStack, &mut AnalysisResult) -> R,1820	) -> (R, Vec<LCompSpec>) {1821		if idx >= specs.len() {1822			return (inside(stack, taint), Vec::new());1823		}1824		match &specs[idx] {1825			CompSpec::IfSpec(IfSpecData { cond, .. }) => {1826				let cond_l = analyze(cond, stack, taint);1827				let (r, mut rest) = go(idx + 1, specs, outer_depth, stack, taint, inside);1828				rest.insert(0, LCompSpec::If(cond_l));1829				(r, rest)1830			}1831			CompSpec::ForSpec(ForSpecData { destruct, over }) => {1832				let mut over_taint = AnalysisResult::default();1833				let over_l = analyze(over, stack, &mut over_taint);1834				let loop_invariant = over_taint.local_dependent_depth > outer_depth;1835				taint.taint_by(over_taint);18361837				let mut alloc = FrameAlloc::new(stack);1838				let closure = alloc.push_locals_closure();1839				let Some(l_destruct) = alloc.alloc_destruct(destruct) else {1840					stack.pop_closure(closure);1841					return go(idx + 1, specs, outer_depth, stack, taint, inside);1842				};1843				let mut pending = alloc.finish();18441845				let var_analysis = AnalysisResult::default();1846				pending.record_spec_init(&l_destruct, var_analysis);18471848				let body_frame = pending.finish();1849				let (r, mut rest) =1850					go(idx + 1, specs, outer_depth, body_frame.stack, taint, inside);1851				body_frame.finish();1852				let frame_shape = stack.pop_closure(closure);18531854				rest.insert(1855					0,1856					LCompSpec::For {1857						frame_shape,1858						destruct: l_destruct,1859						over: over_l,1860						loop_invariant,1861					},1862				);1863				(r, rest)1864			}1865			#[cfg(feature = "exp-object-iteration")]1866			CompSpec::ForObjSpec(_) => todo!(),1867		}1868	}1869	let outer_depth = stack.depth;1870	let (r, compspecs) = go(0, specs, outer_depth, stack, taint, inside);1871	CompSpecResult {1872		inner: r,1873		compspecs,1874	}1875}18761877struct CompSpecResult<R> {1878	inner: R,1879	compspecs: Vec<LCompSpec>,1880}18811882pub fn analyze_root(expr: &Expr, ctx: Vec<(IStr, LocalId)>) -> AnalysisReport {1883	let mut stack = AnalysisStack::new();1884	for (name, id) in ctx {1885		stack.define_external_local(name, id);1886	}18871888	let externals_count: u16 = stack1889		.local_defs1890		.len()1891		.try_into()1892		.expect("more than u16::MAX externals");1893	let closure = stack.push_root_closure(externals_count);18941895	let mut taint = AnalysisResult::default();1896	let lir = analyze(expr, &mut stack, &mut taint);18971898	let root_shape = stack.pop_closure(closure);1899	debug_assert!(1900		stack.closure_stack.is_empty(),1901		"closure stack imbalance after analyze"1902	);19031904	AnalysisReport {1905		lir,1906		root_shape,1907		root_analysis: taint,1908		diagnostics_list: stack.diagnostics,1909		errored: stack.errored,1910	}1911}19121913#[cfg(test)]1914fn render_diagnostics(src: &str, diags: &[Diagnostic]) -> String {1915	use std::fmt::Write;19161917	use hi_doc::{Formatting, SnippetBuilder, Text};19181919	let mut out = String::new();1920	let mut unspanned = Vec::new();1921	let mut spanned: Vec<&Diagnostic> = Vec::new();1922	for d in diags {1923		if d.span.is_some() {1924			spanned.push(d);1925		} else {1926			unspanned.push(d);1927		}1928	}1929	if !spanned.is_empty() {1930		let mut builder = SnippetBuilder::new(src);1931		for d in spanned {1932			let span = d.span.as_ref().expect("spanned");1933			let ab = match d.level {1934				DiagLevel::Error => {1935					builder.error(Text::fragment(d.message.clone(), Formatting::default()))1936				}1937				DiagLevel::Warning => {1938					builder.warning(Text::fragment(d.message.clone(), Formatting::default()))1939				}1940			};1941			ab.range(span.range()).build();1942		}1943		out.push_str(&hi_doc::source_to_ansi(&builder.build()));1944	}1945	for d in unspanned {1946		let prefix = match d.level {1947			DiagLevel::Error => "error",1948			DiagLevel::Warning => "warning",1949		};1950		writeln!(out, "{prefix}: {}", d.message).expect("fmt");1951	}1952	out1953}19541955pub struct AnalysisReport {1956	pub lir: LExpr,1957	pub root_shape: ClosureShape,1958	pub root_analysis: AnalysisResult,1959	pub diagnostics_list: Vec<Diagnostic>,1960	pub errored: bool,1961}19621963#[cfg(test)]1964mod tests {1965	use std::fs;19661967	use insta::{assert_snapshot, glob};1968	use jrsonnet_ir::Source;19691970	use super::*;19711972	#[test]1973	fn snapshots() {1974		glob!("analysis_tests/*.jsonnet", |path| {1975			let code = fs::read_to_string(path).expect("read test file");1976			let src = Source::new_virtual("<test>".into(), code.clone().into());1977			let expr = crate::parse_jsonnet(&code, src.clone()).expect("parse");1978			let report = analyze_root(&expr, Vec::new());19791980			let diagnostics = render_diagnostics(src.code(), &report.diagnostics_list);1981			// Strip ANSI escapes from diagnostics so snapshots are readable.1982			let diagnostics = strip_ansi_escapes::strip_str(&diagnostics);1983			let rendered = format!(1984				"--- source ---\n{}\n--- root analysis ---\nobject_dependent_depth: {}\nlocal_dependent_depth: {}\nerrored: {}\n--- diagnostics ---\n{}--- lir ---\n{:#?}\n",1985				code.trim_end(),1986				fmt_depth(report.root_analysis.object_dependent_depth),1987				fmt_depth(report.root_analysis.local_dependent_depth),1988				report.errored,1989				diagnostics,1990				report.lir,1991			);1992			assert_snapshot!(rendered);1993		});1994	}19951996	fn fmt_depth(d: u32) -> String {1997		if d == u32::MAX {1998			"none".into()1999		} else {2000			d.to_string()2001		}2002	}2003}
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, 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::error::{format_found, suggest_names};3233#[derive(Debug, Clone, Copy)]34#[must_use]35pub struct AnalysisResult {36	/// Highest object, on which identity the value is dependent. `u32::MAX` = not dependent at all37	pub object_dependent_depth: u32,38	/// Highest local frame, on which this value depends. `u32::MAX` = not dependent at all39	pub local_dependent_depth: u32,40}4142impl Default for AnalysisResult {43	fn default() -> Self {44		Self {45			object_dependent_depth: u32::MAX,46			local_dependent_depth: u32::MAX,47		}48	}49}5051impl AnalysisResult {52	fn depend_on_object(&mut self, depth: u32) {53		if depth < self.object_dependent_depth {54			self.object_dependent_depth = depth;55		}56	}57	fn depend_on_local(&mut self, depth: u32) {58		if depth < self.local_dependent_depth {59			self.local_dependent_depth = depth;60		}61	}62	fn taint_by(&mut self, other: AnalysisResult) {63		self.depend_on_object(other.object_dependent_depth);64		self.depend_on_local(other.local_dependent_depth);65	}66}6768#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Acyclic)]69pub struct LocalId(pub u32);7071impl LocalId {72	fn idx(self) -> usize {73		self.0 as usize74	}75	fn defined_before(self, other: Self) -> bool {76		self.0 < other.077	}78}7980#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Acyclic)]81pub enum LSlot {82	/// Enclosing frame locals (sibling letrec, params, etc.).83	Local(LocalSlot),84	/// Enclosing closure's capture pack.85	Capture(CaptureSlot),86}8788#[derive(Debug, Acyclic)]89pub struct ClosureShape {90	pub captures: Box<[LSlot]>,91	pub n_locals: u16,92}9394struct LocalDefinition {95	name: IStr,96	span: Option<Span>,97	/// At which frame depth this local was defined98	defined_at_depth: u32,99	/// Min frame depth, at which this local was used. `u32::MAX` = not used at all.100	/// This check won't catch unused argument closures, i.e:101	/// ```jsonnet102	/// local103	///     a = b,104	///     b = a,105	/// ; 2 + 2106	///107	/// ```108	/// Both `a` and `b` here are "used", but the whole closure was not used here.109	used_at_depth: u32,110	/// Used as part of the current frame closure111	used_by_sibling: bool,112	/// Analysys result for value of this local113	analysis: AnalysisResult,114	/// Has `analysis` been filled in?115	/// For sanity checking, locals are initialized in batchs, use `first_uninitialized_local`116	analyzed: bool,117	/// During walk over uninitialized vars, we can't refer to analysis results of other locals,118	/// but we need to. To make that work, for each variable in variable frame we capture its closure,119	/// by looking at referenced variables.120	scratch_referenced: bool,121}122123impl LocalDefinition {124	fn use_at(&mut self, depth: u32) {125		if depth == self.defined_at_depth {126			self.used_by_sibling = true;127			return;128		}129		if depth < self.used_at_depth {130			self.used_at_depth = depth;131		}132	}133}134135#[derive(Debug, Acyclic)]136pub enum LExpr {137	Slot(LSlot),138	Null,139	Bool(bool),140	Str(IStr),141	Num(NumValue),142	Arr {143		shape: ClosureShape,144		items: Rc<Vec<LExpr>>,145	},146	ArrComp(Box<LArrComp>),147	Obj(LObjBody),148	ObjExtend(Box<LExpr>, LObjBody),149	UnaryOp(UnaryOpType, Box<LExpr>),150	BinaryOp {151		lhs: Box<LExpr>,152		op: BinaryOpType,153		rhs: Box<LExpr>,154	},155	AssertExpr {156		assert: Rc<LAssertStmt>,157		rest: Box<LExpr>,158	},159	Error(Span, Box<LExpr>),160	LocalExpr(Box<LLocalExpr>),161	Import {162		kind: Spanned<ImportKind>,163		kind_span: Span,164		path: IStr,165	},166	Apply {167		applicable: Box<LExpr>,168		args: Spanned<LArgsDesc>,169		tailstrict: bool,170	},171	Index {172		indexable: Box<LExpr>,173		parts: Vec<LIndexPart>,174	},175	Function(Rc<LFunction>),176	IdentityFunction,177	IfElse {178		cond: Box<LExpr>,179		cond_then: Box<LExpr>,180		cond_else: Option<Box<LExpr>>,181	},182	Slice(Box<LSliceExpr>),183	Super,184185	/// Allows partial evaluation of broken expression tree,186	/// expressions with failed static analysis end up here187	BadLocal(&'static str),188}189190#[derive(Debug, Acyclic)]191pub struct LLocalExpr {192	pub frame_shape: ClosureShape,193	pub binds: Vec<LBind>,194	pub body: LExpr,195}196197#[derive(Debug, Acyclic)]198pub struct LFunction {199	pub name: Option<IStr>,200	pub params: Vec<LParam>,201	pub signature: FunctionSignature,202203	pub body_shape: ClosureShape,204	pub body: Rc<LExpr>,205}206207#[derive(Debug, Acyclic)]208pub struct LParam {209	pub name: Option<IStr>,210	pub destruct: LDestruct,211212	pub default: Option<(ClosureShape, Rc<LExpr>)>,213}214215#[derive(Debug, Acyclic)]216pub struct LBind {217	pub destruct: LDestruct,218	pub value_shape: ClosureShape,219	pub value: Rc<LExpr>,220}221222#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Acyclic)]223pub struct CaptureSlot(pub(crate) u16);224#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Acyclic)]225pub struct LocalSlot(pub(crate) u16);226227#[derive(Debug, Acyclic)]228pub enum LDestruct {229	Full(LocalSlot),230	#[cfg(feature = "exp-destruct")]231	Skip,232	#[cfg(feature = "exp-destruct")]233	Array {234		start: Vec<LDestruct>,235		rest: Option<LDestructRest>,236		end: Vec<LDestruct>,237	},238	#[cfg(feature = "exp-destruct")]239	Object {240		fields: Vec<LDestructField>,241		rest: Option<LDestructRest>,242	},243}244245#[derive(Debug, Clone, Copy, Acyclic)]246pub enum LDestructRest {247	Keep(LocalSlot),248	Drop,249}250251#[derive(Debug, Acyclic)]252pub struct LDestructField {253	pub name: IStr,254	pub into: Option<LDestruct>,255	pub default: Option<(ClosureShape, Rc<LExpr>)>,256}257258impl LDestruct {259	pub fn each_slot<F: FnMut(LocalSlot)>(&self, f: &mut F) {260		match self {261			Self::Full(s) => f(*s),262			#[cfg(feature = "exp-destruct")]263			Self::Skip => {}264			#[cfg(feature = "exp-destruct")]265			Self::Array { start, rest, end } => {266				for d in start {267					d.each_slot(f);268				}269				if let Some(LDestructRest::Keep(s)) = rest {270					f(*s);271				}272				for d in end {273					d.each_slot(f);274				}275			}276			#[cfg(feature = "exp-destruct")]277			Self::Object { fields, rest } => {278				for field in fields {279					if let Some(into) = &field.into {280						into.each_slot(f);281					} else {282						unreachable!("shorthand object destruct must store `into`");283					}284				}285				if let Some(LDestructRest::Keep(s)) = rest {286					f(*s);287				}288			}289		}290	}291292	pub fn slots(&self) -> SmallVec<[LocalSlot; 1]> {293		let mut out = SmallVec::new();294		self.each_slot(&mut |s| out.push(s));295		out296	}297}298299#[derive(Debug, Acyclic)]300pub struct LSliceExpr {301	pub value: LExpr,302	pub start: Option<LExpr>,303	pub end: Option<LExpr>,304	pub step: Option<LExpr>,305}306307#[derive(Debug, Acyclic)]308pub struct LArgsDesc {309	pub unnamed: Vec<Rc<LExpr>>,310	pub names: Vec<IStr>,311	pub values: Vec<Rc<LExpr>>,312}313314#[derive(Debug, Acyclic)]315pub struct LAssertStmt {316	pub cond: Spanned<LExpr>,317	pub message: Option<LExpr>,318}319320#[derive(Debug, Acyclic)]321pub struct LIndexPart {322	pub span: Span,323	pub value: LExpr,324	#[cfg(feature = "exp-null-coaelse")]325	pub null_coaelse: bool,326}327328#[derive(Debug, Acyclic)]329pub enum LObjBody {330	MemberList(LObjMembers),331	ObjComp(Box<LObjComp>),332}333334#[derive(Debug, Acyclic)]335pub struct LObjMembers {336	pub frame_shape: ClosureShape,337	/// If current object identity (`super`/`this`/`$`) is used, `this` should338	/// be saved to the specified local slot.339	pub this: Option<LocalSlot>,340	/// Set if dollar should also be assigned to object identity, `this` should also be set (TODO: proper type-level validation)341	pub set_dollar: bool,342	/// True iff `super` is referenced by this object's members.343	pub uses_super: bool,344345	pub locals: Rc<Vec<LBind>>,346	pub asserts: Option<Rc<LObjAsserts>>,347	pub fields: Vec<LFieldMember>,348}349350#[derive(Debug, Acyclic)]351pub struct LObjComp {352	pub frame_shape: Rc<ClosureShape>,353	pub this: Option<LocalSlot>,354	pub set_dollar: bool,355	pub uses_super: bool,356357	pub locals: Rc<Vec<LBind>>,358	pub field: LFieldMember,359	pub compspecs: Vec<LCompSpec>,360}361362#[derive(Debug, Acyclic)]363pub struct LFieldMember {364	pub name: LFieldName,365	pub plus: bool,366	pub visibility: Visibility,367	pub value: Rc<(ClosureShape, LExpr)>,368}369370#[derive(Debug, Acyclic)]371pub struct LClosure<T: Acyclic> {372	pub shape: ClosureShape,373	pub value: T,374}375376#[derive(Debug, Acyclic)]377pub struct LObjAsserts {378	pub shape: ClosureShape,379	pub asserts: Vec<LAssertStmt>,380}381382#[derive(Debug, Acyclic)]383pub enum LFieldName {384	Fixed(IStr),385	Dyn(LExpr),386}387impl LFieldName {388	fn function_name(&self) -> Option<IStr> {389		match self {390			LFieldName::Fixed(istr) => Some(istr.clone()),391			LFieldName::Dyn(_) => None,392		}393	}394}395396#[derive(Debug, Acyclic)]397pub struct LArrComp {398	pub value_shape: ClosureShape,399	pub value: Rc<LExpr>,400	pub compspecs: Vec<LCompSpec>,401}402403#[derive(Debug, Acyclic)]404pub enum LCompSpec {405	If(LExpr),406	For {407		frame_shape: ClosureShape,408		destruct: LDestruct,409		over: LExpr,410		/// Is `over` does not depend on any variable introduced by an earlier for-spec in this comprehension chain411		loop_invariant: bool,412	},413	#[cfg(feature = "exp-object-iteration")]414	ForObj {415		frame_shape: ClosureShape,416		key: LocalSlot,417		visibility: jrsonnet_ir::Visibility,418		value: LDestruct,419		over: LExpr,420		loop_invariant: bool,421	},422}423424struct FrameAlloc<'s> {425	first_in_frame: LocalId,426	stack: &'s mut AnalysisStack,427	bomb: DropBomb,428}429impl<'s> FrameAlloc<'s> {430	fn new(stack: &'s mut AnalysisStack) -> Self {431		FrameAlloc {432			first_in_frame: stack.next_local_id(),433			stack,434			bomb: DropBomb::new("binding frame state"),435		}436	}437438	fn push_locals_closure(&mut self) -> ClosureOnStack {439		self.stack.push_closure_a(self.first_in_frame)440	}441442	fn define_local(&mut self, name: IStr, span: Option<Span>) -> Option<(LocalId, LocalSlot)> {443		let id = self.stack.next_local_id();444		let stack = self.stack.local_by_name.entry(name.clone()).or_default();445		if let Some(&existing) = stack.last()446			&& !existing.defined_before(self.first_in_frame)447		{448			self.stack.report_error(449				format!("local is already defined in the current frame: {name}"),450				span,451			);452			return None;453		}454		stack.push(id);455		self.stack.local_defs.push(LocalDefinition {456			name,457			span,458			defined_at_depth: self.stack.depth,459			used_at_depth: u32::MAX,460			used_by_sibling: false,461			analysis: AnalysisResult::default(),462			analyzed: false,463			scratch_referenced: false,464		});465		let def = self.stack.defining_closure_mut();466		Some((id, def.define_local(id)))467	}468	fn alloc_bind(&mut self, bind: &BindSpec) -> Option<LDestruct> {469		match bind {470			BindSpec::Field { into, .. } => self.alloc_destruct(into),471			BindSpec::Function { name, .. } => {472				let (_, id) = self.define_local(name.clone(), None)?;473				Some(LDestruct::Full(id))474			}475		}476	}477	fn alloc_destruct(&mut self, destruct: &Destruct) -> Option<LDestruct> {478		Some(match destruct {479			Destruct::Full(name) => {480				let (_, id) = self.define_local(name.value.clone(), Some(name.span.clone()))?;481				LDestruct::Full(id)482			}483			#[cfg(feature = "exp-destruct")]484			Destruct::Skip => LDestruct::Skip,485			#[cfg(feature = "exp-destruct")]486			Destruct::Array { start, rest, end } => {487				let start = start488					.iter()489					.map(|d| self.alloc_destruct(d))490					.collect::<Option<Vec<_>>>()?;491				let rest = match rest {492					Some(jrsonnet_ir::DestructRest::Keep(name)) => {493						let (_, id) = self.define_local(name.clone(), None)?;494						Some(LDestructRest::Keep(id))495					}496					Some(jrsonnet_ir::DestructRest::Drop) => Some(LDestructRest::Drop),497					None => None,498				};499				let end = end500					.iter()501					.map(|d| self.alloc_destruct(d))502					.collect::<Option<Vec<_>>>()?;503				LDestruct::Array { start, rest, end }504			}505			#[cfg(feature = "exp-destruct")]506			Destruct::Object { fields, rest } => {507				let mut l_fields: Vec<(IStr, LDestruct)> = Vec::with_capacity(fields.len());508				// Allocate destruct LocalIds, then analyse defaults509				for (name, into, _default) in fields {510					let into = if let Some(inner) = into {511						self.alloc_destruct(inner)?512					} else {513						let (_, id) = self.define_local(name.clone(), None)?;514						LDestruct::Full(id)515					};516					l_fields.push((name.clone(), into));517				}518				// All locals exist, so defaults can reference any sibling.519				let l_fields: Vec<LDestructField> = l_fields520					.into_iter()521					.zip(fields.iter())522					.map(|((name, into), (_n, _i, default))| {523						let default = match default {524							Some(e) => {525								let mut default_taint = AnalysisResult::default();526								Some(self.stack.in_using_closure(|stack| {527									Rc::new(analyze(&e.value, stack, &mut default_taint))528								}))529							}530							None => None,531						};532						LDestructField {533							name,534							into: Some(into),535							default,536						}537					})538					.collect();539				let rest = match rest {540					Some(jrsonnet_ir::DestructRest::Keep(name)) => {541						let (_, id) = self.define_local(name.clone(), None)?;542						Some(LDestructRest::Keep(id))543					}544					Some(jrsonnet_ir::DestructRest::Drop) => Some(LDestructRest::Drop),545					None => None,546				};547				LDestruct::Object {548					fields: l_fields,549					rest,550				}551			}552		})553	}554555	fn finish(self) -> PendingInit<'s> {556		let Self {557			first_in_frame,558			stack,559			bomb,560		} = self;561		let first_after_frame = stack.next_local_id();562		PendingInit {563			first_after_frame,564			stack,565			closures: Closures {566				referenced: vec![],567				spec_shapes: vec![],568				first_in_frame,569			},570			bomb,571		}572	}573}574575/// Frame state: `LocalIds` allocated, values not yet analysed.576struct PendingInit<'s> {577	first_after_frame: LocalId,578	stack: &'s mut AnalysisStack,579	closures: Closures,580	bomb: DropBomb,581}582583impl<'s> PendingInit<'s> {584	/// Record the analysis of a spec's value: stamp every id bound by the585	/// spec with `analysis`, collect the spec's same-frame references, and586	/// append them to `closures`.587	fn record_spec_init(&mut self, destruct: &LDestruct, analysis: AnalysisResult) {588		let mut refs: SmallVec<[LocalId; 4]> = SmallVec::new();589		for i in self.closures.first_in_frame.0..self.first_after_frame.0 {590			let def = &mut self.stack.local_defs[i as usize];591			if def.scratch_referenced {592				refs.push(LocalId(i));593				def.scratch_referenced = false;594			}595		}596597		let mut ids_count = 0;598		let first_local = self.stack.top_defining_local();599		destruct.each_slot(&mut |slot| {600			ids_count += 1;601			let id = LocalId(first_local.0 + u32::from(slot.0));602			let def = &mut self.stack.local_defs[id.idx()];603			debug_assert!(!def.analyzed, "sanity: local {:?} analysed twice", def.name);604			def.analysis = analysis;605			def.analyzed = true;606		});607		self.closures.push_spec(ids_count, &refs);608	}609	/// After all specs are analysed, propagate dependency information between610	/// siblings to a fix-point, then switch to "body" mode.611	fn finish(self) -> PendingBody<'s> {612		let Self {613			first_after_frame,614			closures,615			stack,616			bomb,617		} = self;618619		debug_assert_eq!(620			first_after_frame,621			stack.next_local_id(),622			"frame initialisation left unfinished locals"623		);624625		debug_assert_eq!(626			closures.spec_shapes.iter().map(|(_, d)| *d).sum::<usize>(),627			(first_after_frame.0 - closures.first_in_frame.0) as usize,628			"closures destruct-id counts must match frame local count"629		);630631		let mut changed = true;632		while changed {633			changed = false;634			for spec in closures.iter_specs() {635				for id_raw in spec.ids.clone() {636					let user = LocalId(id_raw);637					for &used in spec.references {638						changed |= stack.propagate_analysis(user, used);639					}640				}641			}642		}643644		stack.depth += 1;645		PendingBody {646			first_after_frame,647			closures,648			stack,649			bomb,650		}651	}652}653654/// Frame state: values analysed, body not yet walked.655struct PendingBody<'s> {656	first_after_frame: LocalId,657	closures: Closures,658	stack: &'s mut AnalysisStack,659	bomb: DropBomb,660}661impl<'s> PendingBody<'s> {662	/// After the body is processed, drop the frame's locals and emit any663	/// "unused local" warnings.664	fn finish(self) {665		let PendingBody {666			first_after_frame,667			closures,668			stack,669			mut bomb,670		} = self;671		bomb.defuse();672		stack.depth -= 1;673674		debug_assert_eq!(675			first_after_frame,676			stack.next_local_id(),677			"nested scopes must be popped before outer frames"678		);679680		let mut changed = true;681		while changed {682			changed = false;683			for spec in closures.iter_specs() {684				// Effective used_at_depth for the spec = min over its ids.685				let mut min_used_at = u32::MAX;686				for id_raw in spec.ids.clone() {687					min_used_at = min_used_at.min(stack.local_defs[id_raw as usize].used_at_depth);688				}689				if min_used_at == u32::MAX {690					continue;691				}692				for &used in spec.references {693					let used_def = &mut stack.local_defs[used.idx()];694					if min_used_at < used_def.used_at_depth {695						used_def.used_at_depth = min_used_at;696						changed = true;697					}698				}699			}700		}701702		let drained: Vec<LocalDefinition> = stack703			.local_defs704			.drain(closures.first_in_frame.idx()..)705			.collect();706		for (i, def) in drained.iter().enumerate().rev() {707			let id = LocalId(closures.first_in_frame.0 + i as u32);708			let stack_locals = stack709				.local_by_name710				.get_mut(&def.name)711				.expect("local must be in name map");712			let popped = stack_locals.pop().expect("name stack should not be empty");713			debug_assert_eq!(popped, id, "name stack integrity");714			if stack_locals.is_empty() {715				stack.local_by_name.remove(&def.name);716			}717718			if def.used_at_depth == u32::MAX {719				if def.used_by_sibling {720					stack.report_warning(721						format!("local is only referenced by unused siblings: {}", def.name),722						def.span.clone(),723					);724				} else {725					stack.report_warning(format!("unused local: {}", def.name), def.span.clone());726				}727			} else if def.analysis.local_dependent_depth > def.defined_at_depth728				&& def.analysis.object_dependent_depth > def.defined_at_depth729				&& def.defined_at_depth != 0730			{731				// The value doesn't depend on anything defined at or inside732				// this local's scope - can be hoisted, unfortunately not automatically.733				stack.report_warning(734					format!("local could be hoisted to an outer scope: {}", def.name),735					def.span.clone(),736				);737			}738		}739	}740}741742struct Closures {743	/// All the referenced locals, maybe repeated multiple times744	/// It is recorded as continous vec of sets, I.e we have745	/// a = 1, 2, 3746	/// b = 3, 4, 5, 6747	/// And in `referenced` we have `[ 1, 2, 3, 3, 4, 5, 6 ]`. To actually get, which closure refers to which element, see `spec_shapes`...748	/// Flat concatenation of sibling-local references across all specs.749	referenced: Vec<LocalId>,750	/// Amount of elements per closure, for the above case it is a = 3, b = 4, so here751	/// lies `[ 3, 4 ]`752	/// ~~closures: Vec<usize>,~~753	/// Finally, we have destructs.754	/// Because single destruct references single closure, but destructs to multiple locals, we have even more complicated structure.755	/// Luckly, every destruct is not interleaved with each other, so here we can have full list...756	/// 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 elements757	/// for each destruct starting from `first_destruct_local`758	/// ~~destructs: Vec<usize>,~~759	///760	/// => two of those fields were merged, as there is currently no per-destruct tracking of closures.761	/// For each spec in order: `(references_count, destruct_ids_count)`.762	/// `references_count` tells us how many entries of `referenced` belong763	/// to this spec; `destruct_ids_count` tells us how many `LocalIds` it764	/// binds.765	spec_shapes: Vec<(usize, usize)>,766	/// This is not a related doccomment, just a continuation of docs for previous fields.767	/// Having768	/// ```jsonnet769	/// local770	///     [a, b, c] = [d, e, f],771	///     [d, e, f] = [a, b, c, h],772	///     h = 1,773	/// ;774	/// ```775	///776	/// We have total of 7 locals777	/// First local here is `a` => `first_destruct_local` = `a`778	/// 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`779	/// [d, e, f] = [a, b, c, h], => `referenced += [a, b, c, h]`, `closures += 4`, `destructs += 3` (Note that this destruct will fail at runtime,780	///                                                                                               this thing should not care about that, it only captures what the value are referencing)781	/// h = 1 => referenced += [], closures += 0, destructs += 1782	/// And the result is783	///784	/// ```rust,ignore785	/// Closures {786	///     referenced: vec![d, e, f, a, b, c, h]787	///     spec_shapes: vec![(3, 3), (4, 3), (0, 1)],788	///     first_destruct_label: a,789	/// }790	/// ```791	///792	/// Reconstruction of that:793	///794	/// We know that we start with a795	/// We get the first number from destructs: `destructs.shift() == 3` => `destructs = [3, 1]`796	/// 3 elements counting from a => [a, b, c]797	/// Then we take first number from closures: `closures.shift() == 3` => `closures = [4, 0]`798	/// Then we take 3 items from referenced: `referenced.shift()x3 == d, e, f` => `referenced = [a, b, c, h]`799	///800	/// Thus we have [a, b, c] = [d, e, f]801	first_in_frame: LocalId,802}803804struct Closure<'a> {805	references: &'a [LocalId],806	ids: std::ops::Range<u32>,807}808809impl Closures {810	fn push_spec(&mut self, destruct_ids_count: usize, refs: &[LocalId]) {811		self.referenced.extend_from_slice(refs);812		self.spec_shapes.push((refs.len(), destruct_ids_count));813	}814815	fn iter_specs(&self) -> impl Iterator<Item = Closure<'_>> {816		let mut refs = self.referenced.as_slice();817		let mut next_id = self.first_in_frame.0;818		self.spec_shapes.iter().map(move |(refs_len, dest_count)| {819			let (this_refs, rest) = refs.split_at(*refs_len);820			refs = rest;821			let start = next_id;822			next_id += *dest_count as u32;823			Closure {824				references: this_refs,825				ids: start..next_id,826			}827		})828	}829}830831#[derive(Debug, Clone, Copy, PartialEq, Eq)]832pub enum DiagLevel {833	Error,834	Warning,835}836837#[derive(Debug, Clone, Acyclic)]838pub struct Diagnostic {839	pub level: DiagLevel,840	pub message: String,841	pub span: Option<Span>,842}843844struct DefiningClosure {845	first_local: LocalId,846	n_locals: u16,847}848849impl DefiningClosure {850	fn resolve(&self, target: LocalId) -> Option<LocalSlot> {851		let end = self.first_local.0 + u32::from(self.n_locals);852		if target.0 >= self.first_local.0 && target.0 < end {853			Some(LocalSlot(854				u16::try_from(target.0 - self.first_local.0).expect("local slots overflow"),855			))856		} else {857			None858		}859	}860	fn define_local(&mut self, local: LocalId) -> LocalSlot {861		let slot = self.n_locals;862		let id = self.first_local.0 + u32::from(slot);863		debug_assert_eq!(local.0, id);864		self.n_locals = self.n_locals.checked_add(1).expect("local slots overflow");865		LocalSlot(slot)866	}867}868869/// Per-closure capture computation state.870struct ClosureFrame {871	/// Closure may allocate locals872	defining: Option<DefiningClosure>,873	/// `LocalId` => capture index874	captures: FxHashMap<LocalId, CaptureSlot>,875	/// Capture sources in insertion order; consumed by `pop_closure_frame`.876	capture_sources: Vec<LSlot>,877}878879#[allow(clippy::struct_excessive_bools)]880pub struct AnalysisStack {881	local_defs: Vec<LocalDefinition>,882	/// Shadowing isn't used in jsonnet much, 2 because `SmallVec` allows to store 2 ptr-sized without overhead.883	/// TODO: Add test for this assumption (sizeof(SmallVec<[usize; 1]>) == sizeof(SmallVec<[usize; 2]>))884	local_by_name: FxHashMap<IStr, SmallVec<[LocalId; 2]>>,885886	/// Depth of the current locals frame.887	depth: u32,888	/// Last depth, at which object has appeared. `u32::MAX` = not appeared at all889	last_object_depth: u32,890	/// First depth, at which object has appeared. `u32::MAX` = not appeared at all891	/// $ refers to this object.892	first_object_depth: u32,893894	/// `LocalId` bound to the innermost object's `this`895	this_local: Option<LocalId>,896	/// Outermost object `this`, aka `$`897	dollar_alias: Option<LocalId>,898	/// True iff `self` has been referenced in the current object immediate members (not nested children).899	cur_self_used: bool,900	/// True iff `super` has been referenced in the current object immediate members.901	cur_super_used: bool,902	/// True iff `$` has been referenced anywhere since the outermost object's scope was entered.903	dollar_used: bool,904905	/// Stack of closure frames (innermost on top).906	closure_stack: Vec<ClosureFrame>,907908	diagnostics: Vec<Diagnostic>,909	/// Whenever analysis would be broken due to static analysis error.910	errored: bool,911}912913#[must_use]914struct ClosureOnStack {915	bomb: DropBomb,916}917918impl AnalysisStack {919	pub fn new() -> Self {920		Self {921			local_defs: Vec::new(),922			local_by_name: FxHashMap::default(),923			depth: 0,924			last_object_depth: u32::MAX,925			first_object_depth: u32::MAX,926			this_local: None,927			dollar_alias: None,928			cur_self_used: false,929			cur_super_used: false,930			dollar_used: false,931			closure_stack: Vec::new(),932			diagnostics: Vec::new(),933			errored: false,934		}935	}936937	fn push_root_closure(&mut self, externals: u16) -> ClosureOnStack {938		assert!(939			self.closure_stack.is_empty(),940			"root is only possible with empty stack"941		);942943		self.closure_stack.push(ClosureFrame {944			defining: Some(DefiningClosure {945				first_local: LocalId(0),946				n_locals: externals,947			}),948			captures: FxHashMap::default(),949			capture_sources: Vec::new(),950		});951952		ClosureOnStack {953			bomb: DropBomb::new("root closure"),954		}955	}956957	fn push_closure_a(&mut self, first_local: LocalId) -> ClosureOnStack {958		self.closure_stack.push(ClosureFrame {959			defining: Some(DefiningClosure {960				first_local,961				n_locals: 0,962			}),963			captures: FxHashMap::default(),964			capture_sources: Vec::new(),965		});966		ClosureOnStack {967			bomb: DropBomb::new("closure with locals"),968		}969	}970971	#[inline]972	fn in_using_closure<T>(973		&mut self,974		inner: impl FnOnce(&mut AnalysisStack) -> T,975	) -> (ClosureShape, T) {976		fn push_closure_b(stack: &mut AnalysisStack) -> ClosureOnStack {977			stack.closure_stack.push(ClosureFrame {978				defining: None,979				captures: FxHashMap::default(),980				capture_sources: Vec::new(),981			});982			ClosureOnStack {983				bomb: DropBomb::new("closure with locals"),984			}985		}986		let closure = push_closure_b(self);987		let v = inner(self);988		let shape = self.pop_closure(closure);989		(shape, v)990	}991992	fn pop_closure(&mut self, mut closure: ClosureOnStack) -> ClosureShape {993		closure.bomb.defuse();994		let frame = self.closure_stack.pop().expect("closure frame");995		ClosureShape {996			captures: frame.capture_sources.into_boxed_slice(),997			n_locals: frame.defining.map(|d| d.n_locals).unwrap_or_default(),998		}999	}10001001	/// Resolve a `LocalId` reference to an `LSlot` against the innermost1002	/// closure frame. May insert capture entries up the closure stack as1003	/// needed.1004	fn resolve_to_slot(&mut self, target: LocalId) -> LSlot {1005		let top = self.closure_stack.len();1006		debug_assert!(top > 0, "resolve_to_slot called with no closure frame");1007		Self::resolve_at(&mut self.closure_stack, top - 1, target)1008	}10091010	fn resolve_at(stack: &mut [ClosureFrame], idx: usize, target: LocalId) -> LSlot {1011		if let Some(def) = &stack[idx].defining {1012			if let Some(resolved) = def.resolve(target) {1013				return LSlot::Local(resolved);1014			}1015		} else {1016			// A sibling letrec slot must never be packed as a capture, or1017			// it would read an empty `OnceCell`.1018			for j in (0..idx).rev() {1019				if let Some(def) = &stack[j].defining {1020					if let Some(resolved) = def.resolve(target) {1021						return LSlot::Local(resolved);1022					}1023					break;1024				}1025			}1026		}1027		if let Some(&cap_idx) = stack[idx].captures.get(&target) {1028			return LSlot::Capture(cap_idx);1029		}1030		debug_assert!(idx > 0, "no enclosing closure frame for target {target:?}");1031		let parent_slot = Self::resolve_at(stack, idx - 1, target);1032		let frame = &mut stack[idx];1033		let cap_idx = CaptureSlot(1034			frame1035				.capture_sources1036				.len()1037				.try_into()1038				.expect("frame has more than u16::MAX captures"),1039		);1040		frame.capture_sources.push(parent_slot);1041		frame.captures.insert(target, cap_idx);1042		LSlot::Capture(cap_idx)1043	}10441045	fn next_local_id(&self) -> LocalId {1046		LocalId(self.local_defs.len() as u32)1047	}10481049	fn report_error(&mut self, msg: impl Into<String>, span: Option<Span>) {1050		self.errored = true;1051		self.diagnostics.push(Diagnostic {1052			level: DiagLevel::Error,1053			message: msg.into(),1054			span,1055		});1056	}1057	fn report_warning(&mut self, msg: impl Into<String>, span: Option<Span>) {1058		self.diagnostics.push(Diagnostic {1059			level: DiagLevel::Warning,1060			message: msg.into(),1061			span,1062		});1063	}10641065	fn use_local(&mut self, name: &IStr, span: Span, taint: &mut AnalysisResult) -> Option<LSlot> {1066		let Some(ids) = self.local_by_name.get(name) else {1067			let names = suggest_names(name, self.local_by_name.keys());1068			self.report_error(1069				format!("undefined local: {name}{}", format_found(&names, "local")),1070				Some(span),1071			);1072			return None;1073		};1074		let id = *ids.last().expect("empty stacks should be removed");1075		let depth = self.depth;1076		let def = &mut self.local_defs[id.idx()];1077		def.use_at(depth);1078		taint.depend_on_local(def.defined_at_depth);1079		if def.analyzed {1080			taint.taint_by(def.analysis);1081		} else {1082			def.scratch_referenced = true;1083		}1084		Some(self.resolve_to_slot(id))1085	}10861087	/// Assign name to the value provided externally, e.g `std`.1088	pub fn define_external_local(&mut self, name: IStr, id: LocalId) {1089		assert!(1090			self.local_defs.iter().all(|d| d.analyzed),1091			"external locals must be defined before the root expression is analysed"1092		);1093		assert_eq!(1094			id,1095			self.next_local_id(),1096			"external local id mismatch for {name} (externals must be defined in allocation order)"1097		);1098		self.local_defs.push(LocalDefinition {1099			name: name.clone(),1100			span: None,1101			defined_at_depth: 0,1102			used_at_depth: u32::MAX,1103			used_by_sibling: false,1104			analysis: AnalysisResult::default(),1105			analyzed: true,1106			scratch_referenced: false,1107		});1108		self.local_by_name.entry(name).or_default().push(id);1109	}11101111	fn defining_closure_mut(&mut self) -> &mut DefiningClosure {1112		self.closure_stack1113			.iter_mut()1114			.rev()1115			.find_map(|c| c.defining.as_mut())1116			.expect("no enclosing defining closure frame")1117	}1118	fn defining_closure(&self) -> &DefiningClosure {1119		self.closure_stack1120			.iter()1121			.rev()1122			.find_map(|c| c.defining.as_ref())1123			.expect("no enclosing defining closure frame")1124	}1125}11261127impl Default for AnalysisStack {1128	fn default() -> Self {1129		Self::new()1130	}1131}11321133impl AnalysisStack {1134	fn top_defining_local(&self) -> LocalId {1135		self.defining_closure().first_local1136	}11371138	/// Merge `used`'s analysis into `user`'s analysis and record that `user`1139	/// transitively depends on `used` (same-frame sibling reference).1140	/// Returns `true` if `user`'s analysis changed.1141	fn propagate_analysis(&mut self, user: LocalId, used: LocalId) -> bool {1142		let (used_analysis, used_defined_at_depth) = {1143			let u = &self.local_defs[used.idx()];1144			(u.analysis, u.defined_at_depth)1145		};1146		let user_def = &mut self.local_defs[user.idx()];1147		let before_obj = user_def.analysis.object_dependent_depth;1148		let before_loc = user_def.analysis.local_dependent_depth;1149		user_def.analysis.taint_by(used_analysis);1150		user_def.analysis.depend_on_local(used_defined_at_depth);1151		before_obj != user_def.analysis.object_dependent_depth1152			|| before_loc != user_def.analysis.local_dependent_depth1153	}1154}11551156mod names {1157	use crate::names;11581159	names! {1160		this: "this",1161	}1162}11631164// Object scope helpers1165impl AnalysisStack {1166	#[inline]1167	fn in_object_scope<T>(1168		&mut self,1169		inner: impl FnOnce(&mut AnalysisStack) -> T,1170	) -> (ObjectUsage, ClosureShape, T) {1171		fn enter_object_scope(stack: &mut AnalysisStack) -> ObjectScope {1172			let is_outermost = stack.first_object_depth == u32::MAX;1173			let this_id = stack.next_local_id();1174			let closure = stack.push_closure_a(this_id);1175			let pushed = stack.push_pseudo_local(names::this());1176			debug_assert_eq!(pushed, this_id, "this pseudo-local id");1177			let scope = ObjectScope {1178				this_id,1179				is_outermost,1180				prev_this_local: stack.this_local,1181				prev_dollar_alias: stack.dollar_alias,1182				prev_cur_self_used: stack.cur_self_used,1183				prev_cur_super_used: stack.cur_super_used,1184				prev_dollar_used: is_outermost.then_some(stack.dollar_used),1185				prev_last_object: stack.last_object_depth,1186				prev_first_object: stack.first_object_depth,1187				closure,1188			};11891190			stack.this_local = Some(scope.this_id);1191			if is_outermost {1192				stack.dollar_alias = Some(scope.this_id);1193				stack.first_object_depth = stack.depth;1194				stack.dollar_used = false;1195			}1196			stack.last_object_depth = stack.depth;1197			stack.cur_self_used = false;1198			stack.cur_super_used = false;1199			scope1200		}12011202		fn leave_object_scope(1203			stack: &mut AnalysisStack,1204			scope: ObjectScope,1205		) -> (ObjectUsage, ClosureShape) {1206			let ObjectScope {1207				this_id,1208				is_outermost,1209				prev_this_local,1210				prev_dollar_alias,1211				prev_cur_self_used,1212				prev_cur_super_used,1213				prev_dollar_used,1214				prev_last_object,1215				prev_first_object,1216				closure,1217			} = scope;1218			let _ = stack.local_defs.pop().expect("this pseudo-local exists");1219			debug_assert_eq!(stack.local_defs.len(), this_id.0 as usize);12201221			let set_dollar = is_outermost && stack.dollar_used;1222			let usage = ObjectUsage {1223				this_used: stack.cur_self_used || stack.cur_super_used || set_dollar,1224				uses_super: stack.cur_super_used,1225				set_dollar,1226			};12271228			stack.this_local = prev_this_local;1229			stack.dollar_alias = prev_dollar_alias;1230			stack.cur_self_used = prev_cur_self_used;1231			stack.cur_super_used = prev_cur_super_used;1232			if let Some(prev) = prev_dollar_used {1233				stack.dollar_used = prev;1234			}1235			stack.last_object_depth = prev_last_object;1236			stack.first_object_depth = prev_first_object;12371238			let frame_shape = stack.pop_closure(closure);1239			(usage, frame_shape)1240		}1241		let scope = enter_object_scope(self);1242		let v = inner(self);1243		let (usage, shape) = leave_object_scope(self, scope);1244		(usage, shape, v)1245	}12461247	fn push_pseudo_local(&mut self, name: IStr) -> LocalId {1248		let id = self.next_local_id();1249		self.local_defs.push(LocalDefinition {1250			name,1251			span: None,1252			defined_at_depth: self.depth,1253			used_at_depth: u32::MAX,1254			used_by_sibling: false,1255			analysis: AnalysisResult::default(),1256			analyzed: true,1257			scratch_referenced: false,1258		});1259		{1260			let def = self.defining_closure_mut();1261			let _ = def.define_local(id);1262		}1263		id1264	}12651266	fn use_this(&mut self, taint: &mut AnalysisResult) -> Option<LSlot> {1267		let id = self.this_local?;1268		self.cur_self_used = true;1269		self.use_pseudo_local(id, taint);1270		Some(self.resolve_to_slot(id))1271	}12721273	fn use_super(&mut self, taint: &mut AnalysisResult) -> Option<()> {1274		let id = self.this_local?;1275		self.cur_super_used = true;1276		self.use_pseudo_local(id, taint);1277		Some(())1278	}12791280	fn use_dollar(&mut self, taint: &mut AnalysisResult) -> Option<LSlot> {1281		let id = self.dollar_alias?;1282		self.dollar_used = true;1283		self.use_pseudo_local(id, taint);1284		Some(self.resolve_to_slot(id))1285	}12861287	// TODO: Dedicated type for object references instead of "pseudo local" BS, idk1288	fn use_pseudo_local(&mut self, id: LocalId, taint: &mut AnalysisResult) {1289		let depth = self.depth;1290		let def = &mut self.local_defs[id.idx()];1291		def.use_at(depth);1292		taint.depend_on_local(def.defined_at_depth);1293		taint.depend_on_object(def.defined_at_depth);1294	}1295}12961297#[must_use]1298struct ObjectScope {1299	this_id: LocalId,1300	is_outermost: bool,1301	prev_this_local: Option<LocalId>,1302	prev_dollar_alias: Option<LocalId>,1303	prev_cur_self_used: bool,1304	prev_cur_super_used: bool,1305	prev_dollar_used: Option<bool>,1306	prev_last_object: u32,1307	prev_first_object: u32,1308	closure: ClosureOnStack,1309}13101311struct ObjectUsage {1312	this_used: bool,1313	uses_super: bool,1314	set_dollar: bool,1315}13161317fn analyze_assert(1318	stmt: &AssertStmt,1319	stack: &mut AnalysisStack,1320	taint: &mut AnalysisResult,1321) -> LAssertStmt {1322	let cond = analyze(&stmt.assertion.value, stack, taint);1323	let message = stmt.message.as_ref().map(|m| analyze(m, stack, taint));1324	LAssertStmt {1325		cond: Spanned::new(cond, stmt.assertion.span.clone()),1326		message,1327	}1328}13291330#[allow(clippy::too_many_lines)]1331pub fn analyze_named(1332	name: IStr,1333	expr: &Expr,1334	stack: &mut AnalysisStack,1335	taint: &mut AnalysisResult,1336) -> LExpr {1337	if let Expr::Function(params, body) = expr {1338		return analyze_function(Some(name), params, body, stack, taint);1339	}1340	analyze(expr, stack, taint)1341}1342#[allow(clippy::too_many_lines)]1343pub fn analyze(expr: &Expr, stack: &mut AnalysisStack, taint: &mut AnalysisResult) -> LExpr {1344	match expr {1345		Expr::Literal(l) => match l {1346			LiteralType::This => stack.use_this(taint).map_or_else(1347				|| {1348					stack.report_error("`self` used outside of object", None);1349					LExpr::BadLocal("self")1350				},1351				LExpr::Slot,1352			),1353			LiteralType::Super => {1354				if stack.use_super(taint).is_some() {1355					LExpr::Super1356				} else {1357					stack.report_error("`super` used outside of object", None);1358					LExpr::BadLocal("super")1359				}1360			}1361			LiteralType::Dollar => stack.use_dollar(taint).map_or_else(1362				|| {1363					stack.report_error("`$` used outside of object", None);1364					LExpr::BadLocal("$")1365				},1366				LExpr::Slot,1367			),1368			LiteralType::Null => LExpr::Null,1369			LiteralType::True => LExpr::Bool(true),1370			LiteralType::False => LExpr::Bool(false),1371		},1372		Expr::Str(s) => LExpr::Str(s.clone()),1373		Expr::Num(n) => LExpr::Num(*n),1374		Expr::Var(v) => stack1375			.use_local(&v.value, v.span.clone(), taint)1376			.map_or_else(|| LExpr::BadLocal("ref"), LExpr::Slot),1377		Expr::Arr(a) => {1378			let (shape, items) = stack1379				.in_using_closure(|stack| a.iter().map(|v| analyze(v, stack, taint)).collect());1380			LExpr::Arr {1381				shape,1382				items: Rc::new(items),1383			}1384		}1385		Expr::ArrComp(inner, comp) => analyze_arr_comp(inner, comp, stack, taint),1386		Expr::Obj(obj) => LExpr::Obj(analyze_obj_body(obj, stack, taint)),1387		Expr::ObjExtend(base, obj) => LExpr::ObjExtend(1388			Box::new(analyze(base, stack, taint)),1389			analyze_obj_body(obj, stack, taint),1390		),1391		Expr::UnaryOp(op, value) => LExpr::UnaryOp(*op, Box::new(analyze(value, stack, taint))),1392		Expr::BinaryOp(op) => {1393			let BinaryOp {1394				lhs,1395				op: optype,1396				rhs,1397			} = &**op;1398			LExpr::BinaryOp {1399				lhs: Box::new(analyze(lhs, stack, taint)),1400				op: *optype,1401				rhs: Box::new(analyze(rhs, stack, taint)),1402			}1403		}1404		Expr::AssertExpr(assert) => {1405			let AssertExpr { assert, rest } = &**assert;1406			let assert = Rc::new(analyze_assert(assert, stack, taint));1407			let rest = Box::new(analyze(rest, stack, taint));1408			LExpr::AssertExpr { assert, rest }1409		}1410		Expr::LocalExpr(binds, body) => analyze_local_expr(binds, body, stack, taint),1411		Expr::Import(kind, path_expr) => {1412			let Expr::Str(path) = &**path_expr else {1413				stack.report_error(1414					"import path must be a string literal",1415					Some(kind.span.clone()),1416				);1417				return LExpr::BadLocal("bad import");1418			};1419			LExpr::Import {1420				kind: kind.clone(),1421				kind_span: kind.span.clone(),1422				path: path.clone(),1423			}1424		}1425		Expr::ErrorStmt(span, inner) => {1426			LExpr::Error(span.clone(), Box::new(analyze(inner, stack, taint)))1427		}1428		Expr::Apply(applicable, args, tailstrict) => {1429			let app = analyze(applicable, stack, taint);1430			let ArgsDesc {1431				unnamed,1432				names,1433				values,1434			} = &args.value;1435			let unnamed_l = unnamed1436				.iter()1437				.map(|a| Rc::new(analyze(a, stack, taint)))1438				.collect();1439			let values_l = values1440				.iter()1441				.map(|a| Rc::new(analyze(a, stack, taint)))1442				.collect();1443			LExpr::Apply {1444				applicable: Box::new(app),1445				args: Spanned::new(1446					LArgsDesc {1447						unnamed: unnamed_l,1448						names: names.clone(),1449						values: values_l,1450					},1451					args.span.clone(),1452				),1453				tailstrict: *tailstrict,1454			}1455		}1456		Expr::Index { indexable, parts } => {1457			let idx = analyze(indexable, stack, taint);1458			let parts_l = parts1459				.iter()1460				.map(|p| {1461					let value = analyze(&p.value, stack, taint);1462					LIndexPart {1463						span: p.span.clone(),1464						value,1465						#[cfg(feature = "exp-null-coaelse")]1466						null_coaelse: p.null_coaelse,1467					}1468				})1469				.collect();1470			LExpr::Index {1471				indexable: Box::new(idx),1472				parts: parts_l,1473			}1474		}1475		Expr::Function(params, body) => analyze_function(None, params, body, stack, taint),1476		Expr::IfElse(ifelse) => {1477			let IfElse {1478				cond,1479				cond_then,1480				cond_else,1481			} = &**ifelse;1482			let cond_l = analyze(&cond.cond, stack, taint);1483			let then_l = analyze(cond_then, stack, taint);1484			let else_l = cond_else1485				.as_ref()1486				.map(|e| Box::new(analyze(e, stack, taint)));1487			LExpr::IfElse {1488				cond: Box::new(cond_l),1489				cond_then: Box::new(then_l),1490				cond_else: else_l,1491			}1492		}1493		Expr::Slice(slice) => {1494			let Slice {1495				value,1496				slice: SliceDesc { start, end, step },1497			} = &**slice;1498			let value_l = analyze(value, stack, taint);1499			let start_l = start.as_ref().map(|e| analyze(&e.value, stack, taint));1500			let end_l = end.as_ref().map(|e| analyze(&e.value, stack, taint));1501			let step_l = step.as_ref().map(|e| analyze(&e.value, stack, taint));1502			LExpr::Slice(Box::new(LSliceExpr {1503				value: value_l,1504				start: start_l,1505				end: end_l,1506				step: step_l,1507			}))1508		}1509	}1510}15111512fn analyze_local_expr(1513	binds: &[BindSpec],1514	body: &Expr,1515	stack: &mut AnalysisStack,1516	taint: &mut AnalysisResult,1517) -> LExpr {1518	if binds.is_empty() {1519		return analyze(body, stack, taint);1520	}1521	let frame_start = stack.next_local_id();1522	let closure = stack.push_closure_a(frame_start);1523	let (l_binds, body_expr) = process_local_frame(binds, stack, taint, |stack, taint| {1524		analyze(body, stack, taint)1525	});1526	let frame_shape = stack.pop_closure(closure);1527	LExpr::LocalExpr(Box::new(LLocalExpr {1528		frame_shape,1529		binds: l_binds,1530		body: body_expr,1531	}))1532}15331534fn analyze_bind_value(1535	bind: &BindSpec,1536	stack: &mut AnalysisStack,1537	taint: &mut AnalysisResult,1538) -> LExpr {1539	match bind {1540		BindSpec::Field {1541			value: Expr::Function(params, value),1542			into: Destruct::Full(name),1543		} => analyze_function(Some(name.value.clone()), params, value, stack, taint),1544		BindSpec::Field { value, .. } => analyze(value, stack, taint),1545		BindSpec::Function {1546			params,1547			value,1548			name,1549		} => analyze_function(Some(name.clone()), params, value, stack, taint),1550	}1551}15521553fn process_local_frame<R>(1554	binds: &[BindSpec],1555	stack: &mut AnalysisStack,1556	taint: &mut AnalysisResult,1557	body_fn: impl FnOnce(&mut AnalysisStack, &mut AnalysisResult) -> R,1558) -> (Vec<LBind>, R) {1559	let mut alloc = FrameAlloc::new(stack);15601561	let mut destructs: Vec<Option<LDestruct>> = Vec::with_capacity(binds.len());1562	for bind in binds {1563		destructs.push(alloc.alloc_bind(bind));1564	}1565	let mut pending = alloc.finish();15661567	let mut l_binds: Vec<LBind> = Vec::with_capacity(binds.len());1568	for (bind, destruct) in binds.iter().zip(destructs.into_iter()) {1569		let mut value_taint = AnalysisResult::default();1570		let (value_shape, value) = pending1571			.stack1572			.in_using_closure(|stack| analyze_bind_value(bind, stack, &mut value_taint));1573		taint.taint_by(value_taint);1574		if let Some(destruct) = destruct {1575			pending.record_spec_init(&destruct, value_taint);1576			l_binds.push(LBind {1577				destruct,1578				value_shape,1579				value: Rc::new(value),1580			});1581		} else {1582			pending.closures.push_spec(0, &[]);1583		}1584	}15851586	let body_frame = pending.finish();1587	let result = body_fn(body_frame.stack, taint);1588	body_frame.finish();15891590	(l_binds, result)1591}15921593fn analyze_function(1594	name: Option<IStr>,1595	params: &ExprParams,1596	body: &Expr,1597	stack: &mut AnalysisStack,1598	taint: &mut AnalysisResult,1599) -> LExpr {1600	let mut alloc = FrameAlloc::new(stack);1601	let closure = alloc.push_locals_closure();16021603	let mut param_destructs: Vec<Option<LDestruct>> = Vec::with_capacity(params.exprs.len());1604	for p in &params.exprs {1605		param_destructs.push(alloc.alloc_destruct(&p.destruct));1606	}16071608	let mut pending = alloc.finish();16091610	let mut l_params: Vec<LParam> = Vec::with_capacity(params.exprs.len());1611	for (p, destruct) in params.exprs.iter().zip(param_destructs.into_iter()) {1612		let mut value_taint = AnalysisResult::default();1613		let default = p.default.as_ref().map_or_else(1614			|| None,1615			|d| {1616				Some(1617					pending1618						.stack1619						.in_using_closure(|stack| Rc::new(analyze(d, stack, &mut value_taint))),1620				)1621			},1622		);1623		taint.taint_by(value_taint);1624		if let Some(destruct) = destruct {1625			let name = match &p.destruct {1626				Destruct::Full(n) => Some(n.value.clone()),1627				#[cfg(feature = "exp-destruct")]1628				_ => None,1629			};1630			pending.record_spec_init(&destruct, value_taint);1631			l_params.push(LParam {1632				name,1633				destruct,1634				default,1635			});1636		} else {1637			pending.closures.push_spec(0, &[]);1638		}1639	}16401641	let body_frame = pending.finish();1642	let body_expr = analyze(body, body_frame.stack, taint);1643	body_frame.finish();1644	let body_shape = stack.pop_closure(closure);16451646	// function(x) x is an identity function1647	if l_params.len() == 1 && l_params[0].default.is_none() {1648		stack.report_warning(1649			"do not define identity functions manually, use std.id instead",1650			None,1651		);1652		#[allow(irrefutable_let_patterns, reason = "refutable with exp-destruct")]1653		if let LDestruct::Full(param_slot) = &l_params[0].destruct1654			&& let LExpr::Slot(LSlot::Local(s)) = &body_expr1655			&& s == param_slot1656		{1657			return LExpr::IdentityFunction {};1658		}1659	}16601661	LExpr::Function(Rc::new(LFunction {1662		name,1663		params: l_params,1664		signature: params.signature.clone(),1665		body_shape,1666		body: Rc::new(body_expr),1667	}))1668}16691670fn analyze_obj_body(1671	obj: &ObjBody,1672	stack: &mut AnalysisStack,1673	taint: &mut AnalysisResult,1674) -> LObjBody {1675	match obj {1676		ObjBody::MemberList(members) => {1677			LObjBody::MemberList(analyze_obj_members(members, stack, taint))1678		}1679		ObjBody::ObjComp(comp) => LObjBody::ObjComp(Box::new(analyze_obj_comp(comp, stack, taint))),1680	}1681}16821683fn analyze_obj_members(1684	members: &ObjMembers,1685	stack: &mut AnalysisStack,1686	taint: &mut AnalysisResult,1687) -> LObjMembers {1688	let ObjMembers {1689		locals,1690		asserts,1691		fields,1692	} = members;16931694	// Names are analyzed in enclosing scope, they can't depend on locals or self/super1695	let field_names: Vec<LFieldName> = fields1696		.iter()1697		.map(|f| match &f.name.value {1698			FieldName::Fixed(s) => LFieldName::Fixed(s.clone()),1699			FieldName::Dyn(e) => LFieldName::Dyn(analyze(e, stack, taint)),1700		})1701		.collect();17021703	let (usage, frame_shape, (l_binds, (l_asserts_opt, l_fields))) =1704		stack.in_object_scope(|stack| {1705			process_local_frame(locals, stack, taint, |stack, taint| {1706				let l_asserts_opt = if asserts.is_empty() {1707					None1708				} else {1709					let (shape, l_asserts) = stack.in_using_closure(|stack| {1710						let mut l_asserts = Vec::with_capacity(asserts.len());1711						for a in asserts {1712							let mut assert_taint = AnalysisResult::default();1713							l_asserts.push(analyze_assert(a, stack, &mut assert_taint));1714							taint.taint_by(assert_taint);1715						}1716						l_asserts1717					});1718					Some(Rc::new(LObjAsserts {1719						shape,1720						asserts: l_asserts,1721					}))1722				};1723				let mut l_fields = Vec::with_capacity(fields.len());1724				for (f, name) in fields.iter().zip(field_names) {1725					let value = stack.in_using_closure(|stack| {1726						if let Some(params) = &f.params {1727							analyze_function(name.function_name(), params, &f.value, stack, taint)1728						} else {1729							analyze(&f.value, stack, taint)1730						}1731					});1732					l_fields.push(LFieldMember {1733						name,1734						plus: f.plus,1735						visibility: f.visibility,1736						value: Rc::new(value),1737					});1738				}1739				(l_asserts_opt, l_fields)1740			})1741		});1742	// `this` was allocated as the first local of the object's frame,1743	// so its slot is 0 within that frame.1744	let this_slot = usage.this_used.then_some(LocalSlot(0));1745	LObjMembers {1746		frame_shape,1747		this: this_slot,1748		set_dollar: usage.set_dollar,1749		uses_super: usage.uses_super,1750		locals: Rc::new(l_binds),1751		asserts: l_asserts_opt,1752		fields: l_fields,1753	}1754}17551756fn analyze_obj_comp(1757	comp: &ObjComp,1758	stack: &mut AnalysisStack,1759	taint: &mut AnalysisResult,1760) -> LObjComp {1761	let res = analyze_comp_specs(&comp.compspecs, stack, taint, |stack, taint| {1762		let field_name = match &comp.field.name.value {1763			FieldName::Fixed(s) => LFieldName::Fixed(s.clone()),1764			FieldName::Dyn(e) => LFieldName::Dyn(analyze(e, stack, taint)),1765		};17661767		let (usage, frame_shape, body) = stack.in_object_scope(|stack| {1768			process_local_frame(&comp.locals, stack, taint, |stack, taint| {1769				let value = stack.in_using_closure(|stack| {1770					if let Some(params) = &comp.field.params {1771						analyze_function(None, params, &comp.field.value, stack, taint)1772					} else {1773						analyze(&comp.field.value, stack, taint)1774					}1775				});1776				LFieldMember {1777					name: field_name,1778					plus: comp.field.plus,1779					visibility: comp.field.visibility,1780					value: Rc::new(value),1781				}1782			})1783		});1784		(usage, frame_shape, body)1785	});1786	let (usage, frame_shape, (locals, field)) = res.inner;1787	let this_slot = usage.this_used.then_some(LocalSlot(0));1788	LObjComp {1789		frame_shape: Rc::new(frame_shape),1790		this: this_slot,1791		set_dollar: usage.set_dollar,1792		uses_super: usage.uses_super,1793		locals: Rc::new(locals),1794		field,1795		compspecs: res.compspecs,1796	}1797}17981799fn analyze_arr_comp(1800	inner: &Expr,1801	specs: &[CompSpec],1802	stack: &mut AnalysisStack,1803	taint: &mut AnalysisResult,1804) -> LExpr {1805	let res = analyze_comp_specs(specs, stack, taint, |stack, taint| {1806		stack.in_using_closure(|stack| analyze(inner, stack, taint))1807	});1808	let (value_shape, value) = res.inner;1809	LExpr::ArrComp(Box::new(LArrComp {1810		value_shape,1811		value: Rc::new(value),1812		compspecs: res.compspecs,1813	}))1814}18151816fn analyze_comp_specs<R>(1817	specs: &[CompSpec],1818	stack: &mut AnalysisStack,1819	taint: &mut AnalysisResult,1820	inside: impl FnOnce(&mut AnalysisStack, &mut AnalysisResult) -> R,1821) -> CompSpecResult<R> {1822	fn go<R>(1823		idx: usize,1824		specs: &[CompSpec],1825		outer_depth: u32,1826		stack: &mut AnalysisStack,1827		taint: &mut AnalysisResult,1828		inside: impl FnOnce(&mut AnalysisStack, &mut AnalysisResult) -> R,1829	) -> (R, Vec<LCompSpec>) {1830		if idx >= specs.len() {1831			return (inside(stack, taint), Vec::new());1832		}1833		match &specs[idx] {1834			CompSpec::IfSpec(IfSpecData { cond, .. }) => {1835				let cond_l = analyze(cond, stack, taint);1836				let (r, mut rest) = go(idx + 1, specs, outer_depth, stack, taint, inside);1837				rest.insert(0, LCompSpec::If(cond_l));1838				(r, rest)1839			}1840			CompSpec::ForSpec(ForSpecData { destruct, over }) => {1841				let mut over_taint = AnalysisResult::default();1842				let over_l = analyze(over, stack, &mut over_taint);1843				let loop_invariant = over_taint.local_dependent_depth > outer_depth;1844				taint.taint_by(over_taint);18451846				let mut alloc = FrameAlloc::new(stack);1847				let closure = alloc.push_locals_closure();1848				let Some(l_destruct) = alloc.alloc_destruct(destruct) else {1849					stack.pop_closure(closure);1850					return go(idx + 1, specs, outer_depth, stack, taint, inside);1851				};1852				let mut pending = alloc.finish();18531854				let var_analysis = AnalysisResult::default();1855				pending.record_spec_init(&l_destruct, var_analysis);18561857				let body_frame = pending.finish();1858				let (r, mut rest) =1859					go(idx + 1, specs, outer_depth, body_frame.stack, taint, inside);1860				body_frame.finish();1861				let frame_shape = stack.pop_closure(closure);18621863				rest.insert(1864					0,1865					LCompSpec::For {1866						frame_shape,1867						destruct: l_destruct,1868						over: over_l,1869						loop_invariant,1870					},1871				);1872				(r, rest)1873			}1874			#[cfg(feature = "exp-object-iteration")]1875			CompSpec::ForObjSpec(data) => {1876				let mut over_taint = AnalysisResult::default();1877				let over_l = analyze(&data.over, stack, &mut over_taint);1878				let loop_invariant = over_taint.local_dependent_depth > outer_depth;1879				taint.taint_by(over_taint);18801881				let mut alloc = FrameAlloc::new(stack);1882				let closure = alloc.push_locals_closure();1883				let Some((_, key_slot)) = alloc.define_local(data.key.clone(), None) else {1884					stack.pop_closure(closure);1885					return go(idx + 1, specs, outer_depth, stack, taint, inside);1886				};1887				let Some(l_value) = alloc.alloc_destruct(&data.value) else {1888					stack.pop_closure(closure);1889					return go(idx + 1, specs, outer_depth, stack, taint, inside);1890				};1891				let mut pending = alloc.finish();18921893				let var_analysis = AnalysisResult::default();1894				pending.record_spec_init(&LDestruct::Full(key_slot), var_analysis);1895				pending.record_spec_init(&l_value, var_analysis);18961897				let body_frame = pending.finish();1898				let (r, mut rest) =1899					go(idx + 1, specs, outer_depth, body_frame.stack, taint, inside);1900				body_frame.finish();1901				let frame_shape = stack.pop_closure(closure);19021903				rest.insert(1904					0,1905					LCompSpec::ForObj {1906						frame_shape,1907						key: key_slot,1908						visibility: data.visibility,1909						value: l_value,1910						over: over_l,1911						loop_invariant,1912					},1913				);1914				(r, rest)1915			}1916		}1917	}1918	let outer_depth = stack.depth;1919	let (r, compspecs) = go(0, specs, outer_depth, stack, taint, inside);1920	CompSpecResult {1921		inner: r,1922		compspecs,1923	}1924}19251926struct CompSpecResult<R> {1927	inner: R,1928	compspecs: Vec<LCompSpec>,1929}19301931pub fn analyze_root(expr: &Expr, ctx: Vec<(IStr, LocalId)>) -> AnalysisReport {1932	let mut stack = AnalysisStack::new();1933	for (name, id) in ctx {1934		stack.define_external_local(name, id);1935	}19361937	let externals_count: u16 = stack1938		.local_defs1939		.len()1940		.try_into()1941		.expect("more than u16::MAX externals");1942	let closure = stack.push_root_closure(externals_count);19431944	let mut taint = AnalysisResult::default();1945	let lir = analyze(expr, &mut stack, &mut taint);19461947	let root_shape = stack.pop_closure(closure);1948	debug_assert!(1949		stack.closure_stack.is_empty(),1950		"closure stack imbalance after analyze"1951	);19521953	AnalysisReport {1954		lir,1955		root_shape,1956		root_analysis: taint,1957		diagnostics_list: stack.diagnostics,1958		errored: stack.errored,1959	}1960}19611962#[cfg(test)]1963fn render_diagnostics(src: &str, diags: &[Diagnostic]) -> String {1964	use std::fmt::Write;19651966	use hi_doc::{Formatting, SnippetBuilder, Text};19671968	let mut out = String::new();1969	let mut unspanned = Vec::new();1970	let mut spanned: Vec<&Diagnostic> = Vec::new();1971	for d in diags {1972		if d.span.is_some() {1973			spanned.push(d);1974		} else {1975			unspanned.push(d);1976		}1977	}1978	if !spanned.is_empty() {1979		let mut builder = SnippetBuilder::new(src);1980		for d in spanned {1981			let span = d.span.as_ref().expect("spanned");1982			let ab = match d.level {1983				DiagLevel::Error => {1984					builder.error(Text::fragment(d.message.clone(), Formatting::default()))1985				}1986				DiagLevel::Warning => {1987					builder.warning(Text::fragment(d.message.clone(), Formatting::default()))1988				}1989			};1990			ab.range(span.range()).build();1991		}1992		out.push_str(&hi_doc::source_to_ansi(&builder.build()));1993	}1994	for d in unspanned {1995		let prefix = match d.level {1996			DiagLevel::Error => "error",1997			DiagLevel::Warning => "warning",1998		};1999		writeln!(out, "{prefix}: {}", d.message).expect("fmt");2000	}2001	out2002}20032004pub struct AnalysisReport {2005	pub lir: LExpr,2006	pub root_shape: ClosureShape,2007	pub root_analysis: AnalysisResult,2008	pub diagnostics_list: Vec<Diagnostic>,2009	pub errored: bool,2010}20112012#[cfg(test)]2013mod tests {2014	use std::fs;20152016	use insta::{assert_snapshot, glob};2017	use jrsonnet_ir::Source;20182019	use super::*;20202021	#[test]2022	#[cfg(not(feature = "exp-null-coaelse"))]2023	fn snapshots() {2024		glob!("analysis_tests/*.jsonnet", |path| {2025			let code = fs::read_to_string(path).expect("read test file");2026			let src = Source::new_virtual("<test>".into(), code.clone().into());2027			let expr = crate::parse_jsonnet(&code, src.clone()).expect("parse");2028			let report = analyze_root(&expr, Vec::new());20292030			let diagnostics = render_diagnostics(src.code(), &report.diagnostics_list);2031			// Strip ANSI escapes from diagnostics so snapshots are readable.2032			let diagnostics = strip_ansi_escapes::strip_str(&diagnostics);2033			let rendered = format!(2034				"--- source ---\n{}\n--- root analysis ---\nobject_dependent_depth: {}\nlocal_dependent_depth: {}\nerrored: {}\n--- diagnostics ---\n{}--- lir ---\n{:#?}\n",2035				code.trim_end(),2036				fmt_depth(report.root_analysis.object_dependent_depth),2037				fmt_depth(report.root_analysis.local_dependent_depth),2038				report.errored,2039				diagnostics,2040				report.lir,2041			);2042			assert_snapshot!(rendered);2043		});2044	}20452046	fn fmt_depth(d: u32) -> String {2047		if d == u32::MAX {2048			"none".into()2049		} else {2050			d.to_string()2051		}2052	}2053}
modifiedcrates/jrsonnet-evaluator/src/evaluate/compspec.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/compspec.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/compspec.rs
@@ -6,6 +6,9 @@
 	destructure::{destruct, evaluate_locals_unbound, fill_letrec_binds},
 	evaluate_field_member_static, evaluate_field_member_unbound,
 };
+#[cfg(feature = "exp-object-iteration")]
+use jrsonnet_interner::IStr;
+
 use crate::{
 	Context, ObjValue, ObjValueBuilder, Result, Thunk, Val,
 	analyze::{
@@ -18,6 +21,12 @@
 	evaluate::{evaluate, evaluate_trivial},
 };
 
+enum CachedOver {
+	Arr(ArrValue),
+	#[cfg(feature = "exp-object-iteration")]
+	Obj(ObjValue),
+}
+
 trait CompCollector {
 	fn reserve(&mut self, _guaranteed: usize) {}
 	fn collect(&mut self, ctx: Context) -> Result<()>;
@@ -215,7 +224,7 @@
 	Ok(Val::arr(items))
 }
 
-fn cache_overs(ctx: &Context, specs: &[LCompSpec]) -> Result<Vec<Option<ArrValue>>> {
+fn cache_overs(ctx: &Context, specs: &[LCompSpec]) -> Result<Vec<Option<CachedOver>>> {
 	specs
 		.iter()
 		.map(|spec| {
@@ -229,7 +238,23 @@
 					let Val::Arr(arr) = val else {
 						bail!(InComprehensionCanOnlyIterateOverArray)
 					};
-					Some(arr)
+					Some(CachedOver::Arr(arr))
+				}
+				#[cfg(feature = "exp-object-iteration")]
+				LCompSpec::ForObj {
+					over,
+					loop_invariant: true,
+					..
+				} => {
+					let val = evaluate(ctx.clone(), over)?;
+					let Val::Obj(obj) = val else {
+						bail!(TypeMismatch(
+							"object iteration over",
+							vec![jrsonnet_types::ValType::Obj],
+							val.value_type(),
+						))
+					};
+					Some(CachedOver::Obj(obj))
 				}
 				_ => None,
 			})
@@ -240,7 +265,7 @@
 fn evaluate_compspecs_eager(
 	ctx: Context,
 	specs: &[LCompSpec],
-	cached_overs: &[Option<ArrValue>],
+	cached_overs: &[Option<CachedOver>],
 	idx: usize,
 	guaranteed_reserve: usize,
 	collector: &mut dyn CompCollector,
@@ -269,7 +294,7 @@
 			over,
 			..
 		} => {
-			let arr = if let Some(cached) = &cached_overs[idx] {
+			let arr = if let Some(CachedOver::Arr(cached)) = &cached_overs[idx] {
 				cached.clone()
 			} else {
 				let arr_val = evaluate(ctx.clone(), over)?;
@@ -304,6 +329,10 @@
 				_ => unreachable!("eager compspecs are not possible with non-full patterns"),
 			}
 		}
+		#[cfg(feature = "exp-object-iteration")]
+		LCompSpec::ForObj { .. } => {
+			unreachable!("eager compspecs filter rejects ForObj");
+		}
 	}
 	Ok(())
 }
@@ -311,7 +340,7 @@
 fn evaluate_compspecs(
 	ctx: Context,
 	specs: &[LCompSpec],
-	cached_overs: &[Option<ArrValue>],
+	cached_overs: &[Option<CachedOver>],
 	idx: usize,
 	guaranteed_reserve: usize,
 	collector: &mut dyn CompCollector,
@@ -340,7 +369,7 @@
 			over,
 			..
 		} => {
-			let arr = if let Some(cached) = &cached_overs[idx] {
+			let arr = if let Some(CachedOver::Arr(cached)) = &cached_overs[idx] {
 				cached.clone()
 			} else {
 				let arr_val = evaluate(ctx.clone(), over)?;
@@ -365,6 +394,61 @@
 				)?;
 			}
 		}
+		#[cfg(feature = "exp-object-iteration")]
+		LCompSpec::ForObj {
+			frame_shape,
+			key,
+			visibility,
+			value,
+			over,
+			..
+		} => {
+			use jrsonnet_ir::Visibility;
+			let obj = if let Some(CachedOver::Obj(cached)) = &cached_overs[idx] {
+				cached.clone()
+			} else {
+				let val = evaluate(ctx.clone(), over)?;
+				let Val::Obj(obj) = val else {
+					bail!(TypeMismatch(
+						"object iteration over",
+						vec![ValType::Obj],
+						val.value_type(),
+					))
+				};
+				obj
+			};
+			let fields = obj.fields_with_visibility(
+				#[cfg(feature = "exp-preserve-order")]
+				false,
+			);
+			let pairs: Vec<(IStr, Visibility)> = fields
+				.into_iter()
+				.filter(|(_, v)| match visibility {
+					Visibility::Normal => v.is_visible(),
+					Visibility::Hidden => !v.is_visible(),
+					Visibility::Unhide => true,
+				})
+				.collect();
+			let inner_reserve = guaranteed_reserve.max(1) * pairs.len();
+			for (i, (field_name, _)) in pairs.into_iter().enumerate() {
+				let key_val = Val::string(field_name.clone());
+				let value_thunk = obj
+					.get_lazy(field_name.clone())
+					.expect("field exists, just enumerated");
+				let inner_ctx = ctx.pack_captures_sup_this(frame_shape).enter(|fill, ctx| {
+					fill.set(*key, Thunk::evaluated(key_val));
+					destruct(value, fill, value_thunk, &ctx);
+				});
+				evaluate_compspecs(
+					inner_ctx,
+					specs,
+					cached_overs,
+					idx + 1,
+					if i == 0 { inner_reserve } else { 0 },
+					collector,
+				)?;
+			}
+		}
 	}
 	Ok(())
 }
modifiedcrates/jrsonnet-evaluator/src/obj/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj/mod.rs
+++ b/crates/jrsonnet-evaluator/src/obj/mod.rs
@@ -909,6 +909,56 @@
 
 		out
 	}
+	pub fn fields_with_visibility(
+		&self,
+		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,
+	) -> Vec<(IStr, Visibility)> {
+		#[cfg(feature = "exp-preserve-order")]
+		if preserve_order {
+			let (mut fields, mut keys): (Vec<_>, Vec<_>) = self
+				.fields_visibility()
+				.into_iter()
+				.enumerate()
+				.map(|(idx, (k, d))| {
+					(
+						(
+							k,
+							d.exists_visible.expect("non-existing fields filtered out"),
+						),
+						(d.sort_key(), idx),
+					)
+				})
+				.unzip();
+			keys.sort_unstable_by_key(|v| v.0);
+			for i in 0..fields.len() {
+				let x = fields[i].clone();
+				let mut j = i;
+				loop {
+					let k = keys[j].1;
+					keys[j].1 = j;
+					if k == i {
+						break;
+					}
+					fields[j] = fields[k].clone();
+					j = k;
+				}
+				fields[j] = x;
+			}
+			return fields;
+		}
+		let mut fields: Vec<_> = self
+			.fields_visibility()
+			.into_iter()
+			.map(|(k, d)| {
+				(
+					k,
+					d.exists_visible.expect("non-existing fields filtered out"),
+				)
+			})
+			.collect();
+		fields.sort_unstable_by(|a, b| a.0.cmp(&b.0));
+		fields
+	}
 	pub fn fields_ex(
 		&self,
 		include_hidden: bool,
modifiedcrates/jrsonnet-peg-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-peg-parser/src/lib.rs
+++ b/crates/jrsonnet-peg-parser/src/lib.rs
@@ -441,6 +441,7 @@
 	use crate::{ParserSettings, parse};
 
 	#[test]
+	#[cfg(not(feature = "exp-null-coaelse"))]
 	fn snapshots() {
 		glob!("tests/*.jsonnet", |path| {
 			let input = fs::read_to_string(path).expect("read test file");