alloy-rs / core

High-performance, well-tested & documented core libraries for Ethereum, in Rust
https://alloy.rs
Apache License 2.0
763 stars 137 forks source link

fix(sol-macro): namespaced custom type resolution #731

Closed klkvr closed 1 week ago

klkvr commented 1 week ago

Motivation

Currently type resolution does not handle namespaces correctly, causing such example to fail:

alloy_sol_types::sol! {
    library LibA {
        struct B {
            uint256 val;
        }

        struct A {
            B val;
        }
    }

    library LibB {
        struct Nested {
            uint256 val1;
            LibA.A val2;
        }
    }

    contract A {
        function get_val(LibB.Nested memory user) external;
    }
}

When resolving LibB.Nested we'll enter LibA.A type which contains B struct. However, we don't set namespace to LibA, and will instead try to resolve B as item of LibB, which would fail.

Solution

I've updated resolve_custom_types fn to use custom Resolver visitor which keeps track of namespace, updates it before entering a type from another namespace and resets it to previous value after type is processed.

PR Checklist