Skip to content
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

Incorrect error message when declaring storage parameters public functions #15567

Open
haoyang9804 opened this issue Nov 4, 2024 · 2 comments
Labels

Comments

@haoyang9804
Copy link
Contributor

Description

The following code throws an error with message Data location must be "memory" or "calldata" for parameter in external function, but "storage" was given.

function f(S storage s) external view {

}

The after changing external into public, I got Data location must be "memory" or "calldata" for parameter in function, but "storage" was given.. It's a simple typo I think. We need to specify that no storage in the parameter list of a public function.

Environment

  • Compiler version: 0.8.28
@Shivu7889
Copy link

You're right; Solidity does not allow parameters to be passed as storage in external or public functions. This is because storage variables are meant to refer to storage in the contract itself, so they can't be used directly as function parameters. Instead, parameters should be passed as memory or calldata in functions that are external or public.

If you want to work with a storage variable within the function, you should either pass a memory or calldata copy of it or declare the storage variable outside the function. Here’s an example of how you can modify your code:

solidity
Copy code
struct S {
uint256 value;
}

S s; // Declared in storage, so we can refer to it

function f() external view returns (uint256) {
// Directly use the storage variable s here without passing it as a parameter
return s.value;
}
Alternatively, if you need to pass a struct as a parameter, you can use memory as the data location:

solidity
Copy code
function f(S memory s) public pure returns (uint256) {
return s.value;
}
This way, the struct s is passed by value (copying the data to memory), and the function can work with it.

@haoyang9804
Copy link
Contributor Author

@Shivu7889 Thanks for your comments. But I actually mean that if we pass external parameters in a public function, the error message is misleading. The compilers said, Data location must be "memory" or "calldata" for the parameter in the function, but "storage" was given.. It should specify that this regulation happens under public and external functions. I think it's an error handling typo

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

No branches or pull requests

2 participants