Skip to content

Commit

Permalink
Fix bugzilla issue 24716
Browse files Browse the repository at this point in the history
checks for `isNeedThisScope` should check up the inheritance chain for nested classes.
  • Loading branch information
thewilsonator committed Aug 25, 2024
1 parent 97b71f1 commit 05e4846
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,18 @@ private bool isNeedThisScope(Scope* sc, Declaration d)
//printf("\ts = %s %s, toParent2() = %p\n", s.kind(), s.toChars(), s.toParent2());
if (AggregateDeclaration ad2 = s.isAggregateDeclaration())
{
bool isSubclassOf(ClassDeclaration base, ClassDeclaration sub)
{
if (!base || !sub) return false;
for (ClassDeclaration cd = sub; cd; cd = cd.baseClass)
if (cd == base)
return true;

Check warning on line 122 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L122

Added line #L122 was not covered by tests
return false;
}
if (ad2 == ad)
return false;
else if (isSubclassOf(ad2.isClassDeclaration(), ad.isClassDeclaration()))
return false;

Check warning on line 128 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L128

Added line #L128 was not covered by tests
else if (ad2.isNested())
continue;
else
Expand Down
38 changes: 38 additions & 0 deletions compiler/test/runnable/issue24716.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// https://issues.dlang.org/show_bug.cgi?id=24716
int i = 1;
void foo(int arg)
{
assert( arg == 100*i);
i++;
}
class Outer1 {
int value1 = 100;

class Inner1 {
void print() {
foo(value1);
}
}

Inner1 make() { return new Inner1; }
}

class Outer2 : Outer1 {
int value2 = 200;

class Inner2 : Outer1.Inner1 {
override void print() {
foo(value1); // <- no problem!
foo(value2); // error: accessing non-static variable `value2` requires an instance of `Outer2`
}
}

override Inner2 make() { return new Inner2; }
}

void main()
{
auto oouter = new Outer2;
auto iinner = oouter.make();
iinner.print();
}

0 comments on commit 05e4846

Please sign in to comment.