diff --git a/crates/sol-macro-input/src/expander.rs b/crates/sol-macro-input/src/expander.rs index 0a8e66016..395875283 100644 --- a/crates/sol-macro-input/src/expander.rs +++ b/crates/sol-macro-input/src/expander.rs @@ -1,6 +1,5 @@ -use proc_macro2::TokenStream; - use crate::SolInput; +use proc_macro2::TokenStream; /// Expands a `SolInput` into a `TokenStream`. pub trait SolInputExpander { diff --git a/crates/sol-macro-input/src/input.rs b/crates/sol-macro-input/src/input.rs index 78815e11d..bfe7997d4 100644 --- a/crates/sol-macro-input/src/input.rs +++ b/crates/sol-macro-input/src/input.rs @@ -56,9 +56,9 @@ impl Parse for SolInput { fn parse(input: ParseStream<'_>) -> Result { let attrs = Attribute::parse_inner(input)?; - // ignore attributes when peeking + // Ignore outer attributes when peeking. let fork = input.fork(); - let _inner = Attribute::parse_outer(&fork)?; + let _fork_outer = Attribute::parse_outer(&fork)?; if fork.peek(LitStr) || (fork.peek(Ident) && fork.peek2(Token![,]) && fork.peek3(LitStr)) { Self::parse_abigen(attrs, input) diff --git a/crates/sol-macro-input/src/json.rs b/crates/sol-macro-input/src/json.rs index a5a4bd9af..70634c18c 100644 --- a/crates/sol-macro-input/src/json.rs +++ b/crates/sol-macro-input/src/json.rs @@ -1,5 +1,4 @@ use crate::{SolInput, SolInputKind}; - use alloy_json_abi::{ContractObject, JsonAbi, ToSolConfig}; use proc_macro2::{Ident, TokenStream}; use quote::{quote, TokenStreamExt}; @@ -8,18 +7,13 @@ use syn::Result; impl SolInput { /// Normalize JSON ABI inputs into Sol inputs. pub fn normalize_json(self) -> Result { - if !matches!(self.kind, SolInputKind::Json(..)) { - return Ok(self); - } - - // irrefutable, as we just checked let SolInput { attrs, path, kind: SolInputKind::Json(name, ContractObject { abi, bytecode, deployed_bytecode }), } = self else { - panic!() + return Ok(self); }; let mut abi = abi.ok_or_else(|| syn::Error::new(name.span(), "ABI not found in JSON"))?; @@ -37,15 +31,15 @@ impl SolInput { let attrs_iter = attrs.iter(); let doc_str = format!( "\n\n\ - Generated by the following Solidity interface... - ```solidity - {sol} - ``` - - ...which was generated by the following JSON ABI: - ```json - {json_s} - ```", +Generated by the following Solidity interface... +```solidity +{sol} +``` + +...which was generated by the following JSON ABI: +```json +{json_s} +```", json_s = serde_json::to_string_pretty(&abi).unwrap() ); let tokens = quote! { diff --git a/crates/sol-macro-input/src/lib.rs b/crates/sol-macro-input/src/lib.rs index 1402105ee..ba4dec845 100644 --- a/crates/sol-macro-input/src/lib.rs +++ b/crates/sol-macro-input/src/lib.rs @@ -14,6 +14,8 @@ #![deny(unused_must_use, rust_2018_idioms)] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +extern crate syn_solidity as ast; + /// Tools for working with `#[...]` attributes. mod attr; pub use attr::{derives_mapped, docs_str, mk_doc, ContainsSolAttrs, SolAttrs}; @@ -26,5 +28,3 @@ pub use expander::SolInputExpander; #[cfg(feature = "json")] mod json; - -extern crate syn_solidity as ast; diff --git a/crates/sol-macro/Cargo.toml b/crates/sol-macro/Cargo.toml index 2e68dbe6d..980174475 100644 --- a/crates/sol-macro/Cargo.toml +++ b/crates/sol-macro/Cargo.toml @@ -22,7 +22,6 @@ rustdoc-args = ["--cfg", "docsrs"] [dependencies] alloy-sol-macro-input.workspace = true - syn-solidity = { workspace = true, features = ["visit", "visit-mut"] } proc-macro2.workspace = true diff --git a/crates/sol-macro/src/lib.rs b/crates/sol-macro/src/lib.rs index 15fead089..08bd2c372 100644 --- a/crates/sol-macro/src/lib.rs +++ b/crates/sol-macro/src/lib.rs @@ -240,8 +240,11 @@ struct SolMacroExpander; impl SolInputExpander for SolMacroExpander { fn expand(&mut self, input: &SolInput) -> syn::Result { let input = input.clone(); - // Convert JSON input to Solidity input + #[cfg(feature = "json")] + let is_json = matches!(input.kind, SolInputKind::Json { .. }); + + // Convert JSON input to Solidity input #[cfg(feature = "json")] let input = input.normalize_json()?; @@ -253,7 +256,12 @@ impl SolInputExpander for SolMacroExpander { let tokens = match kind { SolInputKind::Sol(mut file) => { - file.attrs.extend(attrs); + // Attributes have already been added to the inner contract generated in + // `normalize_json`. + #[cfg(feature = "json")] + if !is_json { + file.attrs.extend(attrs); + } crate::expand::expand(file) } SolInputKind::Type(ty) => { diff --git a/crates/sol-types/tests/macros/sol/json.rs b/crates/sol-types/tests/macros/sol/json.rs index 8f58fbc3d..e1c7156f7 100644 --- a/crates/sol-types/tests/macros/sol/json.rs +++ b/crates/sol-types/tests/macros/sol/json.rs @@ -8,9 +8,14 @@ use std::borrow::Cow; fn large_array() { sol!( #[sol(abi)] + #[derive(Debug)] LargeArray, "../json-abi/tests/abi/LargeArray.json" ); + + let call = LargeArray::callWithLongArrayCall { longArray: [0; 128] }; + let _ = format!("{call:#?}"); + assert_eq!(LargeArray::callWithLongArrayCall::SIGNATURE, "callWithLongArray(uint64[128])"); let contract = LargeArray::abi::contract(); assert_eq!(