Skip to content

Commit 4367298

Browse files
committed
Traverse full chain of nested aggregates when resolving a nested variable
Fixes #3556.
1 parent 4ec8fc0 commit 4367298

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

gen/nested.cpp

+13-17
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ DValue *DtoNestedVariable(Loc &loc, Type *astype, VarDeclaration *vd,
6666
if (currentCtx) {
6767
Logger::println("Using own nested context of current function");
6868
ctx = currentCtx;
69-
} else if (irfunc->decl->isMember2()) {
69+
} else if (AggregateDeclaration *ad = irfunc->decl->isMember2()) {
7070
Logger::println(
7171
"Current function is member of nested class, loading vthis");
72-
73-
AggregateDeclaration *cd = irfunc->decl->isMember2();
74-
LLValue *val = irfunc->thisArg;
75-
if (cd->isClassDeclaration()) {
76-
val = DtoLoad(val);
72+
LLValue *val =
73+
ad->isClassDeclaration() ? DtoLoad(irfunc->thisArg) : irfunc->thisArg;
74+
for (; ad; ad = ad->toParent2()->isAggregateDeclaration()) {
75+
assert(ad->vthis);
76+
val = DtoLoad(DtoGEP(val, 0, getVthisIdx(ad), ".vthis"));
7777
}
78-
ctx = DtoLoad(DtoGEP(val, 0, getVthisIdx(cd), ".vthis"));
78+
ctx = val;
7979
skipDIDeclaration = true;
8080
} else {
8181
Logger::println("Regular nested function, using context arg");
@@ -455,17 +455,13 @@ void DtoCreateNestedContext(FuncGenState &funcGen) {
455455
LLValue *src = irFunc.nestArg;
456456
if (!src) {
457457
assert(irFunc.thisArg);
458-
assert(fd->isMember2());
459-
LLValue *thisval = DtoLoad(irFunc.thisArg);
460-
AggregateDeclaration *cd = fd->isMember2();
461-
assert(cd);
462-
assert(cd->vthis);
458+
AggregateDeclaration *ad = fd->isMember2();
459+
assert(ad);
460+
assert(ad->vthis);
461+
LLValue *thisptr =
462+
ad->isClassDeclaration() ? DtoLoad(irFunc.thisArg) : irFunc.thisArg;
463463
IF_LOG Logger::println("Indexing to 'this'");
464-
if (cd->isStructDeclaration()) {
465-
src = DtoExtractValue(thisval, getVthisIdx(cd), ".vthis");
466-
} else {
467-
src = DtoLoad(DtoGEP(thisval, 0, getVthisIdx(cd), ".vthis"));
468-
}
464+
src = DtoLoad(DtoGEP(thisptr, 0, getVthisIdx(ad), ".vthis"));
469465
}
470466
if (depth > 1) {
471467
src = DtoBitCast(src, getVoidPtrType());

tests/codegen/nested_gh3556.d

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// https://github.com/ldc-developers/ldc/issues/3556
2+
// RUN: %ldc -run %s
3+
4+
class C {
5+
void func() {
6+
import core.stdc.stdio;
7+
printf("hello world!\n");
8+
}
9+
void run1() {
10+
class C2 {
11+
class C3 {
12+
void run3() {
13+
func();
14+
}
15+
}
16+
void run2() {
17+
auto c3 = new C3;
18+
c3.run3();
19+
}
20+
}
21+
auto c2 = new C2;
22+
c2.run2();
23+
}
24+
}
25+
26+
void main() {
27+
auto c = new C;
28+
c.run1();
29+
}

0 commit comments

Comments
 (0)