Subject: module: inline class member function cannot use an extern thread_local int

From: Frank Brokken <[email-masked]>
Date: Tue, 05 Oct 2021 09:56:27 +0200

Package: g++-11
Version: 11.2.0-7
Severity: normal

Dear Maintainer,

   * What led up to the situation?

While developing a module whose interface declares an extern thread_local int
and a class whose member accesses that int the compiler produces an internal
compiler error. The bug is observed when the member's implementation is
provided as an inline function, below the interface. If either instead of
'extern thread_local int' an 'extern int' is declared or if instead of an
inline function the function's implementation is provided in-class the
compiler bug isn't observed.

   * What exactly did you do (or not do) that was effective (or
     ineffective)?

Here's the demo source:

--------------------------------
/*
    Define not THREAD_LOCAL and not INCLASS:    no error
    Define not THREAD_LOCAL and INCLASS:        no error
    Define THTREAD_LOCAL and not INCLASS:       internal compiler error
    Define THTREAD_LOCAL and INCLASS:           no  error
*/

#define THREAD_LOCAL
#define INCLASS

module;

#include <thread>

export module DemoMod;

export
{
    #ifdef THREAD_LOCAL
        extern thread_local int g_errno;
    #else
        extern int g_errno;
    #endif

    struct Demo
    {
        #ifdef INCLASS
            void member()
            {
                g_errno = 0;
            }
        #else
            void member();
        #endif

    };

    #ifndef INCLASS    
        inline void Demo::member()
        {
            g_errno = 0;
        }
    #endif

}   // end export block

--------------------------------

   * What was the outcome of this action?

If INCLASS is not defined then the compiler produces the following bugreport:

--------------------------------
demo.cc:15:8: internal compiler error: in tree_node, at cp/module.cc:9061
   15 | export module DemoMod;
      |        ^~~~~~
0x9d4646 trees_out::tree_node(tree_node*)
    ../../src/gcc/cp/module.cc:9061
0x9d29fa trees_out::core_vals(tree_node*)
    ../../src/gcc/cp/module.cc:5915
0x9d313e trees_out::tree_node_vals(tree_node*)
    ../../src/gcc/cp/module.cc:7056
0x9d5aff trees_out::tree_value(tree_node*)
    ../../src/gcc/cp/module.cc:8892
0x9d466d trees_out::tree_node(tree_node*)
    ../../src/gcc/cp/module.cc:9090
0x9d29fa trees_out::core_vals(tree_node*)
    ../../src/gcc/cp/module.cc:5915
0x9d313e trees_out::tree_node_vals(tree_node*)
    ../../src/gcc/cp/module.cc:7056
0x9d5aff trees_out::tree_value(tree_node*)
    ../../src/gcc/cp/module.cc:8892
0x9d466d trees_out::tree_node(tree_node*)
    ../../src/gcc/cp/module.cc:9090
0x9d29fa trees_out::core_vals(tree_node*)
    ../../src/gcc/cp/module.cc:5915
0x9d313e trees_out::tree_node_vals(tree_node*)
    ../../src/gcc/cp/module.cc:7056
0x9d5aff trees_out::tree_value(tree_node*)
    ../../src/gcc/cp/module.cc:8892
0x9d466d trees_out::tree_node(tree_node*)
    ../../src/gcc/cp/module.cc:9090
0x9d29fa trees_out::core_vals(tree_node*)
    ../../src/gcc/cp/module.cc:5915
0x9d313e trees_out::tree_node_vals(tree_node*)
    ../../src/gcc/cp/module.cc:7056
0x9d5aff trees_out::tree_value(tree_node*)
    ../../src/gcc/cp/module.cc:8892
0x9d466d trees_out::tree_node(tree_node*)
    ../../src/gcc/cp/module.cc:9090
0x9d29fa trees_out::core_vals(tree_node*)
    ../../src/gcc/cp/module.cc:5915
0x9d313e trees_out::tree_node_vals(tree_node*)
    ../../src/gcc/cp/module.cc:7056
0x9d5aff trees_out::tree_value(tree_node*)
    ../../src/gcc/cp/module.cc:8892
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <file:///usr/share/doc/gcc-11/README.Bugs> for instructions.
--------------------------------

   * What outcome did you expect instead?

I would have expected that the compiler would correctly compile the source
file for all four combinations of defining THREAD_LOCAL and INCLASS

-- System Information:
Debian Release: bookworm/sid
  APT prefers testing
  APT policy: (500, 'testing'), (1, 'experimental')
Architecture: amd64 (x86_64)

Kernel: Linux 5.14.0-1-amd64 (SMP w/4 CPU threads)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8), LANGUAGE=en_US:en
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled

Versions of packages g++-11 depends on:
ii  gcc-11            11.2.0-7
ii  gcc-11-base       11.2.0-7
ii  libc6             2.32-4
ii  libgmp10          2:6.2.1+dfsg-2
ii  libisl23          0.23-1
ii  libmpc3           1.2.0-1
ii  libmpfr6          4.1.0-3
ii  libstdc++-11-dev  11.2.0-7
ii  libzstd1          1.4.8+dfsg-2.1
ii  zlib1g            1:1.2.11.dfsg-2

g++-11 recommends no packages.

Versions of packages g++-11 suggests:
pn  g++-11-multilib  <none>
pn  gcc-11-doc       <none>

-- no debconf information