Skip to content

Commit 0dbb785

Browse files
Improve error message for to!string when used on infinite ranges (#10744)
Partial Fix: #10559 Improve the error message to: static assert(0, "Cannot convert infinite range to string. Use `std.range.take` or `std.range.takeExactly` to make it finite."); Also add a unittest to verify that compilation fails when an infinite range is passed for string conversion. Co-authored-by: Paul Backus <snarwin@gmail.com>
1 parent 811f67e commit 0dbb785

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

std/conv.d

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,23 @@ template to(T)
543543
}
544544
}
545545

546+
private T toImpl(T, S)(S)
547+
if (isInputRange!S && isInfinite!S && isExactSomeString!T)
548+
{
549+
static assert(0, "Cannot convert infinite range to string. " ~
550+
"Use `std.range.take` or `std.range.takeExactly` to make it finite.");
551+
}
552+
553+
// Test for issue : https://github.com/dlang/phobos/issues/10559
554+
@safe pure nothrow unittest
555+
{
556+
import std.range : repeat;
557+
import std.conv : to;
558+
559+
// Test that converting an infinite range doesn't compile
560+
static assert(!__traits(compiles, repeat(1).to!string));
561+
}
562+
546563
/**
547564
If the source type is implicitly convertible to the target type, $(D
548565
to) simply performs the implicit conversion.

0 commit comments

Comments
 (0)