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:
Akshat Sharma 2025-04-15 13:04:35 +05:30 committed by GitHub
parent 811f67e95c
commit 0dbb785ae9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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.