Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 24801 - RefRange doesn’t work if range primitives are not const #9056

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 49 additions & 10 deletions std/range/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -12444,7 +12444,7 @@ public:
return (*_range).front;
}

static if (is(typeof((*(cast(const R*)_range)).front))) @property auto front() const
static if (is(typeof(((const R* r) => (*r).front)(null)))) @property auto front() const
{
return (*_range).front;
}
Expand All @@ -12470,7 +12470,7 @@ public:
return (*_range).empty;
}

static if (is(typeof((*cast(const R*)_range).empty))) @property bool empty() const
static if (is(typeof(((const R* r) => (*r).empty)(null)))) @property bool empty() const
{
return (*_range).empty;
}
Expand Down Expand Up @@ -12500,10 +12500,11 @@ public:
else static if (isForwardRange!R)
{
import std.traits : isSafe;
private alias S = typeof((*_range).save);
private alias S = typeof((() => (*_range).save)());

static if (is(typeof(((const R* r) => (*r).save)(null))))
private alias CS = typeof(((const R* r) => (*r).save)(null));

static if (is(typeof((*cast(const R*)_range).save)))
private alias CS = typeof((*cast(const R*)_range).save);

static if (isSafe!((R* r) => (*r).save))
{
Expand All @@ -12512,7 +12513,7 @@ public:
mixin(_genSave());
}

static if (is(typeof((*cast(const R*)_range).save))) @property RefRange!CS save() @trusted const
static if (is(typeof(((const R* r) => (*r).save)(null)))) @property RefRange!CS save() @trusted const
{
mixin(_genSave());
}
Expand All @@ -12524,7 +12525,7 @@ public:
mixin(_genSave());
}

static if (is(typeof((*cast(const R*)_range).save))) @property RefRange!CS save() const
static if (is(typeof(((const R* r) => (*r).save)(null)))) @property RefRange!CS save() const
{
mixin(_genSave());
}
Expand All @@ -12543,7 +12544,7 @@ public:
private static string _genSave() @safe pure nothrow
{
return `import core.lifetime : emplace;` ~
`alias S = typeof((*_range).save);` ~
`alias S = typeof((() => (*_range).save)());` ~
`static assert(isForwardRange!S, S.stringof ~ " is not a forward range.");` ~
`auto mem = new void[S.sizeof];` ~
`emplace!S(mem, cast(S)(*_range).save);` ~
Expand Down Expand Up @@ -12572,7 +12573,7 @@ public:
return (*_range).back;
}

static if (is(typeof((*(cast(const R*)_range)).back))) @property auto back() const
static if (is(typeof(((const R* r) => (*r).back)(null)))) @property auto back() const
{
return (*_range).back;
}
Expand Down Expand Up @@ -12662,7 +12663,7 @@ public:
{
return (*_range).length;
}
static if (is(typeof((*cast(const R*)_range).length))) @property auto length() const
static if (is(typeof(((const R* r) => (*r).length)(null)))) @property auto length() const
{
return (*_range).length;
}
Expand Down Expand Up @@ -13105,6 +13106,44 @@ private:
auto rr2 = refRange(&r2);
}

// https://issues.dlang.org/show_bug.cgi?id=24801
@safe unittest
{

{
static struct R
{
int front() => 0;
void popFront() {}
bool empty() => false;
}
R range;
auto r = RefRange!R(&range);
}

{
static struct R
{
size_t start, end;
size_t length() => end - start;
int opIndex(size_t i) => 0;


int front() => this[0];
int back() => this[length-1];
void popFront() { start++; }
void popBack() { end--; }
bool empty() => length == 0;
R save() const => R();
}

R range;
auto r = RefRange!R(&range);
}


}

/// ditto
auto refRange(R)(R* range)
if (isInputRange!R)
Expand Down
Loading