Skip to content

Commit 3bb4355

Browse files
authored
[clang][bytecode] Report mutable reads when copying unions (#149320)
1 parent 0b7a95a commit 3bb4355

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2905,6 +2905,8 @@ static bool copyRecord(InterpState &S, CodePtr OpPC, const Pointer &Src,
29052905
if (!copyField(F, /*Activate=*/true))
29062906
return false;
29072907
} else {
2908+
if (!CheckMutable(S, OpPC, Src.atField(F.Offset)))
2909+
return false;
29082910
Pointer DestField = Dest.atField(F.Offset);
29092911
zeroAll(DestField);
29102912
}

clang/lib/AST/ByteCode/InterpFrame.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ static bool shouldSkipInBacktrace(const Function *F) {
128128
if (FD->getDeclName().getCXXOverloadedOperator() == OO_New ||
129129
FD->getDeclName().getCXXOverloadedOperator() == OO_Array_New)
130130
return true;
131+
132+
if (const auto *MD = dyn_cast<CXXMethodDecl>(FD);
133+
MD && MD->getParent()->isAnonymousStructOrUnion())
134+
return true;
135+
131136
return false;
132137
}
133138

clang/test/AST/ByteCode/unions.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,20 @@ namespace Activation2 {
847847
}
848848
static_assert(change_member_indirectly() == 4);
849849
}
850+
851+
namespace CopyCtorMutable {
852+
struct E {
853+
union { // expected-note {{read of mutable member 'b'}}
854+
int a;
855+
mutable int b; // both-note {{here}}
856+
};
857+
};
858+
constexpr E e1 = {{1}};
859+
constexpr E e2 = e1; // both-error {{constant}} \
860+
// ref-note {{read of mutable member 'b'}} \
861+
// both-note {{in call}}
862+
}
863+
850864
#endif
851865

852866
namespace AddressComparison {

0 commit comments

Comments
 (0)