Bug 23259 – Visibility violation with variadic templates

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2022-07-19T08:51:51Z
Last change time
2024-12-13T19:23:50Z
Assigned to
No Owner
Creator
LucienPe
Moved to GitHub: dmd#20130 →

Comments

Comment #0 by lucien.perregaux — 2022-07-19T08:51:51Z
In the following code, private methods are called instead of public ones : ``` import std.stdio; class C(T, Args...) { private: this(int i, Args args) { assert(0, "private ctor should not be called !"); } void f(U, V...)(int i, V v) { assert(0, "private function should not be called !"); } public: this(Args args) { writeln("public ctor should be called !"); } void f(V...)(V v) { writeln("public function should be called !"); } } void main() { auto c = new C!string(8); c.f!string(8); } ``` The compiler seems to check for any public matches and then call the most specialized method, ignoring the visibility (`protected` and `package` visibilities are also affected).
Comment #1 by lucien.perregaux — 2022-07-19T11:03:49Z
In fact, the bug has nothing to do with templates, the visibility is totally ignored : ``` class C { private int h = 8; } void main() { C c = new C(); writeln(c.h); // compiles } ```
Comment #2 by lucien.perregaux — 2022-07-19T11:18:02Z
(In reply to LucienPe from comment #1) > In fact, the bug has nothing to do with templates, the visibility is totally > ignored : > > ``` > class C { private int h = 8; } > > void main() > { > C c = new C(); > writeln(c.h); // compiles > } > ``` Nevermind, the DIP states "Private class members are, technically, private module members". However, this example is still (in)valid : main.d: ``` module main; import c; import std.stdio; void main() { auto c = new C!string(8); //writeln(c.h); c.f!string(8); } ``` c.d: ``` module c; import std.stdio; class C(T, Args...) { private: this(int i, Args args) { string str = "private ctor should not be called !"; writeln(str); //assert(0, str); } void f(U, V...)(int i, V v) { string str = "private function should not be called !"; writeln(str); //assert(0, str); } public: this(Args args) { writeln("public ctor should be called !"); } void f(V...)(V v) { writeln("public function should be called !"); } } ``` and produces: ``` private ctor should not be called ! private function should not be called ! ```
Comment #3 by robert.schadek — 2024-12-13T19:23:50Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20130 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB