Repository navigation
Description
Problem
The frontend resolves a name from the innermost local scope first, then falls back to program globals. LLVM rvalue generation uses the reverse precedence for constants: variables::gen checks env.global_consts before env.variables.
A valid local variable or parameter that shadows a global constant can therefore be type-checked as the local binding but generated as the global constant's value. This changes the program result even when both bindings have the same type and are fully initialized.
Code references:
- front/parser/src/verification.rs — Validator::lookup_binding and lexical scopes
- llvm/src/expression/rvalue/variables.rs — gen and global_consts/variables lookup order
- llvm/src/expression/rvalue/dispatch.rs — variable expression dispatch
Source-derived regression
Save as local_over_constant.wave:
const VALUE: i32 = 7;
fun local_value() -> i32 {
var VALUE: i32 = 11;
return VALUE;
}
fun parameter_value(VALUE: i32) -> i32 {
return VALUE;
}
fun main() -> i32 {
var local_result: i32 = local_value();
var parameter_result: i32 = parameter_value(13);
if (local_result != 11) {
return 1;
}
if (parameter_result != 13) {
return 2;
}
if (VALUE != 7) {
return 3;
}
return 0;
}
Expected: exit 0. local_value() reads 11, parameter_value(13) reads 13, and the unshadowed global reference remains 7.
Following the current rvalue lookup order selects the global value 7 inside both helper functions. This is a prediction from the source-level lookup mismatch, not a recorded build or execution of the Wave input. Verify the complete frontend-to-backend path and retain the actual emitted IR in the regression.
Expected behavior and scope
Make rvalue lookup honor the same lexical binding selected by semantic analysis. A visible local or parameter takes precedence; the global constant remains available when no such binding shadows it.
Preserve normal global constant folding, local variable loads, static storage, and scope restoration. Do not work around the defect by banning shadowing or changing the source language. Where constant projection helpers participate in local reads, ensure they do not reintroduce the same name-only preference.
Completion criteria
-
wavec check local_over_constant.waveaccepts the source, and the compiled program exits 0 at unoptimized and optimized settings. - Actual generated code uses the local value and parameter rather than substituting the same-named global constant.
- Cover a local and a parameter shadowing a global constant, including subsequent local assignment and nested scopes.
- Cover differing local/global types so a global value cannot replace a differently typed local expression accidentally.
- Leaving a local scope restores the appropriate outer binding; unshadowed global constants retain their values and constant behavior.
- Existing aggregate constant projection and static-variable regressions continue to pass.
- Add focused compiler tests and runnable corpus cases using the project's supported LLVM toolchain.
Difficulty: medium. Related #679 owns when a new local becomes visible during initialization; this issue owns lookup precedence after the local already exists. #449 owns aggregate constant projection. The minimal reproducer uses scalar constants and literal local initializers to keep those paths independent.
Activity
Fix typed codegen and expand portable std capabilities (#701)