Skip to content

Commit ac724c2

Browse files
authored
fix(forge): avoid preprocessor constructor args struct name conflict (#10313)
1 parent a85488a commit ac724c2

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

crates/common/src/preprocessor/data.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ impl ContractData {
141141
/// import "lib/openzeppelin-contracts/contracts/token/ERC20.sol";
142142
///
143143
/// abstract contract DeployHelper335 is ERC20 {
144-
/// struct ConstructorArgs {
144+
/// struct FoundryPpConstructorArgs {
145145
/// string name;
146146
/// string symbol;
147147
/// }
148148
/// }
149149
///
150-
/// function encodeArgs335(DeployHelper335.ConstructorArgs memory args) pure returns (bytes memory) {
150+
/// function encodeArgs335(DeployHelper335.FoundryPpConstructorArgs memory args) pure returns (bytes memory) {
151151
/// return abi.encode(args.name, args.symbol);
152152
/// }
153153
/// ```
@@ -158,15 +158,15 @@ impl ContractData {
158158
/// ```
159159
/// becomes
160160
/// ```solidity
161-
/// vm.deployCode("artifact path", encodeArgs335(DeployHelper335.ConstructorArgs(name, symbol)))
161+
/// vm.deployCode("artifact path", encodeArgs335(DeployHelper335.FoundryPpConstructorArgs(name, symbol)))
162162
/// ```
163163
/// With named arguments:
164164
/// ```solidity
165165
/// new ERC20({name: name, symbol: symbol})
166166
/// ```
167167
/// becomes
168168
/// ```solidity
169-
/// vm.deployCode("artifact path", encodeArgs335(DeployHelper335.ConstructorArgs({name: name, symbol: symbol})))
169+
/// vm.deployCode("artifact path", encodeArgs335(DeployHelper335.FoundryPpConstructorArgs({name: name, symbol: symbol})))
170170
/// ```
171171
pub fn build_helper(&self) -> Option<String> {
172172
let Self { contract_id, path, name, constructor_data, artifact: _ } = self;
@@ -183,12 +183,12 @@ pragma solidity >=0.4.0;
183183
import "{path}";
184184
185185
abstract contract DeployHelper{contract_id} is {name} {{
186-
struct ConstructorArgs {{
186+
struct FoundryPpConstructorArgs {{
187187
{struct_fields};
188188
}}
189189
}}
190190
191-
function encodeArgs{contract_id}(DeployHelper{contract_id}.ConstructorArgs memory args) pure returns (bytes memory) {{
191+
function encodeArgs{contract_id}(DeployHelper{contract_id}.FoundryPpConstructorArgs memory args) pure returns (bytes memory) {{
192192
return abi.encode({abi_encode_args});
193193
}}
194194
"#,

crates/common/src/preprocessor/deps.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ pub(crate) fn remove_bytecode_dependencies(
320320

321321
update.push_str(", ");
322322
update.push_str(&format!(
323-
"_args: encodeArgs{id}(DeployHelper{id}.ConstructorArgs",
323+
"_args: encodeArgs{id}(DeployHelper{id}.FoundryPpConstructorArgs",
324324
id = dep.referenced_contract.get()
325325
));
326326
if *call_args_offset > 0 {

crates/forge/tests/cli/test_optimizer.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,3 +1300,48 @@ Compiling 1 files with [..]
13001300
13011301
"#]]);
13021302
});
1303+
1304+
// <https://github.com/foundry-rs/foundry/issues/10312>
1305+
forgetest_init!(preprocess_contract_with_constructor_args_struct, |prj, cmd| {
1306+
prj.wipe_contracts();
1307+
prj.update_config(|config| {
1308+
config.dynamic_test_linking = true;
1309+
});
1310+
1311+
prj.add_source(
1312+
"Counter.sol",
1313+
r#"
1314+
contract Counter {
1315+
struct ConstructorArgs {
1316+
uint256 _number;
1317+
}
1318+
1319+
constructor(uint256 no) {
1320+
}
1321+
}
1322+
"#,
1323+
)
1324+
.unwrap();
1325+
1326+
prj.add_test(
1327+
"Counter.t.sol",
1328+
r#"
1329+
import {Test} from "forge-std/Test.sol";
1330+
import {Counter} from "../src/Counter.sol";
1331+
1332+
contract CounterTest is Test {
1333+
function test_assert_constructor_revert() public {
1334+
Counter counter = new Counter(1);
1335+
}
1336+
}
1337+
"#,
1338+
)
1339+
.unwrap();
1340+
// All 20 files should properly compile.
1341+
cmd.args(["test"]).with_no_redact().assert_success().stdout_eq(str![[r#"
1342+
...
1343+
Compiling 20 files with [..]
1344+
...
1345+
1346+
"#]]);
1347+
});

0 commit comments

Comments
 (0)