Bug 24090 – struct private constructor call outside module should not be allowed

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2023-08-18T03:36:39Z
Last change time
2023-09-20T10:20:59Z
Keywords
accepts-invalid
Assigned to
No Owner
Creator
Mike S

Comments

Comment #0 by Michael.Shah — 2023-08-18T03:36:39Z
I have two files below in two different modules and a private constructor that should NOT be called. It looks like private does not prevent a constructor from being called. 'private' does work for member functions fine however. Testing on DMD64 D Compiler v2.102.1 (I did not see anything on changelog otherwise fixing this for earlier versions) ``` // main.d module main; import std.stdio; import student; void main(){ // Works as expected -- public constructor // auto s = Student("mike",1234); // works // writeln(s); // Should not work, constructor is private auto s2 = Student(cast(string)"sue"); // should not work writeln(s2); } ``` ``` // student.d module student; import std.stdio; struct Student{ string name; this(string _name, int _id){ writeln("this(string _name,int _id"); this(_name); } private this(string _name){ writeln("this(string _name)"); name = _name; } this(int _id){ writeln("this(int _id)"); id = _id; } private: int id; } ``` This looks like in 2018 there was an attempt to fix this, but not sure if it was actually fixed or merged. https://issues.dlang.org/show_bug.cgi?id=18979
Comment #1 by nick — 2023-08-19T11:08:18Z
Confirmed, also if the `this(int _id)` ctor is commented out then main.d no longer compiles. The two parameter ctor is not needed to reproduce this AFAICT.
Comment #2 by razvan.nitu1305 — 2023-09-20T10:19:26Z
I cannot reproduce this with git master. I get: main.d(12): Error: constructor `student.Student.this` of type `ref Student(string _name)` is not accessible from module `main` @Mike @Nick what compiler versions are you using? Closing as WORKSFORME.
Comment #3 by razvan.nitu1305 — 2023-09-20T10:20:59Z
It seems that this PR: https://github.com/dlang/dmd/pull/15282 has fixed it.