From e7a81247868e17abe65ca18e384533ee36caeb57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Gir=C3=A1ldez?= Date: Thu, 25 Jul 2024 18:35:52 -0400 Subject: [PATCH] Allow alternate binding assertion anchor for targets at the same column as the comment --- .../src/bindings_assertions/assertions.rs | 19 ++++++++++++++++--- .../contracts/visibility.sol | 4 ++-- .../enums/in_state_vars.sol | 2 +- .../expressions/function_call.sol | 2 +- .../interfaces/visibility.sol | 4 ++-- .../scoping/c99_scopes.sol | 2 +- .../scoping/hoisting_scopes.sol | 2 +- .../variables/destructuring.sol | 2 ++ .../bindings_assertions/variables/params.sol | 2 +- 9 files changed, 27 insertions(+), 12 deletions(-) diff --git a/crates/solidity/outputs/cargo/tests/src/bindings_assertions/assertions.rs b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/assertions.rs index a8a172cf7..564073e09 100644 --- a/crates/solidity/outputs/cargo/tests/src/bindings_assertions/assertions.rs +++ b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/assertions.rs @@ -127,7 +127,7 @@ impl<'a> fmt::Display for DisplayCursor<'a> { /// uint x; /// // ^def:1 /// -/// asserts that at the CST node above the caret symbol (same column, not +/// asserts that at the CST node above the caret symbol `^` (same column, not /// necessarily in the previous line), the identifier `x` should create a /// binding definition, and assigns it an ID of '1'. /// @@ -142,6 +142,13 @@ impl<'a> fmt::Display for DisplayCursor<'a> { /// '1'; and that the CST identifier node `y` should be a binding reference that /// is unresolved for version at or above 0.5.0. /// +/// For assertion targets that are located at the column where the comment +/// begins the alternative anchor `<` can be used. For example: +/// +/// x = y + 1; +/// // ^ref:2 +/// // Result { let mut assertions = Assertions::new(); @@ -173,7 +180,7 @@ enum Assertion { } static ASSERTION_REGEX: Lazy = Lazy::new(|| { - Regex::new(r"[\^](?ref|def):(?[0-9a-zA-Z_-]+|!)([\t ]*\((?[^)]+)\))?") + Regex::new(r"(?[\^]|[<])(?ref|def):(?[0-9a-zA-Z_-]+|!)([\t ]*\((?[^)]+)\))?") .unwrap() }); @@ -191,7 +198,13 @@ fn find_assertion_in_comment( let assertion_id = captures.name("id").unwrap().as_str(); let assertion_type = captures.name("type").unwrap().as_str(); - let assertion_col = comment_col + captures.get(0).unwrap().start(); + let assertion_anchor = captures.name("anchor").unwrap(); + let assertion_col = comment_col + + if assertion_anchor.as_str() == "^" { + assertion_anchor.start() + } else { + 0 + }; let version_req = match captures.name("version") { Some(version) => { let Ok(version_req) = VersionReq::parse(version.as_str()) else { diff --git a/crates/solidity/testing/snapshots/bindings_assertions/contracts/visibility.sol b/crates/solidity/testing/snapshots/bindings_assertions/contracts/visibility.sol index b15deda3d..998553d09 100644 --- a/crates/solidity/testing/snapshots/bindings_assertions/contracts/visibility.sol +++ b/crates/solidity/testing/snapshots/bindings_assertions/contracts/visibility.sol @@ -3,7 +3,7 @@ contract First { enum Choice { One, Two } // ^def:2 Choice choice; -// ^ref:2 + //=0.5.0) // ^ref:6 (<0.5.0) // ^ref:1 diff --git a/crates/solidity/testing/snapshots/bindings_assertions/scoping/hoisting_scopes.sol b/crates/solidity/testing/snapshots/bindings_assertions/scoping/hoisting_scopes.sol index 8bfecc7ad..ba4c63ee2 100644 --- a/crates/solidity/testing/snapshots/bindings_assertions/scoping/hoisting_scopes.sol +++ b/crates/solidity/testing/snapshots/bindings_assertions/scoping/hoisting_scopes.sol @@ -3,7 +3,7 @@ contract Foo { function bar() returns (int x) { // ^def:1 x = y + z; -// ^ref:1 + //=0.5.0) // ^ref:2 (<0.5.0) diff --git a/crates/solidity/testing/snapshots/bindings_assertions/variables/destructuring.sol b/crates/solidity/testing/snapshots/bindings_assertions/variables/destructuring.sol index 103fabcd6..3440cc559 100644 --- a/crates/solidity/testing/snapshots/bindings_assertions/variables/destructuring.sol +++ b/crates/solidity/testing/snapshots/bindings_assertions/variables/destructuring.sol @@ -16,6 +16,8 @@ library Test { // ^ref:7 // ^ref:5 (, Choice c) = (Choice.Yes, Choice.No); + // ^ref:1 + // ^ref:1 // ^def:8 // ^ref:1 diff --git a/crates/solidity/testing/snapshots/bindings_assertions/variables/params.sol b/crates/solidity/testing/snapshots/bindings_assertions/variables/params.sol index 2455343fb..368655962 100644 --- a/crates/solidity/testing/snapshots/bindings_assertions/variables/params.sol +++ b/crates/solidity/testing/snapshots/bindings_assertions/variables/params.sol @@ -16,6 +16,6 @@ contract Foo { // ^def:3 y = x + 5; // ^ref:3 - // ^ref:2 + //