Skip to content

Commit

Permalink
chore: remove spans
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Sep 24, 2023
1 parent 363fea9 commit 704940e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
23 changes: 10 additions & 13 deletions crates/sol-macro/src/expand/event.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! [`ItemEvent`] expansion.

use super::{anon_name, expand_tuple_types, expand_type, ExpCtxt};
use crate::expand::ty::expand_event_tokenize_func;
use super::{anon_name, expand_tuple_types, expand_type, ty, ExpCtxt};
use ast::{EventParameter, ItemEvent, SolIdent, Spanned};
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
Expand Down Expand Up @@ -93,7 +92,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, event: &ItemEvent) -> Result<TokenStream>
.enumerate()
.map(|(i, p)| expand_event_topic_field(i, p, p.name.as_ref()));

let tokenize_body_impl = expand_event_tokenize_func(event.parameters.iter());
let tokenize_body_impl = ty::expand_event_tokenize_func(event.parameters.iter());

let encode_topics_impl = encode_first_topic
.into_iter()
Expand Down Expand Up @@ -176,15 +175,13 @@ fn expand_event_topic_field(
name: Option<&SolIdent>,
) -> TokenStream {
let name = anon_name((i, name));

if param.indexed_as_hash() {
quote! {
#name: <::alloy_sol_types::sol_data::FixedBytes<32> as ::alloy_sol_types::SolType>::RustType
}
let ty = if param.indexed_as_hash() {
ty::expand_rust_type(&ast::Type::FixedBytes(
name.span(),
core::num::NonZeroU16::new(32).unwrap(),
))
} else {
let ty = expand_type(&param.ty);
quote! {
#name: <#ty as ::alloy_sol_types::SolType>::RustType
}
}
ty::expand_rust_type(&param.ty)
};
quote!(#name: #ty)
}
14 changes: 8 additions & 6 deletions crates/sol-macro/src/expand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,9 @@ fn expand_from_into_tuples<P>(name: &Ident, fields: &Parameters<P>) -> TokenStre
let idxs = (0..fields.len()).map(syn::Index::from);

let names3 = names.clone();
let field_tys = fields.types().map(expand_type);
let field_tys = fields
.types()
.map(|ty| expand_type(ty).with_span(Span::call_site()));

let (sol_tuple, rust_tuple) = expand_tuple_types(fields.types());

Expand Down Expand Up @@ -506,9 +508,7 @@ fn expand_from_into_tuples<P>(name: &Ident, fields: &Parameters<P>) -> TokenStre
}
}

/// Returns
/// - `(#(#expanded,)*)`
/// - `(#(<#expanded as ::alloy_sol_types::SolType>::RustType,)*)`
/// Returns `(sol_tuple, rust_tuple)`
fn expand_tuple_types<'a, I: IntoIterator<Item = &'a Type>>(
types: I,
) -> (TokenStream, TokenStream) {
Expand All @@ -522,8 +522,10 @@ fn expand_tuple_types<'a, I: IntoIterator<Item = &'a Type>>(
ty::rec_expand_rust_type(ty, &mut rust);
rust.append(comma.clone());
}
let wrap_in_parens =
|stream| TokenStream::from(TokenTree::Group(Group::new(Delimiter::Parenthesis, stream)));
(
TokenStream::from(TokenTree::Group(Group::new(Delimiter::Parenthesis, sol))),
TokenStream::from(TokenTree::Group(Group::new(Delimiter::Parenthesis, rust))),
wrap_in_parens(sol).with_span(Span::call_site()),
wrap_in_parens(rust).with_span(Span::call_site()),
)
}
15 changes: 10 additions & 5 deletions crates/sol-macro/src/expand/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use super::{
expand_fields, expand_from_into_tuples, expand_type, ty::expand_tokenize_func, ExpCtxt,
};
use ast::{Item, ItemStruct, Spanned, Type, VariableDeclaration};
use proc_macro2::TokenStream;
use ast::{Item, ItemStruct, Spanned, Type};
use proc_macro2::{Span, TokenStream};
use quote::quote;
use std::num::NonZeroU16;
use syn::Result;
Expand Down Expand Up @@ -38,7 +38,12 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, s: &ItemStruct) -> Result<TokenStream> {

let (field_types, field_names): (Vec<_>, Vec<_>) = fields
.iter()
.map(|f| (expand_type(&f.ty), f.name.as_ref().unwrap()))
.map(|f| {
(
expand_type(&f.ty).with_span(Span::call_site()),
f.name.as_ref().unwrap(),
)
})
.unzip();

let eip712_encode_type_fns = expand_encode_type_fns(cx, fields, name);
Expand All @@ -48,8 +53,8 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, s: &ItemStruct) -> Result<TokenStream> {
let encode_data_impl = match fields.len() {
0 => unreachable!("struct with zero fields"),
1 => {
let VariableDeclaration { ty, name, .. } = fields.first().unwrap();
let ty = expand_type(ty);
let name = *field_names.first().unwrap();
let ty = field_types.first().unwrap();
quote!(<#ty as ::alloy_sol_types::SolType>::eip712_data_word(&self.#name).0.to_vec())
}
_ => quote! {
Expand Down
4 changes: 2 additions & 2 deletions crates/sol-macro/src/expand/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::ExpCtxt;
use crate::expand::generate_name;
use ast::{EventParameter, Item, Parameters, Spanned, Type, TypeArray, VariableDeclaration};
use proc_macro2::{Ident, Literal, TokenStream};
use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::{quote, quote_spanned, ToTokens};
use std::{fmt, num::NonZeroU16};

Expand All @@ -29,7 +29,7 @@ pub fn expand_rust_type(ty: &Type) -> TokenStream {
/// Expands a [`VariableDeclaration`] into an invocation of its types tokenize
/// method.
fn expand_tokenize_statement(var: &VariableDeclaration, i: usize) -> TokenStream {
let ty = expand_type(&var.ty);
let ty = expand_type(&var.ty).with_span(Span::call_site());
let name = var.name.clone().unwrap_or_else(|| generate_name(i).into());
quote! {
<#ty as ::alloy_sol_types::SolType>::tokenize(&self.#name)
Expand Down

0 comments on commit 704940e

Please sign in to comment.