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

            Bug ID: 91212
           Summary: const ignored for ctor arguments within return
           Product: gcc
           Version: 9.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: m...@osm-ag.de
  Target Milestone: ---

Created attachment 46613
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=46613&action=edit
preprocessed from -save-temps

g++ 9.1.0, gmp-6.1.1, mpfr-3.1.4, mpc-1.0.3, isl-0.18, cloog-0.18.4

having two ctors:
struct X{
  template <unsigned long N> X(const char (&src)[N]) {}
  template <unsigned long N> X(char (&src)[N]) {}
};

X f() {
  char buf[1];
  return buf;
}

the second ctor with non-const buffer should be used.
with g++ 9.1.0 the first ctor with const buffer is used.

build-option: --prefix=/opt/local/gcc-9.1.0 --enable-languages=c,c++
--disable-multilib
make-target: profiledbootstrap

$ gcc --version
gcc (GCC) 9.1.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ uname -a
Linux refle15sp1 4.12.14-195-default #1 SMP Tue May 7 10:55:11 UTC 2019
(8fba516) x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/os-release
NAME="SLES"
VERSION="15-SP1"
VERSION_ID="15.1"
PRETTY_NAME="SUSE Linux Enterprise Server 15 SP1"
ID="sles"
ID_LIKE="suse"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:suse:sles:15:sp1"

$ /opt/local/gcc-9.1.0/bin/g++ -v -save-temps -o ctor ctor.C
Using built-in specs.
COLLECT_GCC=/opt/local/gcc-9.1.0/bin/g++
COLLECT_LTO_WRAPPER=/opt/local/gcc-9.1.0/libexec/gcc/x86_64-pc-linux-gnu/9.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-9.1.0/configure --prefix=/opt/local/gcc-9.1.0
--enable-languages=c,c++ --disable-multilib
Thread model: posix
gcc version 9.1.0 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-o' 'ctor' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'
 /opt/local/gcc-9.1.0/libexec/gcc/x86_64-pc-linux-gnu/9.1.0/cc1plus -E -quiet
-v -D_GNU_SOURCE ctor.C -mtune=generic -march=x86-64 -fpch-preprocess -o
ctor.ii
ignoring nonexistent directory
"/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:

/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/../../../../include/c++/9.1.0

/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/../../../../include/c++/9.1.0/x86_64-pc-linux-gnu

/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/../../../../include/c++/9.1.0/backward
 /opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/include
 /usr/local/include
 /opt/local/gcc-9.1.0/include
 /opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-o' 'ctor' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'
 /opt/local/gcc-9.1.0/libexec/gcc/x86_64-pc-linux-gnu/9.1.0/cc1plus
-fpreprocessed ctor.ii -quiet -dumpbase ctor.C -mtune=generic -march=x86-64
-auxbase ctor -version -o ctor.s
GNU C++14 (GCC) version 9.1.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 9.1.0, GMP version 6.1.1, MPFR version 3.1.4,
MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++14 (GCC) version 9.1.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 9.1.0, GMP version 6.1.1, MPFR version 3.1.4,
MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: c5c2c4d89679134a97ef259dab7ccdbf
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-o' 'ctor' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'
 as -v --64 -o ctor.o ctor.s
GNU assembler version 2.31.1 (x86_64-suse-linux) using BFD version (GNU
Binutils; SUSE Linux Enterprise 15) 2.31.1.20180828-5
COMPILER_PATH=/opt/local/gcc-9.1.0/libexec/gcc/x86_64-pc-linux-gnu/9.1.0/:/opt/local/gcc-9.1.0/libexec/gcc/x86_64-pc-linux-gnu/9.1.0/:/opt/local/gcc-9.1.0/libexec/gcc/x86_64-pc-linux-gnu/:/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/:/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/:/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-o' 'ctor' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'
 /opt/local/gcc-9.1.0/libexec/gcc/x86_64-pc-linux-gnu/9.1.0/collect2 -plugin
/opt/local/gcc-9.1.0/libexec/gcc/x86_64-pc-linux-gnu/9.1.0/liblto_plugin.so
-plugin-opt=/opt/local/gcc-9.1.0/libexec/gcc/x86_64-pc-linux-gnu/9.1.0/lto-wrapper
-plugin-opt=-fresolution=ctor.res -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc
--eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o
ctor /usr/lib/../lib64/crt1.o /usr/lib/../lib64/crti.o
/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/crtbegin.o
-L/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0
-L/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/../../../../lib64
-L/lib/../lib64 -L/usr/lib/../lib64
-L/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/../../.. ctor.o
-lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/opt/local/gcc-9.1.0/lib/gcc/x86_64-pc-linux-gnu/9.1.0/crtend.o
/usr/lib/../lib64/crtn.o
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-o' 'ctor' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'

$ ./ctor
X(char (&src)[N])
X(const char (&src)[N])
X(const char (&src)[N])
X(char (&src)[N])

$ cat ctor.C
extern "C" {
    long write( int fd, const void *buf, unsigned long count );
}

struct X {
        template <unsigned long N> X(const char (&src)[N]) {
                write( 2, "X(const char (&src)[N])\n", 24 );
        }
        template <unsigned long N> X(char (&src)[N]) {
                write( 2, "X(char (&src)[N])\n", 18 );
        }
};

X
func1()
{
        char buf[2048];
        return buf;
}

X
func2()
{
        char buf[4096];
        return X(buf);
}

int
main()
{
        char buf[1024];

        X a( buf );
        X b( "x" );
        X c = func1();
        X d = func2();
}

Reply via email to