Skip to content

fix: give compile error for infinite range in format #10752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2025

Conversation

DeterminedSage
Copy link
Contributor

@DeterminedSage DeterminedSage commented Apr 17, 2025

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 :

auto s = format("%s", 42.repeat); // hangs forever

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

    static if (!formatTestMode && isInfinite!T)
    {
        static assert(!isInfinite!T, "Cannot format an infinite range. " ~
            "Convert it to a finite range first using `std.range.take` or `std.range.takeExactly`.");
    }

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

import std.format, std.range;

void main() {
    auto r = 1.repeat;
    auto s = format("%s", r); // used to hang at runtime
}

Now results in a compile-time error:

Error: static assert:  "Cannot format an infinite range. Convert it to a finite range first using `std.range.take` or `std.range.takeExactly`."

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 .

Tackled the issue of infinite loop during runtime when providing
format with infinte range . Now it shows compilation error for
the same .

fixes : dlang#10559
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @DeterminedSage! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#10752"

@rikkimax rikkimax added the Merge:72h no objection -> merge The PR will be merged if there are no objections raised. label Apr 21, 2025
@rikkimax
Copy link
Contributor

If no one has any opinions on this, I've set it to merge in 72 hours.
Feel free to remove tag/merge if desired.

@jmdavis jmdavis merged commit 5a142da into dlang:master Apr 25, 2025
10 checks passed
@jmdavis
Copy link
Member

jmdavis commented Apr 25, 2025

It would appear that there's something wrong with the 72h merge label.

@DeterminedSage
Copy link
Contributor Author

It would appear that there's something wrong with the 72h merge label.

Seems to be the case , was about to ping you

@0xEAB
Copy link
Member

0xEAB commented Apr 25, 2025

Like Nic once told me:

[…] the bot has always been a bit flakey

@schveiguy
Copy link
Member

AFAIK, the 72h merge is a label only, the bot doesn't look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Merge:72h no objection -> merge The PR will be merged if there are no objections raised.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

to!string and format don’t handle infinite ranges
6 participants