Bug 17298 – Wrong deprecation warnings about derived class accessing private method

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2017-04-05T11:16:31Z
Last change time
2023-05-18T09:01:49Z
Keywords
diagnostic
Assigned to
No Owner
Creator
Maksim Zholudev

Comments

Comment #0 by maximzms — 2017-04-05T11:16:31Z
Test code: a.d: ---------- module a; class Base { private void func() {} } void foo(T)() { new T().func(); } // line 8 struct Wrap(T) { void boo() { new T().func(); } // line 12 } class Cover(T) { void boo() { new T().func(); } // line 17 } ---------- b.d: ---------- import a; class Derived : Base {} void main() { foo!Base(); // OK foo!Derived(); // Deprecation warning alias A = Wrap!Base; // OK alias B = Wrap!Derived; // Deprecation warning alias C = Cover!Base; // OK alias D = Cover!Derived; // Deprecation warning } ---------- dmd a b ---------- a.d(8): Deprecation: a.Base.func is not visible from class Derived a.d(12): Deprecation: a.Base.func is not visible from class Derived a.d(17): Deprecation: a.Base.func is not visible from class Derived ----------
Comment #1 by ag0aep6g — 2017-04-05T11:32:39Z
The test case doesn't need templates. a.d: ---- import b: Derived; class Base { private void func() {} } void foo() { new Derived().func(); } ---- b.d: ---- import a: Base; class Derived : Base {} ---- `dmd -c a b`: ---- a.d(8): Deprecation: a.Base.func is not visible from class Derived ----
Comment #2 by razvan.nitu1305 — 2023-05-18T09:01:49Z
This bug report is not valid. In D, the encapsulation unit is the module. Therefore, since Derived is defined in a different module than Base, the private method cannot be accessed through it. If you want derived to have access to `func` just define it in the same module as `Base`. Otherwise, you can just access `func` through Base: `new T().Base.func()`.