@@ -53,6 +53,21 @@ static bool Jf(InterpState &S, CodePtr &PC, int32_t Offset) {
5353 return true ;
5454}
5555
56+ static void diagnoseNonConstVariable (InterpState &S, CodePtr OpPC,
57+ const ValueDecl *VD) {
58+ if (!S.getLangOpts ().CPlusPlus )
59+ return ;
60+
61+ const SourceInfo &Loc = S.Current ->getSource (OpPC);
62+ S.FFDiag (Loc,
63+ VD->getType ()->isIntegralOrEnumerationType ()
64+ ? diag::note_constexpr_ltor_non_const_int
65+ : diag::note_constexpr_ltor_non_constexpr,
66+ 1 )
67+ << VD;
68+ S.Note (VD->getLocation (), diag::note_declared_at);
69+ }
70+
5671static bool CheckActive (InterpState &S, CodePtr OpPC, const Pointer &Ptr,
5772 AccessKinds AK) {
5873 if (Ptr.isActive ())
@@ -171,9 +186,7 @@ bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
171186
172187 if (!S.checkingPotentialConstantExpression () && S.getLangOpts ().CPlusPlus ) {
173188 const auto *VD = Ptr.getDeclDesc ()->asValueDecl ();
174- const SourceInfo &Loc = S.Current ->getSource (OpPC);
175- S.FFDiag (Loc, diag::note_constexpr_ltor_non_constexpr, 1 ) << VD;
176- S.Note (VD->getLocation (), diag::note_declared_at);
189+ diagnoseNonConstVariable (S, OpPC, VD);
177190 }
178191 return false ;
179192}
@@ -216,6 +229,24 @@ bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
216229 return true ;
217230}
218231
232+ bool CheckConstant (InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
233+ assert (Desc);
234+ if (const auto *D = Desc->asValueDecl ()) {
235+ if (const auto *VD = dyn_cast<VarDecl>(D);
236+ VD && VD->hasGlobalStorage () &&
237+ !(VD->isConstexpr () || VD->getType ().isConstQualified ())) {
238+ diagnoseNonConstVariable (S, OpPC, VD);
239+ return false ;
240+ }
241+ }
242+
243+ return true ;
244+ }
245+
246+ static bool CheckConstant (InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
247+ return CheckConstant (S, OpPC, Ptr.getDeclDesc ());
248+ }
249+
219250bool CheckDummy (InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
220251 return !Ptr.isZero () && !Ptr.isDummy ();
221252}
@@ -304,6 +335,9 @@ bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
304335bool CheckLoad (InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
305336 if (!CheckLive (S, OpPC, Ptr, AK_Read))
306337 return false ;
338+ if (!CheckConstant (S, OpPC, Ptr))
339+ return false ;
340+
307341 if (!CheckDummy (S, OpPC, Ptr))
308342 return false ;
309343 if (!CheckExtern (S, OpPC, Ptr))
@@ -605,13 +639,7 @@ bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR) {
605639 }
606640 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
607641 if (!VD->getType ().isConstQualified ()) {
608- S.FFDiag (E,
609- VD->getType ()->isIntegralOrEnumerationType ()
610- ? diag::note_constexpr_ltor_non_const_int
611- : diag::note_constexpr_ltor_non_constexpr,
612- 1 )
613- << VD;
614- S.Note (VD->getLocation (), diag::note_declared_at) << VD->getSourceRange ();
642+ diagnoseNonConstVariable (S, OpPC, VD);
615643 return false ;
616644 }
617645
0 commit comments