From 568cd18b43fb2fe98a5eb62f06a00648450d7068 Mon Sep 17 00:00:00 2001 From: Ulrich Kuettler Date: Tue, 17 Feb 2015 20:50:19 +0100 Subject: [PATCH] Fix Issue 13390 - Be explicit about non-empty input and fail early Add unittest --- std/range/package.d | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/std/range/package.d b/std/range/package.d index db9fc9ea557..5c4000df373 100644 --- a/std/range/package.d +++ b/std/range/package.d @@ -2473,6 +2473,8 @@ random access and also offers a constructor taking an initial position $(D index). $(D Cycle) works with static arrays in addition to ranges, mostly for performance reasons. +Note: The input range must not be empty. + Tip: This is a great way to implement simple circular buffers. */ struct Cycle(R) @@ -2698,6 +2700,7 @@ nothrow: Cycle!R cycle(R)(R input) if (isForwardRange!R && !isInfinite!R) { + assert(!input.empty); return Cycle!R(input); } @@ -2713,6 +2716,7 @@ Cycle!R cycle(R)(R input) Cycle!R cycle(R)(R input, size_t index = 0) if (isRandomAccessRange!R && !isInfinite!R) { + assert(!input.empty); return Cycle!R(input, index); } @@ -2890,6 +2894,14 @@ unittest //10845 auto a = recurrence!q{a[n - 1] ~ a[n - 2]}("1", "0"); } +// Issue 13390 +unittest +{ + import std.exception; + import core.exception : AssertError; + assertThrown!AssertError(cycle([0, 1, 2][0..0])); +} + private alias lengthType(R) = typeof(R.init.length.init); /**