From a5240e3eb325574111fe954759c628848e9d46c3 Mon Sep 17 00:00:00 2001 From: MikePopoloski Date: Fri, 28 Apr 2023 21:05:29 -0400 Subject: [PATCH] Fix off-by-one in symbol lookup location for detecting parameters used before declared --- source/ast/Symbol.cpp | 4 ++-- tests/unittests/HierarchyTests.cpp | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/source/ast/Symbol.cpp b/source/ast/Symbol.cpp index 351c0fcd1..8b180a1d4 100644 --- a/source/ast/Symbol.cpp +++ b/source/ast/Symbol.cpp @@ -205,14 +205,14 @@ std::optional Symbol::isDeclaredBefore(LookupLocation target) const { // If target wasn't in a direct scope of any of our own parents, // repeat the process walking up target's scopes. sym = &target.getScope()->asSymbol(); - ll = LookupLocation::before(*sym); + ll = LookupLocation::after(*sym); while ((scope = ll.getScope()) != nullptr && sym->kind != SymbolKind::CompilationUnit) { if (auto it = locMap.find(scope); it != locMap.end()) return it->second < ll; sym = &scope->asSymbol(); - ll = LookupLocation::before(*sym); + ll = LookupLocation::after(*sym); } return std::nullopt; diff --git a/tests/unittests/HierarchyTests.cpp b/tests/unittests/HierarchyTests.cpp index 4639e8016..07b89a6d5 100644 --- a/tests/unittests/HierarchyTests.cpp +++ b/tests/unittests/HierarchyTests.cpp @@ -2254,3 +2254,21 @@ endmodule CHECK(diags[0].code == diag::InfoTask); CHECK(diags[1].code == diag::InfoTask); } + +TEST_CASE("Hierarchical parameter lookup location regress") { + auto tree = SyntaxTree::fromText(R"( +interface I; + parameter int P = 6; + function automatic int getP; return P; endfunction +endinterface + +module m; + I i(); + localparam int a = i.getP(); +endmodule +)"); + + Compilation compilation; + compilation.addSyntaxTree(tree); + NO_COMPILATION_ERRORS; +}