Bug 116911 - [15 regression] reference to a variable in anonymous namespace no longer considered constexpr in C++11 since r15-3910-gaf4471cb422a86
Summary: [15 regression] reference to a variable in anonymous namespace no longer cons...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 15.0
: P3 critical
Target Milestone: 15.0
Assignee: Nathaniel Shead
URL:
Keywords: patch, rejects-valid
Depends on: 113266
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2024-09-30 20:34 UTC by Sam James
Modified: 2024-10-04 10:34 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-09-30 00:00:00


Attachments
qlogging.ii.xz (322.22 KB, application/x-xz)
2024-09-30 20:36 UTC, Sam James
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sam James 2024-09-30 20:34:44 UTC
Had a bunch of reports overnight (none last week) with this week's snapshot:
```
$ x86_64-pc-linux-gnu-g++ -c -o qglobal.o  -O3 -march=native -fno-semantic-interposition -mtls-dialect=gnu2 -fdiagnostics-color=always -Wa,-O2 -Wa,-mtune=znver4 -pipe -Werror=strict-aliasi
ng -Werror=lto-type-mismatch -Werror=odr -frecord-gcc-switches -ggdb3 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -std=c++11 -ffunction-sections -fdata-sections  -I/var/tmp/portage/dev-qt/qtco
re-5.15.14/work/qtbase-everywhere-src-5.15.14/qmake -I/var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/qmake/library -I/var/tmp/portage/dev-qt/qtcore-5.15.14/wor
k/qtbase-everywhere-src-5.15.14/qmake/generators -I/var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/src/3rdparty/tinycbor/src -I/var/tmp/portage/dev-qt/qtcore-5.
15.14/work/qtbase-everywhere-src-5.15.14/qmake/generators/unix -I/var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/qmake/generators/win32 -I/var/tmp/portage/dev-q
t/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/qmake/generators/mac -I/var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/include -I/var/tmp/portage/dev-qt/qtc
ore-5.15.14/work/qtbase-everywhere-src-5.15.14/include/QtCore -I/var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/include/QtCore/5.15.14 -I/var/tmp/portage/dev-qt
/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/include/QtCore/5.15.14/QtCore -I../src/corelib/global -I/var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/mkspe
cs/linux-g++ -DQT_VERSION_STR=\"5.15.14\" -DQT_VERSION_MAJOR=5 -DQT_VERSION_MINOR=15 -DQT_VERSION_PATCH=14 -DQT_BUILD_QMAKE -DQT_BOOTSTRAPPED -DPROEVALUATOR_FULL -DQT_NO_FOREACH /var/tmp
/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/src/corelib/global/qglobal.cpp
In file included from /var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/include/QtCore/qglobalstatic.h:1,
                 from /var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/include/QtCore/../../src/corelib/global/qglobal.h:1310,
                 from /var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/include/QtCore/qglobal.h:1,
                 from /var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/mkspecs/linux-g++/qplatformdefs.h:45,
                 from /var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/src/corelib/global/qglobal.cpp:41:
/var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/include/QtCore/../../src/corelib/global/qglobalstatic.h:159:42: error: the value of ‘{anonymous}::Q_QGS_global_callback_table::guard’ is not usable in a constant expression
  159 |                          Q_QGS_ ## NAME::guard> NAME;
      |                                          ^~~~~
/var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/include/QtCore/../../src/corelib/global/qglobalstatic.h:162:5: note: in expansion of macro ‘Q_GLOBAL_STATIC_WITH_ARGS’
  162 |     Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ())
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
/var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/src/corelib/global/qglobal.cpp:4316:1: note: in expansion of macro ‘Q_GLOBAL_STATIC’
 4316 | Q_GLOBAL_STATIC(QInternal_CallBackTable, global_callback_table)
      | ^~~~~~~~~~~~~~~
/var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/include/QtCore/../../src/corelib/global/qglobalstatic.h:154:25: note: ‘{anonymous}::Q_QGS_global_callback_table::guard’ was not declared ‘constexpr’
  154 |         QBasicAtomicInt guard = Q_BASIC_ATOMIC_INITIALIZER(QtGlobalStatic::Uninitialized); \
      |                         ^~~~~
/var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/include/QtCore/../../src/corelib/global/qglobalstatic.h:162:5: note: in expansion of macro ‘Q_GLOBAL_STATIC_WITH_ARGS’
  162 |     Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ())
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
/var/tmp/portage/dev-qt/qtcore-5.15.14/work/qtbase-everywhere-src-5.15.14/src/corelib/global/qglobal.cpp:4316:1: note: in expansion of macro ‘Q_GLOBAL_STATIC’
 4316 | Q_GLOBAL_STATIC(QInternal_CallBackTable, global_callback_table)
      | ^~~~~~~~~~~~~~~
gmake: *** [Makefile:343: qglobal.o] Error 1
gmake: *** Waiting for unfinished jobs....
```

