public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/98288] New: Accidental equality of classes templated by pointer to local static constant of templated function
@ 2020-12-15 13:58 matthieum.147192 at gmail dot com
  2020-12-15 14:38 ` [Bug c++/98288] " matthieum.147192 at gmail dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: matthieum.147192 at gmail dot com @ 2020-12-15 13:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98288

            Bug ID: 98288
           Summary: Accidental equality of classes templated by pointer to
                    local static constant of templated function
           Product: gcc
           Version: 7.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: matthieum.147192 at gmail dot com
  Target Milestone: ---

Based on https://stackoverflow.com/q/65306562/147192.

The behavior of GCC (and Clang) is inconsistent (between compile-time and
run-time), however it is unclear to me whether the inconsistency is conforming
with the C++17 standard or not.


The following reduced program is expected to return 0 (invoking g++ with
-std=c++17):

template <typename, typename>
struct is_same { static constexpr bool value = false; };

template <typename T>
struct is_same<T, T> { static constexpr bool value = true; };

template <typename T, typename U>
static constexpr bool is_same_v = is_same<T, U>::value;

using uintptr_t = unsigned long long;

template <int const* I>
struct Parameterized { int const* member; };

template <typename T>
auto create() {
    static constexpr int const I = 2;

    return Parameterized<&I>{ &I };
}

int main() {
    auto one = create<short>();
    auto two = create<int>();

    if (is_same_v<decltype(one), decltype(two)>) {
        return reinterpret_cast<uintptr_t>(one.member) ==
reinterpret_cast<uintptr_t>(two.member) ? 1 : 2;
    }

    return 0;
}

Yet, on all versions of GCC where it compiles (from 7.2 onwards), and for all
optimization levels (from -O0 to -O3), it returns 2, indicating:

- That `one` and `two` have the same type -- which according to 17.4
[temp.type] should mean that they point to the same object.
- Yet they point to different objects -- there are two instances of
`create<T>()::I`, one for `T = short` and one for `T = int`.

The assembly listing clearly contains 2 different instances of
`create<T>()::I`.


Notes:

- If `I` is initialized with `= sizeof(T)`, instead, then the program returns 0
as expected.
- Clang (up to 11.0) fails to compile the program with -O0 (the linker failing
to find a `create()::I` variable), and otherwise produces the same result as
GCC with -O1 to -O3.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/98288] Accidental equality of classes templated by pointer to local static constant of templated function
  2020-12-15 13:58 [Bug c++/98288] New: Accidental equality of classes templated by pointer to local static constant of templated function matthieum.147192 at gmail dot com
@ 2020-12-15 14:38 ` matthieum.147192 at gmail dot com
  2021-01-04 11:57 ` matthieum.147192 at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: matthieum.147192 at gmail dot com @ 2020-12-15 14:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98288

--- Comment #1 from Matthieu M <matthieum.147192 at gmail dot com> ---
The above program leads to an ICE on gcc (trunk): https://godbolt.org/z/dGe1T6,
from SO user https://stackoverflow.com/users/4832499/passer-by.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/98288] Accidental equality of classes templated by pointer to local static constant of templated function
  2020-12-15 13:58 [Bug c++/98288] New: Accidental equality of classes templated by pointer to local static constant of templated function matthieum.147192 at gmail dot com
  2020-12-15 14:38 ` [Bug c++/98288] " matthieum.147192 at gmail dot com
@ 2021-01-04 11:57 ` matthieum.147192 at gmail dot com
  2021-02-02  9:44 ` matthieum.147192 at gmail dot com
  2021-10-02  6:10 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: matthieum.147192 at gmail dot com @ 2021-01-04 11:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98288

--- Comment #2 from Matthieu M <matthieum.147192 at gmail dot com> ---
Richard Smith (Clang) agreed that the following program should return 0, and
fixed the behavior in Clang (trunk) in
https://bugs.llvm.org/show_bug.cgi?id=48517.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/98288] Accidental equality of classes templated by pointer to local static constant of templated function
  2020-12-15 13:58 [Bug c++/98288] New: Accidental equality of classes templated by pointer to local static constant of templated function matthieum.147192 at gmail dot com
  2020-12-15 14:38 ` [Bug c++/98288] " matthieum.147192 at gmail dot com
  2021-01-04 11:57 ` matthieum.147192 at gmail dot com
@ 2021-02-02  9:44 ` matthieum.147192 at gmail dot com
  2021-10-02  6:10 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: matthieum.147192 at gmail dot com @ 2021-02-02  9:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98288

Matthieu M <matthieum.147192 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|7.2.0                       |11.0

--- Comment #3 from Matthieu M <matthieum.147192 at gmail dot com> ---
Updated version of compiler.

Also, on gcc trunk this is an ICE:

<source>:18:32: internal compiler error: in discriminator_for_local_entity, at
cp/mangle.c:1949
   18 |     static constexpr int const I = 2;
      |                                ^
0x1ce5ce9 internal_error(char const*, ...)
        ???:0
0x6b6f1b fancy_abort(char const*, int, char const*)
        ???:0
0x812f3b mangle_decl(tree_node*)
        ???:0
0x135b052 decl_assembler_name(tree_node*)
        ???:0
0x13da4da make_decl_rtl(tree_node*)
        ???:0
0x13dd8fd lookup_constant_def(tree_node*)
        ???:0

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/98288] Accidental equality of classes templated by pointer to local static constant of templated function
  2020-12-15 13:58 [Bug c++/98288] New: Accidental equality of classes templated by pointer to local static constant of templated function matthieum.147192 at gmail dot com
                   ` (2 preceding siblings ...)
  2021-02-02  9:44 ` matthieum.147192 at gmail dot com
@ 2021-10-02  6:10 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-10-02  6:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98288

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 98930.

*** This bug has been marked as a duplicate of bug 98930 ***

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-10-02  6:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-15 13:58 [Bug c++/98288] New: Accidental equality of classes templated by pointer to local static constant of templated function matthieum.147192 at gmail dot com
2020-12-15 14:38 ` [Bug c++/98288] " matthieum.147192 at gmail dot com
2021-01-04 11:57 ` matthieum.147192 at gmail dot com
2021-02-02  9:44 ` matthieum.147192 at gmail dot com
2021-10-02  6:10 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).