Bug 19229 – formattedWrite destructively iterates over forward ranges
Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2018-09-06T09:16:04Z
Last change time
2019-12-07T10:03:45Z
Keywords
pull
Assigned to
No Owner
Creator
Simen Kjaeraas
Comments
Comment #0 by simen.kjaras — 2018-09-06T09:16:04Z
unittest {
import std.conv;
import std.algorithm;
auto a = [sort([1,2,3])];
assert(text(a) == text(a));
}
The above assert triggers. The issue is std.format.formatRange destructively iterates over a[i], leaving an exhausted range behind.
std.format.formatRange or its callers need to be made aware of isForwardRange.
In general, if you pass a range by value, then it's copied, and you have to assume that the original is then unusable, because the semantics of copying a range are unspecified and can vary wildly depending on the range type. If you want to pass a range to a function and then continue to use it (including passing it to another function in the same expression), then you need to call save. IMHO, there is no bug here. If you want to do anything with the range after passing it to text, then you need to call save on it when passing it.
Comment #3 by bugzilla — 2019-12-07T10:03:45Z
IMHO this is part of the we-need-some-improvement-on-ranges thing, which I think will not be done before the transition to D3. Up to then, we have to do it the way Jonathan M Davis write in comment 2.