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 @@ -2503,7 +2503,8 @@ bool VarDecl::isUsableInConstantExpressions(const ASTContext &Context) const {
2503
2503
if (!DefVD->mightBeUsableInConstantExpressions (Context))
2504
2504
return false ;
2505
2505
// ... and its initializer is a constant initializer.
2506
- if (Context.getLangOpts ().CPlusPlus && !DefVD->hasConstantInitialization ())
2506
+ if ((Context.getLangOpts ().CPlusPlus || getLangOpts ().C23 ) &&
2507
+ !DefVD->hasConstantInitialization ())
2507
2508
return false ;
2508
2509
// C++98 [expr.const]p1:
2509
2510
// An integral constant-expression can involve only [...] const variables
@@ -2610,8 +2611,11 @@ bool VarDecl::hasICEInitializer(const ASTContext &Context) const {
2610
2611
}
2611
2612
2612
2613
bool VarDecl::hasConstantInitialization () const {
2613
- // In C, all globals (and only globals) have constant initialization.
2614
- if (hasGlobalStorage () && !getASTContext ().getLangOpts ().CPlusPlus )
2614
+ // In C, all globals and constexpr variables should have constant
2615
+ // initialization. For constexpr variables in C check that initializer is a
2616
+ // constant initializer because they can be used in constant expressions.
2617
+ if (hasGlobalStorage () && !getASTContext ().getLangOpts ().CPlusPlus &&
2618
+ !isConstexpr ())
2615
2619
return true ;
2616
2620
2617
2621
// In C++, it depends on whether the evaluation at the point of definition
Original file line number Diff line number Diff line change @@ -357,3 +357,20 @@ void infsNaNs() {
357
357
constexpr double db5 = LD_SNAN ; // expected-error {{constexpr initializer evaluates to nan which is not exactly representable in type 'const double'}}
358
358
constexpr double db6 = INF ;
359
359
}
360
+
361
+ struct S11 {
362
+ int len ;
363
+ };
364
+ void ghissue112516 () {
365
+ struct S11 * s11 = 0 ;
366
+ constexpr int num = s11 -> len ; // expected-error {{constexpr variable 'num' must be initialized by a constant expression}}
367
+ void * Arr [num ];
368
+ }
369
+
370
+ void ghissue109095 () {
371
+ constexpr char c [] = { 'a' };
372
+ constexpr int i = c [1 ]; // expected-error {{constexpr variable 'i' must be initialized by a constant expression}}\
373
+ // expected-note {{declared here}}
374
+ _Static_assert (i == c [0 ]); // expected-error {{static assertion expression is not an integral constant expression}}\
375
+ // expected-note {{initializer of 'i' is not a constant expression}}
376
+ }
You can’t perform that action at this time.
0 commit comments