[clang] [clang] Reject initializing constexpr struct/union types with compound literals that contain non null pointers (PR #228776)

Abdulmalek Almkainzi via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 4 00:45:54 PDT 2026


https://github.com/aalmkainzi updated https://github.com/llvm/llvm-project/pull/228776

>From dd487bdfdcc50fccb07b93d4d2029e8becdcb3ea Mon Sep 17 00:00:00 2001
From: Abdulmalek Almkainzi <aalmkainzi at gmail.com>
Date: Sun, 4 Oct 2026 00:24:23 +0300
Subject: [PATCH 1/2] made CheckLValueConstantExpression return false when
 initializing constexpr pointer variable with non-null in C23

---
 clang/lib/AST/ExprConstant.cpp                    |  8 ++++++++
 clang/test/Parser/c23-reject-non-null-constexpr.c | 12 ++++++++++++
 2 files changed, 20 insertions(+)
 create mode 100644 clang/test/Parser/c23-reject-non-null-constexpr.c

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 3f295f35d1361..f40cf9e8e316b 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -2175,6 +2175,14 @@ static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,
                                           QualType Type, const LValue &LVal,
                                           ConstantExprKind Kind,
                                           CheckedTemporaries &CheckedTemps) {
+  if (Info.getLangOpts().C23 && !LVal.isNullPointer()) {
+    auto D = Info.EvaluatingDecl;
+    if (auto *ValD = D.dyn_cast<const ValueDecl *>())
+      if (auto *VarD = dyn_cast<VarDecl>(ValD))
+        if (VarD->isConstexpr())
+          return false;
+  }
+
   bool IsReferenceType = Type->isReferenceType();
 
   APValue::LValueBase Base = LVal.getLValueBase();
diff --git a/clang/test/Parser/c23-reject-non-null-constexpr.c b/clang/test/Parser/c23-reject-non-null-constexpr.c
new file mode 100644
index 0000000000000..148b08a7ff1f8
--- /dev/null
+++ b/clang/test/Parser/c23-reject-non-null-constexpr.c
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -std=c23 -verify -pedantic %s
+
+const int a = 0;
+struct C { const int *p; };
+constexpr struct C c1 = {&a}; // expected-error{{constexpr pointer initializer is not null}}
+constexpr struct C c2 = (struct C){&a}; // expected-error{{constexpr variable 'c2' must be initialized by a constant expression}}
+
+union U { const int *p; int x; };
+constexpr union U u1 = {.p = &a}; // expected-error{{constexpr pointer initializer is not null}}
+constexpr union U u2 = (union U){.p = &a}; // expected-error{{constexpr variable 'u2' must be initialized by a constant expression}}
+constexpr union U u3 = {.x = 1};
+constexpr union U u4 = (union U){.x = 1};

>From 65ec1613caf082b3e6f019228bc95987f4a6bb0a Mon Sep 17 00:00:00 2001
From: aalmkainzi <aa.almkainzi at wakeb.tech>
Date: Sun, 4 Oct 2026 10:41:48 +0300
Subject: [PATCH 2/2] moved test to Sema tests

---
 clang/test/{Parser => Sema}/c23-reject-non-null-constexpr.c | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename clang/test/{Parser => Sema}/c23-reject-non-null-constexpr.c (100%)

diff --git a/clang/test/Parser/c23-reject-non-null-constexpr.c b/clang/test/Sema/c23-reject-non-null-constexpr.c
similarity index 100%
rename from clang/test/Parser/c23-reject-non-null-constexpr.c
rename to clang/test/Sema/c23-reject-non-null-constexpr.c



More information about the cfe-commits mailing list