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