Skip to content

Commit 491e4cc

Browse files
ntrelthewilsonator
authored andcommitted
Fix: std.range.cycle with a forward-range does not compile
Fixes #10852.
1 parent 64c2c92 commit 491e4cc

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

std/range/package.d

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4446,7 +4446,7 @@ if (isForwardRange!R && !isInfinite!R)
44464446
return _original[_index];
44474447
}
44484448

4449-
static if (is(typeof((cast(const R)_original)[_index])))
4449+
static if (__traits(compiles, (const R r) => r[0]))
44504450
{
44514451
/// ditto
44524452
@property auto ref front() const
@@ -4484,8 +4484,8 @@ if (isForwardRange!R && !isInfinite!R)
44844484
return _original[(n + _index) % _original.length];
44854485
}
44864486

4487-
static if (is(typeof((cast(const R)_original)[_index])) &&
4488-
is(typeof((cast(const R)_original).length)))
4487+
static if (__traits(compiles, (const R r) => r[0]) &&
4488+
__traits(compiles, (const R r) => r.length))
44894489
{
44904490
/// ditto
44914491
auto ref opIndex(size_t n) const
@@ -4559,7 +4559,7 @@ if (isForwardRange!R && !isInfinite!R)
45594559
return _current.front;
45604560
}
45614561

4562-
static if (is(typeof((cast(const R)_current).front)))
4562+
static if (__traits(compiles, (const R r) => r.front))
45634563
{
45644564
/// ditto
45654565
@property auto ref front() const
@@ -4948,6 +4948,23 @@ pure @safe unittest
49484948
}
49494949
}
49504950

4951+
// https://github.com/dlang/phobos/issues/10852
4952+
@safe unittest
4953+
{
4954+
// forward range
4955+
struct R
4956+
{
4957+
int i;
4958+
int front() => i;
4959+
bool empty() => i == 0;
4960+
void popFront() {--i;}
4961+
R save() => this;
4962+
}
4963+
4964+
auto r = R(10).cycle.take(20);
4965+
assert(r.array == [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
4966+
}
4967+
49514968
private alias lengthType(R) = typeof(R.init.length.init);
49524969

49534970
/**

0 commit comments

Comments
 (0)