Bug 10653 – non-shared pure constructors can conflict with shared constructors

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-07-16T13:23:19Z
Last change time
2024-12-13T18:09:31Z
Assigned to
No Owner
Creator
Jonathan M Davis
Moved to GitHub: dmd#18633 →

Comments

Comment #0 by issues.dlang — 2013-07-16T13:23:19Z
This code compiles and runs just fine import core.sync.mutex; import std.stdio; class FixedList(T){ // list private T[] list; // number of items private size_t numberOfItems; // capacity private size_t capacity; // mutex private Mutex listMutex; // get capacity @property public size_t Capacity(){ return capacity; } @property public shared size_t Capacity(){ return capacity; } // constructor public this( size_t capacity ){ // initialise numberOfItems = 0; this.capacity = capacity; writeln("Cons Normal"); } // constructor public shared this( size_t capacity ){ // initialise numberOfItems = 0; this.capacity = capacity; // create mutex listMutex = cast(shared)(new Mutex()); writeln("Cons Shared"); } } void main() { auto list1 = new shared FixedList!int( 128 ); auto list2 = new FixedList!int( 128 ); } as it should. But if you comment out the calls to writeln (making the constructors pure), then you get this compilation error q.d(45): Error: called with argument types: (int) shared matches both: q.d(22): q.FixedList!int.FixedList.this(ulong capacity) and: q.d(31): q.FixedList!int.FixedList.this(ulong capacity) q.d(45): Error: no constructor for FixedList because the compiler can now use either constructor to construct a shared object thanks to the fact that it knows that the constructors will return unique objects. So, this the usability improvement of making pure constructors able to construct shared or immutable objects is biting us here. I would suggest that in cases where one of the constructors can construct a shared (or immutable) object thanks to its purity and a constructor which is explictly shared (or immutable) exists, the one which is explicitly shared (or immutable) should take precedence.
Comment #1 by issues.dlang — 2013-07-16T13:30:15Z
Comment #2 by robert.schadek — 2024-12-13T18:09:31Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18633 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB