Bug 6578 – Ignored const with struct with constructor
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-08-30T04:57:00Z
Last change time
2013-04-26T04:16:29Z
Keywords
accepts-invalid, pull
Assigned to
yebblies
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2011-08-30T04:57:48Z
In DMD 2.055head this compiles and runs with no errors:
struct Foo {
int x;
this(int x_) { this.x = x_; }
}
void main() {
auto f1 = new const(Foo)(1);
f1.x++;
auto f2 = new immutable(Foo)(1);
f2.x++;
auto f3 = const(Foo)(1);
f3.x++;
auto f4 = immutable(Foo)(1);
f4.x++;
}
While this generates two errors:
struct Foo {
int x;
}
void main() {
auto f5 = const(Foo)(1);
f5.x++; // error
auto f6 = immutable(Foo)(1);
f6.x++; // error
}
I think in the first program the f1,f2,f3,f4 structs too need to raise a compile-time error.
Thanks to Timon Gehr for a suggestion.
Yet another test case:
struct S {
this(int[] arr) immutable {}
}
void main() {
auto s = S([1,2,3]);
pragma(msg, typeof(s)); // prints immutable(S)
}
Can be fixed by:
https://github.com/D-Programming-Language/dmd/pull/1726
Comment #5 by k.hara.pg — 2013-04-26T04:16:29Z
(In reply to comment #4)
> Yet another test case:
>
> struct S {
> this(int[] arr) immutable {}
> }
> void main() {
> auto s = S([1,2,3]);
> pragma(msg, typeof(s)); // prints immutable(S)
> }
>
> Can be fixed by:
> https://github.com/D-Programming-Language/dmd/pull/1726
Pull-request 1726 is merged, and this issue has been fixed properly.