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

Data locations should be tied to types, not variable declarations. #13730

Open
k06a opened this issue Nov 21, 2022 · 3 comments
Open

Data locations should be tied to types, not variable declarations. #13730

k06a opened this issue Nov 21, 2022 · 3 comments
Labels
challenging difficulty high effort A lot to implement but still doable by a single person. The task is large or difficult. high impact Changes are very prominent and affect users or the project in a major way. needs design The proposal is too vague to be implemented right away roadmap

Comments

@k06a
Copy link

k06a commented Nov 21, 2022

Abstract

Solidity could allow to have storage references in memory structures or even memory arrays.

Motivation

Sometimes it is handy to have memory structure with some storage refs. Now we can use this trick, uncomfortable but works:

type DataPtr is uint256;

function get(DataPtr ptr) internal pure returns(Data storage data) {
    assembly {
        data.slot := ptr
    }
}

Specification

It would be convenient to have storage refs in memory struct by allowing following syntax:

struct Data {
    // ...
}

struct Object {
    Data storage data; // <- allow storage keyword here
    bytes32 context;
}
@ekpyron
Copy link
Member

ekpyron commented Nov 29, 2022

Yes, we plan on associating data locations with the type in the future, not with the variable declaration (which is currently the case). The same goes for calldata references in memory. Just the nonsensical combinations like memory references in storage should remain invalid. Not entirely sure we have an issue for that yet, though.

@k06a
Copy link
Author

k06a commented Nov 29, 2022

@ekpyron glad to hear that this kind of functionality was already planned

@ekpyron ekpyron changed the title Solidity could allow storage references in memory structures Data locations should be tied to types, not variable declarations. Nov 29, 2022
@ekpyron ekpyron added high effort A lot to implement but still doable by a single person. The task is large or difficult. high impact Changes are very prominent and affect users or the project in a major way. roadmap labels Nov 29, 2022
@ekpyron ekpyron moved this to To do in Solidity Focus Board Nov 29, 2022
@ekpyron
Copy link
Member

ekpyron commented Nov 29, 2022

As for some more implementation details on this: for nested reference types, the outermost data location should become the implicit default data location for the inner types, so e.g. uint[][] memory x; would be short for uint[] memory [] memory x; (while after solving this issue we'll also have uint[] calldata [] memory x;). Similarly, for struct members the default, non-mentioned data location means inheriting the location from the struct itself. On the other hand variable declarations of variables of reference type will still have to specify the outermost data location explicitly. This way we should be able to pull this of in a non-breaking backwards compatible way.

Furthermore, in the longer term, this will be a prerequisite for a generics-ready type system, i.e. it's related to #13725 and #869

@ekpyron ekpyron added needs design The proposal is too vague to be implemented right away and removed needs design The proposal is too vague to be implemented right away labels Nov 29, 2022
@NunoFilipeSantos NunoFilipeSantos added the needs design The proposal is too vague to be implemented right away label Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
challenging difficulty high effort A lot to implement but still doable by a single person. The task is large or difficult. high impact Changes are very prominent and affect users or the project in a major way. needs design The proposal is too vague to be implemented right away roadmap
Projects
None yet
Development

No branches or pull requests

3 participants