From c91fd211cfca997ac5908e013660d2ab21e0e2e4 Mon Sep 17 00:00:00 2001 From: waw666waw666 <184799977+waw666waw666@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:33:13 +0000 Subject: [PATCH] Fix optimizer reading a non-constant map literal property as () The template of a map literal holds every key, with () standing in for values that are not constant. When optimizing #{...}.prop or #{...}["prop"], the optimizer checked the template first, so a property whose value is pure but only becomes constant later (e.g. a block, an if/switch on a constant, or ??) was replaced by (). Look among the non-constant values first and only fall back to the template. --- src/optimizer.rs | 40 ++++++++++++++++++---------------------- tests/maps.rs | 6 ++++++ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/optimizer.rs b/src/optimizer.rs index 6ff8a07b5..77a97a825 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -972,17 +972,15 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) { Expr::Dot(x, ..) if !_chaining => match (&mut x.lhs, &mut x.rhs) { // map.string (Expr::Map(m, pos), Expr::Property(p, ..)) if m.0.iter().all(|(.., x)| x.is_pure()) => { - if m.1.contains_key(p.2.as_str()) { - // Constant map literal property - state.set_dirty(); - *expr = Expr::from_dynamic(mem::take(m.1.get_mut(p.2.as_str()).unwrap()).into(), *pos); - } else { - // Map literal where everything is pure - promote the indexed item. - // All other items can be thrown away. - state.set_dirty(); - *expr = mem::take(&mut m.0).into_iter().find(|(x, ..)| x.name == p.2) - .map_or_else(|| Expr::Unit(*pos), |(.., mut expr)| { expr.set_position(*pos); expr }); - } + // Map literal where everything is pure - promote the indexed item. + // All other items can be thrown away. + // The template holds every key, with `()` standing in for a value that is + // not a constant, so look among the non-constant values first. + state.set_dirty(); + *expr = match mem::take(&mut m.0).into_iter().find(|(x, ..)| x.name == p.2) { + Some((.., mut expr)) => { expr.set_position(*pos); expr } + None => m.1.get_mut(p.2.as_str()).map_or_else(|| Expr::Unit(*pos), |v| Expr::from_dynamic(mem::take(v), *pos)), + }; } // var.property where var is a constant map (Expr::Variable(v, _, pos) , Expr::Property(p, ..)) if state.propagate_constants && state.find_literal_constant(&v.1).map_or(false,Dynamic::is_map) => { @@ -1045,17 +1043,15 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) { } // map[string] (Expr::Map(m, pos), Expr::StringConstant(s, ..)) if m.0.iter().all(|(.., x)| x.is_pure()) => { - if m.1.contains_key(s.as_str()) { - // Constant map literal property - state.set_dirty(); - *expr = Expr::from_dynamic(mem::take(m.1.get_mut(s.as_str()).unwrap()).into(), *pos); - } else { - // Map literal where everything is pure - promote the indexed item. - // All other items can be thrown away. - state.set_dirty(); - *expr = mem::take(&mut m.0).into_iter().find(|(x, ..)| x.name == s) - .map_or_else(|| Expr::Unit(*pos), |(.., mut expr)| { expr.set_position(*pos); expr }); - } + // Map literal where everything is pure - promote the indexed item. + // All other items can be thrown away. + // The template holds every key, with `()` standing in for a value that is + // not a constant, so look among the non-constant values first. + state.set_dirty(); + *expr = match mem::take(&mut m.0).into_iter().find(|(x, ..)| x.name == s) { + Some((.., mut expr)) => { expr.set_position(*pos); expr } + None => m.1.get_mut(s.as_str()).map_or_else(|| Expr::Unit(*pos), |v| Expr::from_dynamic(mem::take(v), *pos)), + }; } // map[string] - constant #[cfg(not(feature = "no_object"))] diff --git a/tests/maps.rs b/tests/maps.rs index d04a34f92..033adc801 100644 --- a/tests/maps.rs +++ b/tests/maps.rs @@ -101,10 +101,16 @@ fn test_map_template_evaluation_and_isolation() { assert_eq!(engine.eval::("#{ a: 42, b: () }.a").unwrap(), 42); assert_eq!(engine.eval_with_scope::(&mut scope, "#{ a: 42, b: input }.a").unwrap(), 42); + // Values that only become constant after optimization are not in the template yet + assert_eq!(engine.eval::("#{ a: 42, b: { 1 } }.b").unwrap(), 1); + assert_eq!(engine.eval::("#{ a: if true { 1 } else { 2 } }.a").unwrap(), 1); + assert_eq!(engine.eval::("#{ a: 42, b: (() ?? 1) }.b").unwrap(), 1); + #[cfg(not(feature = "no_index"))] { assert_eq!(engine.eval::(r#"#{ a: 42, b: () }["a"]"#).unwrap(), 42); assert_eq!(engine.eval_with_scope::(&mut scope, r#"#{ a: 42, b: input }["a"]"#).unwrap(), 42); + assert_eq!(engine.eval::(r#"#{ a: 42, b: { 1 } }["b"]"#).unwrap(), 1); let ast = engine.compile("#{ values: [#{ x: 1 }], computed: input }").unwrap();