Skip to content

[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

Merged
merged 3 commits into from
Mar 25, 2025

Conversation

mariusdr
Copy link
Contributor

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'.

Raise an error when placement new is used to modify a const-qualified
variable in a constexpr function.
Copy link

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2025

@llvm/pr-subscribers-clang

Author: marius doerner (mariusdr)

Changes

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'.


Full diff: https://github.com/llvm/llvm-project/pull/132460.diff

2 Files Affected:

  • (modified) clang/lib/AST/ExprConstant.cpp (+9)
  • (modified) clang/test/AST/ByteCode/placement-new.cpp (+39)
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);
+}

@tbaederr tbaederr requested a review from cor3ntin March 22, 2025 04:12
@tbaederr
Copy link
Contributor

The tests should go into a file not under ByteCode/ since they aren't specific to the bytecode interpreter. There should be existing placement new tests in SemaCXX/.

@mariusdr
Copy link
Contributor Author

The tests should go into a file not under ByteCode/ since they aren't specific to the bytecode interpreter. There should be existing placement new tests in SemaCXX/.

Thanks for the hint, moved them to clang/test/SemaCXX/cxx2c-constexpr-placement-new.cpp

@mariusdr mariusdr force-pushed the users/mariusdr/placement_new_const_error branch from b596108 to 1970638 Compare March 23, 2025 08:31
@cor3ntin
Copy link
Contributor

This change needs a release note.
Please add an entry to clang/docs/ReleaseNotes.rst in the section the most adapted to the change, and referencing any Github issue this change fixes. Thanks!

@mariusdr
Copy link
Contributor Author

Added release notes under "Bug Fixes to C++ Support". Thanks for the review.

Copy link
Contributor

@cor3ntin cor3ntin left a 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?

@mariusdr
Copy link
Contributor Author

Thanks. Will you need me to merge that for you?

Yes, please

@cor3ntin cor3ntin merged commit 4067581 into llvm:main Mar 25, 2025
7 checks passed
Copy link

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Placement new can modify const-qualified variables
4 participants