Skip to content

Commit 64c5ecd

Browse files
committed
Use Path, add named args test
1 parent ebda056 commit 64c5ecd

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

crates/compilers/src/preprocessor/deps.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ enum BytecodeDependencyKind {
9696
/// `type(Contract).creationCode`
9797
CreationCode,
9898
/// `new Contract`. Holds the name of the contract and args length.
99-
New(String, usize),
99+
New(String, usize, usize),
100100
}
101101

102102
/// Represents a single bytecode dependency.
@@ -173,18 +173,26 @@ impl<'hir> Visit<'hir> for BytecodeDependencyCollector<'hir> {
173173

174174
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) -> ControlFlow<Self::BreakValue> {
175175
match &expr.kind {
176-
ExprKind::Call(ty, _, _) => {
176+
ExprKind::Call(ty, call_args, named_args) => {
177177
if let ExprKind::New(ty_new) = &ty.kind {
178178
if let TypeKind::Custom(item_id) = ty_new.kind {
179179
if let Some(contract_id) = item_id.as_contract() {
180180
let name_loc =
181181
SourceMapLocation::from_span(self.source_map, ty_new.span);
182182
let name = &self.src[name_loc.start..name_loc.end];
183+
184+
let offset = if named_args.is_some() && !call_args.is_empty() {
185+
(call_args.span().lo() - ty_new.span.hi()).to_usize() - 1
186+
} else {
187+
0
188+
};
189+
183190
let args_len = expr.span.hi() - ty_new.span.hi();
184191
self.collect_dependency(BytecodeDependency {
185192
kind: BytecodeDependencyKind::New(
186193
name.to_string(),
187194
args_len.to_usize(),
195+
offset,
188196
),
189197
loc: SourceMapLocation::from_span(self.source_map, ty.span),
190198
referenced_contract: contract_id,
@@ -252,7 +260,7 @@ pub(crate) fn remove_bytecode_dependencies(
252260
format!("{vm}.getCode(\"{artifact}\")"),
253261
));
254262
}
255-
BytecodeDependencyKind::New(name, args_length) => {
263+
BytecodeDependencyKind::New(name, args_length, offset) => {
256264
if constructor_data.is_none() {
257265
// if there's no constructor, we can just call deployCode with one
258266
// argument
@@ -266,7 +274,7 @@ pub(crate) fn remove_bytecode_dependencies(
266274
used_helpers.insert(dep.referenced_contract);
267275
updates.insert((
268276
dep.loc.start,
269-
dep.loc.end,
277+
dep.loc.end + offset,
270278
format!(
271279
"deployCode{id}(DeployHelper{id}.ConstructorArgs",
272280
id = dep.referenced_contract.get()

crates/compilers/src/preprocessor/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl Preprocessor<MultiCompiler> for TestOptimizerPreprocessor {
146146
}
147147

148148
/// Helper function to compute hash of [`interface_representation`] of the source.
149-
pub(crate) fn interface_representation_hash(source: &Source, file: &PathBuf) -> String {
149+
pub(crate) fn interface_representation_hash(source: &Source, file: &Path) -> String {
150150
let Ok(repr) = interface_representation(&source.content, file) else {
151151
return source.content_hash();
152152
};
@@ -161,10 +161,9 @@ pub(crate) fn interface_representation_hash(source: &Source, file: &PathBuf) ->
161161
/// - External functions bodies
162162
///
163163
/// Preserves all libraries and interfaces.
164-
fn interface_representation(content: &str, file: &PathBuf) -> Result<String, EmittedDiagnostics> {
164+
fn interface_representation(content: &str, file: &Path) -> Result<String, EmittedDiagnostics> {
165165
let mut spans_to_remove: Vec<Span> = Vec::new();
166-
let sess =
167-
solar_parse::interface::Session::builder().with_buffer_emitter(Default::default()).build();
166+
let sess = Session::builder().with_buffer_emitter(Default::default()).build();
168167
sess.enter(|| {
169168
let arena = solar_parse::ast::Arena::new();
170169
let filename = FileName::Real(file.to_path_buf());

test-data/preprocessor/test/CounterTest.sol

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ contract CounterTest {
88
Counter public counter;
99
Counter public counter2 = new Counter();
1010
CounterB public counter3 = new CounterB(address(this), 44, true, address(this));
11+
CounterB public counter4 = new CounterB({a:address(this), b: 44, c: true, d: address(this)});
1112
CounterV1 public counterv1;
13+
Counter public counter5 = new Counter{salt: bytes32("123")}();
14+
CounterB public counter6 = new CounterB {salt: bytes32("123")} (address(this), 44, true, address(this));
1215

1316
function setUp() public {
1417
counter = new Counter();

0 commit comments

Comments
 (0)