Skip to content

Commit e49833d

Browse files
fix: give compile error for infinite range in format
Tackled the issue of infinite loop during runtime when providing format with infinte range . Now it shows compilation error for the same . fixes : #10559
1 parent 0dbb785 commit e49833d

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

std/format/internal/write.d

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,12 @@ if (isInputRange!T)
14121412
// if the format spec is valid
14131413
enum formatTestMode = is(Writer == NoOpSink);
14141414

1415+
static if (!formatTestMode && isInfinite!T)
1416+
{
1417+
static assert(!isInfinite!T, "Cannot format an infinite range. " ~
1418+
"Convert it to a finite range first using `std.range.take` or `std.range.takeExactly`.");
1419+
}
1420+
14151421
// Formatting character ranges like string
14161422
if (f.spec == 's')
14171423
{
@@ -1582,18 +1588,16 @@ if (isInputRange!T)
15821588
throw new FormatException(text("Incorrect format specifier for range: %", f.spec));
15831589
}
15841590

1585-
// https://issues.dlang.org/show_bug.cgi?id=20218
15861591
@safe pure unittest
15871592
{
1588-
void notCalled()
1589-
{
1590-
import std.range : repeat;
1593+
import std.range : repeat;
1594+
import std.format : format;
15911595

1592-
auto value = 1.repeat;
1596+
auto value = 1.repeat;
15931597

1594-
// test that range is not evaluated to completion at compiletime
1595-
format!"%s"(value);
1596-
}
1598+
// This should fail to compile — so we assert that it *doesn't* compile
1599+
static assert(!__traits(compiles, format!"%s"(value)),
1600+
"Test failed: formatting an infinite range should not compile.");
15971601
}
15981602

15991603
// character formatting with ecaping

0 commit comments

Comments
 (0)