From 7e0f25b41b8b63531a65339314c1c2f069f1b75e Mon Sep 17 00:00:00 2001 From: Alex Zhou <126853855+aexzhou@users.noreply.github.com> Date: Thu, 23 Apr 2026 08:17:14 -0700 Subject: [PATCH] Fix access to static variable inside function (#7474) (#7475) Fixes #7474. --- src/V3LinkDot.cpp | 7 +++++++ test_regress/t/t_static_var_xref.py | 18 ++++++++++++++++++ test_regress/t/t_static_var_xref.v | 26 ++++++++++++++++++++++++++ test_regress/t/t_var_xref_bad.out | 4 ++-- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100755 test_regress/t/t_static_var_xref.py create mode 100644 test_regress/t/t_static_var_xref.v diff --git a/src/V3LinkDot.cpp b/src/V3LinkDot.cpp index 2aa4561c9..148e639ab 100644 --- a/src/V3LinkDot.cpp +++ b/src/V3LinkDot.cpp @@ -4331,6 +4331,13 @@ class LinkDotResolveVisitor final : public VNVisitor { << cellp->modp()->prettyNameQ()); } } + } else if (allowScope && !allowFTask && VN_IS(foundp->nodep(), NodeFTask)) { + // Function/task used as scope for static variable access (IEEE std 1800-2017 6.21) + // Don't set m_dotText so the static variable resolves as a VarRef rather than + // VarXRef. This is so that V3Begin won't move it out of the function/task's scope. + ok = true; + m_ds.m_dotSymp = foundp; + m_ds.m_dotPos = DP_SCOPE; } else if (allowFTask && VN_IS(foundp->nodep(), NodeFTask)) { AstNodeFTaskRef* taskrefp; if (VN_IS(foundp->nodep(), Task)) { diff --git a/test_regress/t/t_static_var_xref.py b/test_regress/t/t_static_var_xref.py new file mode 100755 index 000000000..3cc73805c --- /dev/null +++ b/test_regress/t/t_static_var_xref.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of either the GNU Lesser General Public License Version 3 +# or the Perl Artistic License Version 2.0. +# SPDX-FileCopyrightText: 2024 Wilson Snyder +# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 + +import vltest_bootstrap + +test.scenarios('simulator') + +test.compile() + +test.execute() + +test.passes() diff --git a/test_regress/t/t_static_var_xref.v b/test_regress/t/t_static_var_xref.v new file mode 100644 index 000000000..ae6d83976 --- /dev/null +++ b/test_regress/t/t_static_var_xref.v @@ -0,0 +1,26 @@ +// DESCRIPTION: Verilator: Dotted reference that uses another dotted reference +// as the select expression +// +// This file ONLY is placed under the Creative Commons Public Domain. +// SPDX-FileCopyrightText: 2020 Wilson Snyder +// SPDX-License-Identifier: CC0-1.0 + +package test_pkg; + class foo; + virtual function void visit(); + static int compiled_regex; + compiled_regex = 1; + endfunction + + function void end_v(); + visit.compiled_regex = 0; + endfunction + endclass +endpackage + +module t; + initial begin + $write("*-* All Finished *-*\n"); + $finish; + end +endmodule diff --git a/test_regress/t/t_var_xref_bad.out b/test_regress/t/t_var_xref_bad.out index 36b8a9fef..0fccf4d8f 100644 --- a/test_regress/t/t_var_xref_bad.out +++ b/test_regress/t/t_var_xref_bad.out @@ -1,5 +1,5 @@ -%Error: t/t_var_xref_bad.v:11:11: Found definition of 'tsk' as a TASK but expected a scope/variable +%Error: t/t_var_xref_bad.v:11:15: Can't find definition of variable/method: 'bad_missing_ref' 11 | initial tsk.bad_missing_ref = 0; - | ^~~ + | ^~~~~~~~~~~~~~~ ... See the manual at https://verilator.org/verilator_doc.html?v=latest for more assistance. %Error: Exiting due to