Bug 23844 – chain(only) doesn't support immutable structs
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2023-04-18T11:41:58Z
Last change time
2023-05-10T09:54:17Z
Keywords
pull
Assigned to
No Owner
Creator
FeepingCreature
Comments
Comment #0 by default_357-line — 2023-04-18T11:41:58Z
Consider this code:
```
void main() {
import std.array : array;
import std.range : chain, only;
struct S
{
immutable int value;
}
auto range = chain(only(S(5)), only(S(6)));
assert(range.array == [S(5), S(6)]);
}
```
It errors with a flood of
```
std/range/package.d(1172): Error: cannot modify struct instance `result.__source_field_0` of type `OnlyResult!(S)` because it contains `const` or `immutable` members
```
If I remove `immutable`, it works.
Comment #1 by dlang-bot — 2023-04-18T14:55:11Z
@FeepingCreature updated dlang/phobos pull request #8736 "Fix issue 23844: Support ranges with immutable fields (like `only` with `immutable struct`) in `chain`." fixing this issue:
- Fix issue 23844: Support ranges with immutable fields (like `only` with `immutable struct`) in `chain`.
`only` is a range that may be *mutable*, but not *assignable*. `chain` falls over here because it assumes it can make a struct with ranges, and reassign them with new values, which isn't necessarily the case even if the ranges are not `const`.
Solved by creating a separate tuple of `Rebindable` ranges for this case.
https://github.com/dlang/phobos/pull/8736
Comment #2 by dlang-bot — 2023-05-10T09:54:17Z
dlang/phobos pull request #8736 "Fix issue 23844: Support ranges with immutable fields (like `only` with `immutable struct`) in `chain`." was merged into master:
- b9c6e3ca4b6fec799964394ad3e080d7b347f658 by Mathis Beer:
Fix issue 23844: Support ranges with immutable fields (like `only` with `immutable struct`) in `chain`.
`only` is a range that may be *mutable*, but not *assignable*. `chain` falls over here because it assumes it can make a struct with ranges, and reassign them with new values, which isn't necessarily the case even if the ranges are not `const`.
Solved by creating a separate tuple of `Rebindable` ranges for this case.
https://github.com/dlang/phobos/pull/8736