public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/116746] New: Explicit specializations of static variable templates are incorrectly given external linkage
@ 2024-09-16 21:16 tom at honermann dot net
  2024-09-16 21:53 ` [Bug c++/116746] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: tom at honermann dot net @ 2024-09-16 21:16 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 116746
           Summary: Explicit specializations of static variable templates
                    are incorrectly given external linkage
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tom at honermann dot net
  Target Milestone: ---

As demonstrated by the following test case, GCC emits symbols with external
linkage for explicit specializations of variable templates declared with the
`static` storage class specifier. Such explicit specializations should be given
the same linkage as the primary template; internal linkage for this example.
This expectation is supported by the following sections of the C++ standard:

Per [temp.expl.spec]p4:
> An explicit specialization does not introduce a name ([basic.scope.scope]). …

Per [basic.link]p2:
> A name can have external linkage, module linkage, internal linkage, or no linkage, as determined by the rules below.

The test case consists of two source files, each of which declares a static
variable template named `ptr` and an associated explicit specialization of
`ptr<int>`. When compiled and linked, link fails due to multiple definitions of
`ptr<int>` (thus demonstrating that they were given external linkage).
Commenting out one or both of the explicit specializations suffices for the
program to link successfully and to run without assertion failures thus
demonstrating that the primary templates (and the specializations implicitly
instantiated from them) were given internal linkage.

Makefile:
```
all: main
clean:
        rm -f main.o t.o main
main.o: main.cpp
        g++ -c main.cpp
t.o: t.cpp
        g++ -c t.cpp
main: main.o t.o
        g++ main.o t.o -o main
```

main.cpp:
```
#include <cassert>
static int x;
template <typename T> static void* ptr = &x;
template <> void* ptr<int> = &x;
extern void* f();
extern void* g();
int main() {
  assert(ptr<char> != f());
  assert(ptr<int> != g());
}
```

t.cpp:
```
static int x;
template <typename T> static void* ptr = &x;
template <> void* ptr<int> = &x;
void* f() {
  return ptr<char>;
}
void* g() {
  return ptr<int>;
}
```

An example build and invocation follows:
```
$ make
g++ -c main.cpp
g++ -c t.cpp
g++ main.o t.o -o main
ld: t.o:(.data+0x0): multiple definition of `ptr<int>'; main.o:(.data+0x0):
first defined here
collect2: error: ld returned 1 exit status
make: *** [Makefile:9: main] Error 1
```

This appears to be a common compiler bug as both Clang and MSVC exhibit similar
behavior as can be observed at https://godbolt.org/z/fzsdTG9rM. (Unfortunately,
that link won't demonstrate MSVC behavior currently due to a timeout issue
tracked at https://github.com/compiler-explorer/compiler-explorer/issues/6742).
A Clang bug report can be found at
https://github.com/llvm/llvm-project/issues/35211 and a MSVC bug report is
available at
https://developercommunity.visualstudio.com/t/Explicit-specializations-of-static-varia/10746502.

Note that the C++ standard does not permit an explicit specialization to be
declared with the static storage class specifier and it is therefore not
possible to explicitly declare that the explicit specializations have internal
linkage. Per [temp.expl.spec]p2:
> The declaration in an explicit-specialization shall not be an export-declaration. An explicit specialization shall not use a storage-class-specifier ([dcl.stc]) other than thread_local.

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

* [Bug c++/116746] Explicit specializations of static variable templates are incorrectly given external linkage
  2024-09-16 21:16 [Bug c++/116746] New: Explicit specializations of static variable templates are incorrectly given external linkage tom at honermann dot net
@ 2024-09-16 21:53 ` pinskia at gcc dot gnu.org
  2024-09-16 21:57 ` pinskia at gcc dot gnu.org
  2024-09-16 21:58 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-09-16 21:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 59127
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=59127&action=edit
Dejagnu testcase for easier testing

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

* [Bug c++/116746] Explicit specializations of static variable templates are incorrectly given external linkage
  2024-09-16 21:16 [Bug c++/116746] New: Explicit specializations of static variable templates are incorrectly given external linkage tom at honermann dot net
  2024-09-16 21:53 ` [Bug c++/116746] " pinskia at gcc dot gnu.org
@ 2024-09-16 21:57 ` pinskia at gcc dot gnu.org
  2024-09-16 21:58 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-09-16 21:57 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2024-09-16
             Status|UNCONFIRMED                 |NEW
      Known to fail|                            |5.1.0

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

Note looks like clang and MSVC has the same issue too.

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

* [Bug c++/116746] Explicit specializations of static variable templates are incorrectly given external linkage
  2024-09-16 21:16 [Bug c++/116746] New: Explicit specializations of static variable templates are incorrectly given external linkage tom at honermann dot net
  2024-09-16 21:53 ` [Bug c++/116746] " pinskia at gcc dot gnu.org
  2024-09-16 21:57 ` pinskia at gcc dot gnu.org
@ 2024-09-16 21:58 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-09-16 21:58 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://github.com/llvm/llv
                   |                            |m-project/issues/35211

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>This appears to be a common compiler bug as both Clang and MSVC exhibit similar behavior as can be observed at

Oh I didn't read this part until after I looked to see if clang/MSVC had the
same issue.

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

end of thread, other threads:[~2024-09-16 21:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-16 21:16 [Bug c++/116746] New: Explicit specializations of static variable templates are incorrectly given external linkage tom at honermann dot net
2024-09-16 21:53 ` [Bug c++/116746] " pinskia at gcc dot gnu.org
2024-09-16 21:57 ` pinskia at gcc dot gnu.org
2024-09-16 21:58 ` 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).