Skip to content

Commit

Permalink
enforce no empty structs in parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed May 8, 2023
1 parent 8ce0e4c commit 29fdc4e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
4 changes: 3 additions & 1 deletion abi/sol-type-parser/src/common/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ impl Parse for Parameters<Token![,]> {
impl Parse for Parameters<Token![;]> {
fn parse(input: ParseStream) -> Result<Self> {
let this = input.parse_terminated(VariableDeclaration::parse_for_struct, Token![;])?;
if !this.is_empty() && !this.trailing_punct() {
if this.is_empty() {
Err(input.error("defining empty structs is disallowed"))
} else if !this.trailing_punct() {
Err(input.error("expected trailing semicolon"))
} else {
Ok(Self(this))
Expand Down
9 changes: 2 additions & 7 deletions abi/sol-type-parser/src/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
r#type::Type,
};
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use quote::quote;
use std::fmt;
use syn::{
braced,
Expand Down Expand Up @@ -75,7 +75,7 @@ impl Struct {
};

let encode_data_impl = match self.fields.len() {
0 => quote! { vec![] },
0 => unreachable!(),
1 => {
let VariableDeclaration { ty, name, .. } = self.fields.first().unwrap();
quote!(<#ty as ::ethers_abi_enc::SolDataType>::eip712_data_word(&self.#name).0.to_vec())
Expand Down Expand Up @@ -136,11 +136,6 @@ impl Struct {
}

pub fn to_tokens(&self, tokens: &mut TokenStream, attrs: &[Attribute]) {
if self.fields.is_empty() {
tokens.extend(quote_spanned! {self.name.span()=>
compile_error!("defining empty structs is disallowed.");
});
}
tokens.extend(self.expand_impl(attrs))
}

Expand Down
6 changes: 3 additions & 3 deletions abi/tests/ui/empty.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ error: unexpected end of input, expected at least one of: `type`, `struct`, `fun
|
= note: this error originates in the macro `sol` (in Nightly builds, run with -Z macro-backtrace for more info)

error: defining empty structs is disallowed.
--> tests/ui/empty.rs:6:12
error: unexpected end of input, defining empty structs is disallowed
--> tests/ui/empty.rs:6:25
|
6 | struct EmptyStruct {}
| ^^^^^^^^^^^
| ^

0 comments on commit 29fdc4e

Please sign in to comment.