Summary
Const expressions are currently evaluated in HIR allocation order. Directly nested const expressions are allocated inner-first, but a const expression reached through a referenced function or constant may still be unevaluated when its caller is evaluated.
The lowerer currently handles this by transparently lowering the inner Hir::Const value. Successful evaluation remains semantically correct, but the inner expression can be evaluated repeatedly and as part of the caller's cost budget.
Problems
- Nested const work can be repeated rather than using the cached HIR result.
- Inner evaluation cost is charged to the outer const expression.
- Recursive calls can multiply the repeated work.
- Failures can produce duplicate or misleading diagnostics at both inner and outer source locations.
Proposed direction
- Track const nodes as pending, evaluating, or done.
- Evaluate const markers reachable through referenced symbols before evaluating their dependents.
- Detect dependency cycles through the evaluating state.
- Once ordering is guaranteed, treat an unevaluated
Hir::Const reaching lowering as an invariant violation rather than transparently unwrapping it.
This is not currently a final-program correctness issue for successfully evaluated expressions; it is primarily a compiler performance, cost-accounting, and diagnostics improvement.
Summary
Const expressions are currently evaluated in HIR allocation order. Directly nested const expressions are allocated inner-first, but a const expression reached through a referenced function or constant may still be unevaluated when its caller is evaluated.
The lowerer currently handles this by transparently lowering the inner
Hir::Constvalue. Successful evaluation remains semantically correct, but the inner expression can be evaluated repeatedly and as part of the caller's cost budget.Problems
Proposed direction
Hir::Constreaching lowering as an invariant violation rather than transparently unwrapping it.This is not currently a final-program correctness issue for successfully evaluated expressions; it is primarily a compiler performance, cost-accounting, and diagnostics improvement.