Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit type arguments for generic structs/enums in the JSON ABI file and update the function selector to include those type arguments #1993

Closed
mohammadfawaz opened this issue Jun 15, 2022 · 2 comments · Fixed by #2218
Labels
ABI Everything to do the ABI, especially the JSON representation compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen

Comments

@mohammadfawaz
Copy link
Contributor

mohammadfawaz commented Jun 15, 2022

This is needed to support Vec<T> in the ABI. The struct Vec does not have data fields that actually use the type T. Therefore, the ABI won't currently have that type information that is needed by the SDK. This needs to change. Spec changes to be made as well once we're done spec-ing this out.

Note that unresolved generic types in the ABI are not allowed. However, something like this is allowed:

abi Foo {
    fn foo(v: Vec<u64>); // This `u64` should show up in the JSON ABI file
}

We may also need to update the function selector to include the type information above. This is probably needed to be consistent with how we handle non-generic types. Vec<u64> is a totally different type from Vec<bool>.

@mohammadfawaz mohammadfawaz added ABI Everything to do the ABI, especially the JSON representation compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen labels Jun 15, 2022
@mohammadfawaz mohammadfawaz changed the title Emit type arguments for generic structs/enums in the JSON ABI file Emit type arguments for generic structs/enums in the JSON ABI file and update the function selector Jun 15, 2022
@mohammadfawaz mohammadfawaz changed the title Emit type arguments for generic structs/enums in the JSON ABI file and update the function selector Emit type arguments for generic structs/enums in the JSON ABI file and update the function selector to include those type arguments Jun 15, 2022
@adlerjohn adlerjohn moved this to Todo in Fuel Network Jun 15, 2022
@mohammadfawaz
Copy link
Contributor Author

mohammadfawaz commented Jun 29, 2022

Here's how this might look like in the general case:

contract;

struct Foo<T, U> {
}

struct S {
    x: u64,
    y: b256,
}

abi MyContract {
    fn test_function(f: Foo<str[3], S>) -> bool;
}

impl MyContract for Contract {
    fn test_function(f: Foo<str[3], S>) -> bool {
        true
    }
}

JSON ABI:

[
  {
    "type": "function",
    "inputs": [
      {
        "name": "f",
        "type": "struct Foo",
        "components": [],
        "typeArguments": [
          {
            "name": "T",
            "type": "str[3]",
            "components": null,
            "typeArguments": null
          },
          {
            "name": "U",
            "type": "struct S",
            "components": [
              {
                "name": "x",    
                "type": "u64",
                "components": null
                "typeArguments": null
              },
              {
                "name": "y",    
                "type": "b256",
                "components": null
                "typeArguments": null
              }
            ],
            "typeArguments": [] 
          }
        ]
      }
    ],
    "name": "test_function",
    "outputs": [
      {
        "name": "",
        "type": "bool",
        "components": null
        "typeArguments": null
      }
    ]
  }
]

Note the new field "type_arguments" which will be

  • [] for structs and enums that don't have type parameters.
  • null for types that are not structs or enums.
  • Non-empty array otherwise.

Thoughts? @digorithm @adlerjohn

@digorithm
Copy link
Member

@mohammadfawaz That looks reasonable to me, though implementation problems/challenges may or may not arise once we're actually implementing it on the SDK side. But at first glance, it looks simple enough!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ABI Everything to do the ABI, especially the JSON representation compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants