[clang] [clang][bytecode] Fix a diagnostic difference with extern variables (PR #201369)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 3 07:26:53 PDT 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/201369
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.
>From b6aa2da7fe413b4145085deb367fc4fe2a39b5e4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 3 Jun 2026 16:25:19 +0200
Subject: [PATCH] [clang][bytecode] Fix a diagnostic difference with extern
variables
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.
---
clang/lib/AST/ByteCode/Interp.cpp | 1 +
clang/test/AST/ByteCode/extern.cpp | 7 +++++--
2 files changed, 6 insertions(+), 2 deletions(-)
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}}
+
More information about the cfe-commits
mailing list