1

If I have the following inheritance:

pragma solidity ^0.4.24;

interface A {
    function foo(uint256 _x) external pure returns(uint256);
}

contract B1 is A {
    function foo(uint256 _x) public pure returns(uint256) { return _x + 1; }
}

contract B2 is A {

}

contract C_OK is B1, B2 {
    function foo(uint256 _x) public pure returns(uint256) { return super.foo(_x); }
}

contract C_NOT_OK is B1, A {
    function foo(uint256 _x) public pure returns(uint256) { return super.foo(_x); }
}

contract C_OK compiles fine, but for contract C_NOT_OK I get a

TypeError: Linearization of inheritance graph impossible
contract C_NOT_OK is B1, A {

I don't really see why I get an error here but not for C_OK, since the only difference is that C_OK inherits from B2 instead of from A, but B2 is empty and directly inherits from A, so C_OK and C_NOT_OK should be equivalent.

What's going on here?

1

This is an instance of the "Diamond problem" when a language supports multiple inheritance. It gets impossible to draw the inheritance graph.

Solidity uses something called C3 linearization (https://en.wikipedia.org/wiki/C3_linearization) for flattening the inherited contracts into the end result. As you have noticed, you can't just randomly throw around inheritance and "see what happens".

Your biggest problem is that Solidity required the inheritance to be in certain order (https://solidity.readthedocs.io/en/develop/contracts.html#multiple-inheritance-and-linearization):

You have to list the direct base contracts in the order from “most base-like” to “most derived”

Therefore, as A is "more base-like" than B1, if you just change in your C_NOT_OK contract the order of inheritance it will work just fine.

  • I know about that, but it seems it‘s easily possible to circumvent this by simply creating an empty intermediate contract and inheriting the base from (as in C_OK) – user1282931 Dec 5 '18 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.