-
Notifications
You must be signed in to change notification settings - Fork 11
fix: function param shadowing and nested inline call arg resolution #46
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3559,3 +3559,62 @@ fn empty_array_statement_expr_evaluation_compiles_to_empty_array_data() { | |
| assert_eq!(compiled.script[0], OpFalse); | ||
| assert_eq!(compiled.script[1], OpFalse); | ||
| } | ||
|
|
||
| #[test] | ||
| fn function_param_shadows_constructor_constant_with_same_name() { | ||
| // When a constructor constant and a function parameter share the same name, | ||
| // the function parameter value must be used (not the constant). | ||
| let source = r#" | ||
| contract Shadow(int fee) { | ||
| entrypoint function main(int fee) { | ||
| int local = fee + 1; | ||
| require(local == 4); | ||
| } | ||
| } | ||
| "#; | ||
|
|
||
| // Constructor fee=2, param fee=3 => local = 3+1 = 4 => pass | ||
| let compiled = compile_contract(source, &[Expr::int(2)], CompileOptions::default()).expect("compile succeeds"); | ||
| let sigscript = compiled.build_sig_script("main", vec![Expr::int(3)]).expect("sigscript builds"); | ||
| let result = run_script_with_sigscript(compiled.script.clone(), sigscript); | ||
| assert!(result.is_ok(), "function param should shadow constructor constant: {}", result.unwrap_err()); | ||
|
|
||
| // Constructor fee=2, param fee=2 => local = 2+1 = 3 != 4 => fail (proves it's not always the constant) | ||
| let sigscript_wrong = compiled.build_sig_script("main", vec![Expr::int(2)]).expect("sigscript builds"); | ||
| let result_wrong = run_script_with_sigscript(compiled.script, sigscript_wrong); | ||
| assert!(result_wrong.is_err(), "require(3==4) should fail, proving the param value matters"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nested_inline_calls_with_args_compile_and_execute() { | ||
| // Nested inline calls must propagate synthetic __arg_ bindings so that | ||
| // deeply nested calls can resolve arguments that flow through outer calls. | ||
| let source = r#" | ||
| contract NestedArgs() { | ||
| function inner(int x) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add another level? To see the fix didn't push the bug one level further |
||
| int y = x + 1; | ||
| require(y > 0); | ||
| } | ||
|
|
||
| function outer(int v) { | ||
| inner(v); | ||
| require(v >= 0); | ||
| } | ||
|
|
||
| function top(int z) { | ||
| outer(z); | ||
| require(z >= 0); | ||
| } | ||
|
|
||
| entrypoint function main(int a) { | ||
| top(a); | ||
| require(a >= 0); | ||
| } | ||
| } | ||
| "#; | ||
|
|
||
| let compiled = compile_contract(source, &[], CompileOptions::default()).expect("nested inline calls should compile"); | ||
| let sigscript = compiled.build_sig_script("main", vec![Expr::int(5)]).expect("sigscript builds"); | ||
| let result = run_script_with_sigscript(compiled.script, sigscript); | ||
| assert!(result.is_ok(), "nested inline calls should execute correctly: {}", result.unwrap_err()); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Shouldn't this even be disallowed to reduce confusion?
Scope precedence is unclear
Bonus: how should contract-level state be treated as-well
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.
This is very common in most modern programming languages.