symtab: Fold &a == &b to 0 if folding_initializer [PR94716]
authorJakub Jelinek <jakub@redhat.com>
Mon, 3 Jan 2022 12:47:53 +0000 (13:47 +0100)
committerJakub Jelinek <jakub@redhat.com>
Mon, 3 Jan 2022 12:47:53 +0000 (13:47 +0100)
commit91031bffa42fdea3c985727d042cd1882a64be9c
tree77c152759b6dbb0787713ed52737e8461731a4f7
parent814c221c9eb350dc636556738f37aa73ebba8fdc
symtab: Fold &a == &b to 0 if folding_initializer [PR94716]

On Thu, Dec 09, 2021 at 06:09:12PM -0500, Jason Merrill wrote:
> For the more general comparison of decls like your a != b example above I
> think clang is in the right; in manifestly constant-evaluated context
> (folding_initializer) we should return that they are unequal and prevent a
> later alias declaration, like we do for comparison to 0 in
> maybe_nonzero_address.  It's possible that this gives a wrong answer based
> on something in another translation unit, but that's unlikely, and taking
> that chance seems better than rejecting code that needs a constant answer.

I agree.  This is an incremental patch to do that.

2022-01-03  Jakub Jelinek  <jakub@redhat.com>

PR c++/94716
gcc/
* symtab.c: Include fold-const.h.
(symtab_node::equal_address_to): If folding_initializer is true,
handle it like memory_accessed.  Simplify.
gcc/testsuite/
* gcc.dg/init-compare-1.c: New test.
* g++.dg/cpp0x/constexpr-compare1.C: New test.
* g++.dg/cpp1y/constexpr-94716.C: New test.
* g++.dg/cpp1z/constexpr-compare1.C: New test.
gcc/symtab.c
gcc/testsuite/g++.dg/cpp0x/constexpr-compare1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-94716.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1z/constexpr-compare1.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/init-compare-1.c [new file with mode: 0644]