[clang] [clang][bytecode] Fix a diagnostic difference with extern variables (PR #201369)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 3 07:27:41 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
If the extern variable is constexpr of of non-array type, we should diagnose it as missing an initializer. Otherwise, we diagose a read of non-constexpr variable.
---
Full diff: https://github.com/llvm/llvm-project/pull/201369.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Interp.cpp (+1)
- (modified) clang/test/AST/ByteCode/extern.cpp (+5-2)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 6bcebf3ee892b..80707a7ae68fb 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -154,6 +154,7 @@ static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC,
if (const auto *VarD = dyn_cast<VarDecl>(VD);
VarD && VarD->getType().isConstQualified() &&
+ (VarD->isConstexpr() || !VarD->getType()->isArrayType()) &&
!VarD->getAnyInitializer()) {
diagnoseMissingInitializer(S, OpPC, VD);
return;
diff --git a/clang/test/AST/ByteCode/extern.cpp b/clang/test/AST/ByteCode/extern.cpp
index c3215931d41f8..bb2a367d0fcf4 100644
--- a/clang/test/AST/ByteCode/extern.cpp
+++ b/clang/test/AST/ByteCode/extern.cpp
@@ -1,8 +1,6 @@
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=both,expected %s
// RUN: %clang_cc1 -verify=both,ref %s
-// both-no-diagnostics
-
extern const double Num;
extern const double Num = 12;
@@ -13,3 +11,8 @@ constexpr int getE() {
const int E = 10;
static_assert(getE() == 10);
+
+extern const int carr[]; // both-note {{declared here}}
+constexpr int n = carr[0]; // both-error {{must be initialized by a constant expression}} \
+ // both-note {{read of non-constexpr variable}}
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/201369
More information about the cfe-commits
mailing list