Allow later contract fields to reference earlier fields (#229) - #238
Open
palacharlanarendra wants to merge 1 commit into
Open
Allow later contract fields to reference earlier fields (#229)#238palacharlanarendra wants to merge 1 commit into
palacharlanarendra wants to merge 1 commit into
Conversation
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.
Allow later contract fields to reference earlier fields #229
Contract field initializers can reference constructor parameters and constants, and static checking accepts references to earlier contract fields. However, bytecode compilation evaluates each field initializer without adding previously resolved fields to the constant environment. This causes compilation to fail with
RuntimeEvaluationRequiredwhen a later field references an earlier field.We resolve this by accumulating successfully resolved contract fields into the constant environment sequentially in declaration order during compilation.
The Defect
In
silverscript-lang/src/compiler/compile/helpers.rs, the compiler compiles contract fields incompile_contract_fieldsby iterating over them and resolving constant references using onlybase_constants:base_constantsonly contains constructor parameters and global constants. Because the resolved expressions of preceding fields are not accumulated, references to earlier fields (e.g.mirrored = amount) remain unresolved as rawExprKind::Identifierexpressions.When the compiler attempts to encode the value as a constant push (for fixed-size types like
int), it fails to encode the identifier and aborts withRuntimeEvaluationRequired.The Fix
base_constantsinto a mutable environment mapcurrent_constantsat the beginning ofcompile_contract_fields.current_constants.current_constantstoresolve_constant_references,fixed_type_size,encode_value_with_constant_size, and the expression environmentExprEnv.Tests
We added three regression tests in
silverscript-lang/tests/compiler_tests.rs:contract_field_initializer_accepts_a_reference_to_an_earlier_field: Verifies that compiling a contract with prior field references succeeds and matches the expected behavior.contract_field_initializer_rejects_forward_and_undefined_references: Verifies that undefined variables and forward-references to fields declared later are clearly rejected during static checks.contract_field_initializer_rejects_cyclic_references: Verifies that circular field reference dependencies are clearly rejected.