-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: properly propagate constant value to assigned var in mast phase #183
Conversation
On a related topic, I am wondering if we should allow either const or non-const field for condition branches, eg: fn from_bits(bits: [Bool; LEN]) -> Field {
let mut lc1 = 0;
let mut e2 = 1; // non-constant
let zero = 0; // constant
for index in 0..LEN {
// the current type check throws error from the if then else condition check
// branches should allow either const or non const field?
lc1 = lc1 + if bits[index] {e2} else {zero};
e2 = e2 + e2;
}
return lc1;
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed I think this should be a temporary solution and we should create an issue/discussion on how to make this kind of code work :o
examples/generic_for_loop.no
Outdated
@@ -11,6 +11,15 @@ fn join(const LEN: Field, arr1: [Field; LLEN], arr2: [Field; RLEN]) -> [Field; L | |||
return arr; | |||
} | |||
|
|||
fn accumulate_mut(const LEN: Field) -> Field { | |||
// shouldn't fold mutable variables as constants |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cryptic comment that will confuse us later when we forget what this was about :o
wrote up an issue to better address this problem: #188 |
I reverted the changes, as it is non issue any more because they are better fixed in another PR. See the comment: #188 (comment) So this PR only focus on fixing: let var = CST + 1 // this PR fix this: constant value can't be propagated to var And update the relevant tests to cover them. |
This PR allows propagating constant values in assignment statement.
Sometimes we want to propagate a constant value from the right hand side to a variable in assignment, eg:
Meanwhile, we need to ensure it won't propagate to the constant value and treat the mutable variable as a constant. Otherwise, in some cases, the mutable variable will mistakenly folded as a constant. eg:
The reason it needs to fold the constant values during mast phase is because during initializing a generic function, the constants need to be folded to resolve the concrete value. eg: