Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions crates/linking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use alloy_primitives::{Address, B256, Bytes};
use foundry_compilers::{
Artifact, ArtifactId,
artifacts::{CompactContractBytecodeCow, Libraries},
artifacts::{CompactBytecode, CompactContractBytecodeCow, Libraries},
contracts::ArtifactContracts,
};
use rayon::prelude::*;
Expand Down Expand Up @@ -105,23 +105,28 @@ impl<'a> Linker<'a> {
) -> Result<(), LinkerError> {
let contract = self.contracts.get(target).ok_or(LinkerError::MissingTargetArtifact)?;

let mut references = BTreeMap::new();
let mut references: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
let mut extend = |bytecode: &CompactBytecode| {
for (file, libs) in &bytecode.link_references {
references.entry(file.clone()).or_default().extend(libs.keys().cloned());
}
};
if let Some(bytecode) = &contract.bytecode {
references.extend(bytecode.link_references.clone());
extend(bytecode);
}
if let Some(deployed_bytecode) = &contract.deployed_bytecode
&& let Some(bytecode) = &deployed_bytecode.bytecode
{
references.extend(bytecode.link_references.clone());
extend(bytecode);
}

for (file, libs) in &references {
for contract in libs.keys() {
for (file, libs) in references {
for name in libs {
let id = self
.find_artifact_id_by_library_path(file, contract, Some(&target.version))
.find_artifact_id_by_library_path(&file, &name, Some(&target.version))
.ok_or_else(|| LinkerError::MissingLibraryArtifact {
file: file.to_string(),
name: contract.to_string(),
file: file.clone(),
name,
})?;
if deps.insert(id) {
self.collect_dependencies(id, deps)?;
Expand Down Expand Up @@ -744,6 +749,31 @@ mod tests {
});
}

#[test]
fn link_samefile_union() {
link_test(testdata().join("default/linking/samefile_union"), |linker| {
linker
.assert_dependencies("default/linking/samefile_union/Libs.sol:LInit", &[])
.assert_dependencies("default/linking/samefile_union/Libs.sol:LRun", &[])
.assert_dependencies(
"default/linking/samefile_union/SameFileUnion.t.sol:UsesBoth",
&[
(
"default/linking/samefile_union/Libs.sol:LInit",
Address::from_str("0x5a443704dd4b594b382c22a083e2bd3090a6fef3")
.unwrap(),
),
(
"default/linking/samefile_union/Libs.sol:LRun",
Address::from_str("0x47e9fbef8c83a1714f1951f142132e6e90f5fa5d")
.unwrap(),
),
],
)
.test_with_sender_and_nonce(Address::default(), 1);
});
}

#[test]
fn linking_failure() {
let linker = LinkerTest::new(&testdata().join("default/linking/simple"), true);
Expand Down
14 changes: 14 additions & 0 deletions testdata/default/linking/samefile_union/Libs.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;

library LInit {
function f() external view returns (uint256) {
return block.number;
}
}

library LRun {
function g() external view returns (uint256) {
return block.timestamp;
}
}
18 changes: 18 additions & 0 deletions testdata/default/linking/samefile_union/SameFileUnion.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;

import "./Libs.sol";

contract UsesBoth {
uint256 public x;

constructor() {
// used only in creation bytecode
x = LInit.f();
}

function y() external view returns (uint256) {
// used only in deployed bytecode
return LRun.g();
}
}
Loading