Bug 94716

Summary: comparison of address of template variables should be constexpr
Product: gcc Reporter: Francesco Biscani <bluescarni>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: daniel.kruegler, jason, johelegp, marxin, mpolacek, pdimov, sjeltsch, webrown.cpp
Priority: P3 Keywords: rejects-valid
Version: 10.0   
Target Milestone: ---   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69681
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85428
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89367
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2020-04-23 00:00:00
Bug Depends on:    
Bug Blocks: 55004    

Description Francesco Biscani 2020-04-22 15:36:57 UTC
The following code does not compile in GCC trunk:

-----
template <typename T>
inline constexpr char test_impl = 0;

template <typename T>
inline constexpr auto *id = &test_impl<T>;

int main() {
    static_assert(id<int> != id<float>, "should not be equal");
}
-----

The error message:

-----
<source>: In function 'int main()':

<source>:8:27: error: non-constant condition for static assertion

    8 |     static_assert(id<int> != id<float>, "should not be equal");

      |                   ~~~~~~~~^~~~~~~~~~~~

<source>:8:27: error: '((& test_impl<int>) != (& test_impl<float>))' is not a constant expression

Compiler returned: 1
-----

See on godbolt:

https://godbolt.org/z/CS4D8B

Clang and MSVC accept the code.
Comment 1 Martin Liška 2020-04-23 05:14:42 UTC
Confirmed, ale GCC versions suffer.
Comment 2 Andrew Pinski 2021-08-28 22:27:05 UTC
Reduced testcase:
template <int> char test_impl = 0;
static_assert(&test_impl<0> != &test_impl<1>, "should not be equal");
Comment 3 Johel Ernesto Guerrero Peña 2021-09-10 22:46:05 UTC
*** Bug 102267 has been marked as a duplicate of this bug. ***
Comment 4 Andrew Pinski 2021-12-28 10:40:00 UTC
Dup of bug 91693.

*** This bug has been marked as a duplicate of bug 91693 ***
Comment 5 GCC Commits 2022-01-03 12:48:46 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:91031bffa42fdea3c985727d042cd1882a64be9c

commit r12-6188-g91031bffa42fdea3c985727d042cd1882a64be9c
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Jan 3 13:47:53 2022 +0100

    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.
Comment 6 Patrick Palka 2022-01-04 19:15:34 UTC
*** Bug 101622 has been marked as a duplicate of this bug. ***
Comment 7 Patrick Palka 2022-01-06 16:17:17 UTC
*** Bug 86354 has been marked as a duplicate of this bug. ***