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

Fix handling of associated constants in ABI/traits #6768

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

tritao
Copy link
Contributor

@tritao tritao commented Dec 2, 2024

Description

Fix handling of initialized associated constants in ABIs and traits.

Fix impl trait associated constant reference wrongly resolving to the ABI value

Fixes #6343 and #6310.

Checklist

  • 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).
  • 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.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@tritao tritao added the compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen label Dec 2, 2024
@tritao tritao self-assigned this Dec 2, 2024
Copy link

codspeed-hq bot commented Dec 2, 2024

CodSpeed Performance Report

Merging #6768 will not alter performance

Comparing tritao:non-required-associated-const-values (2f4596a) with master (5423c56)

Summary

✅ 22 untouched benchmarks

@tritao tritao force-pushed the non-required-associated-const-values branch 3 times, most recently from 05f031e to 47159fe Compare December 2, 2024 11:44
@tritao tritao marked this pull request as ready for review December 2, 2024 12:13
@tritao tritao requested a review from a team as a code owner December 2, 2024 12:13
From the reporting issue:

Current implementation of the initialized associated constants in ABIs
and traits is contradictory. Namely, even if we initialize them, we
anyway always have to implement them in the contract or type `impl`s.
This eliminates the whole purpose of being able to initialize them on
the ABI/trait level.

E.g., for ABIs (and the same is for traits):

```
abi ABI {
const CONST: u32 = 111; // <<<--- Initialized here. But this value
cannot be used, because...
}

impl ABI for Contract {
// ... associated constants MUST always be implemented in the
contract/type and initialized anew.
const CONST: u32 = 222; // <<<--- This must be here and this value
wins.
}
```

What we want to have instead, is the same behavior as in Rust. If an
associated constant is already initialized in the ABI or trait
declaration, the `impl`s do not need to provide the constant. If they
do, the value from the `impl` wins. The existing checks for having the
same constant type in the ABI/trait declaration and `impl`s, of course,
remain.

Fixes FuelLabs#6343.
@tritao tritao force-pushed the non-required-associated-const-values branch from 06ea12e to 37710f8 Compare December 2, 2024 12:59
@tritao tritao changed the title Associated constant ABI fixes Fix handling of associated constants in ABI/traits Dec 2, 2024
@IGI-111 IGI-111 requested a review from a team December 3, 2024 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Associated ABI or trait constant must be declared in impls only if it is not initialized in the ABI or trait
3 participants