[clang] [clang][bytecode] Diagnose reading mutable fields in Memcpy op (PR #197197)
via cfe-commits
cfe-commits at lists.llvm.org
Tue May 12 06:47:47 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/197197.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+6-4)
- (modified) clang/test/AST/ByteCode/records.cpp (+10)
``````````diff
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 5ba15b7ad4f63..726bae10b300d 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -6649,15 +6649,17 @@ static bool copyRecord(InterpState &S, CodePtr OpPC, const Pointer &Src,
assert(SrcDesc->ElemRecord == DestDesc->ElemRecord);
const Record *R = DestDesc->ElemRecord;
for (const Record::Field &F : R->fields()) {
+ Pointer FP = Src.atField(F.Offset);
+
+ if (!CheckMutable(S, OpPC, FP))
+ return false;
+
if (R->isUnion()) {
// For unions, only copy the active field. Zero all others.
- const Pointer &SrcField = Src.atField(F.Offset);
- if (SrcField.isActive()) {
+ if (FP.isActive()) {
if (!copyField(F, /*Activate=*/true))
return false;
} else {
- if (!CheckMutable(S, OpPC, Src.atField(F.Offset)))
- return false;
Pointer DestField = Dest.atField(F.Offset);
zeroAll(DestField);
}
diff --git a/clang/test/AST/ByteCode/records.cpp b/clang/test/AST/ByteCode/records.cpp
index 20ead8cffdd99..b3f07c3cf007e 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -2011,3 +2011,13 @@ namespace RVOPtrIsExtern {
}
static_assert(test_all(), "");
}
+
+namespace MutableInMemcpy {
+ union H {
+ mutable struct {} gx; // both-note {{declared here}}
+ };
+ constexpr H h1 = {};
+ constexpr H h2 = h1; // both-error {{must be initialized by a constant expression}} \
+ // both-note {{read of mutable member 'gx' is not allowed in a constant expression}} \
+ // both-note {{in call}}
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/197197
More information about the cfe-commits
mailing list