solang icon indicating copy to clipboard operation
solang copied to clipboard

Incorrect constructor inheritance

Open xermicus opened this issue 2 years ago • 0 comments

The code example about linearization of constructors from the Solidity docs produces wrong code:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract Base1 {
        uint public foo;
    constructor() {}
}

abstract contract Base2 {
    constructor() {}
}

// Constructors are executed in the following order:
//  1 - Base1
//  2 - Base2
//  3 - Derived1
contract Derived1 is Base1, Base2 {
    constructor() Base1() Base2() {}
}

// Constructors are executed in the following order:
//  1 - Base2
//  2 - Base1
//  3 - Derived2
contract Derived2 is Base2, Base1 {
    constructor() Base2() Base1() {}
}

// Constructors are still executed in the following order:
//  1 - Base2
//  2 - Base1
//  3 - Derived3
contract Derived3 is Base2, Base1 {
    constructor() Base1() Base2() {}
}

Instead of what is explained in the comment of the example contracts, the constructors look like this:

# constructor Derived1::Derived1::constructor::872ccdc6190148bc public:true selector:872ccdc6190148bc nonpayable:true
# params: 
# returns: 
block0: # entry
         = call Derived1::Base2::constructor::872ccdc6190148bc 
         = call Derived1::Base1::constructor::872ccdc6190148bc 
        return 
# constructor Derived2::Derived2::constructor::872ccdc6190148bc public:true selector:872ccdc6190148bc nonpayable:true
# params: 
# returns: 
block0: # entry
         = call Derived2::Base1::constructor::872ccdc6190148bc 
         = call Derived2::Base2::constructor::872ccdc6190148bc 
        return 
# constructor Derived3::Derived3::constructor::872ccdc6190148bc public:true selector:872ccdc6190148bc nonpayable:true
# params: 
# returns: 
block0: # entry
         = call Derived3::Base1::constructor::872ccdc6190148bc 
         = call Derived3::Base2::constructor::872ccdc6190148bc 
        return

The same is the case when compiling for the polkadot target.

xermicus avatar Jul 06 '23 12:07 xermicus