This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: C++ PATCH to fix wrong-code with pointer-to-data-members (PR c++/79687)
- From: Jason Merrill <jason at redhat dot com>
- To: Marek Polacek <polacek at redhat dot com>
- Cc: GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Fri, 24 Feb 2017 11:11:05 -0800
- Subject: Re: C++ PATCH to fix wrong-code with pointer-to-data-members (PR c++/79687)
- Authentication-results: sourceware.org; auth=none
- References: <20170224162241.GR3892@redhat.com>
On Fri, Feb 24, 2017 at 8:22 AM, Marek Polacek <polacek@redhat.com> wrote:
> I had an interesting time tracking down some of the problems with this code.
> Hopefully I've sussed out now how this stuff works.
>
> We've got
>
> struct A { char c; };
> char A::*p = &A::c;
> static char A::*const q = p;
> and then
> &(a.*q) - &a.c
> which should evaluate to 0. Here "p" will be 0, that's the offset from the
> start of the struct to "c". "q" is const-qualified and static and initialized
> with "p", so we get to cp_fold_maybe_rvalue -> decl_constant_value ->
> constant_value_1. Now, NULL pointer-to-data-members are represented by -1, so
> that a null pointer is distinguishable from an offset of the first member of a
> struct (0). So constant_value_1 looks at the DECL_INITIAL of "q", which is -1,
> a constant, we fold "q" to -1, and sadness ensues. I believe the -1 value is
> only an internal representation and shouldn't be used like that.
Since q is initialized from p, it shouldn't have a DECL_INITIAL of -1;
that sounds like the bug.
Jason