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