Bug 24279 – Conflicting constructors/functions due to default arguments should not compile

Status
NEW
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2023-12-12T17:06:49Z
Last change time
2024-12-13T19:32:09Z
Assigned to
No Owner
Creator
HuskyNator
Moved to GitHub: dmd#20369 →

Comments

Comment #0 by HuskyNator — 2023-12-12T17:06:49Z
When constructors or functions conflict due to having the same calling signature, they should not be allowed to compile. This is already the case in most scenarios, but when default arguments cause conflict, the compiler does not catch this issue. Even more so, in the case of constructors, visibility rules are ignored. Example: ```d module text; import std.stdio; class Text { string text; private this(string text) { writeln("private"); } this(string filename, string arg2 = ".txt") { writeln("public"); } } ``` ```d module app; void main(){ Text t = new Text("file"); // writes "private" } ``` Calling `new Text("test")` from anywhere will call the first constructor, ignoring its `private` attribute. If this is done with functions instead of constructors, the same thing will happen with one exception: the compiler will state the function is not accessible. (It will however try to resolve to the private function, ignoring the accessible alternative) 1. This hints at issues with constructors ignoring visibility attributes! 2. The code should not compile when conflicts occur (even when this is due to default values).
Comment #1 by destructionator — 2023-12-12T17:09:42Z
I think it has to do with overload resolution not considering the visibility
Comment #2 by robert.schadek — 2024-12-13T19:32:09Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20369 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB