Strings.toString(...) only works with statically defined uint256 and fails otherwise

I am currently using @openzeppelin/contracts@^4.9.0 with a Hardhat project (hardhat@^2.14.0) and solc 0.8.18.x. I'm getting a Strings.toString(...) error: Member "toString" not unique after argument-dependent lookup in type(library Strings).

I've tried installing other solc and package versions without success and have no clue why this is all of the sudden happening—IDE tried with both Remix and VS Code.

Here is the code that fails:

// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.10 <0.9.0;

import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

contract Template {
  string private test;

  constructor() {
    test = string.concat(Strings.toString(1)," Bobby Tables");
  }
}

And if, instead, it's statically defined as a variable v, it works as expected:

// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.10 <0.9.0;

import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

contract Template {
  string private test;

  constructor() {
    uint256 v = 1;
    test = string.concat(Strings.toString(v)," Bobby Tables");
  }
}

The first example was working in very recent projects that use this dep, but it not longer is working as expected.

Not sure what you mean by that, but with solc 0.8.19, both examples are working fine for me.

For all it matters, I am using hardhat 2.12.7 and openzeppelin 4.8.2.


UPDATE:

OK, I think I understand what the problem is.

A hint for it is in Member "toString" not unique.

See v4.9.0 release notes:

  • Strings: add toString method for signed integers. (#3773)

Once this method is added, there is no way for the compiler to resolve Strings.toString(1) unequivocally, since both function toString(uint256 value) and function toString(uint256 value) are now suitable candidates.

So you need to solve this manually, by explicitly casting that constant value into one of these types.

In other words, use either Strings.toString(uint256(1)) or Strings.toString(int256(1)).

1 Like

Sorry for that solc 0.8.18.x typo—I meant solc 0.8.1x (wasn't working with any versions).

Ahh casting it to uint256...that's such a simple fix that I wasted hours on lol. Thanks, resolved!

Right. This was an unintended side effect. I've opened an issue:

2 Likes