Skip to content

Commit 6b0c17b

Browse files
authored
fix(syn-solidity): correctly parse invalid bytes* etc as custom (#830)
1 parent ea88d20 commit 6b0c17b

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

crates/sol-types/tests/macros/sol/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,3 +1223,22 @@ fn mapping_getters() {
12231223
let _ =
12241224
TestIbc::channelsReturn { _0: 0u8, _1: 0u32, _2: 0u32, _3: bytes![], _4: String::new() };
12251225
}
1226+
1227+
// https://github.com/alloy-rs/core/issues/829
1228+
#[test]
1229+
fn bytes64() {
1230+
sol! {
1231+
struct bytes64 {
1232+
bytes32 a;
1233+
bytes32 b;
1234+
}
1235+
1236+
function f(bytes64 x) public returns(bytes64 y) {
1237+
bytes64 z = x;
1238+
y = z;
1239+
}
1240+
}
1241+
1242+
let x = bytes64 { a: B256::ZERO, b: B256::ZERO };
1243+
assert_eq!(bytes64::abi_encode_packed(&x), alloy_primitives::B512::ZERO.as_slice());
1244+
}

crates/syn-solidity/src/expr/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,11 @@ impl Expr {
222222
input.parse().map(Self::Delete)
223223
} else if lookahead.peek(Ident::peek_any) {
224224
let ident = input.call(Ident::parse_any)?;
225-
match Type::parse_ident(ident.clone()) {
226-
Ok(ty) if !ty.is_custom() => ty.parse_payable(input).map(Self::Type),
227-
_ => Ok(Self::Ident(ident.into())),
225+
let ty = Type::parse_ident(ident.clone()).parse_payable(input);
226+
if ty.is_custom() {
227+
Ok(Self::Ident(ident.into()))
228+
} else {
229+
Ok(Self::Type(ty))
228230
}
229231
} else {
230232
Err(lookahead.error())

crates/syn-solidity/src/type/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ impl Type {
228228
/// keyword separately.
229229
///
230230
/// [ref]: https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.elementaryTypeName
231-
pub fn parse_ident(ident: Ident) -> Result<Self> {
231+
pub fn parse_ident(ident: Ident) -> Self {
232+
Self::try_parse_ident(ident.clone()).unwrap_or_else(|_| Self::custom(ident))
233+
}
234+
235+
pub fn try_parse_ident(ident: Ident) -> Result<Self> {
232236
let span = ident.span();
233237
let s = ident.to_string();
234238
let ret = match s.as_str() {
@@ -271,11 +275,11 @@ impl Type {
271275

272276
/// Parses the `payable` keyword from the input stream if this type is an
273277
/// address.
274-
pub fn parse_payable(mut self, input: ParseStream<'_>) -> Result<Self> {
278+
pub fn parse_payable(mut self, input: ParseStream<'_>) -> Self {
275279
if let Self::Address(_, opt @ None) = &mut self {
276-
*opt = input.parse()?;
280+
*opt = input.parse().unwrap();
277281
}
278-
Ok(self)
282+
self
279283
}
280284

281285
/// Returns whether this type is ABI-encoded as a single EVM word (32 bytes).
@@ -478,7 +482,7 @@ impl Type {
478482
input.parse().map(Self::Custom)
479483
} else if input.peek(Ident::peek_any) {
480484
let ident = input.call(Ident::parse_any)?;
481-
Self::parse_ident(ident)?.parse_payable(input)
485+
Ok(Self::parse_ident(ident).parse_payable(input))
482486
} else {
483487
Err(input.error(
484488
"expected a Solidity type: \

0 commit comments

Comments
 (0)