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
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.
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.