1 // https://bugzilla.gdcproject.org/show_bug.cgi?id=283 2 // { dg-do run } 3 // { dg-skip-if "needs gcc/config.d" { ! d_runtime } } 4 5 struct Impl 6 { 7 size_t _count; 8 } 9 10 struct RefCountedStore 11 { 12 Impl* _store; 13 initialize()14 void initialize() 15 { 16 import core.stdc.stdlib : malloc; 17 _store = cast(Impl*) malloc(Impl.sizeof); 18 _store._count = 1; 19 } 20 isInitialized()21 bool isInitialized() 22 { 23 return _store !is null; 24 } 25 ensureInitialized()26 void ensureInitialized() 27 { 28 if (!isInitialized) 29 initialize(); 30 } 31 } 32 33 struct RefCounted14443 34 { 35 RefCountedStore _refCounted; 36 thisRefCounted1444337 this(int) 38 { 39 _refCounted.initialize(); 40 } 41 thisRefCounted1444342 this(this) 43 { 44 ++_refCounted._store._count; 45 } 46 ~thisRefCounted1444347 ~this() 48 { 49 if (--_refCounted._store._count) 50 return; 51 52 import core.stdc.stdlib : free; 53 free(_refCounted._store); 54 _refCounted._store = null; 55 } 56 refCountedPayloadRefCounted1444357 int refCountedPayload() 58 { 59 _refCounted.ensureInitialized(); 60 return 1; 61 } 62 } 63 64 struct PathRange14443 65 { 66 RefCounted14443 path; 67 front()68 @property PathElement14443 front() 69 { 70 return PathElement14443(this, path.refCountedPayload()); 71 } 72 } 73 74 struct PathElement14443 75 { 76 PathRange14443 range; 77 thisPathElement1444378 this(PathRange14443 range, int) 79 { 80 this.range = range; 81 } 82 } 83 main()84void main() 85 { 86 auto path = RefCounted14443(12); 87 if (path._refCounted._store._count != 1) 88 assert(0); 89 { 90 auto _r = PathRange14443(path); 91 if (path._refCounted._store._count != 2) 92 assert(0); 93 { 94 auto element = _r.front; 95 if (path._refCounted._store._count != 3) 96 assert(0); 97 } 98 if (path._refCounted._store._count != 2) 99 assert(0); 100 } 101 if (path._refCounted._store._count != 1) 102 assert(0); 103 } 104