Skip to content

Commit 829cb7c

Browse files
esdrubaltritao
andauthored
Adds documentation for Sway attributes. (FuelLabs#4203)
## Description Adds Documentation for allow, doc, inline, payable, storage, and test attributes. Closes FuelLabs#3302 Closes FuelLabs#4114 ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] 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. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: João Matos <[email protected]>
1 parent 1bba22e commit 829cb7c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

docs/book/src/reference/attributes.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Attributes
2+
3+
The Sway compiler supports a list of attributes that perform various operations that are useful for building, testing and documenting Sway programs. Below is a list of all available attributes:
4+
5+
## Allow
6+
7+
The `#[allow(dead_code)]` attribute overrides the check for dead code so that violations will go unreported.
8+
9+
## Doc
10+
11+
The `#[doc(..)]` attribute specifies documentation.
12+
13+
Line doc comments beginning with exactly three slashes `///`, are interpreted as a special syntax for doc attributes. That is, they are equivalent to writing `#[doc("...")]` around the body of the comment, i.e., `/// Foo` turns into `#[doc("Foo")]`
14+
15+
Line comments beginning with `//!` are doc comments that apply to the module of the source file they are in. That is, they are equivalent to writing `#![doc("...")]` around the body of the comment. `//!` module level doc comments should be at the top of Sway files.
16+
17+
Documentation can be generated from doc attributes using `forc doc`.
18+
19+
## Inline
20+
21+
The inline attribute suggests that a copy of the attributed function should be placed in the caller, rather than generating code to call the function where it is defined.
22+
23+
> **Note**: The Sway compiler automatically inlines functions based on internal heuristics. Incorrectly inlining functions can make the program slower, so this attribute should be used with care.
24+
25+
The `#[inline(never)]` attribute *suggests* that an inline expansion should never be performed.
26+
27+
The `#[inline(always)]` attribute *suggests* that an inline expansion should always be performed.
28+
29+
> **Note**: `#[inline(..)]` in every form is a hint, with no *requirements*
30+
on the language to place a copy of the attributed function in the caller.
31+
32+
## Payable
33+
34+
The lack of `#[payable]` implies the method is non-payable. When calling an ABI method that is non-payable, the compiler emits an error if the amount of coins forwarded with the call is not guaranteed to be zero. Note that this is strictly a compile-time check and does not incur any runtime cost.
35+
36+
## Storage
37+
38+
In Sway, functions are pure by default but can be opted into impurity via the `storage` function attribute. The `storage` attribute may take `read` and/or `write` arguments indicating which type of access the function requires.
39+
40+
The `#[storage(read)]` attribute indicates that a function requires read access to the storage.
41+
42+
The `#[storage(write)]` attribute indicates that a function requires write access to the storage.
43+
44+
More details in [Purity](../blockchain-development/purity.md).
45+
46+
## Test
47+
48+
The `#[test]` attribute marks a function to be executed as a test.
49+
50+
The `#[test(should_revert)]` attribute marks a function to be executed as a test that should revert.
51+
52+
More details in [Unit Testing](../testing/unit-testing.md).

docs/book/src/reference/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Sway Reference
22

33
- [Compiler Intrinsics](./compiler_intrinsics.md)
4+
- [Attributes](./attributes.md)
45
- [Style Guide](./style_guide.md)
56
- [Known Issues and Workarounds](./known_issues_and_workarounds.md)
67
- [Differences from Rust](./rust_differences.md)

0 commit comments

Comments
 (0)