Last active
August 29, 2015 14:22
-
-
Save Hackerpilot/d665a0d5c80ddc163454 to your computer and use it in GitHub Desktop.
DIP 25 bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// rdmd -unittest -main -dip25 dip25.d | |
private struct Container | |
{ | |
this(int c) @safe | |
{ | |
arr = new int[](c); | |
arr[] = c; | |
} | |
~this() @safe | |
{ | |
arr[] = -1; | |
} | |
ref Range opSlice() return @safe // remove "return" and this code correctly fails to compile | |
{ | |
r.index = 0; | |
r.c = &this; | |
return r; | |
} | |
@safe struct Range | |
{ | |
int front() { return c.arr[index]; } | |
bool empty() { return index >= c.arr.length; } | |
void popFront() { index++; } | |
size_t index; | |
Container* c; | |
} | |
private: | |
Range r; | |
int[] arr; | |
} | |
private struct S | |
{ | |
void takesContainer(ref Container c) @safe | |
{ | |
this.r = c[]; | |
} | |
void print() @safe | |
{ | |
import std.stdio:writeln; | |
writeln(r); | |
} | |
Container.Range r; | |
} | |
void doStuff(ref S s) @safe | |
{ | |
auto c = Container(20); | |
s.takesContainer(c); | |
} | |
@safe unittest | |
{ | |
S s; | |
doStuff(s); | |
s.print(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment