File tree 2 files changed +24
-3
lines changed
2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -2512,7 +2512,8 @@ bool VarDecl::isUsableInConstantExpressions(const ASTContext &Context) const {
2512
2512
if (!DefVD->mightBeUsableInConstantExpressions (Context))
2513
2513
return false ;
2514
2514
// ... and its initializer is a constant initializer.
2515
- if (Context.getLangOpts ().CPlusPlus && !DefVD->hasConstantInitialization ())
2515
+ if ((Context.getLangOpts ().CPlusPlus || getLangOpts ().C23 ) &&
2516
+ !DefVD->hasConstantInitialization ())
2516
2517
return false ;
2517
2518
// C++98 [expr.const]p1:
2518
2519
// An integral constant-expression can involve only [...] const variables
@@ -2619,8 +2620,11 @@ bool VarDecl::hasICEInitializer(const ASTContext &Context) const {
2619
2620
}
2620
2621
2621
2622
bool VarDecl::hasConstantInitialization () const {
2622
- // In C, all globals (and only globals) have constant initialization.
2623
- if (hasGlobalStorage () && !getASTContext ().getLangOpts ().CPlusPlus )
2623
+ // In C, all globals and constexpr variables should have constant
2624
+ // initialization. For constexpr variables in C check that initializer is a
2625
+ // constant initializer because they can be used in constant expressions.
2626
+ if (hasGlobalStorage () && !getASTContext ().getLangOpts ().CPlusPlus &&
2627
+ !isConstexpr ())
2624
2628
return true ;
2625
2629
2626
2630
// In C++, it depends on whether the evaluation at the point of definition
Original file line number Diff line number Diff line change @@ -374,3 +374,20 @@ void constexprif() {
374
374
void constevalif () {
375
375
if consteval (300 ) {} //expected-error {{expected '(' after 'if'}}
376
376
}
377
+
378
+ struct S11 {
379
+ int len ;
380
+ };
381
+ void ghissue112516 () {
382
+ struct S11 * s11 = 0 ;
383
+ constexpr int num = s11 -> len ; // expected-error {{constexpr variable 'num' must be initialized by a constant expression}}
384
+ void * Arr [num ];
385
+ }
386
+
387
+ void ghissue109095 () {
388
+ constexpr char c [] = { 'a' };
389
+ constexpr int i = c [1 ]; // expected-error {{constexpr variable 'i' must be initialized by a constant expression}}\
390
+ // expected-note {{declared here}}
391
+ _Static_assert (i == c [0 ]); // expected-error {{static assertion expression is not an integral constant expression}}\
392
+ // expected-note {{initializer of 'i' is not a constant expression}}
393
+ }
You can’t perform that action at this time.
0 commit comments