Bug 21129 – [REG2.090] std.range.only broken for reference conversions of local copies of parameters
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-08-06T13:45:13Z
Last change time
2020-09-10T12:58:32Z
Keywords
industry, pull
Assigned to
No Owner
Creator
johanengelen
Comments
Comment #0 by johanengelen — 2020-08-06T13:45:13Z
std.range.only was broken by this PR: https://github.com/dlang/phobos/pull/7253
The problem is that inside only there is a conversion of types if the parameters do not all have the same type but can be converted to a common type. After this PR, this conversion happens on the local copy and then the local copy is thrown away (OnlyResult copies the converted value).
Example of code that broke (added some debug lines that show the pointer values to understand what is going wrong):
```
import std.range;
import std.stdio;
import std.algorithm;
void main()
{
// Comment-out one of the two to test the other, both are broken.
broken_staticarray();
broken_fixedstring();
}
void broken_staticarray()
{
char[9] something = "something";
writeln(__LINE__,":", cast(size_t)something.ptr);
auto range = only("one", something, "three");
foreach (ref v; range) {
writeln(__LINE__,":", cast(size_t)v.ptr, " ,", v.length);
}
assert(only("one", something, "three").joiner(" ").equal("one something three"));
}
struct FixedLengthArray(T, size_t capacity_)
{
enum capacity = capacity_;
private size_t _length;
private T[capacity] _elems;
this(U)(U val) {
opAssign(val);
}
auto ref opAssign(U)(U[] arr) {
_length = arr.length;
_elems[0 .. _length] = arr;
}
@property inout(T)[] slice() inout nothrow @nogc pure @safe {
return _elems[0 .. _length];
}
alias slice this;
}
alias FixedString = FixedLengthArray!(char, 256);
void broken_fixedstring()
{
FixedString something = "something";
writeln(__LINE__,":", cast(size_t)something.slice.ptr);
auto range = only("one", something, "three");
foreach (ref v; range) {
writeln(__LINE__,":", cast(size_t)v.ptr, " ,", v.length);
}
assert(only("one", something, "three").joiner(" ").equal("one something three"));
}
```
Comment #1 by dlang-bot — 2020-08-07T05:08:55Z
@FeepingCreature created dlang/phobos pull request #7584 "Fix issue 21129: Make `OnlyResult` store a value tuple instead of a static array of CommonType." fixing this issue:
- Fix issue 21129: Make `OnlyResult` store the actual types passed to it as a tuple, instead of a static array of CommonType.
Remove the unittest that verifies that `OnlyResult` doesn't depend on argument order - since it now does.
Add unittest for issue 21129.
https://github.com/dlang/phobos/pull/7584
Comment #2 by dlang-bot — 2020-08-17T17:02:11Z
dlang/phobos pull request #7584 "Fix issue 21129: Make `OnlyResult` store a value tuple instead of a static array of CommonType." was merged into stable:
- 89109110d96651ca0718bc040cdc2a9c6aa3d868 by Mathis Beer:
Fix issue 21129: Make `OnlyResult` store the actual types passed to it as a tuple, instead of a static array of CommonType.
Remove the unittest that verifies that `OnlyResult` doesn't depend on argument order - since it now does.
Add unittest for issue 21129.
https://github.com/dlang/phobos/pull/7584
Comment #3 by dlang-bot — 2020-09-10T12:58:32Z
dlang/phobos pull request #7623 "merge stable" was merged into master:
- 1f1a80d0a0cb7e61cea174a708436d44fab391c4 by FeepingCreature:
Fix issue 21129: Make `OnlyResult` store the actual types passed to it as a tuple, instead of a static array of CommonType. (#7584)
Remove the unittest that verifies that `OnlyResult` doesn't depend on argument order - since it now does.
Add unittest for issue 21129.
https://github.com/dlang/phobos/pull/7623