foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.35k stars 1.77k forks source link

Bug: `forge bind` fails to compile on keyword `enum` (alloy) #5847

Open 0xJepsen opened 1 year ago

0xJepsen commented 1 year ago

Component

Forge

Have you ensured that all of these are up to date?

What version of Foundry are you on?

forge 0.2.0 (ecf9a10 2023-09-18T00:24:28.742120000Z)

What command(s) is the bug in?

forge bind

Operating System

macOS (Apple Silicon)

Describe the bug

To reproduce the bug run

forge init bug_repro
cd bug_repro
forge install ethereum-optimism/optimism@develop
cd lib/optimsim/packages/contracts-bedrock
forge bind

then in the project root run

cargo init --bin

and add foundry-contracts = { path = "lib/optimism/packages/contracts-bedrock/forge-artifacts/bindings" } to the cargo.toml

at this point the static analyzer will pick up the compile errors in the bindings. but you can verify by attempting to import them to the main file with use foundry_contracts::l2_output_oracle;

the bindings are mostly correct but generate duplicate struct definitions for the https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L1/L2OutputOracle.sol contract that then implicate the https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L1/OptimismPortal.sol

From what i can tell the duplicate structs are in fact identical.

Desired functionality would be for forge bind to not produce codegen that doesn't compile.

zerosnacks commented 5 months ago

Able to reproduce with given instructions

forge bind (ethers) generates bindings with the duplicate struct definitions as reported.

Unfortunately forge bind --alloy fails to generate with:

Generating bindings for 394 contracts
Error: 
expected identifier, found keyword `enum`

cc @DaniPopes + @yash-atreya could be a good setup for stress testing Alloy's binding generation

Created a repo with instructions here to simplify reproduction: https://github.com/zerosnacks/foundry-bug-5847-repro

mattsse commented 5 months ago

closing since this is legacy ethers bind

reopen because alloy bind also fails -.-

zerosnacks commented 1 week ago

Confirming this is still an active issue with Alloy, keeping it in the milestone

Updating title to scope to Alloy only

Generating bindings for 525 contracts
Error: expected identifier, found keyword `enum`

cc @yash-atreya / @DaniPopes

DaniPopes commented 1 week ago

This is because one of the generated modules is named enum which is a keyword in Rust. The fix is to prefix invalid module names with r#. We can either do it unconditionally or first check if the identifier is invalid