Discussion:
Postblit bug
IgorStepanov via Digitalmars-d
2014-10-17 00:42:24 UTC
Permalink
I've found a strange postblit bug (or not a bug?):

************************************

struct A
{
this(this)
{
}
}

struct B
{
A a;
}


struct C
{
const B b;
}

void main()
{
C c;
}

************************************

When I try to compile it, compiler raises the error: (Error:
mutable method testaa2.B.__fieldPostBlit is not callable using a
const object)

If I mark this(this) as const (BTW, is "this(this) const" a
correct definition?) error still raises.
When I tried to reduce this code, I've found the next:

************************************

struct A
{
this(this)
{
}
}

struct B
{
const A a;
}


void main()
{
B b;
}

************************************

Error: mutable method testaa2.A.__postblit is not callable using
a const object

If I change this(this) to const, error has been lost.

************************************

struct A
{
this(this) const
{
}
}

struct B
{
const A a;
}


void main()
{
B b;
}

************************************

Can someone comment this code? Should I think that it's a bug.
ketmar via Digitalmars-d
2014-10-17 00:55:13 UTC
Permalink
On Fri, 17 Oct 2014 00:42:24 +0000
Post by IgorStepanov via Digitalmars-d
Can someone comment this code? Should I think that it's a bug.
it's just an anomaly. const postblit can do alot of things besides
adjusting struct fields, and it's logical that compiler cannot call
non-const methods for const objects.

yet it's still on of those "unforseen consequences" that arises from
conjunction of different features.

i don't think that it's a bug, but i think that this must be discussed
anyway, and then documented.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20141017/dd7f1881/attachment-0001.sig>
IgorStepanov via Digitalmars-d
2014-10-17 01:30:51 UTC
Permalink
On Friday, 17 October 2014 at 00:55:25 UTC, ketmar via
Post by ketmar via Digitalmars-d
On Fri, 17 Oct 2014 00:42:24 +0000
IgorStepanov via Digitalmars-d <digitalmars-d at puremagic.com>
Post by IgorStepanov via Digitalmars-d
Can someone comment this code? Should I think that it's a bug.
it's just an anomaly. const postblit can do alot of things
besides
adjusting struct fields, and it's logical that compiler cannot
call
non-const methods for const objects.
yet it's still on of those "unforseen consequences" that arises
from
conjunction of different features.
i don't think that it's a bug, but i think that this must be
discussed
anyway, and then documented.
I think, this is unexpected behaviour:

Compiler generates "__fieldPostBlit" for all structs, and call it
when postblit is needed.

In this example compiler generates something like the next code:

struct A
{
this(this)
{
}
}

struct B
{
A a;

void __fieldPostBlit()
{
a.__postblit(); //a has an explicit postblit
}
}


struct C
{
const B b;
void __fieldPostBlit()
{
b.__fieldPostBlit(); //Error: b is const
}
}

void main()
{
C c;
C c2 = c; //=>
//memcpy(&c2, &c, C.sizeof)
//c2.__fieldPostBlit();

}


However, __postblit and __fieldPostBlit are always called for new
object, thus it can neglect const guarantee and generate
postblits like:
void __fieldPostBlit()
{
(cast()b).__fieldPostBlit();
}
ketmar via Digitalmars-d
2014-10-17 01:42:14 UTC
Permalink
On Fri, 17 Oct 2014 01:30:51 +0000
that's what i mean when i was talking about "unforeseen
consequences". ;-)

when exactly "const" should be in effect? that is the question! ;-)
let's wait what other people will say about this.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20141017/83ff4ad9/attachment.sig>
Loading...