Fix modification of members of object with const handle (#7433)

Signed-off-by: Kamil Danecki <kdanecki@internships.antmicro.com>
This commit is contained in:
Kamil Danecki 2026-04-16 15:43:12 +02:00 committed by GitHub
parent 4fe121e5aa
commit 3587ac48a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 104 additions and 2 deletions

View File

@ -218,6 +218,7 @@ class WidthVisitor final : public VNVisitor {
bool m_underFork = false; // Visiting under a fork
bool m_underSExpr = false; // Visiting under a sequence expression
bool m_underPackedArray = false; // Visiting under a AstPackArrayDType
bool m_underMemberSel = false; // Viting under a MemberSel
bool m_hasNamedType = false; // Packed array is defined using named type
AstNode* m_seqUnsupp = nullptr; // Property has unsupported node
bool m_hasSExpr = false; // Property has a sequence expression
@ -2924,8 +2925,8 @@ class WidthVisitor final : public VNVisitor {
}
} else if (nodep->access().isWriteOrRW() && nodep->varp()->isConst() && !m_paramsOnly
&& (!m_ftaskp || !m_ftaskp->isConstructor())
&& !VN_IS(m_procedurep, InitialAutomatic)
&& !VN_IS(m_procedurep, InitialStatic)) {
&& !VN_IS(m_procedurep, InitialAutomatic) && !VN_IS(m_procedurep, InitialStatic)
&& !m_underMemberSel) {
// Too loose, but need to allow our generated first assignment
// Move this to a property of the AstInitial block
nodep->v3warn(E_CONSTWRITTEN, "Writing to 'const' data-typed variable "
@ -3561,6 +3562,8 @@ class WidthVisitor final : public VNVisitor {
void visit(AstMemberSel* nodep) override {
UINFO(5, " MEMBERSEL " << nodep);
if (nodep->didWidth()) return;
VL_RESTORER(m_underMemberSel);
m_underMemberSel = true;
UINFOTREE(9, nodep, "", "mbs-in");
userIterateChildren(nodep, WidthVP{SELF, BOTH}.p());
UINFOTREE(9, nodep, "", "mbs-ic");
@ -3709,6 +3712,11 @@ class WidthVisitor final : public VNVisitor {
VL_DO_DANGLING(pushDeletep(nodep), nodep);
return true;
}
if (nodep->access().isWriteOrRW() && varp->isConst()) {
nodep->v3warn(E_CONSTWRITTEN, "Writing to 'const' data-typed variable "
<< nodep->prettyNameQ()
<< " (IEEE 1800-2023 6.20.6)");
}
nodep->dtypep(foundp->dtypep());
nodep->varp(varp);
nodep->didWidth(true);

View File

@ -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: 2026 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()

View File

@ -0,0 +1,25 @@
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain.
// SPDX-FileCopyrightText: 2026 Antmicro
// SPDX-License-Identifier: CC0-1.0
// verilog_format: off
`define stop $stop
`define checkd(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d: got=%0d exp=%0d\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0);
// verilog_format: on
class Cls;
int x;
endclass
module t;
initial begin
const automatic Cls cls = new;
cls.x = 1;
`checkd(cls.x, 1);
$write("*-* All Finished *-*\n");
$finish;
end
endmodule

View File

@ -0,0 +1,14 @@
%Error-CONSTWRITTEN: t/t_const_handle_bad.v:15:9: Writing to 'const' data-typed variable 'x' (IEEE 1800-2023 6.20.6)
: ... note: In instance 't'
15 | cls.x = 1;
| ^
... For error description see https://verilator.org/warn/CONSTWRITTEN?v=latest
%Error-CONSTWRITTEN: t/t_const_handle_bad.v:16:10: Writing to 'const' data-typed variable 'x' (IEEE 1800-2023 6.20.6)
: ... note: In instance 't'
16 | cls2.x = 1;
| ^
%Error-CONSTWRITTEN: t/t_const_handle_bad.v:18:5: Writing to 'const' data-typed variable 'cls' (IEEE 1800-2023 6.20.6)
: ... note: In instance 't'
18 | cls = cls2;
| ^~~
%Error: Exiting due to

View File

@ -0,0 +1,16 @@
#!/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: 2026 Wilson Snyder
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
import vltest_bootstrap
test.scenarios('vlt')
test.lint(fails=True, expect_filename=test.golden_filename)
test.passes()

View File

@ -0,0 +1,21 @@
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain.
// SPDX-FileCopyrightText: 2026 Antmicro
// SPDX-License-Identifier: CC0-1.0
class Cls;
const int x;
endclass
module t;
initial begin
const automatic Cls cls = new;
automatic Cls cls2 = new;
cls.x = 1;
cls2.x = 1;
cls = cls2;
cls2 = cls;
end
endmodule