From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 376173858D37; Tue, 23 May 2023 12:39:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 376173858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684845597; bh=RIgLFpxp9lpTrIWqWeJmE3Nb7Z+FSCbUyYE73cbS0tw=; h=From:To:Subject:Date:From; b=LWMFFaDq7oWdXrx/nLPg8vXc2Cl1JZg0othbHtsqeIxajqAUB6yXkYWSvIMQ5qaoR mRqr4G2OcxoPkyAz6RSKeqMzVMuDJgreoknxqsPXegXk2GyS4yHcCYooB1NbDg2f94 3EwN/wvyStXDH0n4oDPKCMfgWEeflwtOO21D4y9A= From: "simon-pfahler at gmx dot de" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/109942] New: Function template declared extern is implicitly instantiated on -O optimization level when return type is auto Date: Tue, 23 May 2023 12:39:56 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: simon-pfahler at gmx dot de X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109942 Bug ID: 109942 Summary: Function template declared extern is implicitly instantiated on -O optimization level when return type is auto Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: simon-pfahler at gmx dot de Target Milestone: --- Created attachment 55143 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D55143&action=3Dedit preprocessed *.ii file Declaring a template function as extern, but not providing a definition sho= uld lead to a linker error. This does not work on higher optimization levels, if the template function's return type is `auto`. The code is: ``` template auto foo() { return; } extern template auto foo(); int main() { foo(); } ``` This fails to link when the command line is `g++ test.cpp`, as it should. B= ut for `g++ -O test.cpp`, the compilation and linkage works. Explicitly stating the return type (replacing `auto` by `void`) leads to correct behavior (lin= ker failure on any optimization level). Similar examples can be constructed where compilation with optimizatio <=3D= -O1 leads to expected failure, while compilation is successfull for optimization >=3D-O2. This is problematic when return types can not easily be determined and explicitly stated. The bug leads to unwanted instantiation of the template, which means longer compile time if a definition would be provided in a different compilation u= nit. Additional info: gcc -v: Using built-in specs. COLLECT_GCC=3Dgcc COLLECT_LTO_WRAPPER=3D/usr/lib/gcc/x86_64-linux-gnu/12/lto-wrapper OFFLOAD_TARGET_NAMES=3Dnvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=3D1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion=3D'Debian 12.2.0-14' --with-bugurl=3Dfile:///usr/share/doc/gcc-12/README.Bugs --enable-languages=3Dc,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=3D/usr --with-gcc-major-version-only --program-suffix=3D-12 --program-prefix=3Dx86_64-linux-gnu- --enable-shared --enable-linker-build-= id --libexecdir=3D/usr/lib --without-included-gettext --enable-threads=3Dposix --libdir=3D/usr/lib --enable-nls --enable-clocale=3Dgnu --enable-libstdcxx-= debug --enable-libstdcxx-time=3Dyes --with-default-libstdcxx-abi=3Dnew --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=3Drelea= se --with-target-system-zlib=3Dauto --enable-objc-gc=3Dauto --enable-multiarch --disable-werror --enable-cet --with-arch-32=3Di686 --with-abi=3Dm64 --with-multilib-list=3Dm32,m64,mx32 --enable-multilib --with-tune=3Dgeneric --enable-offload-targets=3Dnvptx-none=3D/build/gcc-12-bTRWOB/gcc-12-12.2.0/= debian/tmp-nvptx/usr,amdgcn-amdhsa=3D/build/gcc-12-bTRWOB/gcc-12-12.2.0/deb= ian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=3Drelease --build=3Dx86_64-linux-gnu --host=3Dx86_64-linux-gnu --target=3Dx86_64-linu= x-gnu Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 12.2.0 (Debian 12.2.0-14) Complete command line: `g++ test.cpp` or `g++ -O test.cpp` Compiler output message for `g++ test.cpp`: /usr/bin/ld: /tmp/ccBQx0I3.o: in function `main': test.cpp:(.text+0x5): undefined reference to `auto foo()' collect2: error: ld returned 1 exit status Compiler output message for `g++ -O test.cpp`: Nothing, as compilation is successful. Preprocessed file (*.ii) is attached. Let me know if there is information missing.=