mirror of
https://github.com/dlang/phobos.git
synced 2025-04-26 13:10:35 +03:00
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>
This commit is contained in:
parent
811f67e95c
commit
0dbb785ae9
1 changed files with 17 additions and 0 deletions
17
std/conv.d
17
std/conv.d
|
@ -543,6 +543,23 @@ template to(T)
|
|||
}
|
||||
}
|
||||
|
||||
private T toImpl(T, S)(S)
|
||||
if (isInputRange!S && isInfinite!S && isExactSomeString!T)
|
||||
{
|
||||
static assert(0, "Cannot convert infinite range to string. " ~
|
||||
"Use `std.range.take` or `std.range.takeExactly` to make it finite.");
|
||||
}
|
||||
|
||||
// Test for issue : https://github.com/dlang/phobos/issues/10559
|
||||
@safe pure nothrow unittest
|
||||
{
|
||||
import std.range : repeat;
|
||||
import std.conv : to;
|
||||
|
||||
// Test that converting an infinite range doesn't compile
|
||||
static assert(!__traits(compiles, repeat(1).to!string));
|
||||
}
|
||||
|
||||
/**
|
||||
If the source type is implicitly convertible to the target type, $(D
|
||||
to) simply performs the implicit conversion.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue