Bug 4416 – Function with ref argument breaks struct method const attribute
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-07-02T05:29:00Z
Last change time
2011-01-26T18:27:24Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2010-07-02T05:29:05Z
This is a wrong D2 program, because bar() modifies the state of the Foo instance, despite bar is const, but with v2.047 it compiles and runs with no errors:
void spam(ref int x) {
x++;
}
struct Foo {
int x;
void bar() const {
spam(x);
}
}
void main() {
Foo b;
assert(b.x == 0);
b.bar();
assert(b.x == 1);
}
Comment #1 by andrej.mitrovich — 2010-08-29T19:47:59Z
This also breaks immutable class objects, which is really bad:
void spam(ref int x)
{
x++;
}
class Foo
{
int x = 5;
void bar() immutable {
spam(x);
}
}
void main()
{
auto b = new immutable(Foo);
//~ b.x = 10; // uncommment for error
assert(b.x == 5);
b.bar();
assert(b.x == 6);
}
If you uncomment b.x = 10, you will of course get an error. Maybe raise this bug to a higher priority?
Comment #2 by bearophile_hugs — 2010-08-29T23:26:49Z
This is a normal bug, no need to raise its priority. There are far more urgent bugs to fix. I have recently shown in the D newsgroup a list of my bug reports that need higher priority. What needs higher priority are the non-additive changes that are not just bugs.
Comment #3 by yebblies — 2011-01-26T18:27:24Z
*** This issue has been marked as a duplicate of issue 5493 ***