Bug 11503 – Type system breaking caused by implicit conversion for the value returned from pure function
Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-11T20:13:00Z
Last change time
2014-01-26T13:52:32Z
Keywords
bounty, pull
Assigned to
nobody
Creator
k.hara.pg
Comments
Comment #0 by k.hara.pg — 2013-11-11T20:13:27Z
From: http://forum.dlang.org/post/[email protected]
If an immutable value is returned by pure function, compiler _must_ not implicitly convert it to mutable.
Test case:
import std.stdio;
struct S
{
immutable(S)* s;
this(int) immutable pure
{
s = &this;
}
int data;
}
immutable(S)* makes() pure
{
return new immutable S(0);
}
void main()
{
S* s = makes(); // s is mutable and contains an immutable reference to itself
pragma(msg, typeof(s)); // mutable
pragma(msg, typeof(s.s)); // immutable
writefln("%s", s); // same address
writefln("%s", s.s); // same address
//s.s.data = 7; // this is immutable
s.data = 3; // but this is not!!!
}
Comment #1 by yebblies — 2013-11-11T23:31:46Z
Simpler example:
import std.stdio;
immutable int[] x = [1, 2, 3];
auto makes() pure
{
return x;
}
void main()
{
auto a = x;
int[] b = makes();
writefln("%s %s", a.ptr, b.ptr);
}
Comment #2 by andrei — 2014-01-12T10:08:39Z
Placed a $150 bounty.
Comment #3 by code — 2014-01-12T11:05:27Z
Seems to be a fairly simple issue. Regression-testing a fix right now.