Compiler Error: Unable to Resolve Variable 'res' When Trying to Insert Owners

I’m working on a contract and I’m facing an issue during the compilation process. In a function, I receive a vec of Identities and I need to check if they are duplicated before inserting them into a StorageMap. To achieve this, I’m using the try_insert method and checking its return value.

Here’s the relevant code:

// Add the owners
let mut i = 0;
while i < owners_count {
    let owner = owners_list.get(i).unwrap();
    let res = storage.owners.try_insert(owner, ());
    // If the owner is already in the list, revert
    require(res.is_ok(), MultisigError::DuplicatedOwner);
    i += 1;
}

The linter doesn’t find any errors in the code, and the types are resolved correctly. However, when I try to compile the contract using forc build , I get the following error:

 Compiling library core (/Users/lucaauet/.forc/git/checkouts/std-9be0d6062747ea7/0dc6570377ee9c4a6359ade597fa27351e02a728/sway-lib-core)
 Compiling library std (git+https://github.com/fuellabs/sway?tag=v0.49.3#0dc6570377ee9c4a6359ade597fa27351e02a728)
 Compiling contract fuel-multisig (/Users/lucaauet/Development/Protofire/Fuel/fuel-multisig/multisig-contract)
error: Internal compiler error: Unable to resolve variable 'res'.
Please file an issue on the repository and include the code that triggered this error.
____

  Aborting due to 1 error.
Error: Failed to compile fuel-multisig

This is the output of fuelup show:

installed toolchains
--------------------
beta-3-aarch64-apple-darwin
beta-4-aarch64-apple-darwin
latest-aarch64-apple-darwin (default)

active toolchain
----------------
latest-aarch64-apple-darwin (default)
  forc : 0.49.3
    - forc-client
      - forc-deploy : 0.49.3
      - forc-run : 0.49.3
    - forc-crypto : 0.49.3
    - forc-doc : 0.49.3
    - forc-explore : 0.28.1
    - forc-fmt : 0.49.3
    - forc-lsp : 0.49.3
    - forc-tx : 0.49.3
    - forc-wallet : 0.4.3
  fuel-core : 0.22.1
  fuel-core-keygen : 0.22.1

fuels versions
--------------
forc : 0.54.0
forc-wallet : 0.54.0

I’m not sure what’s causing this issue, as the code seems to be correct. Any help in resolving this compiler error would be greatly appreciated.

1 Like

i don’t believe something like try_insert exists ( I could be wrong).

Instead, try this:

storage.owners.insert(owner, ITEM);

if you want to check if item exists already, you can try:

let item = storage.owners.get(owner).try_read().unwrap_or(DEFAULT_VALUE);
if item != DEFAULT_VALUE {
    // item doesn't exist
}

3 Likes

The try_insert method does exist in the Sway standard library.

Even though the try_insert method is available, the compiler is unable to resolve the res variable, which is causing the “Internal compiler error: Unable to resolve variable ‘res’” issue.

I made it work using an approach similar to what you mentioned:

// Add the owners
let mut i = 0;
while i < owners_count {
    let owner_to_add = owners_list.get(i).unwrap();
    let owner = storage.owners.get(owner_to_add).try_read();
    require(owner.is_none(), MultisigError::DuplicatedOwner);

    storage.owners.insert(owner_to_add, ());

    i += 1;
}

However, the fact that the compiler is crashing is concerning and should be reported as a potential bug.

Hi there!
I’ve tried to replicate using the following code and toolchain

contract;

use std::{
    storage::storage_map::StorageMap,
    constants::ZERO_B256,
    hash::Hash
};


storage {
    owners: StorageMap<Identity, ()> = StorageMap {}
}

abi MyContract {
    #[storage(read, write)]
    fn my_fn();
}

impl MyContract for Contract {
    #[storage(read, write)]
    fn my_fn() {
        let owner = Identity::Address(Address::from(ZERO_B256));
        let res = storage.owners.try_insert(owner , ());
        require(res.is_ok(), "DuplicatedOwner");
    }
}

///
latest-x86_64-unknown-linux-gnu (default)
  forc : 0.49.3
    - forc-client
      - forc-deploy : 0.49.3
      - forc-run : 0.49.3
    - forc-crypto : 0.49.3
    - forc-doc : 0.49.3
    - forc-explore : 0.28.1
    - forc-fmt : 0.49.3
    - forc-lsp : 0.49.3
    - forc-tx : 0.49.3
    - forc-wallet : 0.4.3
  fuel-core : 0.22.4
  fuel-core-keygen : 0.22.4

fuels versions
--------------
forc : 0.54.0
forc-wallet : 0.54.0

And I’m having the same error

 match val {
    |               ^^^ Internal compiler error: Failed to get variant type from enum in `unsigned downcast`.
Please file an issue on the repository and include the code that triggered this error.
189 |             Option::Some(v) => {
190 |                 Result::Err(StorageMapError::OccupiedError(v))
    |
____

error: Internal compiler error: Unable to resolve variable 'res'.
Please file an issue on the repository and include the code that triggered this error.
____

  Aborting due to 2 errors.
Error: Failed to compile contract

Please note that if you comment out the require function, the Unable to resolve variable ‘res’ dissapears.

        // require(res.is_ok(), "DuplicatedOwner");

It seems like this is a compiler bug.

1 Like