Skip to content

Commit 3a31eae

Browse files
committed
[clang][Interp] Fix refers_to_enclosing_variable_or_capture DREs
They do not count into lambda captures, so visit them lazily.
1 parent 5d6acf8 commit 3a31eae

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3894,6 +3894,13 @@ bool ByteCodeExprGen<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
38943894
if (IsPtr)
38953895
return this->emitGetThisFieldPtr(Offset, E);
38963896
return this->emitGetPtrThisField(Offset, E);
3897+
} else if (const auto *DRE = dyn_cast<DeclRefExpr>(E);
3898+
DRE && DRE->refersToEnclosingVariableOrCapture() &&
3899+
isa<VarDecl>(D)) {
3900+
if (!this->visitVarDecl(cast<VarDecl>(D)))
3901+
return false;
3902+
// Retry.
3903+
return this->visitDeclRef(D, E);
38973904
}
38983905

38993906
// Try to lazily visit (or emit dummy pointers for) declarations

clang/test/AST/Interp/lambda.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,6 @@ namespace CaptureDefaults {
264264
};
265265
static_assert(f2() == 3, "");
266266
}
267+
268+
constexpr auto t4 = ([x=42]() consteval { return x; }());
269+
static_assert(t4 == 42, "");

0 commit comments

Comments
 (0)