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

            Bug ID: 122908
           Summary: [modules] Member functions of class templates that
                    have function friend definitions expose their bodies
           Product: gcc
           Version: 15.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: b.gomes.negri at gmail dot com
  Target Milestone: ---

Created attachment 62937
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62937&action=edit
mod.cpp.ii

The bodies of member functions that belong to class templates are considered to
expose their function bodies for the purposes of the "TU-local" stuff in C++ 20
modules if the class template has a definition of a friend function.

In the example attachment (a preprocessed .ii file), the member function
Class::testFn is erroneously marked by g++ as exposing internalLink simply
because internalLink is in the function body. If the friend function definition
friendFn inside Class is removed, or if Class is converted from a class
template to a normal class, the problem disappears.

I believe this to be a bug in the compiler because non-inline function bodies
(and testFn is not an inline function) are not supposed to expose the entities
named within, and because the combination required to trigger the error seems
very arbitrary (the diagnostic does not say anything about the friend function
but removing it or even turning it from a definition to a declaration makes the
error disappear)

$ g++ -v -fmodules-ts -std=gnu++20 -c -o ./mod.cpp.o ./mod.cpp.ii
Using built-in specs.
COLLECT_GCC=g++
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,m2,cobol,lto
--prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --enable-libstdcxx-backtrace
--with-libstdcxx-zoneinfo=/usr/share/zoneinfo --with-linker-hash-style=gnu
--enable-plugin --enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-15.2.1-build/gcc-15.2.1-20251111/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none,amdgcn-amdhsa --enable-offload-defaulted
--without-cuda-driver --enable-gnu-indirect-function --enable-cet
--with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
--with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 15.2.1 20251111 (Red Hat 15.2.1-4) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-fmodules' '-std=gnu++20' '-c' '-o' './mod.cpp.o'
'-shared-libgcc' '-mtune=generic' '-march=x86-64' '-dumpdir' './'
 /usr/libexec/gcc/x86_64-redhat-linux/15/cc1plus -fpreprocessed ./mod.cpp.ii
-quiet -dumpdir ./ -dumpbase mod.cpp.ii -dumpbase-ext .ii -mtune=generic
-march=x86-64 -std=gnu++20 -version -fmodules -o /tmp/ccGPo40S.s
GNU C++20 (GCC) version 15.2.1 20251111 (Red Hat 15.2.1-4)
(x86_64-redhat-linux)
        compiled by GNU C version 15.2.1 20251111 (Red Hat 15.2.1-4), GMP
version 6.3.0, MPFR version 4.2.2, MPC version 1.3.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: f4bbeda15da6b5264ad63c15be66cb3e
In file included from src/mod.cpp:2:
src/test2.h:12:10: error: ‘template<class A> void Class<A>::testFn()’ exposes
TU-local entity ‘void internalLink()’
src/test2.h:1:13: note: ‘void internalLink()’ declared with internal linkage

Reply via email to