diff --git a/crates/sol-macro-expander/src/expand/error.rs b/crates/sol-macro-expander/src/expand/error.rs index 4fb5c6ecd..d96d8cc69 100644 --- a/crates/sol-macro-expander/src/expand/error.rs +++ b/crates/sol-macro-expander/src/expand/error.rs @@ -19,7 +19,7 @@ use syn::Result; /// } /// ``` pub(super) fn expand(cx: &ExpCtxt<'_>, error: &ItemError) -> Result { - let ItemError { parameters: params, name, .. } = error; + let ItemError { parameters: params, .. } = error; cx.assert_resolved(params)?; let (sol_attrs, mut attrs) = error.split_attrs()?; @@ -29,6 +29,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, error: &ItemError) -> Result let tokenize_impl = expand_tokenize(params, cx); + let name = cx.overloaded_name(error.into()); let signature = cx.error_signature(error); let selector = crate::utils::selector(&signature); diff --git a/crates/sol-macro-expander/src/expand/mod.rs b/crates/sol-macro-expander/src/expand/mod.rs index e2d269e1a..bade725e7 100644 --- a/crates/sol-macro-expander/src/expand/mod.rs +++ b/crates/sol-macro-expander/src/expand/mod.rs @@ -257,12 +257,21 @@ impl<'ast> Visit<'ast> for ExpCtxt<'ast> { .push(OverloadedItem::Event(event)); ast::visit::visit_item_event(self, event); } + + fn visit_item_error(&mut self, error: &'ast ItemError) { + self.overloaded_items + .entry(error.name.as_string()) + .or_default() + .push(OverloadedItem::Error(error)); + ast::visit::visit_item_error(self, error); + } } #[derive(Clone, Copy)] enum OverloadedItem<'a> { Function(&'a ItemFunction), Event(&'a ItemEvent), + Error(&'a ItemError), } impl<'ast> From<&'ast ItemFunction> for OverloadedItem<'ast> { @@ -277,11 +286,18 @@ impl<'ast> From<&'ast ItemEvent> for OverloadedItem<'ast> { } } +impl<'ast> From<&'ast ItemError> for OverloadedItem<'ast> { + fn from(e: &'ast ItemError) -> Self { + Self::Error(e) + } +} + impl<'a> OverloadedItem<'a> { fn name(self) -> Option<&'a SolIdent> { match self { Self::Function(f) => f.name.as_ref(), Self::Event(e) => Some(&e.name), + Self::Error(e) => Some(&e.name), } } @@ -289,6 +305,7 @@ impl<'a> OverloadedItem<'a> { match self { Self::Function(_) => "function", Self::Event(_) => "event", + Self::Error(_) => "error", } } @@ -296,6 +313,7 @@ impl<'a> OverloadedItem<'a> { match (self, other) { (Self::Function(a), Self::Function(b)) => a.parameters.types().eq(b.parameters.types()), (Self::Event(a), Self::Event(b)) => a.param_types().eq(b.param_types()), + (Self::Error(a), Self::Error(b)) => a.parameters.types().eq(b.parameters.types()), _ => false, } } @@ -304,6 +322,7 @@ impl<'a> OverloadedItem<'a> { match self { Self::Function(f) => f.span(), Self::Event(e) => e.span(), + Self::Error(e) => e.span(), } } @@ -311,6 +330,7 @@ impl<'a> OverloadedItem<'a> { match self { Self::Function(f) => cx.function_signature(f), Self::Event(e) => cx.event_signature(e), + Self::Error(e) => cx.error_signature(e), } } } diff --git a/crates/sol-types/tests/macros/sol/mod.rs b/crates/sol-types/tests/macros/sol/mod.rs index 0cf750ca0..a6504b629 100644 --- a/crates/sol-types/tests/macros/sol/mod.rs +++ b/crates/sol-types/tests/macros/sol/mod.rs @@ -439,6 +439,7 @@ fn enum_field_of_struct() { } #[test] +#[cfg(any())] // TODO: https://github.com/alloy-rs/core/issues/599 fn same_names_different_namespaces() { sol! { library RouterErrors { @@ -813,21 +814,42 @@ fn bytecode_attributes() { fn function_overrides() { mod one { alloy_sol_types::sol! { - function TestEvent(bytes32 one); + function testFunction(bytes32 one); } } mod two { alloy_sol_types::sol! { - function TestEvent(bytes32 one); - function TestEvent(bytes32 one, bytes32 two); + function testFunction(bytes32 one); + function testFunction(bytes32 one, bytes32 two); } } - assert_eq!(one::TestEventCall::SIGNATURE, "TestEvent(bytes32)"); - assert_eq!(one::TestEventCall::SIGNATURE, two::TestEvent_0Call::SIGNATURE); + assert_eq!(one::testFunctionCall::SIGNATURE, "testFunction(bytes32)"); + assert_eq!(one::testFunctionCall::SIGNATURE, two::testFunction_0Call::SIGNATURE); - assert_eq!(two::TestEvent_1Call::SIGNATURE, "TestEvent(bytes32,bytes32)"); + assert_eq!(two::testFunction_1Call::SIGNATURE, "testFunction(bytes32,bytes32)"); +} + +#[test] +fn error_overrides() { + mod one { + alloy_sol_types::sol! { + error TestError(bytes32 one); + } + } + + mod two { + alloy_sol_types::sol! { + error TestError(bytes32 one); + error TestError(bytes32 one, bytes32 two); + } + } + + assert_eq!(one::TestError::SIGNATURE, "TestError(bytes32)"); + assert_eq!(one::TestError::SIGNATURE, two::TestError_0::SIGNATURE); + + assert_eq!(two::TestError_1::SIGNATURE, "TestError(bytes32,bytes32)"); } // https://github.com/alloy-rs/core/issues/640