Skip to content

Commit f4eee43

Browse files
grandizzyzerosnacksDaniPopes
authored andcommitted
fix(coverage): allow ir-minimum for versions < 0.8.5 (foundry-rs#9341)
* fix(coverage): allow ir-minimum for versions < 0.8.5 * Fix * Remove 0.8.13 restriction, update message and sanitize for 0.8.4 if version cannot be detected * Update crates/forge/bin/cmd/coverage.rs Co-authored-by: DaniPopes <[email protected]> --------- Co-authored-by: zerosnacks <[email protected]> Co-authored-by: DaniPopes <[email protected]>
1 parent b19d487 commit f4eee43

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

crates/forge/bin/cmd/coverage.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use forge::{
1616
use foundry_cli::utils::{LoadConfig, STATIC_FUZZ_SEED};
1717
use foundry_common::{compile::ProjectCompiler, fs};
1818
use foundry_compilers::{
19-
artifacts::{sourcemap::SourceMap, CompactBytecode, CompactDeployedBytecode},
19+
artifacts::{sourcemap::SourceMap, CompactBytecode, CompactDeployedBytecode, SolcLanguage},
2020
Artifact, ArtifactId, Project, ProjectCompileOutput,
2121
};
2222
use foundry_config::{Config, SolcReq};
@@ -94,23 +94,12 @@ impl CoverageArgs {
9494
// Set up the project
9595
let mut project = config.create_project(false, false)?;
9696
if self.ir_minimum {
97-
// TODO: How to detect solc version if the user does not specify a solc version in
98-
// config case1: specify local installed solc ?
99-
// case2: multiple solc versions used and auto_detect_solc == true
100-
if let Some(SolcReq::Version(version)) = &config.solc {
101-
if *version < Version::new(0, 8, 13) {
102-
return Err(eyre::eyre!(
103-
"viaIR with minimum optimization is only available in Solidity 0.8.13 and above."
104-
));
105-
}
106-
}
107-
10897
// print warning message
10998
sh_warn!("{}", concat!(
110-
"Warning! \"--ir-minimum\" flag enables viaIR with minimum optimization, \
99+
"`--ir-minimum` enables viaIR with minimum optimization, \
111100
which can result in inaccurate source mappings.\n",
112101
"Only use this flag as a workaround if you are experiencing \"stack too deep\" errors.\n",
113-
"Note that \"viaIR\" is only available in Solidity 0.8.13 and above.\n",
102+
"Note that \"viaIR\" is production ready since Solidity 0.8.13 and above.\n",
114103
"See more: https://github.com/foundry-rs/foundry/issues/3357",
115104
))?;
116105

@@ -119,7 +108,15 @@ impl CoverageArgs {
119108
// And also in new releases of solidity:
120109
// https://github.com/ethereum/solidity/issues/13972#issuecomment-1628632202
121110
project.settings.solc.settings =
122-
project.settings.solc.settings.with_via_ir_minimum_optimization()
111+
project.settings.solc.settings.with_via_ir_minimum_optimization();
112+
let version = if let Some(SolcReq::Version(version)) = &config.solc {
113+
version
114+
} else {
115+
// Sanitize settings for solc 0.8.4 if version cannot be detected.
116+
// See <https://github.com/foundry-rs/foundry/issues/9322>.
117+
&Version::new(0, 8, 4)
118+
};
119+
project.settings.solc.settings.sanitize(version, SolcLanguage::Solidity);
123120
} else {
124121
project.settings.solc.optimizer.disable();
125122
project.settings.solc.optimizer.runs = None;

crates/forge/tests/cli/coverage.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,3 +1413,37 @@ contract AContractTest is DSTest {
14131413
14141414
"#]]);
14151415
});
1416+
1417+
// <https://github.com/foundry-rs/foundry/issues/9322>
1418+
// Test coverage with `--ir-minimum` for solidity < 0.8.5.
1419+
forgetest!(test_ir_minimum_coverage, |prj, cmd| {
1420+
prj.insert_ds_test();
1421+
prj.add_source(
1422+
"AContract.sol",
1423+
r#"
1424+
pragma solidity 0.8.4;
1425+
1426+
contract AContract {
1427+
function isContract(address account) internal view returns (bool) {
1428+
bytes32 codehash;
1429+
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
1430+
assembly {
1431+
codehash := extcodehash(account)
1432+
}
1433+
return (codehash != accountHash && codehash != 0x0);
1434+
}
1435+
}
1436+
"#,
1437+
)
1438+
.unwrap();
1439+
1440+
// Assert coverage doesn't fail with `Error: Unknown key "inliner"`.
1441+
cmd.arg("coverage").arg("--ir-minimum").assert_success().stdout_eq(str![[r#"
1442+
...
1443+
| File | % Lines | % Statements | % Branches | % Funcs |
1444+
|-------------------|-------------|--------------|---------------|-------------|
1445+
| src/AContract.sol | 0.00% (0/4) | 0.00% (0/4) | 100.00% (0/0) | 0.00% (0/1) |
1446+
| Total | 0.00% (0/4) | 0.00% (0/4) | 100.00% (0/0) | 0.00% (0/1) |
1447+
1448+
"#]]);
1449+
});

0 commit comments

Comments
 (0)