I haven't tried to look at it yet but my C++ isn't great either anyway.

---

```
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/15/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /var/tmp/portage/sys-devel/gcc-15.0.0_pre20240929/work/gcc-15-20240929/configure --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/15 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/15/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/15 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/15/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/15/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/15/include/g++-v15 --disable-silent-rules --disable-dependency-tracking --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/15/python --enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt --disable-werror --with-system-zlib --enable-nls --without-included-gettext --disable-libunwind-exceptions --enable-checking=yes,extra,rtl --with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo Hardened 15.0.0_pre20240929 p13' --with-gcc-major-version-only --enable-libstdcxx-time --enable-lto --disable-libstdcxx-pch --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64 --disable-fixed-point --enable-targets=all --enable-libgomp --disable-libssp --disable-libada --enable-cet --disable-systemtap --disable-valgrind-annotations --disable-vtable-verify --disable-libvtv --with-zstd --without-isl --enable-default-pie --enable-host-pie --enable-host-bind-now --enable-default-ssp --disable-fixincludes --with-build-config='bootstrap-O3 bootstrap-lto bootstrap-cet'
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 15.0.0 20240929 (experimental) (Gentoo Hardened 15.0.0_pre20240929 p13)
```
Comment 1 Sam James 2024-09-30 20:36:21 UTC
Created attachment 59241 [details]
qlogging.ii.xz
Comment 2 Sam James 2024-09-30 23:00:20 UTC
```
template <int &> struct a {};
namespace {
namespace Q_QGS_global_callback_table {
int guard;
}
} // namespace
a<Q_QGS_global_callback_table::guard> b;
```

rejected by gcc w/ -std=c++11
accepted by gcc w/ -std=c++17
accepted by clang w/ -std=c++11
accepted by clang w/ -std=c++17

Is this OK?
Comment 3 Sam James 2024-09-30 23:06:13 UTC
edg accepts it with 11 too
Comment 4 Drea Pinski 2024-09-30 23:09:23 UTC
Slightly more reduced:
```
template <int &> struct a {};
namespace {
  int guard;
}
a<guard> b;
```

YES this should be accepted with C++11.  Anonynous namespaces are still considered external linkage; even though they get localized.
Comment 5 Drea Pinski 2024-09-30 23:14:23 UTC
Note we should accept this for C++11 too:
```
template <int &> struct a {};
static int guard;
a<guard> b;
```

But that has always been rejected.
clang warns about the original reduced testcase for c++98 but that is valid as anonymous namespaces are not local linkage in C++98 (even though they are localized).
Comment 6 Drea Pinski 2024-09-30 23:22:34 UTC
r15-3910-gaf4471cb422a86



    I'm not convinced that even in C++98 it was intended that e.g. types in
    anonymous namespaces should be external, but some tests in the testsuite
    rely on this, so for compatibility I restricted those modifications to
    C++11 and later.
Comment 7 Drea Pinski 2024-09-30 23:28:27 UTC
comment #5 is PR 113266.
Comment 8 Drea Pinski 2024-09-30 23:39:54 UTC
(In reply to Andrew Pinski from comment #7)
> comment #5 is PR 113266.

Which I think is the underyling issue here, just exposed by r15-3910-gaf4471cb422a86 change.
Comment 9 Nathaniel Shead 2024-10-04 02:43:19 UTC
Patch submitted: https://gcc.gnu.org/pipermail/gcc-patches/2024-October/664449.html

Sorry about the breakage.
Comment 10 Sam James 2024-10-04 02:44:12 UTC
(In reply to Nathaniel Shead from comment #9)
> Patch submitted:
> https://gcc.gnu.org/pipermail/gcc-patches/2024-October/664449.html
> 
> Sorry about the breakage.

No worries, it's part of the fun and point of testing stuff early en-masse. Thank you!
Comment 11 GCC Commits 2024-10-04 10:32:02 UTC
The master branch has been updated by Nathaniel Shead <nshead@gcc.gnu.org>:

https://gcc.gnu.org/g:247643c7e21b087e6c93b8b8d49b4268bf84b03b

commit r15-4061-g247643c7e21b087e6c93b8b8d49b4268bf84b03b
Author: Nathaniel Shead <nathanieloshead@gmail.com>
Date:   Fri Oct 4 12:01:38 2024 +1000

    c++: Allow references to internal-linkage vars in C++11 [PR113266]
    
    [temp.arg.nontype] changed in C++11 to allow naming internal-linkage
    variables and functions.  We currently already handle internal-linkage
    functions, but variables were missed; this patch updates this.
    
            PR c++/113266
            PR c++/116911
    
    gcc/cp/ChangeLog:
    
            * parser.cc (cp_parser_template_argument): Allow
            internal-linkage variables since C++11.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/nontype6.C: New test.
    
    Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Comment 12 Nathaniel Shead 2024-10-04 10:34:13 UTC
Should be fixed on trunk, thanks!