Skip to content

Commit 322e80a

Browse files
committed
Handle named args with call args and offset
1 parent 6a81147 commit 322e80a

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

crates/compilers/src/preprocessor/deps.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ impl PreprocessorDependencies {
9595
enum BytecodeDependencyKind {
9696
/// `type(Contract).creationCode`
9797
CreationCode,
98-
/// `new Contract`. Holds the name of the contract, args length and offset, `msg.value` (if
99-
/// any) and salt (if any).
98+
/// `new Contract`. Holds the name of the contract, args length and call args offset,
99+
/// `msg.value` (if any) and salt (if any).
100100
New(String, usize, usize, Option<String>, Option<String>),
101101
}
102102

@@ -182,8 +182,12 @@ impl<'hir> Visit<'hir> for BytecodeDependencyCollector<'hir> {
182182
SourceMapLocation::from_span(self.source_map, ty_new.span);
183183
let name = &self.src[name_loc.start..name_loc.end];
184184

185-
let offset = if named_args.is_some() && !call_args.is_empty() {
186-
(call_args.span().lo() - ty_new.span.hi()).to_usize() - 1
185+
// Calculate offset to remove named args, e.g. for an expression like
186+
// `new Counter {value: 333} ( address(this))`
187+
// the offset will be used to replace `{value: 333} ( ` with `(`
188+
let call_args_offset = if named_args.is_some() && !call_args.is_empty()
189+
{
190+
(call_args.span().lo() - ty_new.span.hi()).to_usize()
187191
} else {
188192
0
189193
};
@@ -193,7 +197,7 @@ impl<'hir> Visit<'hir> for BytecodeDependencyCollector<'hir> {
193197
kind: BytecodeDependencyKind::New(
194198
name.to_string(),
195199
args_len.to_usize(),
196-
offset,
200+
call_args_offset,
197201
named_arg(self.src, named_args, "value", self.source_map),
198202
named_arg(self.src, named_args, "salt", self.source_map),
199203
),
@@ -278,7 +282,7 @@ pub(crate) fn remove_bytecode_dependencies(
278282
format!("{vm}.getCode(\"{artifact}\")"),
279283
));
280284
}
281-
BytecodeDependencyKind::New(name, args_length, offset, value, salt) => {
285+
BytecodeDependencyKind::New(name, args_length, call_args_offset, value, salt) => {
282286
let mut update = format!("{name}(payable({vm}.deployCode({{");
283287
update.push_str(&format!("_artifact: \"{artifact}\""));
284288

@@ -301,7 +305,10 @@ pub(crate) fn remove_bytecode_dependencies(
301305
"_args: encodeArgs{id}(DeployHelper{id}.ConstructorArgs",
302306
id = dep.referenced_contract.get()
303307
));
304-
updates.insert((dep.loc.start, dep.loc.end + offset, update));
308+
if *call_args_offset > 0 {
309+
update.push('(');
310+
}
311+
updates.insert((dep.loc.start, dep.loc.end + call_args_offset, update));
305312
updates.insert((
306313
dep.loc.end + args_length,
307314
dep.loc.end + args_length,

test-data/preprocessor/test/CounterTest.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ contract CounterTest {
1212
CounterB public counter4 = new CounterB({a:address(this), b: 44, c: true, d: address(this)});
1313
CounterV1 public counterv1;
1414
Counter public counter5 = new Counter{salt: bytes32("123")}();
15-
CounterB public counter6 = new CounterB {salt: bytes32("123")} (address(this), 44, true, address(this));
15+
CounterB public counter6 = new CounterB {salt: bytes32("123")} ( address(this), 44, true, address(this));
1616
CounterE public counter7 = new CounterE{ value: 111, salt: bytes32("123")}();
1717
CounterF public counter8 = new CounterF{value: 222, salt: bytes32("123")}(11);
18-
CounterG public counter9 = new CounterG { value: 333, salt: bytes32("123") } (address(this));
18+
CounterG public counter9 = new CounterG { value: 333, salt: bytes32("123") } ( address(this));
1919
CounterG public counter10 = new CounterG{ value: 333 }(address(this));
2020

2121
function setUp() public {

0 commit comments

Comments
 (0)