[PATCH] D136751: [clang][Interp] This pointers are writable in constructors
Timm Bäder via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 26 02:32:21 PDT 2022
tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, tahonermann, shafik.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
This is a c++20 extension as far as I know.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D136751
Files:
clang/lib/AST/Interp/Interp.cpp
clang/lib/AST/Interp/Pointer.h
clang/test/AST/Interp/cxx20.cpp
Index: clang/test/AST/Interp/cxx20.cpp
===================================================================
--- clang/test/AST/Interp/cxx20.cpp
+++ clang/test/AST/Interp/cxx20.cpp
@@ -113,3 +113,20 @@
// ref-error {{must be initialized by a constant expression}} \
// ref-note {{subobject of type 'int' is not initialized}}
};
+
+namespace ConstThis {
+ class Foo {
+ const int T = 12; // expected-note {{declared const here}} \
+ // ref-note {{declared const here}}
+ int a;
+ public:
+ constexpr Foo() {
+ this->a = 10;
+ T = 13; // expected-error {{cannot assign to non-static data member 'T' with const-qualified type}} \
+ // ref-error {{cannot assign to non-static data member 'T' with const-qualified type}}
+ }
+ };
+ constexpr Foo F; // expected-error {{must be initialized by a constant expression}} \
+ // ref-error {{must be initialized by a constant expression}}
+
+};
Index: clang/lib/AST/Interp/Pointer.h
===================================================================
--- clang/lib/AST/Interp/Pointer.h
+++ clang/lib/AST/Interp/Pointer.h
@@ -282,6 +282,8 @@
/// Returns the number of elements.
unsigned getNumElems() const { return getSize() / elemSize(); }
+ Block *block() const { return Pointee; }
+
/// Returns the index into an array.
int64_t getIndex() const {
if (isElementPastEnd())
Index: clang/lib/AST/Interp/Interp.cpp
===================================================================
--- clang/lib/AST/Interp/Interp.cpp
+++ clang/lib/AST/Interp/Interp.cpp
@@ -261,6 +261,14 @@
return true;
}
+ const Function *Func = S.Current->getFunction();
+ if (Func && Func->isConstructor()) {
+ // The This pointer is writable in constructors, even if
+ // isConst() returns true.
+ if (Ptr.block() == S.Current->getThis().block())
+ return true;
+ }
+
const QualType Ty = Ptr.getType();
const SourceInfo &Loc = S.Current->getSource(OpPC);
S.FFDiag(Loc, diag::note_constexpr_modify_const_type) << Ty;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136751.470755.patch
Type: text/x-patch
Size: 2102 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221026/f0e434e9/attachment-0001.bin>
More information about the cfe-commits
mailing list