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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions docs/book/src/reference/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Attributes are a form of metadata that can additionally instruct Sway compiler o

Below is the list of attributes supported by the Sway compiler, ordered alphabetically:

- [ABI Name](#abi-name)
- [Allow](#allow)
- [Cfg](#cfg)
- [Deprecated](#deprecated)
Expand All @@ -15,6 +16,62 @@ Below is the list of attributes supported by the Sway compiler, ordered alphabet
- [Storage](#payable)
- [Test](#test)

## ABI Name

The `#[abi_name]` attribute allows to specify the ABI name for an item.
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.

> **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:

```sway
contract;

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

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

abi MyAbi {
fn my_struct() -> RenamedMyStruct;
fn my_enum() -> RenamedMyEnum;
}

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

This generates the following JSON ABI:

```json
{
"concreteTypes": [
{
"concreteTypeId": "215af2bca9e1aa8fec647dab22a0cd36c63ce5ed051a132d51323807e28c0d67",
"metadataTypeId": 1,
"type": "enum MyEnum"
},
{
"concreteTypeId": "d31db280ac133d726851d8003bd2f06ec2d3fc76a46f1007d13914088fbd0791",
"type": "struct MyStruct"
}
],
...
}
```

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.
This means consumers of this contract will still get the original names, keeping compatibility at the ABI level.

## Allow

The `#[allow(...)]` attribute disables compiler checks so that certain warnings will go unreported. The following warnings can be disabled:
Expand Down
1 change: 1 addition & 0 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,7 @@ pub fn compile(
program: typed_program,
abi_with_callpaths: true,
type_ids_to_full_type_str: HashMap::<String, String>::new(),
unique_names: HashSet::<String>::new(),
},
engines,
if experimental.new_encoding {
Expand Down
5 changes: 5 additions & 0 deletions sway-ast/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ pub const ERROR_TYPE_ATTRIBUTE_NAME: &str = "error_type";
pub const ERROR_ATTRIBUTE_NAME: &str = "error";
pub const ERROR_M_ARG_NAME: &str = "m";

// Abi names.
pub const ABI_NAME_ATTRIBUTE_NAME: &str = "abi_name";
pub const ABI_NAME_NAME_ARG_NAME: &str = "name";

pub const KNOWN_ATTRIBUTE_NAMES: &[&str] = &[
STORAGE_ATTRIBUTE_NAME,
DOC_COMMENT_ATTRIBUTE_NAME,
Expand All @@ -62,6 +66,7 @@ pub const KNOWN_ATTRIBUTE_NAMES: &[&str] = &[
CFG_ATTRIBUTE_NAME,
DEPRECATED_ATTRIBUTE_NAME,
FALLBACK_ATTRIBUTE_NAME,
ABI_NAME_ATTRIBUTE_NAME,
];

/// An attribute declaration. Attribute declaration
Expand Down
Loading
Loading