Skip to content

[clang][bytecode] Fix const-in-mutable fields #149286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/lib/AST/ByteCode/Descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ static void initField(Block *B, std::byte *Ptr, bool IsConst, bool IsMutable,
Desc->IsConst = IsConst || D->IsConst;
Desc->IsFieldMutable = IsMutable || D->IsMutable;
Desc->IsVolatile = IsVolatile || D->IsVolatile;
// True if this field is const AND the parent is mutable.
Desc->IsConstInMutable = Desc->IsConst && IsMutable;

if (auto Fn = D->CtorFn)
Fn(B, Ptr + FieldOffset, Desc->IsConst, Desc->IsFieldMutable,
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/AST/ByteCode/Descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ struct InlineDescriptor {
/// Flag indicating if the field is mutable (if in a record).
LLVM_PREFERRED_TYPE(bool)
unsigned IsFieldMutable : 1;
/// Flag indicating if this field is a const field nested in
/// a mutable parent field.
LLVM_PREFERRED_TYPE(bool)
unsigned IsConstInMutable : 1;
/// Flag indicating if the field is an element of a composite array.
LLVM_PREFERRED_TYPE(bool)
unsigned IsArrayElement : 1;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/ByteCode/Disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ LLVM_DUMP_METHOD void InlineDescriptor::dump(llvm::raw_ostream &OS) const {
OS << "InUnion: " << InUnion << "\n";
OS << "IsFieldMutable: " << IsFieldMutable << "\n";
OS << "IsArrayElement: " << IsArrayElement << "\n";
OS << "IsConstInMutable: " << IsConstInMutable << '\n';
OS << "Desc: ";
if (Desc)
Desc->dump(OS);
Expand Down
5 changes: 4 additions & 1 deletion clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,10 @@ bool CheckDowncast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,

bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
assert(Ptr.isLive() && "Pointer is not live");
if (!Ptr.isConst() || Ptr.isMutable())
if (!Ptr.isConst())
return true;

if (Ptr.isMutable() && !Ptr.isConstInMutable())
return true;

if (!Ptr.isBlockPointer())
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/ByteCode/Pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,11 @@ class Pointer {
return true;
return isRoot() ? getDeclDesc()->IsConst : getInlineDesc()->IsConst;
}
bool isConstInMutable() const {
if (!isBlockPointer())
return false;
return isRoot() ? false : getInlineDesc()->IsConstInMutable;
}

/// Checks if an object or a subfield is volatile.
bool isVolatile() const {
Expand Down
56 changes: 48 additions & 8 deletions clang/test/AST/ByteCode/mutable.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++11 -verify=expected,expected11,both,both11 %s
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++14 -verify=expected,expected14,both %s
// RUN: %clang_cc1 -std=c++11 -verify=ref,ref11,both,both11 %s
// RUN: %clang_cc1 -std=c++14 -verify=ref,ref14,both %s




// RUN: %clang_cc1 -std=c++11 -verify=expected,expected11,both,both11 %s -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -std=c++14 -verify=expected,expected14,both %s -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -std=c++11 -verify=ref,ref11,both,both11 %s
// RUN: %clang_cc1 -std=c++14 -verify=ref,ref14,both %s

namespace Simple {
struct S {
Expand All @@ -26,3 +22,47 @@ namespace Simple {
static_assert(s2.a2 == 12, ""); // both11-error {{not an integral constant expression}} \
// both11-note {{initializer of 's2' is not a constant expression}}
}
#if __cplusplus >= 201402L
namespace ConstInMutable {
class B {
public:

const int f;
constexpr B() : f(12) {}
};
class A {
public:
mutable B b;
constexpr A() = default;
};
constexpr int constInMutable() {
A a;

int *m = (int*)&a.b.f;
*m = 12; // both-note {{modification of object of const-qualified type 'const int' is not allowed in a constant expression}}
return 1;
}
static_assert(constInMutable() == 1, ""); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}
}

namespace MutableInConst {
class C {
public:
mutable int c;
constexpr C() : c(50) {}
};
class D {
public:
C c;
constexpr D() {}
};
constexpr int mutableInConst() {
const D d{};
int *m = (int*)&d.c.c;
*m = 12;
return 1;
}
static_assert(mutableInConst() == 1, "");
}
#endif