-
Notifications
You must be signed in to change notification settings - Fork 245
Fix: Match Solidity behavior for memory array deletion (issue #1785) #1795
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
base: main
Are you sure you want to change the base?
Fix: Match Solidity behavior for memory array deletion (issue #1785) #1795
Conversation
…dger-solang#1785) Signed-off-by: Pratyksh <pratykshgupta9999@gmail.com>
f2d4df4
to
4de1994
Compare
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.
Thank you for your contribution. The main problem is that output from sema should be the same as the source tree, i.e. you can't replace a delete
statement with something else. Many components, e.g. solang language server, depend on the AST being accurate.
} else { | ||
// For memory arrays and other types, we should only delete the element | ||
// by assigning the default value, not delete the entire array | ||
// Issue #1785 - match Solc behavior for delete array[index] | ||
ns.diagnostics.push(Diagnostic::warning( | ||
*loc, | ||
"argument to 'delete' should be storage reference".to_string(), |
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.
Here are you still generating a warning. Should be removed for array elements.
right: Box::new(default_expr), | ||
}; | ||
|
||
res.push(Statement::Expression(*loc, true, assign)); |
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.
The problem here is that the AST should be an accurate representation of the source code, not of the code it will generate - that happens in codegen.
So in sema, add:
res.push(Statement::Delete(...);
Then in codegen, for array elements, set a default value, here:
https://github.com/hyperledger-solang/solang/blob/main/src/codegen/statements/mod.rs#L217
@@ -2752,3 +2775,64 @@ fn try_catch( | |||
|
|||
Ok((stmt, finally_reachable)) | |||
} | |||
|
|||
/// Helper function to get the default value expression for a type | |||
fn get_default_value( |
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.
We already have a function for default values in codegen. Let me know if you need some help locating it
Description
This PR fixes behavior of
delete
operator when used with memory arrays to match Solidity's behavior. Previously, the delete operator was not correctly handling memory array elements, which could lead to unexpected behavior.Related Issue
Closes #1785