[Bug c++/91873] -Wreturn-type diagnostic location changes depending on optimization level and destructor declaration

dov.murik at gmail dot com gcc-bugzilla@gcc.gnu.org
Sun Mar 21 13:05:55 GMT 2021


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

Dov Murik <dov.murik at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dov.murik at gmail dot com

--- Comment #2 from Dov Murik <dov.murik at gmail dot com> ---
I encountered a similar issue to the one reported by m101010a, in which the
following program bug.cpp causes a -Wreturn-type with -O0 (and with
-DHAS_DESTRUCTOR -DUSE_A_AFTER_RETURN).


$ cat bug.cpp
struct st {
#ifdef HAS_DESTRUCTOR
  ~st();
#endif
  void g();
};

int f() {
  st a;
  return 0;
#ifdef USE_A_AFTER_RETURN
  a.g();
#else
  return 7;
#endif
}

$ g++ -O0 -DHAS_DESTRUCTOR -DUSE_A_AFTER_RETURN -Werror -c bug.cpp
bug.cpp: In function ‘int f()’:
bug.cpp:16:1: error: control reaches end of non-void function
[-Werror=return-type]
   16 | }
      | ^
cc1plus: all warnings being treated as errors


The following changes causes the compiler to behave nicely and
not issue the warning:

* Use -O1 or higher.
* Remove the destructor (compile without -DHAS_DESTRUCTOR)
* Don't use the object in the dead code area (compile without
-DUSE_A_AFTER_RETURN).
* Use clang++
* Use g++ 7.5.0


$ g++ -O1 -DHAS_DESTRUCTOR -DUSE_A_AFTER_RETURN -Werror -c bug.cpp

$ g++ -O0 -DUSE_A_AFTER_RETURN -Werror -c bug.cpp

$ g++ -O0 -DHAS_DESTRUCTOR -Werror -c bug.cpp

$ clang++ -O0 -DHAS_DESTRUCTOR -DUSE_A_AFTER_RETURN -Werror -c bug.cpp


I'm not sure if this is the exact same issue as the original post or not, but
looks very similar.

As mentioned, this doesn't reproduce with gcc 7.5.0. I managed to reproduce
this
with gcc 8.3.0, gcc 9.3.0, 10.2.0, and also with gcc 11.0.1 20210320
(using godbolt). The full -v reports are below:

Using built-in specs.
COLLECT_GCC=g++-8
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 8.3.0-6'
--with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr
--with-gcc-major-version-only --program-suffix=-8
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie
--with-system-zlib --with-target-system-zlib --enable-objc-gc=auto
--enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 8.3.0 (Debian 8.3.0-6)

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
9.3.0-17ubuntu1~20.04' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-9
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new
--enable-gnu-unique-object --disable-vtable-verify --enable-plugin
--enable-default-pie --with-system-zlib --with-target-system-zlib=auto
--enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib
--with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-9-HskZEa/gcc-9-9.3.0/debian/tmp-nvptx/usr,hsa
--without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-linux-gnu/10.2.0/lto-wrapper
Target: x86_64-linux-gnu
Configured with: /usr/src/gcc/configure --build=x86_64-linux-gnu
--disable-multilib --enable-languages=c,c++,fortran,go
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (GCC)

Using built-in specs.
COLLECT_GCC=/opt/compiler-explorer/gcc-snapshot/bin/g++
Target: x86_64-linux-gnu
Configured with: ../gcc-trunk-20210321/configure
--prefix=/opt/compiler-explorer/gcc-build/staging --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu --disable-bootstrap
--enable-multiarch --with-abi=m64 --with-multilib-list=m32,m64,mx32
--enable-multilib --enable-clocale=gnu --enable-languages=c,c++,fortran,ada,d
--enable-ld=yes --enable-gold=yes --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --enable-linker-build-id --enable-lto
--enable-plugins --enable-threads=posix
--with-pkgversion=Compiler-Explorer-Build
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.0.1 20210320 (experimental) (Compiler-Explorer-Build)


More information about the Gcc-bugs mailing list