'Why does this code throw a (ambiguous) internal compiler error?

This function produces the following error (compiled by hardhat)

Solidity 0.8.7

contract TestContract {
  function slice(string calldata source, uint from, uint to) public pure returns(string memory) {
    bytes calldata b = (bytes(source));
    uint len = b.length;
    require(to < len && to>=from, "indexes out of bounds"); 
    string memory retval = string(b[from:to]);
    return retval;
}    

}

The error I get is Compiling 1 file with 0.8.7 InternalCompilerError: Internal compiler error (/Users/distiller/project/libsolidity/codegen/CompilerUtils.cpp:1116) Error HH600: Compilation failed



Solution 1:[1]

I'm not sure about how you convert bytes to string, but the error might come from string(b[from:to]);. You can reference the byteToString function provided here https://ethereum.stackexchange.com/questions/29295/how-to-convert-a-bytes-to-string-in-solidity.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kaiyee0