Skip to content

Commit 591b97a

Browse files
committed
Address review feedback.
1 parent 99c3829 commit 591b97a

File tree

8 files changed

+31
-46
lines changed

8 files changed

+31
-46
lines changed

docs/book/src/reference/attributes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ compatibility at the contract ABI level.
2525

2626
> **Note**: At the moment, only enum and struct types support the attribute.
2727
28-
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:
28+
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:
2929

3030
```sway
3131
contract;

sway-core/src/abi_generation/fuel_abi.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ pub enum AbiNameDiagnosticSpan {
2929
impl AbiNameDiagnosticSpan {
3030
pub fn span(self) -> Span {
3131
match self {
32-
Self::Attribute(span) => span,
33-
Self::Type(span) => span,
32+
Self::Attribute(span) | Self::Type(span) => span,
3433
}
3534
}
3635
}
@@ -56,7 +55,7 @@ impl AbiContext<'_> {
5655

5756
pub fn extract_abi_name_inner(span: &Span) -> Option<Span> {
5857
let text = &span.src().text;
59-
let full_attr: &str = &text[span.start()..span.end()];
58+
let full_attr = span.as_str();
6059

6160
// Find the "name" key.
6261
let name_key_pos = full_attr.find("name")?;
@@ -155,14 +154,10 @@ impl TypeId {
155154
);
156155

157156
if should_check_name {
158-
let mut has_abi_name_attribute = false;
159-
let (name, attribute_span) =
157+
let (has_abi_name_attribute, name, attribute_span) =
160158
match Self::get_abi_name_and_span_from_type_id(engines, resolved_type_id)? {
161-
Some(res) => {
162-
has_abi_name_attribute = true;
163-
res
164-
}
165-
None => (String::new(), Span::dummy()),
159+
Some(res) => (true, res.0, res.1),
160+
None => (false, String::new(), Span::dummy()),
166161
};
167162

168163
let attribute_name_span =

sway-error/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3123,7 +3123,7 @@ impl ToDiagnostic for CompileError {
31233123
},
31243124
},
31253125
ABIDuplicateName { span, other_span: other, is_attribute } => Diagnostic {
3126-
reason: Some(Reason::new(code(1), "Duplicated name found for renamed ABI type.".into())),
3126+
reason: Some(Reason::new(code(1), "Duplicated name found for renamed ABI type".into())),
31273127
issue: Issue::error(
31283128
source_engine,
31293129
span.clone(),
@@ -3133,7 +3133,7 @@ impl ToDiagnostic for CompileError {
31333133
Hint::help(
31343134
source_engine,
31353135
other.clone(),
3136-
format!("This is the existing {} with conflicting name", if *is_attribute { "attribute" } else { "type" }),
3136+
format!("This is the existing {} with conflicting name.", if *is_attribute { "attribute" } else { "type" }),
31373137
)
31383138
],
31393139
help: vec![],
@@ -3146,7 +3146,7 @@ impl ToDiagnostic for CompileError {
31463146
String::new()
31473147
),
31483148
hints: vec![],
3149-
help: vec![format!("The name must be a valid Sway identifier{}", if name.is_empty() { " and cannot be empty" } else { "" })],
3149+
help: vec![format!("The name must be a valid Sway identifier{}.", if name.is_empty() { " and cannot be empty" } else { "" })],
31503150
},
31513151
_ => Diagnostic {
31523152
// TODO: Temporarily we use `self` here to achieve backward compatibility.

test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/Forc.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
authors = ["Fuel Labs <contact@fuel.sh>"]
3-
entry = "lib.sw"
3+
entry = "main.sw"
44
license = "Apache-2.0"
55
name = "attributes_invalid_abi_names"
66

File renamed without changes.

test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/stdout.snap

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ output:
77
Building test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names
88
Compiling library std (sway-lib-std)
99
Compiling contract attributes_invalid_abi_names (test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names)
10-
error: Duplicated name found for renamed ABI type.
11-
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/lib.sw:37:19
10+
error: Duplicated name found for renamed ABI type
11+
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/main.sw:37:19
1212
|
1313
...
1414
37 | #[abi_name(name = "OtherEnum")]
@@ -18,40 +18,40 @@ error: Duplicated name found for renamed ABI type.
1818
|
1919
...
2020
7 | pub enum OtherEnum {
21-
| --------- help: This is the existing type with conflicting name
21+
| --------- help: This is the existing type with conflicting name.
2222
|
2323
____
2424

25-
error: Duplicated name found for renamed ABI type.
26-
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/lib.sw:10:19
25+
error: Duplicated name found for renamed ABI type
26+
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/main.sw:10:19
2727
|
2828
...
2929
8 | struct MyStruct {}
30-
| -------- help: This is the existing type with conflicting name
30+
| -------- help: This is the existing type with conflicting name.
3131
9 |
3232
10 | #[abi_name(name = "MyStruct")]
3333
| ^^^^^^^^^^
3434
|
3535
____
3636

37-
error: Duplicated name found for renamed ABI type.
38-
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/lib.sw:13:19
37+
error: Duplicated name found for renamed ABI type
38+
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/main.sw:13:19
3939
|
4040
...
4141
7 | #[abi_name(name = "SameName")]
42-
| ------------------------------ help: This is the existing attribute with conflicting name
42+
| ------------------------------ help: This is the existing attribute with conflicting name.
4343
...
4444
13 | #[abi_name(name = "SameName")]
4545
| ^^^^^^^^^^
4646
|
4747
____
4848

49-
error: Duplicated name found for renamed ABI type.
50-
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/lib.sw:16:19
49+
error: Duplicated name found for renamed ABI type
50+
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/main.sw:16:19
5151
|
5252
...
5353
7 | #[abi_name(name = "SameName")]
54-
| ------------------------------ help: This is the existing attribute with conflicting name
54+
| ------------------------------ help: This is the existing attribute with conflicting name.
5555
8 | struct MyStruct {}
5656
9 |
5757
10 | #[abi_name(name = "MyStruct")]
@@ -64,27 +64,27 @@ error: Duplicated name found for renamed ABI type.
6464
____
6565

6666
error: Invalid name found for renamed ABI type.
67-
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/lib.sw:19:19
67+
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/main.sw:19:19
6868
|
6969
...
7070
19 | #[abi_name(name = "")]
7171
| ^^
7272
|
73-
= help: The name must be a valid Sway identifier and cannot be empty
73+
= help: The name must be a valid Sway identifier and cannot be empty.
7474
____
7575

7676
error: Invalid name found for renamed ABI type.
77-
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/lib.sw:22:19
77+
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/main.sw:22:19
7878
|
7979
...
8080
22 | #[abi_name(name = "this !s n0t an identif1er")]
8181
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
8282
|
83-
= help: The name must be a valid Sway identifier
83+
= help: The name must be a valid Sway identifier.
8484
____
8585

86-
error: Duplicated name found for renamed ABI type.
87-
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/lib.sw:28:19
86+
error: Duplicated name found for renamed ABI type
87+
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/main.sw:28:19
8888
|
8989
...
9090
28 | #[abi_name(name = "OtherStruct")]
@@ -94,7 +94,7 @@ error: Duplicated name found for renamed ABI type.
9494
|
9595
...
9696
3 | pub struct OtherStruct {
97-
| ----------- help: This is the existing type with conflicting name
97+
| ----------- help: This is the existing type with conflicting name.
9898
|
9999
____
100100

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
library;
22

33
#[abi_name(name = true)]
4-
pub fn not_ok() {}
4+
pub struct NotOk {}

test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_args_values_types/stdout.snap

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ error: Attribute argument value has a wrong type
4545
|
4646
____
4747

48-
error: Attribute cannot annotate item
49-
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_args_values_types/src/abi_name_attr.sw:3:3
50-
|
51-
...
52-
3 | #[abi_name(name = true)]
53-
| ^^^^^^^^ "abi_name" attribute cannot annotate a function declaration.
54-
|
55-
= help: "abi_name" attribute can only annotate structs and enums.
56-
____
57-
5848
error: Attribute argument value has a wrong type
5949
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_args_values_types/src/error_attr.sw:6:17
6050
|
@@ -75,5 +65,5 @@ error: Attribute argument value has a wrong type
7565
|
7666
____
7767

78-
Aborting due to 6 errors.
68+
Aborting due to 5 errors.
7969
error: Failed to compile attributes_invalid_args_values_types

0 commit comments

Comments
 (0)