Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

guptapratykshh
Copy link

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

@guptapratykshh guptapratykshh force-pushed the fix/1785-memory-array-delete branch from f2d4df4 to 4de1994 Compare May 15, 2025 20:58
Copy link
Contributor

@seanyoung seanyoung left a 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(),
Copy link
Contributor

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));
Copy link
Contributor

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(
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Inconsistent with solc]: Deleting element in arrary causes inconsistent execution results.
3 participants