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 2 commits 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.

Remarks

The checking for ABI names is being done in the ABI generation phase, which made it a requirement to change the interface of ABI checking to return Results everywhere and passing Handler all the way down, so emitted errors can be passed down to the driver for reporting.

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 (952ebae) with master (5c22e33)

Summary

✅ 24 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 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
@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.

@tritao tritao force-pushed the annotation-abi-name branch from ec8d86b to fbd45c2 Compare June 3, 2025 18:39
@tritao tritao force-pushed the annotation-abi-name branch from fbd45c2 to f4e44fb Compare June 11, 2025 15:34
@tritao tritao temporarily deployed to fuel-sway-bot June 11, 2025 15:34 — with GitHub Actions Inactive
@tritao tritao force-pushed the annotation-abi-name branch from f4e44fb to 6ee826b Compare June 13, 2025 12:02
@tritao tritao temporarily deployed to fuel-sway-bot June 13, 2025 12:02 — with GitHub Actions Inactive
@tritao tritao force-pushed the annotation-abi-name branch from 6ee826b to 7f06ed7 Compare June 13, 2025 12:26
@tritao tritao temporarily deployed to fuel-sway-bot June 13, 2025 12:26 — with GitHub Actions Inactive
@tritao tritao force-pushed the annotation-abi-name branch from 7f06ed7 to ec159db Compare June 13, 2025 13:40
@tritao tritao temporarily deployed to fuel-sway-bot June 13, 2025 13:40 — with GitHub Actions Inactive
@tritao
Copy link
Contributor Author

tritao commented Jun 13, 2025

The PR has been updated with a significantly more robust approach for name checking, it now can find naming conflicts for more cases, namely existing types previously defined in the ABI interface surface, both in the main and imported modules. The diagnostics have also been improved, with support for expressive diagnostics and more detailed error messages.

@tritao tritao requested a review from ironcev June 13, 2025 14:00
Add `#[abi_name(name = "foo")]` attribute to rename enum and struct ABI
items.

Here is example of how it can be used:

