Skip to content

Commit 9589a49

Browse files
authored
fix: add fallback parser for contract names (#229)
Fixes after bump foundry-rs/foundry#9433
1 parent 569c3a1 commit 9589a49

File tree

7 files changed

+34
-10
lines changed

7 files changed

+34
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ cfg-if = "1.0"
4343
dunce = "1.0"
4444
md-5 = "0.10"
4545
memmap2 = "0.9"
46-
once_cell = "1.19"
4746
path-slash = "0.2"
4847
rayon = "1.8"
4948
regex = "1.10"
@@ -54,7 +53,7 @@ similar-asserts = "1"
5453
solar-parse = { version = "=0.1.0", default-features = false }
5554
svm = { package = "svm-rs", version = "0.5", default-features = false }
5655
tempfile = "3.9"
57-
thiserror = "1"
56+
thiserror = "2"
5857
tracing = "0.1"
5958
walkdir = "2.5"
6059
yansi = "1.0"

crates/compilers/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ thiserror.workspace = true
2929
path-slash.workspace = true
3030
yansi.workspace = true
3131
solar-parse.workspace = true
32-
once_cell = { workspace = true, optional = true }
3332
futures-util = { workspace = true, optional = true }
3433
tokio = { workspace = true, optional = true }
3534

@@ -85,7 +84,6 @@ svm-solc = [
8584
"dep:svm-builds",
8685
"dep:sha2",
8786
"foundry-compilers-core/svm-solc",
88-
"dep:once_cell",
8987
]
9088
# Utilities for creating and testing project workspaces.
9189
project-util = [

crates/compilers/src/compilers/solc/compiler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ macro_rules! take_solc_installer_lock {
4545
/// we should download.
4646
/// The boolean value marks whether there was an error accessing the release list
4747
#[cfg(feature = "svm-solc")]
48-
pub static RELEASES: once_cell::sync::Lazy<(svm::Releases, Vec<Version>, bool)> =
49-
once_cell::sync::Lazy::new(|| {
48+
pub static RELEASES: std::sync::LazyLock<(svm::Releases, Vec<Version>, bool)> =
49+
std::sync::LazyLock::new(|| {
5050
match serde_json::from_str::<svm::Releases>(svm_builds::RELEASE_LIST_JSON) {
5151
Ok(releases) => {
5252
let sorted_versions = releases.clone().into_versions();

crates/compilers/src/resolver/parse.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ impl SolData {
133133
if imports.is_empty() {
134134
imports = capture_imports(content);
135135
}
136+
if contract_names.is_empty() {
137+
utils::RE_CONTRACT_NAMES.captures_iter(content).for_each(|cap| {
138+
contract_names.push(cap[1].to_owned());
139+
});
140+
}
136141
}
137142
let license = content.lines().next().and_then(|line| {
138143
utils::capture_outer_and_inner(
@@ -313,6 +318,12 @@ mod tests {
313318
assert_eq!(data.version_req, version_req.map(|v| v.parse().unwrap()), "src:\n{src}");
314319
}
315320

321+
#[track_caller]
322+
fn assert_contract_names(names: &[&str], src: &str) {
323+
let data = SolData::parse(src, "test.sol".as_ref());
324+
assert_eq!(data.contract_names, names, "src:\n{src}");
325+
}
326+
316327
#[test]
317328
fn soldata_parsing() {
318329
assert_version(None, "");
@@ -332,6 +343,18 @@ contract BugReport {
332343
}
333344
"#,
334345
);
346+
347+
assert_contract_names(
348+
&["A", "B69$_", "C_", "$D"],
349+
r#"
350+
contract A {}
351+
library B69$_ {}
352+
abstract contract C_ {} interface $D {}
353+
354+
uint constant x = .1e10;
355+
uint constant y = .1 ether;
356+
"#,
357+
);
335358
}
336359

337360
#[test]

crates/compilers/tests/project.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use foundry_compilers_core::{
3030
error::SolcError,
3131
utils::{self, canonicalize, RuntimeOrHandle},
3232
};
33-
use once_cell::sync::Lazy;
3433
use semver::Version;
3534
use similar_asserts::assert_eq;
3635
use std::{
@@ -39,10 +38,11 @@ use std::{
3938
io,
4039
path::{Path, PathBuf, MAIN_SEPARATOR},
4140
str::FromStr,
41+
sync::LazyLock,
4242
};
4343
use svm::{platform, Platform};
4444

45-
pub static VYPER: Lazy<Vyper> = Lazy::new(|| {
45+
pub static VYPER: LazyLock<Vyper> = LazyLock::new(|| {
4646
RuntimeOrHandle::new().block_on(async {
4747
#[cfg(target_family = "unix")]
4848
use std::{fs::Permissions, os::unix::fs::PermissionsExt};

crates/core/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ workspace = true
1818
alloy-primitives.workspace = true
1919
cfg-if.workspace = true
2020
dunce.workspace = true
21-
once_cell.workspace = true
2221
path-slash.workspace = true
2322
regex.workspace = true
2423
semver.workspace = true

crates/core/src/utils.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use crate::error::{SolcError, SolcIoError};
44
use alloy_primitives::{hex, keccak256};
55
use cfg_if::cfg_if;
6-
use once_cell::sync::Lazy;
76
use regex::{Match, Regex};
87
use semver::{Version, VersionReq};
98
use serde::{de::DeserializeOwned, Serialize};
@@ -13,6 +12,7 @@ use std::{
1312
io::Write,
1413
ops::Range,
1514
path::{Component, Path, PathBuf},
15+
sync::LazyLock as Lazy,
1616
};
1717
use walkdir::WalkDir;
1818

@@ -46,6 +46,11 @@ pub static RE_THREE_OR_MORE_NEWLINES: Lazy<Regex> = Lazy::new(|| Regex::new("\n{
4646
pub static RE_VYPER_VERSION: Lazy<Regex> =
4747
Lazy::new(|| Regex::new(r"#(?:pragma version|@version)\s+(?P<version>.+)").unwrap());
4848

49+
/// A regex that matches the contract names in a Solidity file.
50+
pub static RE_CONTRACT_NAMES: Lazy<Regex> = Lazy::new(|| {
51+
Regex::new(r"\b(?:contract|library|abstract\s+contract|interface)\s+([\w$]+)").unwrap()
52+
});
53+
4954
/// Extensions acceptable by solc compiler.
5055
pub const SOLC_EXTENSIONS: &[&str] = &["sol", "yul"];
5156

0 commit comments

Comments
 (0)