From 05e4846d9d0a3fe4b3f8c7ce682bf54b662a670c Mon Sep 17 00:00:00 2001 From: Nicholas Wilson Date: Sun, 25 Aug 2024 15:10:23 +0800 Subject: [PATCH] Fix bugzilla issue 24716 checks for `isNeedThisScope` should check up the inheritance chain for nested classes. --- compiler/src/dmd/expressionsem.d | 10 ++++++++ compiler/test/runnable/issue24716.d | 38 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 compiler/test/runnable/issue24716.d diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index 0005ea71b26d..fd1413867a3c 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -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; + return false; + } if (ad2 == ad) return false; + else if (isSubclassOf(ad2.isClassDeclaration(), ad.isClassDeclaration())) + return false; else if (ad2.isNested()) continue; else diff --git a/compiler/test/runnable/issue24716.d b/compiler/test/runnable/issue24716.d new file mode 100644 index 000000000000..431ca84334a2 --- /dev/null +++ b/compiler/test/runnable/issue24716.d @@ -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(); +}