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

            Bug ID: 93296
           Summary: Compiler error when assigning array to const reference
                    with implicit constructor call.
           Product: gcc
           Version: 9.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: benjamin at readyatdawn dot com
  Target Milestone: ---

I'm seeing this error running godbolt.org compiler explorer. I don't have gcc
installed on my PC so I have not attached a preprocessed file, but the bug is
very simple to reproduce. I see the issue with all versions of gcc available on
compiler explorer.

This is the simplest repro I ended up with:

<code>
struct Container {
    Container(int (&)[3]) { }
};

int main() {
    int data[3];
    const Container& container = data;
    return 0;
}
</code>

error: invalid initialization of reference of type 'const Container&' from
expression of type 'int*'

A few changes avoid the issue, these both work:

<code>
    const Container container = data;    // no reference
    const Container& container { data }; // explicit construction
</code>

Compiler output with '-v':

Using built-in specs.

COLLECT_GCC=/opt/compiler-explorer/gcc-9.2.0/bin/g++

Target: x86_64-linux-gnu

Configured with: ../gcc-9.2.0/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

gcc version 9.2.0 (Compiler-Explorer-Build) 

COLLECT_GCC_OPTIONS='-fdiagnostics-color=always' '-g' '-o' './output.s'
'-masm=intel' '-S' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'


/opt/compiler-explorer/gcc-9.2.0/bin/../libexec/gcc/x86_64-linux-gnu/9.2.0/cc1plus
-quiet -v -imultiarch x86_64-linux-gnu -iprefix
/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/
-D_GNU_SOURCE <source> -quiet -dumpbase example.cpp -masm=intel -mtune=generic
-march=x86-64 -auxbase-strip ./output.s -g -version -fdiagnostics-color=always
-o ./output.s

GNU C++14 (Compiler-Explorer-Build) version 9.2.0 (x86_64-linux-gnu)

        compiled by GNU C version 7.4.0, GMP version 6.1.0, 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

ignoring nonexistent directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../x86_64-linux-gnu/include"

ignoring duplicate directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0"

ignoring duplicate directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/x86_64-linux-gnu"

ignoring duplicate directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/backward"

ignoring duplicate directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/9.2.0/include"

ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"

ignoring duplicate directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/9.2.0/include-fixed"

ignoring nonexistent directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../x86_64-linux-gnu/include"

#include "..." search starts here:

#include <...> search starts here:


/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0


/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/x86_64-linux-gnu


/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/backward

 /opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/include


/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/include-fixed

 /usr/local/include

 /opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../include

 /usr/include/x86_64-linux-gnu

 /usr/include

End of search list.

GNU C++14 (Compiler-Explorer-Build) version 9.2.0 (x86_64-linux-gnu)

        compiled by GNU C version 7.4.0, GMP version 6.1.0, 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: 35bcd684e0bc8b3378dcc1f162909bba

<source>: In function 'int main()':

<source>:7:34: error: invalid initialization of reference of type 'const
Container&' from expression of type 'int*'

    7 |     const Container& container = data;

      |                                  ^~~~

ASM generation compiler returned: 1

Using built-in specs.

COLLECT_GCC=/opt/compiler-explorer/gcc-9.2.0/bin/g++

COLLECT_LTO_WRAPPER=/opt/compiler-explorer/gcc-9.2.0/bin/../libexec/gcc/x86_64-linux-gnu/9.2.0/lto-wrapper

Target: x86_64-linux-gnu

Configured with: ../gcc-9.2.0/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

gcc version 9.2.0 (Compiler-Explorer-Build) 

COLLECT_GCC_OPTIONS='-fdiagnostics-color=always' '-g' '-o' './output.s' '-v'
'-shared-libgcc' '-mtune=generic' '-march=x86-64'


/opt/compiler-explorer/gcc-9.2.0/bin/../libexec/gcc/x86_64-linux-gnu/9.2.0/cc1plus
-quiet -v -imultiarch x86_64-linux-gnu -iprefix
/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/
-D_GNU_SOURCE <source> -quiet -dumpbase example.cpp -mtune=generic
-march=x86-64 -auxbase example -g -version -fdiagnostics-color=always -o
/tmp/cc5PFAVP.s

GNU C++14 (Compiler-Explorer-Build) version 9.2.0 (x86_64-linux-gnu)

        compiled by GNU C version 7.4.0, GMP version 6.1.0, 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

ignoring nonexistent directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../x86_64-linux-gnu/include"

ignoring duplicate directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0"

ignoring duplicate directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/x86_64-linux-gnu"

ignoring duplicate directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/backward"

ignoring duplicate directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/9.2.0/include"

ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"

ignoring duplicate directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/9.2.0/include-fixed"

ignoring nonexistent directory
"/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../x86_64-linux-gnu/include"

#include "..." search starts here:

#include <...> search starts here:


/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0


/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/x86_64-linux-gnu


/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/backward

 /opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/include


/opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/x86_64-linux-gnu/9.2.0/include-fixed

 /usr/local/include

 /opt/compiler-explorer/gcc-9.2.0/bin/../lib/gcc/../../include

 /usr/include/x86_64-linux-gnu

 /usr/include

End of search list.

GNU C++14 (Compiler-Explorer-Build) version 9.2.0 (x86_64-linux-gnu)

        compiled by GNU C version 7.4.0, GMP version 6.1.0, 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: 35bcd684e0bc8b3378dcc1f162909bba

<source>: In function 'int main()':

<source>:7:34: error: invalid initialization of reference of type 'const
Container&' from expression of type 'int*'

    7 |     const Container& container = data;

      |                                  ^~~~

Execution build compiler returned: 1

Reply via email to