Add error when accessing a non-static class field from a static function (#6948)

This commit is contained in:
Artur Bieniek 2026-01-23 16:18:24 +01:00 committed by GitHub
parent ddbcd66722
commit a7db9ee32f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 46 additions and 0 deletions

View File

@ -2770,6 +2770,12 @@ class WidthVisitor final : public VNVisitor {
<< nodep->prettyNameQ()
<< " (IEEE 1800-2023 6.20.6)");
}
if (nodep->varp()->isClassMember() && !nodep->varp()->isFuncLocal()
&& !nodep->varp()->lifetime().isStatic() && m_ftaskp && m_ftaskp->isStatic()) {
nodep->v3error("Cannot access non-static member variable "
<< nodep->prettyNameQ() << " from a static method "
<< m_ftaskp->prettyNameQ() << " without object (IEEE 1800-2023 8.10)");
}
nodep->didWidth(true);
}
@ -3309,6 +3315,10 @@ class WidthVisitor final : public VNVisitor {
void visit(AstThisRef* nodep) override {
if (nodep->didWidthAndSet()) return;
nodep->dtypep(iterateEditMoveDTypep(nodep, nodep->childDTypep()));
if (m_ftaskp && m_ftaskp->isStatic()) {
nodep->v3error("Cannot use 'this' in a static method "
<< m_ftaskp->prettyNameQ() << " (IEEE 1800-2023 8.10-8.11)");
}
}
void visit(AstClassRefDType* nodep) override {
if (nodep->didWidthAndSet()) return;

View File

@ -0,0 +1,8 @@
%Error: t/t_class_static_member_bad.v:10:11: Cannot access non-static member variable 'mb' from a static method 'foo' without object (IEEE 1800-2023 8.10)
10 | void'(mb.try_put(this));
| ^~
... See the manual at https://verilator.org/verilator_doc.html?v=latest for more assistance.
%Error: t/t_class_static_member_bad.v:10:22: Cannot use 'this' in a static method 'foo' (IEEE 1800-2023 8.10-8.11)
10 | void'(mb.try_put(this));
| ^~~~
%Error: Exiting due to

View File

@ -0,0 +1,16 @@
#!/usr/bin/env python3
# DESCRIPTION: Verilator: Verilog Test driver/expect definition
#
# Copyright 2026 by Wilson Snyder. 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-License-Identifier: LGPL-3.0-only OR Artistic-2.0
import vltest_bootstrap
test.scenarios('linter')
test.lint(fails=True, expect_filename=test.golden_filename)
test.passes()

View File

@ -0,0 +1,12 @@
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2026 by Antmicro.
// SPDX-License-Identifier: CC0-1.0
class Cls;
mailbox #(Cls) mb = new();
static task foo();
void'(mb.try_put(this));
endtask
endclass