Bug 6300 – Returning a cast(immutable) struct erroneously calls a matching constructor

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-07-12T12:40:00Z
Last change time
2015-06-09T05:14:49Z
Assigned to
nobody
Creator
sludwig

Comments

Comment #0 by sludwig — 2011-07-12T12:40:53Z
The following snipped fails to compile. What seems to happen is that the return statement implicitly calls the constructor which in turn returns a mutable S which is not implicitly castable to immutable - thus the error. Happens since DMD 2.054 --- struct S { int[] c; this(in S){} immutable(S) f() const { S s; return cast(immutable)s; // the following works: //immutable(S) sr = cast(immutable)s; //return sr; } } --- dbug2.d(6): Error: cannot implicitly convert expression ((immutable immutable(S) __ctmp3 = 0; , __ctmp3).this(s)) of type S to immutable(S) .. leaving the severity as normal, as there is an easy workaround
Comment #1 by lt.infiltrator — 2014-03-19T18:59:32Z
The provided code compiles and runs as of v2.065.