sway icon indicating copy to clipboard operation
sway copied to clipboard

Some usability issues around `abi` and `Contract`

Open mohammadfawaz opened this issue 3 years ago • 1 comments

Below are some examples that should not compile but do. I bundled those together in the same issue because they are somewhat related and there may be others. Whoever takes this on should also think about other corner cases that are not covered below.

Self impl instead of impl ABI for Contract

contract;

abi MyContract {
    fn foo();
}

impl Contract { // self impl for a `Contract`?
    fn foo() {
    }
}

Mismatch in return types

Interestingly this is only a problem for integer types. I still don't think this should compile (Similar playground link)

contract;

abi MyContract {
    fn foo() -> u32;
}

impl MyContract for Contract {
    fn foo() -> u64 {
        0 
    }
}

Traits in Sway have the same problem btw. The following should not compile but does:

trait T {
    fn foo() -> u64;
}

struct S {
    x: u64
}

impl T for S {
    fn foo() -> u32 {
        0
    }
}

This also seems to be a problem in this situation which compiles but shouldn't:

contract;

abi MyContract {
    fn foo()-> u64;
}

impl Contract {
    fn foo() { // No return type here
    }
}

Repeated abi declarations:

The following compiles but I don't expect it to:

contract;

abi MyContract {
    fn foo() -> u64;
}

abi MyContract {
    fn bar() -> u64;
}

Multiple definitions of abi methods

Example:

contract;

abi MyContract {
    fn foo() -> u64;
}

impl MyContract for Contract {
    fn foo() -> u64 {
        0
    }
}

impl MyContract for Contract {
    fn foo() -> u64 {
        0
    }
}

Multiple impl blocks for the same type or trait should be allowed but they should not allow redefinition of methods. This applies for ABIs, regular traits, and types.

self inside ABI function signature

ABI function should accept self as the first argument.

contract;

abi MyContract {
    fn foo(self);
}

mohammadfawaz avatar Jul 27 '22 18:07 mohammadfawaz

Oh wow, these are quite surprising. OTOH, they aren't since all our tests are written by humans. We should consider using a fuzzer to come up with these sorts of examples.

Edit: #2400

otrho avatar Jul 27 '22 23:07 otrho

Sorry @anton-trunov I put myself under the assigned section on accident.

emilyaherbert avatar Nov 30 '22 22:11 emilyaherbert

Looks like all the tasks are now complete. Closing.

mohammadfawaz avatar Dec 30 '22 01:12 mohammadfawaz