Skip to content

Conversation

@guptapratykshh
Copy link
Contributor

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.

// 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No warnings are generated for memory array delete operations

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handled array elements in codegen

}

/// 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the redundant function

@guptapratykshh
Copy link
Contributor Author

Please look into this @seanyoung

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