8000 Add `#[abi_name(name = "foo")]` attribute to rename ABI items. by tritao · Pull Request #7057 · FuelLabs/sway · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add #[abi_name(name = "foo")] attribute to rename ABI items. #7057

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tritao
Copy link
Contributor
@tritao tritao commented Apr 2, 2025

Description

Add #[abi_name(name = "foo")] attribute to rename enum and struct ABI items.

Here is example of how it can be used:

contract;

#[abi_name(name = "RenamedMyStruct")]
struct MyStruct {}

#[abi_name(name = "RenamedMyEnum")]
enum MyEnum {
  A: ()
}

abi MyAbi {
    fn my_struct() -> MyStruct;
    fn my_enum() -> MyEnum;
}

impl MyAbi for Contract {
  fn my_struct() -> MyStruct { MyStruct{} }
  fn my_enum() -> MyEnum { MyEnum::A }
}

Closes #5955.

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 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 Apr 2, 2025
@tritao tritao self-assigned this Apr 2, 2025
@tritao tritao temporarily deployed to fuel-sway-bot April 2, 2025 19:08 — with GitHub Actions Inactive
@tritao tritao changed the title Add #[abi_name() attribute to rename ABI items. Add #[abi_name(name = "foo") attribute to rename ABI items. Apr 2, 2025
Copy link
codspeed-hq bot commented Apr 2, 2025

CodSpeed Performance Report

Merging #7057 will not alter performance

Comparing tritao:annotation-abi-name (ec8d86b) with master (804d6df)

Summary

✅ 22 untouched benchmarks

@tritao tritao force-pushed the annotation-abi-name branch from 04f5114 to 0bddb9d Compare April 2, 2025 19:51
@tritao tritao added the enhancement New feature or request label Apr 2, 2025
@tritao tritao temporarily deployed to fuel-sway-bot April 2, 2025 19:52 — with GitHub Actions Inactive
@tritao tritao marked this pull request as ready for review April 3, 2025 00:05
@tritao tritao requested a review from a team as a code owner April 3, 2025 00:05
IGI-111
IGI-111 previously approved these changes Apr 3, 2025
@tritao tritao enabled auto-merge (squash) April 3, 2025 10:47
@IGI-111 IGI-111 requested a review from a team April 3, 2025 11:26
@ironcev ironcev changed the title Add #[abi_name(name = "foo") attribute to rename ABI items. Add #[abi_name(name = "foo")] attribute to rename ABI items. Apr 3, 2025
Copy link
Member
@ironcev ironcev left a comment

Choose a reason for hiding this comment

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

The attribute should be added to the Attributes chapter in the documentation.

I've changed that documentation page fully in #7058 because it was seriously outdated. So, whoever gets the PR merged later will have some slight merge conflicts to solve on that page 😄

@tritao tritao marked this pull request as draft April 3, 2025 16:16
auto-merge was automatically disabled April 3, 2025 16:16

Pull request was converted to draft

@tritao tritao force-pushed the annotation-abi-name branch from 0bddb9d to e45048c Compare April 4, 2025 18:06
@tritao tritao temporarily deployed to fuel-sway-bot April 4, 2025 18:07 — with GitHub Actions Inactive
@tritao tritao force-pushed the annotation-abi-name branch from e45048c to e364c5c Compare April 7, 2025 11:32
@tritao tritao temporarily deployed to fuel-sway-bot April 7, 2025 11:32 — with GitHub Actions Inactive
@tritao tritao force-pushed the annotation-abi-name branch from e364c5c to b993940 Compare April 7, 2025 11:49
@tritao tritao temporarily deployed to fuel-sway-bot April 7, 2025 11:50 — with GitHub Actions Inactive
@tritao tritao force-pushed the annotation-abi-name branch from b993940 to a9d5c2a Compare April 15, 2025 22:15
@tritao tritao temporarily deployed to fuel-sway-bot April 15, 2025 22:16 — with GitHub Actions Inactive
@tritao tritao force-pushed the annotation-abi-name branch from a9d5c2a to 9046048 Compare April 15, 2025 22:26
@tritao tritao temporarily deployed to fuel-sway-bot April 15, 2025 22:27 — with GitHub Actions Inactive
@tritao tritao force-pushed the annotation-abi-name branch from 9046048 to 236cb14 Compare April 16, 2025 15:28
@tritao tritao temporarily deployed to fuel-sway-bot April 16, 2025 15:29 — with GitHub Actions Inactive
@tritao tritao force-pushed the annotation-abi-name branch from 236cb14 to be2a5ec Compare April 16, 2025 15:41
@tritao tritao temporarily deployed to fuel-sway-bot April 16, 2025 15:42 — with GitHub Actions Inactive
@tritao tritao force-pushed the annotation-abi-name branch from be2a5ec to a5e0532 Compare April 16, 2025 15:46
@tritao tritao temporarily deployed to fuel-sway-bot April 16, 2025 15:48 — with GitHub Actions Inactive
@tritao tritao marked this pull request as ready for review April 16, 2025 16:05
@tritao tritao requested review from a team as code owners April 16, 2025 16:05
xunilrj
xunilrj previously approved these changes Apr 22, 2025
@tritao tritao requested a review from a team as a code owner April 30, 2025 08:13
@tritao tritao force-pushed the annotation-abi-name branch from 6e51087 to 295a938 Compare May 5, 2025 13:26
@tritao tritao temporarily deployed to fuel-sway-bot May 5, 2025 13:27 — with GitHub Actions Inactive
Add `#[abi_name(name = "foo")` attribute to rename enum and struct ABI
items.

