sway icon indicating copy to clipboard operation
sway copied to clipboard

Need to re-import in order to access functionality

Open Braqzen opened this issue 3 years ago • 2 comments

Summary

When importing a type into file2 and using that imported type in a custom type I must reimport the same type (not the custom one) into file1 in order to obtain the same functionality despite never using the type explicitly.

main.sw

contract;

dep utils;

use std::constants::BASE_ASSET_ID;

// uncomment to make this build
// use std::contract_id::ContractId;

use utils::Wrapper;

abi MyContract {
    fn test_function() -> bool;
}

impl MyContract for Contract {
    fn test_function() -> bool {
        let w1 = Wrapper { asset: BASE_ASSET_ID };
        let w2 = Wrapper { asset: BASE_ASSET_ID };

        // works
        // w1 == w2

        // fails to find "eq"
        w1.asset == w2.asset
    }
}

I expect the above to work without having to import ContractId however it fails to build if it is not imported again.

utils.sw

library utils;

use core::ops::Eq;
use std::contract_id::ContractId;

pub struct Wrapper {
    asset: ContractId
}

impl Eq for Wrapper {
    fn eq(self, other: Self) -> bool {
        self.asset == other.asset
    }
}

Screenshot

1

Braqzen avatar Jul 08 '22 17:07 Braqzen

Another instance (from https://github.com/FuelLabs/sway/issues/2322) where this happens is:

contract;

use std::{
    address::Address,
    chain::auth::msg_sender,
    identity::Identity,
    result::Result,
};

abi MyContract {
    fn test();
}

impl MyContract for Contract {
    fn test() {
        let sender = msg_sender().unwrap();
        if let Identity::Address(addr) = sender {
            let lalala = addr.into();
        }
    }
}
  1. I need to import Result for the sender line to pick up on .unwrap(). I thought this was the issue that was resolved for introspection so that e.g. some_func().struct_field could work, likewise I'm pretty sure that this worked before on chained method calls

mohammadfawaz avatar Jul 27 '22 20:07 mohammadfawaz

Also, one thing I noticed is that importing:

use utils::*;

instead of:

use utils::Wrapper;

compiles! So we clearly have logic in the glob import code that is transitive 🤔

mohammadfawaz avatar Jul 31 '22 02:07 mohammadfawaz

Hi @Braqzen, I wasn't able to reproduce the error and I believe that this bug is likely resolved. Can you confirm? I have a PR this PR up. #3710. CC @mohammadfawaz

emilyaherbert avatar Jan 06 '23 18:01 emilyaherbert

Hi @Braqzen, I wasn't able to reproduce the error and I believe that this bug is likely resolved. Can you confirm? I have a PR this PR up. #3710. CC @mohammadfawaz

Having directly copy and pasted my previous examples on forc v0.32.2 I have no issues building.

Braqzen avatar Jan 06 '23 18:01 Braqzen