Skip to content

Commit

Permalink
fix: allow top level event declarations (#251)
Browse files Browse the repository at this point in the history
- ref foundry-rs/foundry#9876: add
ErrorDefinition as part of SourceUnitPart group
  • Loading branch information
grandizzy authored Feb 14, 2025
1 parent 6acbb69 commit 9fbda06
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/artifacts/solc/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ node_group! {
VariableDeclaration,
EnumDefinition,
ErrorDefinition,
EventDefinition,
FunctionDefinition,
StructDefinition,
UserDefinedValueTypeDefinition,
Expand Down
3 changes: 3 additions & 0 deletions crates/artifacts/solc/src/ast/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ impl_walk!(SourceUnitPart, |part, visitor| {
SourceUnitPart::ErrorDefinition(error) => {
error.walk(visitor);
}
SourceUnitPart::EventDefinition(event) => {
event.walk(visitor);
}
SourceUnitPart::StructDefinition(struct_) => {
struct_.walk(visitor);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/compilers/src/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl Flattener {
let mut ids = ids.clone().into_iter().collect::<Vec<_>>();
if needs_rename {
// `loc.path` is expected to be different for each id because there can't be 2
// top-level eclarations with the same name in the same file.
// top-level declarations with the same name in the same file.
//
// Sorting by index loc.path in sorted files to make the renaming process
// deterministic.
Expand Down
41 changes: 41 additions & 0 deletions crates/compilers/tests/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4113,3 +4113,44 @@ contract SimpleContract {}
]
);
}

// <https://github.com/foundry-rs/foundry/issues/9876>
#[test]
fn can_flatten_top_level_event_declaration() {
let project = TempProject::<MultiCompiler>::dapptools().unwrap();

let target = project
.add_source(
"A",
r#"pragma solidity ^0.8.10;
import "./B.sol";
contract A { }
"#,
)
.unwrap();

project
.add_source(
"B",
r#"
event TestEvent();
"#,
)
.unwrap();

test_flatteners(&project, &target, |result| {
assert_eq!(
result,
r"pragma solidity ^0.8.10;
// src/B.sol
event TestEvent();
// src/A.sol
contract A { }
"
);
});
}

0 comments on commit 9fbda06

Please sign in to comment.