```
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 ec159db to 34b3572 Compare June 24, 2025 15:58
@tritao tritao temporarily deployed to fuel-sway-bot June 24, 2025 15:58 — with GitHub Actions Inactive
@tritao tritao force-pushed the annotation-abi-name branch from 34b3572 to 952ebae Compare June 25, 2025 09:19
@tritao tritao temporarily deployed to fuel-sway-bot June 25, 2025 09:19 — with GitHub Actions Inactive
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.

One small mistake in the docs definitely worth fixing, otherwise only nitpicks 😄

Regarding the comments about diagnostics guidelines, we should add those guidelines to the repo (currently they are only in the RFC) and polish existing diagnostics that violate them, but that's a low prio so far.


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

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:
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
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:
In the example that follows, we originally had `MyStruct` and `MyEnum` types, which we, later on, renamed to `RenamedMyStruct` and `RenamedMyEnum` 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:

impl AbiNameDiagnosticSpan {
pub fn span(self) -> Span {
match self {
Self::Attribute(span) => span,
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick!

Suggested change
Self::Attribute(span) => span,
Self::Attribute(span) | Self::Type(span) => span,

@@ -34,7 +54,79 @@ impl AbiContext<'_> {
}
}

pub fn extract_abi_name_inner(span: &Span) -> Option<Span> {
let text = &span.src().text;
let full_attr: &str = &text[span.start()..span.end()];
Copy link
Member

Choose a reason for hiding this comment

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

Aren't these two lines the same as span.as_str()?

Suggested change
let full_attr: &str = &text[span.start()..span.end()];
let full_attr: = span.as_str();

);

if should_check_name {
let mut has_abi_name_attribute = false;
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick. Having a mut boolean here feels a bit over-complicated. One trivial possibility is to "calculate" has_abi_name_attribute after the let (name, attribute_span) or assign it within that same let: let (has_abi_name, name, attr_span).

}
None => (String::new(), Span::dummy()),
};

Copy link
Member

Choose a reason for hiding this comment

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

E.g., here: let has_abi_name_attribute = !(name.is_empty() || attribute_span.is_dummy());

@@ -3112,6 +3122,32 @@ impl ToDiagnostic for CompileError {
help
},
},
ABIDuplicateName { span, other_span: other, is_attribute } => Diagnostic {
reason: Some(Reason::new(code(1), "Duplicated name found for renamed ABI type.".into())),
Copy link
Member

Choose a reason for hiding this comment

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

According to the guidelines, Reasons shouldn't end up in punctuation (we have messages that violates that, I know...)

Suggested change
reason: Some(Reason::new(code(1), "Duplicated name found for renamed ABI type.".into())),
reason: Some(Reason::new(code(1), "Duplicated name found for renamed ABI type".into())),

Hint::help(
source_engine,
other.clone(),
format!("This is the existing {} with conflicting name", if *is_attribute { "attribute" } else { "type" }),
Copy link
Member

Choose a reason for hiding this comment

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

According to the guidelines, hints shoul end up in punctuation (we have messages that violates that, I know...).

Suggested change
format!("This is the existing {} with conflicting name", if *is_attribute { "attribute" } else { "type" }),
format!("This is the existing {} with conflicting name.", if *is_attribute { "attribute" } else { "type" }),

help: vec![],
},
ABIInvalidName { span, name } => Diagnostic {
reason: Some(Reason::new(code(1), "Invalid name found for renamed ABI type.".into())),
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
reason: Some(Reason::new(code(1), "Invalid name found for renamed ABI type.".into())),
reason: Some(Reason::new(code(1), "Invalid name found for renamed ABI type".into())),

String::new()
),
hints: vec![],
help: vec![format!("The name must be a valid Sway identifier{}", if name.is_empty() { " and cannot be empty" } else { "" })],
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
help: vec![format!("The name must be a valid Sway identifier{}", if name.is_empty() { " and cannot be empty" } else { "" })],
help: vec![format!("The name must be a valid Sway identifier{}.", if name.is_empty() { " and cannot be empty" } else { "" })],

@@ -105,6 +105,29 @@ pub fn lex(
lex_commented(handler, src, start, end, &source_id).map(|stream| stream.strip_comments())
}

pub fn is_valid_identifier_or_path(s: &str) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

If I interpret the last line of the implementation correctly, these strings will be consider valid:

  • _:
  • :::
  • a:b:c:::
  • a::1234::b
  • etc.

And these will not, because : is not a XID_Start, although a valid Sway import path can start with it:

  • ::some_module::in_the_same::package

I assume such path, relative to the current package, cannot be used in the #[abi_name] attribute, but this is a general function in the token.rs so it should IMO accept all supported paths. Additional check can be done in the ABI name handling.

Proposal is to add Rust unit tests for this function.

Copy link
Member

Choose a reason for hiding this comment

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

One more remark here, it looks like the current implementation will also accept these as valid identifiers:

  • __ (two underscores)
  • __this_is_not_a_valid_identifier

In the regular, non compiler-generated, Sway, identifiers starting with two underscores are not allowed.

name = "attributes_invalid_abi_names"

[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.

A reduced lib sway-lib-std-core can be used here.

@@ -0,0 +1,70 @@
contract;
Copy link
Member

Choose a reason for hiding this comment

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

This is a contract, so it feels very strange that the entry point is not main.sw but lib.sw. Proposal is to rename this file to main.sw.

@@ -0,0 +1,9 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "lib.sw"
Copy link
Member

Choose a reason for hiding this comment

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

Here as well.

Suggested change
entry = "lib.sw"
entry = "main.sw"

library;

#[abi_name(name = true)]
pub fn not_ok() {}
Copy link
Member

Choose a reason for hiding this comment

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

This must be either a struct or an enum, otherwise the test is not testing that the name argument value is of the wrong type, because the argument itself is misplaced.

Suggested change
pub fn not_ok() {}
pub struct NotOk {}

--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_args_values_types/src/abi_name_attr.sw:3:3
|
...
3 | #[abi_name(name = true)]
Copy link
Member

Choose a reason for hiding this comment

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

As explained above, the test should show error that true is not a valid value, but it is showing that the function cannot be annotated with #[abi_name].

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