From b0698bbe0ed8a3d0799845a12e8f1ec3fbc69144 Mon Sep 17 00:00:00 2001 From: grandizzy <38490174+grandizzy@users.noreply.github.com> Date: Tue, 26 Mar 2024 16:37:30 +0200 Subject: [PATCH] fix(config): inline config intermingled (#7496) fix(config): inline config intermingled when multiple contracts in same file --- crates/config/src/inline/natspec.rs | 51 +++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/crates/config/src/inline/natspec.rs b/crates/config/src/inline/natspec.rs index 3b62d40cc1c2..27742eb56e4b 100644 --- a/crates/config/src/inline/natspec.rs +++ b/crates/config/src/inline/natspec.rs @@ -189,13 +189,13 @@ impl SolangParser { } let Ok((pt, comments)) = solang_parser::parse(src, 0) else { return }; - let mut prev_end = 0; for item in &pt.0 { let pt::SourceUnitPart::ContractDefinition(c) = item else { continue }; let Some(id) = c.name.as_ref() else { continue }; if id.name != contract_name { continue }; + let mut prev_end = c.loc.start(); for part in &c.parts { let pt::ContractPart::FunctionDefinition(f) = part else { continue }; let start = f.loc.start(); @@ -215,7 +215,6 @@ impl SolangParser { } prev_end = f.loc.end(); } - prev_end = c.loc.end(); } } } @@ -401,4 +400,52 @@ contract FuzzInlineConf is DSTest { docs: conf.to_string(), } } + + #[test] + fn parse_solang_multiple_contracts_from_same_file() { + let src = r#" +// SPDX-License-Identifier: MIT OR Apache-2.0 +pragma solidity >=0.8.0; + +import "ds-test/test.sol"; + +contract FuzzInlineConf is DSTest { + /// forge-config: default.fuzz.runs = 1 + function testInlineConfFuzz1() {} +} + +contract FuzzInlineConf2 is DSTest { + /// forge-config: default.fuzz.runs = 2 + function testInlineConfFuzz2() {} +} + "#; + let mut natspecs = vec![]; + let solang = SolangParser::new(); + let id = || "inline/FuzzInlineConf.t.sol:FuzzInlineConf".to_string(); + let default_line = || "0:0:0".to_string(); + solang.parse(&mut natspecs, src, &id(), "FuzzInlineConf"); + assert_eq!( + natspecs, + [NatSpec { + contract: id(), + function: "testInlineConfFuzz1".to_string(), + line: default_line(), + docs: "forge-config: default.fuzz.runs = 1".to_string(), + },] + ); + + let mut natspecs = vec![]; + let id = || "inline/FuzzInlineConf2.t.sol:FuzzInlineConf2".to_string(); + solang.parse(&mut natspecs, src, &id(), "FuzzInlineConf2"); + assert_eq!( + natspecs, + [NatSpec { + contract: id(), + function: "testInlineConfFuzz2".to_string(), + line: default_line(), + // should not get config from previous contract + docs: "forge-config: default.fuzz.runs = 2".to_string(), + },] + ); + } }