Skip to content

New rules for checking that no non-initialized immutables are used in the declaration of other immutables #658

@PaulRBerg

Description

@PaulRBerg

It'd be helpful to provide a new Solhint rule for checking that no non-initialized immutable variables are used in the declaration of other immutable variables.

Note that 'non-initialized' means that the immutable Foo is declared AFTER the other immutable Bar uses Foo.

It's clearer to see the issue with this code:

contract Immutables {
    // CASE 1: Constants can be referenced before declaration
    // constA correctly uses constB even though constB is declared later
    uint256 public constA = constB + 100;     // Will be 150
    uint256 internal constant constB = 50;    // Constants are compile-time values

    // CASE 2: Immutables CANNOT be used before declaration
    // immA tries to use immB before immB is declared - this causes a problem!
    uint256 public immA = immB + 100;         // Will be 100, NOT 125!
    uint256 internal immutable immB = 25;     // immB isn't available when immA is initialized

    // CASE 3: Correct order for immutables
    // Declare immutables first, then use them
    uint256 internal immutable immD = 75;     // Declare immutable first
    uint256 public immC = immD + 100;         // Will be 175 as expected

    // For clarity, constructor shows the actual values
    constructor() {
        // Runtime values demonstration
        assert(constA == 150);    // Constants always work regardless of order
        assert(immC == 175);      // Correct usage of immutables

        // Uncomment this assertion and see the revert
        // assert(immA == 125);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions