Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sol-macro): expand all getter return types #812

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions crates/sol-macro-expander/src/expand/var_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ fn expand_returns(cx: &ExpCtxt<'_>, f: &mut ItemFunction) -> Result<()> {
if let Type::Custom(name) = ty {
ty = cx.custom_type(name);
}

// skip if not tuple with complex types
let Type::Tuple(tup) = ty else { return Ok(()) };
if !tup.types.iter().any(type_is_complex) {
return Ok(());
}

// retain only non-complex types
// TODO: assign return types' names from the original struct
Expand Down Expand Up @@ -103,6 +98,8 @@ fn expand_returns(cx: &ExpCtxt<'_>, f: &mut ItemFunction) -> Result<()> {
/// }
/// }
/// ```
///
/// Ref: <https://github.com/ethereum/solidity/blob/9d7cc42bc1c12bb43e9dccf8c6c36833fdfcbbca/libsolidity/ast/Types.cpp#L2887-L2891>
fn type_is_complex(ty: &Type) -> bool {
matches!(ty, Type::Mapping(_) | Type::Array(_))
}
52 changes: 52 additions & 0 deletions crates/sol-types/tests/macros/sol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,3 +1171,55 @@ fn event_check_signature() {
assert!(MyEventAnonymous::ANONYMOUS);
let MyEventAnonymous {} = MyEventAnonymous::decode_raw_log(no_topics, &[], false).unwrap();
}

// https://github.com/alloy-rs/core/issues/811
#[test]
fn mapping_getters() {
sol! {
#![sol(all_derives)]

contract TestIbc {
/// ConnectionId -> Connection
mapping(uint32 => Connection) public connections;
/// ChannelId -> Channel
mapping(uint32 => Channel) public channels;

enum ConnectionState {
Unspecified,
Init,
TryOpen,
Open
}

struct Connection {
ConnectionState state;
uint32 client_id;
uint32 counterparty_client_id;
uint32 counterparty_connection_id;
}

enum ChannelState {
Unspecified,
Init,
TryOpen,
Open,
Closed
}

struct Channel {
ChannelState state;
uint32 connection_id;
uint32 counterparty_channel_id;
bytes counterparty_port_id;
string version;
}
}
}

assert_eq!(TestIbc::connectionsCall::SIGNATURE, "connections(uint32)");
let _ = TestIbc::connectionsReturn { _0: 0u8, _1: 0u32, _2: 0u32, _3: 0u32 };

assert_eq!(TestIbc::channelsCall::SIGNATURE, "channels(uint32)");
let _ =
TestIbc::channelsReturn { _0: 0u8, _1: 0u32, _2: 0u32, _3: bytes![], _4: String::new() };
}