Repository navigation
dslx: Prefer local constants over parametric bindings - #5303
Open
goutamadwant wants to merge 2 commits into
Open
goutamadwant wants to merge 2 commits into
goutamadwant wants to merge 2 commits into
Conversation
When constexpr evaluation resolves a lexical constant, let it replace a same-named parametric value that seeded the environment. This makes const conditionals honor local shadowing under TIv2. Fixes google#4161
richmckeever
reviewed
Nov 9, 2026
| TEST(TypecheckV2Test, ConstConditionalLocalConstShadowsParametric) { | ||
| EXPECT_THAT(R"( | ||
| fn f<N: u32>() -> u32 { | ||
| const N = N % u32:4; |
Collaborator
There was a problem hiding this comment.
We may be better off disallowing the shadowing of a parametric, to tell you the truth. The good of disallowing it seems to outweigh the good of trying to support it. It would probably also be easier to verify all cases of (error in the parser?). What do you think?
Author
There was a problem hiding this comment.
Agree @richmckeever disallowing it is cleaner. I've updated the parser to reject parametric shadowing instead of trying to support it. Pls have another look. thanks!
Reject lexical bindings that shadow an in-scope parametric during parsing, including parametrics inherited by struct and proc implementations. Report the original binding location and preserve ordinary local shadowing. Remove the constexpr environment workaround, add parser and typechecking regressions, and document the restriction. Fixes google#4161
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Constexpr evaluation seeds its environment with parametric bindings. When a
local constant shadows a parametric with the same name, inserting the
resolved lexical value left the original parametric value in place. A
const ifcould therefore select the wrong branch under TIv2.Replace the seeded value with the resolved lexical binding. Add a reduced
regression test whose local constant must select the
u32branch insteadof the incompatible
u16branch.Validation:
typechecks for all four instantiations.
//xls/dslx:constexpr_evaluator_test, all 19 test targets under//xls/dslx/type_system_v2/..., and thefunction_converter_testandproc_config_ir_converter_testtargets.//xls/passes:oss_optimization_passesafter 7 of 14 tests passed.Runtime execution of the original example was not completed.
Fixes #4161