Skip to content

Commit

Permalink
Fixes ABI alias metadata types.
Browse files Browse the repository at this point in the history
Before this push using an alias in a contract would produce an ABI with
two distinct metadata types for tuples, one for `(_,_)` and another one
for `(u64,u64)`.

With this change alias are bypassed and we only produce metadata for the
string type of the inner alias type. If alias is `(u64, u64)` we will
produce a metadatatype for `(_,_)`.

Fixes #6488
  • Loading branch information
esdrubal committed Sep 4, 2024
1 parent 8cb7796 commit ce47bf6
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 2 deletions.
6 changes: 4 additions & 2 deletions sway-core/src/abi_generation/abi_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ impl TypeId {
&*type_engine.get(resolved_type_id),
) {
(TypeInfo::Custom { .. }, TypeInfo::Struct { .. })
| (TypeInfo::Custom { .. }, TypeInfo::Enum { .. })
| (TypeInfo::Custom { .. }, TypeInfo::Alias { .. }) => type_engine
| (TypeInfo::Custom { .. }, TypeInfo::Enum { .. }) => type_engine
.get(resolved_type_id)
.abi_str(ctx, engines, true),
(_, TypeInfo::Alias { ty, .. }) => {
ty.type_id.get_abi_type_str(ctx, engines, ty.type_id)
}
(TypeInfo::Tuple(fields), TypeInfo::Tuple(resolved_fields)) => {
assert_eq!(fields.len(), resolved_fields.len());
let field_strs = resolved_fields
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[package]]
name = "abi_with_alias"
source = "member"
dependencies = ["core"]

[[package]]
name = "core"
source = "path+from-root-DE4DCECD674C309C"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
implicit-std = false
license = "Apache-2.0"
name = "abi_with_alias"

[dependencies]
core = { path = "../../../../../../../sway-lib-core" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"concreteTypes": [
{
"concreteTypeId": "2e38e77b22c314a449e91fafed92a43826ac6aa403ae6a8acb6cf58239fbaf5d",
"type": "()"
},
{
"concreteTypeId": "41bd1a98f0a59642d8f824c805b798a5f268d1f7d05808eb05c4189c493f1be0",
"metadataTypeId": 0,
"type": "(u64, u64)"
}
],
"configurables": [],
"encodingVersion": "1",
"functions": [
{
"attributes": null,
"inputs": [
{
"concreteTypeId": "41bd1a98f0a59642d8f824c805b798a5f268d1f7d05808eb05c4189c493f1be0",
"name": "arg1"
}
],
"name": "aliased_tuple",
"output": "2e38e77b22c314a449e91fafed92a43826ac6aa403ae6a8acb6cf58239fbaf5d"
},
{
"attributes": null,
"inputs": [
{
"concreteTypeId": "41bd1a98f0a59642d8f824c805b798a5f268d1f7d05808eb05c4189c493f1be0",
"name": "_arg1"
}
],
"name": "tuple",
"output": "2e38e77b22c314a449e91fafed92a43826ac6aa403ae6a8acb6cf58239fbaf5d"
}
],
"loggedTypes": [],
"messagesTypes": [],
"metadataTypes": [
{
"components": [
{
"name": "__tuple_element",
"typeId": 1
},
{
"name": "__tuple_element",
"typeId": 1
}
],
"metadataTypeId": 0,
"type": "(_, _)"
},
{
"metadataTypeId": 1,
"type": "u64"
}
],
"programType": "contract",
"specVersion": "1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"concreteTypes": [
{
"concreteTypeId": "2e38e77b22c314a449e91fafed92a43826ac6aa403ae6a8acb6cf58239fbaf5d",
"type": "()"
},
{
"concreteTypeId": "41bd1a98f0a59642d8f824c805b798a5f268d1f7d05808eb05c4189c493f1be0",
"metadataTypeId": 0,
"type": "(u64, u64)"
}
],
"configurables": [],
"encodingVersion": "1",
"functions": [
{
"attributes": null,
"inputs": [
{
"concreteTypeId": "41bd1a98f0a59642d8f824c805b798a5f268d1f7d05808eb05c4189c493f1be0",
"name": "arg1"
}
],
"name": "aliased_tuple",
"output": "2e38e77b22c314a449e91fafed92a43826ac6aa403ae6a8acb6cf58239fbaf5d"
},
{
"attributes": null,
"inputs": [
{
"concreteTypeId": "41bd1a98f0a59642d8f824c805b798a5f268d1f7d05808eb05c4189c493f1be0",
"name": "_arg1"
}
],
"name": "tuple",
"output": "2e38e77b22c314a449e91fafed92a43826ac6aa403ae6a8acb6cf58239fbaf5d"
}
],
"loggedTypes": [],
"messagesTypes": [],
"metadataTypes": [
{
"components": [
{
"name": "__tuple_element",
"typeId": 1
},
{
"name": "__tuple_element",
"typeId": 1
}
],
"metadataTypeId": 0,
"type": "(_, _)"
},
{
"metadataTypeId": 1,
"type": "u64"
}
],
"programType": "contract",
"specVersion": "1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
contract;

type AliasedTuple = (u64, u64);

abi MyContract {
fn tuple(arg1: (u64, u64)); // Inline
fn aliased_tuple(arg1: AliasedTuple); // Alias
}

impl MyContract for Contract {
fn tuple(_arg1: (u64, u64)) {
}
fn aliased_tuple(arg1: AliasedTuple) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "compile"
validate_abi = true
expected_warnings = 1

0 comments on commit ce47bf6

Please sign in to comment.