Skip to content

Commit 425b953

Browse files
authored
[clang][bytecode] Emit diagnostics from GetGlobalUnchecked (#137203)
If the global is uninitialized.
1 parent 63f5c6a commit 425b953

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

clang/lib/AST/ByteCode/Interp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
14461446
template <PrimType Name, class T = typename PrimConv<Name>::T>
14471447
bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I) {
14481448
const Pointer &Ptr = S.P.getPtrGlobal(I);
1449-
if (!Ptr.isInitialized())
1449+
if (!CheckInitialized(S, OpPC, Ptr, AK_Read))
14501450
return false;
14511451
S.Stk.push<T>(Ptr.deref<T>());
14521452
return true;

clang/test/AST/ByteCode/cxx11.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,11 @@ namespace DynamicCast {
194194
int g : (S*)(void*)(sptr) == sptr;
195195
};
196196
}
197+
198+
namespace GlobalInitializer {
199+
extern int &g; // both-note {{here}}
200+
struct S {
201+
int G : g; // both-error {{constant expression}} \
202+
// both-note {{initializer of 'g' is unknown}}
203+
};
204+
}

clang/test/SemaCXX/constant-expression-p2280r4.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %clang_cc1 -std=c++23 -verify=expected,nointerpreter %s
2-
// RUN: %clang_cc1 -std=c++23 -verify %s -fexperimental-new-constant-interpreter
1+
// RUN: %clang_cc1 -std=c++23 -verify %s
2+
// RUN: %clang_cc1 -std=c++23 -verify=expected,interpreter %s -fexperimental-new-constant-interpreter
33

44
using size_t = decltype(sizeof(0));
55

@@ -48,10 +48,11 @@ void splash(Swim& swam) {
4848
}
4949

5050
extern Swim dc;
51-
extern Swim& trident;
51+
extern Swim& trident; // interpreter-note {{declared here}}
5252

5353
constexpr auto& sandeno = typeid(dc); // ok: can only be typeid(Swim)
54-
constexpr auto& gallagher = typeid(trident); // expected-error {{constexpr variable 'gallagher' must be initialized by a constant expression}}
54+
constexpr auto& gallagher = typeid(trident); // expected-error {{constexpr variable 'gallagher' must be initialized by a constant expression}} \
55+
// interpreter-note {{initializer of 'trident' is unknown}}
5556

5657
namespace explicitThis {
5758
struct C {
@@ -157,18 +158,18 @@ int g() {
157158

158159
namespace GH128409 {
159160
int &ff();
160-
int &x = ff(); // nointerpreter-note {{declared here}}
161+
int &x = ff(); // expected-note {{declared here}}
161162
constinit int &z = x; // expected-error {{variable does not have a constant initializer}} \
162163
// expected-note {{required by 'constinit' specifier here}} \
163-
// nointerpreter-note {{initializer of 'x' is not a constant expression}}
164+
// expected-note {{initializer of 'x' is not a constant expression}}
164165
}
165166

166167
namespace GH129845 {
167168
int &ff();
168-
int &x = ff(); // nointerpreter-note {{declared here}}
169+
int &x = ff(); // expected-note {{declared here}}
169170
struct A { int& x; };
170171
constexpr A g = {x}; // expected-error {{constexpr variable 'g' must be initialized by a constant expression}} \
171-
// nointerpreter-note {{initializer of 'x' is not a constant expression}}
172+
// expected-note {{initializer of 'x' is not a constant expression}}
172173
const A* gg = &g;
173174
}
174175

0 commit comments

Comments
 (0)