fix: give compile error for infinite range in format #10752
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Remaining Fix: #10559
Summary
This PR tackles the issue of infinite loop during runtime when providing format with infinte range . Now it shows compilation error for the same .
Root Cause
Passing an infinite range (e.g., 42.repeat) to format("%s", ...) would result in the format engine trying to consume the entire range, which leads to a non-terminating loop at runtime .
format did not have a guard to catch infinite ranges. This allowed constructs like :
to compile successfully but hang during execution.
Fix
This patch adds a static assert within the formatRange function that triggers at compile time if the range is detected to be infinite and the formatting is not in test mode (Writer != NoOpSink).
This ensures that formatting infinite ranges directly is disallowed unless the user explicitly converts them to finite ranges using 'std.range.take' or 'std.range.takeExactly' .
Example Reproducer
Now results in a compile-time error:
Notes
A corresponding unittest has been updated to explicitly verify this behavior using static assert(!__traits(compiles, ...)), confirming that formatting infinite ranges is correctly disallowed at compile time.
Verified manually using a custom Phobos build and test suite.
Completes the second half of issue #10559, which was partially fixed in PR #10744 .