Skip to content

Commit a2213c5

Browse files
committed
[clang][Interp] Fix init chain in local initializers
1 parent 8ce1aed commit a2213c5

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/lib/AST/Interp/Compiler.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3577,6 +3577,7 @@ VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD, bool Topleve
35773577
return !Init || (checkDecl() && initGlobal(*GlobalIndex));
35783578
} else {
35793579
VariableScope<Emitter> LocalScope(this, VD);
3580+
InitLinkScope<Emitter> ILS(this, InitLink::Decl(VD));
35803581

35813582
if (VarT) {
35823583
unsigned Offset = this->allocateLocalPrimitive(
@@ -3911,7 +3912,8 @@ bool Compiler<Emitter>::VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {
39113912
SourceLocScope<Emitter> SLS(this, E);
39123913

39133914
bool Old = InitStackActive;
3914-
InitStackActive = !isa<FunctionDecl>(E->getUsedContext());
3915+
InitStackActive =
3916+
!(E->getUsedContext()->getDeclKind() == Decl::CXXConstructor);
39153917
bool Result = this->delegate(E->getExpr());
39163918
InitStackActive = Old;
39173919
return Result;

clang/test/AST/Interp/records.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,3 +1482,15 @@ namespace FloatAPValue {
14821482
ClassTemplateArgRefTemplate<ClassTemplateArgObj.Arg> ClassTemplateArgRefObj;
14831483
}
14841484
#endif
1485+
1486+
namespace LocalWithThisPtrInit {
1487+
struct S {
1488+
int i;
1489+
int *p = &i;
1490+
};
1491+
constexpr int foo() {
1492+
S s{2};
1493+
return *s.p;
1494+
}
1495+
static_assert(foo() == 2, "");
1496+
}

0 commit comments

Comments
 (0)