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

Associated constants cannot be used to initialize storage fields #6543

Open
ironcev opened this issue Sep 13, 2024 · 0 comments
Open

Associated constants cannot be used to initialize storage fields #6543

ironcev opened this issue Sep 13, 2024 · 0 comments
Labels
bug Something isn't working compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler General compiler. Should eventually become more specific as the issue is triaged

Comments

@ironcev
Copy link
Member

ironcev commented Sep 13, 2024

Associated constants cannot be used to initialize storage fields. E.g., the following code results in a compilation error:

contract;

struct S {}

impl S {
    const S_ASSOC: u8 = 0;
}

storage {
    s_assoc: u8 = S::S_ASSOC,
                     ^^^^^^^ Could not evaluate initializer to a const declaration.
}

If the associated constant was used before to initialize some other module constant, e.g., const CONST: u8 = S::S_ASSOC; the initialization will be possible.

This is very likely because we have a dedicated step that compiles module constants solely for the purpose of using them in the storage initialization. Associated constants are not part of that compilation step.

When fixing this issue, adjust the associated_const_in_decls_of_other_constants test which is linked to this issue via GitHub link in the test TODO.

@ironcev ironcev added bug Something isn't working compiler General compiler. Should eventually become more specific as the issue is triaged compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen labels Sep 13, 2024
ironcev added a commit that referenced this issue Sep 14, 2024
…ges (#6545)

## Description

This PR adds additional tests for constants. Those test cases are
important for the implementation of #6351 which will completely
restructure compilation of constants.

Tests for recursive `const` definitions should also be considered before
we start implementing `const fn` and `const trait`. In particular, we
want to better track and provide precise error messages in case of
unintended attempt to recursively define a constant in a complex
situation that includes `const fn` and `const trait`.

Related to #6534, #6537, #6538, #6539, #6540, #6543, and #6544.

## Checklist

- [x] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler General compiler. Should eventually become more specific as the issue is triaged
Projects
None yet
Development

No branches or pull requests

1 participant