Skip to content

Commit cb0f1d8

Browse files
committed
Handle vars without name in ctor
1 parent af7cc59 commit cb0f1d8

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

crates/compilers/src/preprocessor/data.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::preprocessor::SourceMapLocation;
22
use foundry_compilers_artifacts::{Source, Sources};
3-
use itertools::Itertools;
43
use solar_parse::interface::{Session, SourceMap};
54
use solar_sema::{
65
hir::{Contract, ContractId, Hir},
@@ -92,22 +91,30 @@ impl ContractData {
9291
.map(|ctor_id| hir.function(ctor_id))
9392
.filter(|ctor| !ctor.parameters.is_empty())
9493
.map(|ctor| {
95-
let abi_encode_args = ctor
96-
.parameters
97-
.iter()
98-
.map(|param_id| format!("args.{}", hir.variable(*param_id).name.unwrap().name))
99-
.join(", ");
100-
let struct_fields = ctor
101-
.parameters
102-
.iter()
103-
.map(|param_id| {
104-
let src = source.file.src.as_str();
105-
let loc =
106-
SourceMapLocation::from_span(source_map, hir.variable(*param_id).span);
107-
src[loc.start..loc.end].replace(" memory ", " ").replace(" calldata ", " ")
108-
})
109-
.join("; ");
110-
ContractConstructorData { abi_encode_args, struct_fields }
94+
let mut abi_encode_args = vec![];
95+
let mut struct_fields = vec![];
96+
let mut arg_index = 0;
97+
for param_id in ctor.parameters {
98+
let src = source.file.src.as_str();
99+
let loc =
100+
SourceMapLocation::from_span(source_map, hir.variable(*param_id).span);
101+
let mut new_src =
102+
src[loc.start..loc.end].replace(" memory ", " ").replace(" calldata ", " ");
103+
if let Some(ident) = hir.variable(*param_id).name {
104+
abi_encode_args.push(format!("args.{}", ident.name));
105+
} else {
106+
// Generate an unique name if constructor arg doesn't have one.
107+
arg_index += 1;
108+
abi_encode_args.push(format!("args.foundry_pp_ctor_arg{arg_index}"));
109+
new_src.push_str(&format!(" foundry_pp_ctor_arg{arg_index}"));
110+
}
111+
struct_fields.push(new_src);
112+
}
113+
114+
ContractConstructorData {
115+
abi_encode_args: abi_encode_args.join(", "),
116+
struct_fields: struct_fields.join("; "),
117+
}
111118
});
112119

113120
Self {

crates/compilers/src/preprocessor/deps.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::{
88
use itertools::Itertools;
99
use solar_parse::interface::Session;
1010
use solar_sema::{
11-
ast::Span,
1211
hir::{ContractId, Expr, ExprKind, Hir, TypeKind, Visit},
1312
interface::{data_structures::Never, source_map::FileName, SourceMap},
1413
};
@@ -184,10 +183,7 @@ impl<'hir> Visit<'hir> for BytecodeDependencyCollector<'hir> {
184183
let args_len = self.src[name_loc.end..].split_once(';').unwrap().0.len();
185184
self.collect_dependency(BytecodeDependency {
186185
kind: BytecodeDependencyKind::New(name.to_string(), args_len),
187-
loc: SourceMapLocation::from_span(
188-
self.source_map,
189-
expr.span,
190-
),
186+
loc: SourceMapLocation::from_span(self.source_map, expr.span),
191187
referenced_contract: contract_id,
192188
});
193189
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Contract with constructor args without name
2+
contract CounterD {
3+
constructor(address, uint256 x, uint256) {}
4+
}

test-data/preprocessor/test/CounterTest.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Counter} from "src/Counter.sol";
22
import {Counter as CounterV1} from "src/v1/Counter.sol";
33
import "src/CounterB.sol";
44
import "src/CounterC.sol";
5+
import "src/CounterD.sol";
56

67
contract CounterTest {
78
Counter public counter;
@@ -19,5 +20,6 @@ contract CounterTest {
1920
35,
2021
address(this)
2122
);
23+
CounterD counterD = new CounterD(address(this), 15, 15);
2224
}
2325
}

0 commit comments

Comments
 (0)