D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10753 - std.array.array of a range of structs with immutable fields too
Summary: std.array.array of a range of structs with immutable fields too
Status: RESOLVED DUPLICATE of issue 9528
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2013-08-03 07:02 UTC by bearophile_hugs
Modified: 2013-08-28 02:17 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2013-08-03 07:02:11 UTC
import std.algorithm: map;
import std.array: array;
struct Foo {
    immutable dchar d;
}
struct Bar {
    immutable int x;
}
void main() {
    "12".map!Foo.array;
    [1, 2].map!Bar.array;
}



DMD 2.064alpha gives:

...\dmd2\src\phobos\std\array.d(2309): Error: cannot modify struct delegate Foo[]()

{

return (cast(Foo*)(*this._data).arr)[len..len + 1u];

}

()[0u] Foo with immutable members
...\dmd2\src\phobos\std\array.d(69): Error: template instance std.array.Appender!(Foo[]).Appender.put!(Foo) error instantiating
temp.d(10):        instantiated from here: array!(MapResult!(Foo, string))
temp.d(10): Error: template instance std.array.array!(MapResult!(Foo, string)) error instantiating
...\dmd2\src\phobos\std\array.d(58): Error: cannot modify struct result[i] Bar with immutable members
temp.d(11): Error: template instance std.array.array!(MapResult!(Bar, int[])) error instantiating
Comment 1 monarchdodra 2013-08-05 03:06:51 UTC
(In reply to comment #0)
> import std.algorithm: map;
> import std.array: array;
> struct Foo {
>     immutable dchar d;
> }
> struct Bar {
>     immutable int x;
> }
> void main() {
>     "12".map!Foo.array;
>     [1, 2].map!Bar.array;
> }
> 
> 
> 
> DMD 2.064alpha gives:
> 
> ...\dmd2\src\phobos\std\array.d(2309): Error: cannot modify struct delegate
> Foo[]()
> 
> {
> 
> return (cast(Foo*)(*this._data).arr)[len..len + 1u];
> 
> }
> 
> ()[0u] Foo with immutable members
> ...\dmd2\src\phobos\std\array.d(69): Error: template instance
> std.array.Appender!(Foo[]).Appender.put!(Foo) error instantiating
> temp.d(10):        instantiated from here: array!(MapResult!(Foo, string))
> temp.d(10): Error: template instance std.array.array!(MapResult!(Foo, string))
> error instantiating
> ...\dmd2\src\phobos\std\array.d(58): Error: cannot modify struct result[i] Bar
> with immutable members
> temp.d(11): Error: template instance std.array.array!(MapResult!(Bar, int[]))
> error instantiating

This is an interesting scenario, as it reveals bugs in several steps of the "tool chain". In regards to "Bar", the first bug is in "std.array.array", and will soon be fixed. Unfortunately, it will pass through std.conv.emplace, which is not fixed, so it will stop working there.

I have an open pull for fixing said conv, but I'm unsure it handles this scenario (I'm pretty confident it actually *doesn't*). I'll double check, but I don't think I'll make any changes to it to support this scenario, I'd rather make slow but confirmed steps to improving emplace.

Still, this is an interesting bug. I'll check back to it often.
Comment 2 monarchdodra 2013-08-28 02:17:47 UTC

*** This issue has been marked as a duplicate of issue 9528 ***