File tree Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -2504,7 +2504,8 @@ bool VarDecl::isUsableInConstantExpressions(const ASTContext &Context) const {
2504
2504
if (!DefVD->mightBeUsableInConstantExpressions (Context))
2505
2505
return false ;
2506
2506
// ... and its initializer is a constant initializer.
2507
- if (Context.getLangOpts ().CPlusPlus && !DefVD->hasConstantInitialization ())
2507
+ if ((Context.getLangOpts ().CPlusPlus || getLangOpts ().C23 ) &&
2508
+ !DefVD->hasConstantInitialization ())
2508
2509
return false ;
2509
2510
// C++98 [expr.const]p1:
2510
2511
// An integral constant-expression can involve only [...] const variables
@@ -2611,8 +2612,11 @@ bool VarDecl::hasICEInitializer(const ASTContext &Context) const {
2611
2612
}
2612
2613
2613
2614
bool VarDecl::hasConstantInitialization () const {
2614
- // In C, all globals (and only globals) have constant initialization.
2615
- if (hasGlobalStorage () && !getASTContext ().getLangOpts ().CPlusPlus )
2615
+ // In C, all globals and constexpr variables should have constant
2616
+ // initialization. For constexpr variables in C check that initializer is a
2617
+ // constant initializer because they can be used in constant expressions.
2618
+ if (hasGlobalStorage () && !getASTContext ().getLangOpts ().CPlusPlus &&
2619
+ !isConstexpr ())
2616
2620
return true ;
2617
2621
2618
2622
// In C++, it depends on whether the evaluation at the point of definition
Original file line number Diff line number Diff line change @@ -364,3 +364,20 @@ void constexprif() {
364
364
void constevalif () {
365
365
if consteval (300 ) {} //expected-error {{expected '(' after 'if'}}
366
366
}
367
+
368
+ struct S11 {
369
+ int len ;
370
+ };
371
+ void ghissue112516 () {
372
+ struct S11 * s11 = 0 ;
373
+ constexpr int num = s11 -> len ; // expected-error {{constexpr variable 'num' must be initialized by a constant expression}}
374
+ void * Arr [num ];
375
+ }
376
+
377
+ void ghissue109095 () {
378
+ constexpr char c [] = { 'a' };
379
+ constexpr int i = c [1 ]; // expected-error {{constexpr variable 'i' must be initialized by a constant expression}}\
380
+ // expected-note {{declared here}}
381
+ _Static_assert (i == c [0 ]); // expected-error {{static assertion expression is not an integral constant expression}}\
382
+ // expected-note {{initializer of 'i' is not a constant expression}}
383
+ }
You can’t perform that action at this time.
0 commit comments