From f00e1da135773b7c04adb08a00a7567061e48f61 Mon Sep 17 00:00:00 2001 From: alosslessdev Date: Thu, 5 Nov 2026 07:20:13 -0500 Subject: [PATCH] dax: document the variable index and look through VAR/RETURN in operand checks (#2228) --- crates/dax/src/binder.rs | 7 ++++++- crates/dax/src/types.rs | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/dax/src/binder.rs b/crates/dax/src/binder.rs index 9e475178f..adf35ae4b 100644 --- a/crates/dax/src/binder.rs +++ b/crates/dax/src/binder.rs @@ -63,7 +63,12 @@ pub enum BoundExpr { TableConstructor(Vec), /// `( value, ... )`. Tuple(Vec), - /// A variable reference, indexed in the enclosing [`BoundExpr::VarReturn`]. + /// A variable reference. + /// + /// `index` is the variable's absolute slot in the cumulative stack of every enclosing + /// [`BoundExpr::VarReturn`] block — outer blocks first, each block appending its own + /// declarations in order — not its position inside the innermost `vars` list. The evaluator + /// pushes each block's values onto the same stack, so the index stays valid across nesting. Variable { index: usize, name: String, diff --git a/crates/dax/src/types.rs b/crates/dax/src/types.rs index 128334049..d7c906159 100644 --- a/crates/dax/src/types.rs +++ b/crates/dax/src/types.rs @@ -321,7 +321,14 @@ pub const fn literal_type(literal: &Literal) -> DataType { /// evaluation time (`LASTDATE('Date'[Date]) - 1`), so it is unknown here rather than a table. fn scalar_operand(expr: &Expr) -> Result { let inferred = infer_type(expr)?; - Ok(if inferred == InferredType::Table && matches!(&expr.kind, ExprKind::Call(_)) { InferredType::Unknown } else { inferred }) + // A `VAR`/`RETURN` block is transparent for this check: its body is what the operator sees, + // so `(VAR d = 1 RETURN LASTDATE('Date'[Date])) - 1` is the same table-valued operand as the + // unwrapped call and must be rejected identically. + let mut inner = expr; + while let ExprKind::VarReturn { body, .. } = &inner.kind { + inner = body; + } + Ok(if inferred == InferredType::Table && matches!(&inner.kind, ExprKind::Call(_)) { InferredType::Unknown } else { inferred }) } /// Infers the static type of an expression.