Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forge bind): prefix keyword mod names with r# #9761

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions crates/forge/tests/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3519,3 +3519,34 @@ Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests)
.is_json(),
);
});

// <https://github.com/foundry-rs/foundry/issues/5847>
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 [..]"#]]);
});
27 changes: 14 additions & 13 deletions crates/sol-macro-gen/src/sol_macro_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
}

Expand Down Expand Up @@ -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())?;
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -344,3 +334,14 @@ edition = "2021"
Ok(())
}
}

fn write_mod_name(contents: &mut String, name: &str) -> Result<()> {
if syn::parse_str::<syn::File>(&format!("pub mod {name};"))
.is_err_and(|e| e.to_string().contains("expected identifier, found keyword"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should do it, alternatively we could try to parse name as Ident perhaps?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be syn::parse_str::<Ident>().is_ok()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
write!(contents, "pub mod r#{name};")?;
} else {
write!(contents, "pub mod {name};")?;
}
Ok(())
}
Loading