diff --git a/crates/forge/tests/cli/cmd.rs b/crates/forge/tests/cli/cmd.rs index 35a7bd4411a7..1f82a6be95e2 100644 --- a/crates/forge/tests/cli/cmd.rs +++ b/crates/forge/tests/cli/cmd.rs @@ -3519,3 +3519,34 @@ Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests) .is_json(), ); }); + +// +forgetest_init!(can_bind_enum_modules, |prj, cmd| { + prj.clear(); + + prj.add_source( + "Enum.sol", + r#" + contract Enum { + enum MyEnum { A, B, C } + } + "#, + ) + .unwrap(); + + prj.add_source( + "UseEnum.sol", + r#" + import "./Enum.sol"; + contract UseEnum { + Enum.MyEnum public myEnum; + }"#, + ) + .unwrap(); + + cmd.arg("bind").assert_success().stdout_eq(str![[r#"[COMPILING_FILES] with [SOLC_VERSION] +[SOLC_VERSION] [ELAPSED] +Compiler run successful! +Generating bindings for 11 contracts +Bindings have been generated to [..]"#]]); +}); diff --git a/crates/sol-macro-gen/src/sol_macro_gen.rs b/crates/sol-macro-gen/src/sol_macro_gen.rs index 8c146aa05dda..9b907eb3ad08 100644 --- a/crates/sol-macro-gen/src/sol_macro_gen.rs +++ b/crates/sol-macro-gen/src/sol_macro_gen.rs @@ -165,7 +165,7 @@ edition = "2021" write!(&mut lib_contents, "{contents}")?; } else { fs::write(path, contents).wrap_err("failed to write to file")?; - writeln!(&mut lib_contents, "pub mod {name};")?; + write_mod_name(&mut lib_contents, &name)?; } } @@ -194,12 +194,7 @@ edition = "2021" let name = instance.name.to_lowercase(); if !single_file { // Module - write!( - mod_contents, - r#"pub mod {}; - "#, - instance.name.to_lowercase() - )?; + write_mod_name(&mut mod_contents, &name)?; let mut contents = String::new(); write!(contents, "{}", instance.expansion.as_ref().unwrap())?; @@ -270,12 +265,7 @@ edition = "2021" .to_string(); self.check_file_contents(&path, &tokens)?; - - write!( - &mut super_contents, - r#"pub mod {name}; - "# - )?; + write_mod_name(&mut super_contents, &name)?; } let super_path = @@ -344,3 +334,12 @@ edition = "2021" Ok(()) } } + +fn write_mod_name(contents: &mut String, name: &str) -> Result<()> { + if syn::parse_str::(&format!("pub mod {name};")).is_ok() { + write!(contents, "pub mod {name};")?; + } else { + write!(contents, "pub mod r#{name};")?; + } + Ok(()) +}