Bug 11325 – swap accepts aggregates with non-mutable fields
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-10-22T09:40:00Z
Last change time
2013-10-22T23:28:05Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
monarchdodra
Comments
Comment #0 by monarchdodra — 2013-10-22T09:40:30Z
Which basically means it mutates the immutable:
unittest
{
struct S
{
const int i;
}
S s1 = S(0);
S s2 = S(1);
swap(s1, s2); //Clobbers i.
}
Swap being a trusted function, this is not acceptable.
https://github.com/D-Programming-Language/phobos/pull/1603
Comment #1 by andrej.mitrovich — 2013-10-22T09:48:03Z
In git-head I can't reproduce your test-case:
-----
import std.algorithm;
void main()
{
struct S
{
const int i;
}
S s1 = S(0);
S s2 = S(1);
swap(s1, s2); // Error: cannot modify struct lhs S with immutable members
}
-----
Comment #2 by monarchdodra — 2013-10-22T09:57:00Z
Are you on the *latest* head? It was partially introduced with my latest non-assignable fix.
However, it was also a pre-existing case (albeit harder to hit), if the type actually had an elaborate assign:
//----
struct S
{
void opAssign(S){}
const int i;
}
S s1 = S(0);
S s2 = S(1);
swap(s1, s2); //Clobbers i.
//----
This "correctly" bypasses the opAssign, clobering i.
The question is: do we want to cater to types that *have* an opAssign, but can't be memory *moved*? Tough question imo.
Although I guess we could cater to that in a separate ER.
Comment #3 by andrej.mitrovich — 2013-10-22T12:44:21Z
(In reply to comment #2)
> Are you on the *latest* head? It was partially introduced with my latest
> non-assignable fix.
Ah, ok had a slightly outdated Phobos. I can reproduce now.