[clang][Interp] Lazily visit constant locals in C++ While we _do_ get them registered via visitInitializer(), they are still local, so gone on the next call to e.g. evaluateAsRValue(). Visit them lazily, similarly like we do in C. GitOrigin-RevId: a52c0c770056e040390839e753dbbaccbf4d63c4
diff --git a/lib/AST/Interp/ByteCodeExprGen.cpp b/lib/AST/Interp/ByteCodeExprGen.cpp index a27a0cb..975d3b6 100644 --- a/lib/AST/Interp/ByteCodeExprGen.cpp +++ b/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -3237,9 +3237,20 @@ // Try to lazily visit (or emit dummy pointers for) declarations // we haven't seen yet. if (Ctx.getLangOpts().CPlusPlus) { - if (const auto *VD = dyn_cast<VarDecl>(D); VD && VD->isStaticLocal()) { - if (std::optional<unsigned> I = P.getOrCreateDummy(D)) - return this->emitGetPtrGlobal(*I, E); + if (const auto *VD = dyn_cast<VarDecl>(D)) { + // Dummy for static locals + if (VD->isStaticLocal()) { + if (std::optional<unsigned> I = P.getOrCreateDummy(D)) + return this->emitGetPtrGlobal(*I, E); + return false; + } + // Visit local const variables like normal. + if (VD->isLocalVarDecl() && VD->getType().isConstQualified()) { + if (!this->visitVarDecl(VD)) + return false; + // Retry. + return this->VisitDeclRefExpr(E); + } } } else { if (const auto *VD = dyn_cast<VarDecl>(D);
diff --git a/test/AST/Interp/arrays.cpp b/test/AST/Interp/arrays.cpp index 3c06ab5..a9450d8 100644 --- a/test/AST/Interp/arrays.cpp +++ b/test/AST/Interp/arrays.cpp
@@ -537,3 +537,11 @@ return s3->array[t.field] == s3->array[t.field]; // both-warning {{self-comparison always evaluates to true}} }; } + +namespace LocalIndex { + void test() { + const int const_subscript = 3; + int array[2]; // both-note {{declared here}} + array[const_subscript] = 0; // both-warning {{array index 3 is past the end of the array (that has type 'int[2]')}} + } +}