Skip to content
Merged
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 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 contract ABI level.

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

```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 their previous Sway names.
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 @@ -1791,6 +1791,7 @@ pub fn compile(
panic_occurrences: &asm.panic_occurrences,
abi_with_callpaths: true,
type_ids_to_full_type_str: HashMap::<String, String>::new(),
unique_names: HashMap::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 @@ -57,6 +57,10 @@ pub const TRACE_ATTRIBUTE_NAME: &str = "trace";
pub const TRACE_NEVER_ARG_NAME: &str = "never";
pub const TRACE_ALWAYS_ARG_NAME: &str = "always";

// 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 @@ -67,6 +71,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