Here is example of how it can be used:

```sway
contract;

struct MyStruct {}

enum MyEnum {
  A: ()
}

abi MyAbi {
    fn my_struct() -> MyStruct;
    fn my_enum() -> MyEnum;
}

impl MyAbi for Contract {
  fn my_struct() -> MyStruct { MyStruct{} }
  fn my_enum() -> MyEnum { MyEnum::A }
}
```

Closes FuelLabs#5955.
@tritao tritao force-pushed the annotation-abi-name branch from 295a938 to ec8d86b Compare May 5, 2025 14:17
@tritao tritao temporarily deployed to fuel-sway-bot May 5, 2025 14:18 — with GitHub Actions Inactive
@tritao
Copy link
Contributor Author
tritao commented May 5, 2025

Rebased, feedback address, ready for re-review.

Copy link
Member
@ironcev ironcev left a comment

Choose a reason for hiding this comment

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

The changes added since the previous review are quite substantial so it will take some time to re-review fully 😄

Here some quick observations and suggestions.

And one general question. I assume the checking was moved to the ABI generation phase, which results in returning Results everywhere and passing Handler all the way down, because there is no option to detect at the type checking phase which types will actually end up in the ABI?

If so, maybe this assumption can be added to the issue description and shortly elaborated, it will help in understanding the reason for the widespread change.

Comment on lines +22 to +24
This means that when a contract ABI JSON file is generated, the name that is output is the one specified
by the attribute. This can be useful to allow renaming items, while allowing for keeping backwards
compatibility at the contract ABI level.
Copy link
Member

Choose a reason for hiding this comment

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

I would remove "contract" here because scripts and predicates also have ABI JSON generated, and the attribute is effective there as well.

Suggested change
This means that when a contract ABI JSON file is generated, the name that is output is the one specified
by the attribute. This can be useful to allow renaming items, while allowing for keeping backwards
compatibility at the contract ABI level.
This means that when an ABI JSON file is generated, the name that is output is the one specified
by the attribute. This can be useful to allow renaming items, while allowing for keeping backwards
compatibility at the ABI level.


> **Note**: At the moment, only enum and struct types support the attribute.

In the example that follows, we originally had a `MyStruct` and `MyEnum` types, which we renamed in Sway:
Copy link
Member

Choose a reason for hiding this comment

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

I was very confused with this part until I read the conclusion that came after the example. Perhaps formulation along these lines, to make it fully clear:

Suggested change
In the example that follows, we originally had a `MyStruct` and `MyEnum` types, which we renamed in Sway:
In the example that follows, we originally had `MyStruct` and `MyEnum` types, which we, later on, renamed to "MyStruct" and "MyEnum" in code. To keep the backward compatibility of the ABI, we annotate the types with the `#[abi_name]` attribute and give them the original names:

```

We get the same JSON ABI output both before and after renaming the types, due to attributing them with
`#[abi_name(name = ...)]`, which forces them to be generated with the previous Sway name.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
`#[abi_name(name = ...)]`, which forces them to be generated with the previous Sway name.
`#[abi_name(name = ...)]`, which forces them to be generated with their previous Sway names.

match *engines.te().get(type_id) {
TypeInfo::Enum(decl_id) => {
let enum_decl = engines.de().get_enum(&decl_id);
if let Some(abi_name_attr) = enum_decl.attributes.abi_name() {
Copy link
Member

Choose a reason for hiding this comment

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

Why not match on Option here instead of if else?

}
TypeInfo::Struct(decl_id) => {
let struct_decl = engines.de().get_struct(&decl_id);
if let Some(abi_name_attr) = struct_decl.attributes.abi_name() {
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

name = "attributes_abi_name"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Copy link
Member

Choose a reason for hiding this comment

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

We don't need the full-blown std here. We can use the reduced sway-lib-std-core.

4 | struct MyStruct {}
5 |
6 | #[abi_name(name = "SameName")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Duplicated name found for renamed ABI type.
Copy link
Member

Choose a reason for hiding this comment

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

We can heavily benefit here from expressive diagnostics, by pointing to the source of the other name. Because this name can be a name of the type which is not in the same file, or even of an another one #[abi_name] that is also not in the same file.

The proposal is:

  • to use expressive diagnostics to point to the other name.
  • to extend the test and cover the cases where the duplicated name is a name of another type, or another #[abi_name] in the current or some other file.

Another suggestion is to underline only the name itself, "SameName" in this example, because that's where the issue actually is. The rest of the attribute usage is perfectly fine and underlining it gives a misleading impression that the usage of the attribute is wrong.

|
10 | struct MyStruct2 {}
11 |
12 | #[abi_name(name = "")]
Copy link
Member

Choose a reason for hiding this comment

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

Here also, underlining only the "" and not the whole attribute better points to the actual problem.

Personally, I would have expressing diagnostics here as well, explaining that the name must be a valid Sway identifier, and in this case with addition 'and cannot be empty'.

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 compiler General compiler. Should eventually become more specific as the issue is triaged enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create #[abi_name(name)] annotation
4 participants
0