-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] Placement new error when modifying consts #132460
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
[clang] Placement new error when modifying consts #132460
Conversation
Raise an error when placement new is used to modify a const-qualified variable in a constexpr function.
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: marius doerner (mariusdr) ChangesCloses #131432 Raise an error when placement new is used to modify a const-qualified variable in a constexpr function. Note that diag::note_constexpr_modify_const_type is used since the it is also raised when the sample program is compiled with '-fexperimental-new-constant-interpreter'. Full diff: https://github.com/llvm/llvm-project/pull/132460.diff 2 Files Affected:
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 92a28897cf3ee..b4fc3d4471064 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -10415,7 +10415,16 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) {
typedef bool result_type;
bool failed() { return false; }
+ bool checkConst(QualType QT) {
+ if (QT.isConstQualified()) {
+ Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT;
+ return false;
+ }
+ return true;
+ }
bool found(APValue &Subobj, QualType SubobjType) {
+ if (!checkConst(SubobjType))
+ return false;
// FIXME: Reject the cases where [basic.life]p8 would not permit the
// old name of the object to be used to name the new object.
unsigned SubobjectSize = 1;
diff --git a/clang/test/AST/ByteCode/placement-new.cpp b/clang/test/AST/ByteCode/placement-new.cpp
index c353162a7aab0..9b12c9f2b1714 100644
--- a/clang/test/AST/ByteCode/placement-new.cpp
+++ b/clang/test/AST/ByteCode/placement-new.cpp
@@ -376,3 +376,42 @@ constexpr int N = [] // expected-error {{must be initialized by a constant expre
return s.a[0];
}();
#endif
+
+constexpr int modify_const_variable() {
+ const int a = 10;
+ new ((int *)&a) int(12); // both-note {{modification of object of const-qualified type 'const int' is not allowed in a constant expression}}
+ return a;
+}
+static_assert(modify_const_variable()); // both-error {{not an integral constant expression}} \
+ // both-note {{in call to}}
+
+typedef const int T0;
+typedef T0 T1;
+constexpr T1 modify_const_variable_td() {
+ T1 a = 10;
+ new ((int *)&a) int(12); // both-note {{modification of object of const-qualified type 'T1' (aka 'const int') is not allowed in a constant expression}}
+ return a;
+}
+static_assert(modify_const_variable_td()); // both-error {{not an integral constant expression}} \
+ // both-note {{in call to}}
+
+template<typename T>
+constexpr T modify_const_variable_tmpl() {
+ T a = 10;
+ new ((int *)&a) int(12); // both-note {{modification of object of const-qualified type 'const int' is not allowed in a constant expression}}
+ return a;
+}
+static_assert(modify_const_variable_tmpl<const int>()); // both-error {{not an integral constant expression}} \
+ // both-note {{in call to}}
+
+namespace ModifyMutableMember {
+ struct S {
+ mutable int a {10};
+ };
+ constexpr int modify_mutable_member() {
+ const S s;
+ new ((int *)&s.a) int(12);
+ return s.a;
+ }
+ static_assert(modify_mutable_member() == 12);
+}
|
The tests should go into a file not under |
Thanks for the hint, moved them to clang/test/SemaCXX/cxx2c-constexpr-placement-new.cpp |
b596108
to
1970638
Compare
This change needs a release note. |
Added release notes under "Bug Fixes to C++ Support". Thanks for the review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
Will you need me to merge that for you?
Yes, please |
@mariusdr Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Closes #131432
Raise an error when placement new is used to modify a const-qualified variable in a constexpr function.
Note that diag::note_constexpr_modify_const_type is used since the it is also raised when the sample program is compiled with '-fexperimental-new-constant-interpreter'.