Skip to content

Commit 9b1b4af

Browse files
committed
Add improved checking for ABI names.
1 parent 271a850 commit 9b1b4af

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

sway-core/src/abi_generation/fuel_abi.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ impl TypeId {
182182
};
183183

184184
if has_abi_name_attribute {
185-
if name.is_empty() || !is_valid_identifier_or_path(name.as_str()) {
185+
if name.is_empty()
186+
|| !is_valid_identifier_or_path(name.as_str())
187+
|| name.starts_with("::")
188+
{
186189
err = Some(handler.emit_err(CompileError::ABIInvalidName {
187190
span: attribute_name_span.clone(),
188191
name,

test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/main.sw

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ struct MyStruct5 {}
2828
#[abi_name(name = "OtherStruct")]
2929
struct MyStruct6 {}
3030

31+
#[abi_name(name = "::some_module::in_the_same::package")]
32+
struct MyStruct7 {}
33+
3134
// OK because enums are on a different namespace
3235
#[abi_name(name = "SameName")]
3336
enum MyEnum {
@@ -49,6 +52,7 @@ abi MyAbi {
4952
fn my_struct4() -> MyStruct4;
5053
fn my_struct5() -> MyStruct5;
5154
fn my_struct6() -> MyStruct6;
55+
fn my_struct7() -> MyStruct7;
5256
fn other_enum() -> OtherEnum;
5357
fn my_enum() -> MyEnum;
5458
fn my_enum1() -> MyEnum1;
@@ -64,6 +68,7 @@ impl MyAbi for Contract {
6468
fn my_struct4() -> MyStruct4 { MyStruct4{} }
6569
fn my_struct5() -> MyStruct5 { MyStruct5{} }
6670
fn my_struct6() -> MyStruct6 { MyStruct6{} }
71+
fn my_struct7() -> MyStruct7 { MyStruct7{} }
6772
fn other_enum() -> OtherEnum { OtherEnum::A }
6873
fn my_enum() -> MyEnum { MyEnum::A }
6974
fn my_enum1() -> MyEnum1 { MyEnum1::A }

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ output:
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)
1010
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
11+
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/main.sw:40:19
1212
|
1313
...
14-
37 | #[abi_name(name = "OtherEnum")]
14+
40 | #[abi_name(name = "OtherEnum")]
1515
| ^^^^^^^^^^^
1616
|
1717
::: test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/other.sw:7:10
@@ -98,5 +98,15 @@ error: Duplicated name found for renamed ABI type
9898
|
9999
____
100100

101-
Aborting due to 7 errors.
101+
error: Invalid name found for renamed ABI type.
102+
--> test/src/e2e_vm_tests/test_programs/should_fail/attributes_invalid_abi_names/src/main.sw:31:19
103+
|
104+
...
105+
31 | #[abi_name(name = "::some_module::in_the_same::package")]
106+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
107+
|
108+
= help: The name must be a valid Sway identifier.
109+
____
110+
111+
Aborting due to 8 errors.
102112
error: Failed to compile attributes_invalid_abi_names

0 commit comments

Comments
 (0)