Bug 66857 - [5/6 Regression] Reference not bound to lvalue
Summary: [5/6 Regression] Reference not bound to lvalue
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.1.1
: P3 normal
Target Milestone: 5.3
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2015-07-13 12:23 UTC by Sebastian Lauwers
Modified: 2015-08-14 16:33 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.9.3
Known to fail:
Last reconfirmed: 2015-07-13 00:00:00


Attachments
preprocessed code (539 bytes, text/plain)
2015-07-13 12:23 UTC, Sebastian Lauwers
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastian Lauwers 2015-07-13 12:23:10 UTC
Created attachment 35967 [details]
preprocessed code

Reporting this as it seems to be a regression from gcc 4.9.2. The following code works on gcc 4.9.2, but asserts on gcc 5.1.1:

=========== 8< ==========
#include <cassert>

const int i = 0;

struct Test {
    Test(const int& rhs) {
        assert(&rhs == &i);
    }
};

int main(void) {
    Test test = i;
}
=========== >8 ==========

If line 12 is changed to:

    Test test(i);

The assertion is not triggered.

Please let me know if further information is required.
Comment 1 Sebastian Lauwers 2015-07-13 12:24:46 UTC
Command: g++ -v -save-temps test.cpp

Output:

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/5.1.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,objc,obj-c++,fortran,ada,go,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-linker-hash-style=gnu --enable-plugin --enable-initfini-array --disable-libgcj --with-default-libstdcxx-abi=c++98 --with-isl --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 5.1.1 20150618 (Red Hat 5.1.1-4) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/5.1.1/cc1plus -E -quiet -v -D_GNU_SOURCE test.cpp -mtune=generic -march=x86-64 -fpch-preprocess -o test.ii
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/5.1.1/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/5.1.1/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/5.1.1/../../../../include/c++/5.1.1
 /usr/lib/gcc/x86_64-redhat-linux/5.1.1/../../../../include/c++/5.1.1/x86_64-redhat-linux
 /usr/lib/gcc/x86_64-redhat-linux/5.1.1/../../../../include/c++/5.1.1/backward
 /usr/lib/gcc/x86_64-redhat-linux/5.1.1/include
 /usr/local/include
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/5.1.1/cc1plus -fpreprocessed test.ii -quiet -dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase test -version -o test.s
GNU C++ (GCC) version 5.1.1 20150618 (Red Hat 5.1.1-4) (x86_64-redhat-linux)
	compiled by GNU C version 5.1.1 20150618 (Red Hat 5.1.1-4), GMP version 6.0.0, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++ (GCC) version 5.1.1 20150618 (Red Hat 5.1.1-4) (x86_64-redhat-linux)
	compiled by GNU C version 5.1.1 20150618 (Red Hat 5.1.1-4), GMP version 6.0.0, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: a5de1e39a9a4df73d4ab91471f1f5942
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 as -v --64 -o test.o test.s
GNU assembler version 2.25 (x86_64-redhat-linux) using BFD version version 2.25-8.fc22
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/5.1.1/:/usr/libexec/gcc/x86_64-redhat-linux/5.1.1/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/5.1.1/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/5.1.1/:/usr/lib/gcc/x86_64-redhat-linux/5.1.1/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/5.1.1/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/5.1.1/collect2 -plugin /usr/libexec/gcc/x86_64-redhat-linux/5.1.1/liblto_plugin.so -plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/5.1.1/lto-wrapper -plugin-opt=-fresolution=test.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 --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/gcc/x86_64-redhat-linux/5.1.1/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/5.1.1/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/5.1.1/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/5.1.1 -L/usr/lib/gcc/x86_64-redhat-linux/5.1.1/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/5.1.1/../../.. test.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-redhat-linux/5.1.1/crtend.o /usr/lib/gcc/x86_64-redhat-linux/5.1.1/../../../../lib64/crtn.o
Comment 2 Jonathan Wakely 2015-07-13 13:07:14 UTC
The gimple dump shows a temporary int being created from the global, and the temporary is passed to the constructor.
Comment 3 Markus Trippelsdorf 2015-07-13 13:41:40 UTC
Started with r217814.
Comment 4 Patrick Palka 2015-07-13 23:44:18 UTC
Reverting the change made to ocp_convert in r217814 fixes it:

diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index 13bc1f7..6d4bd9a 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -687,7 +687,8 @@ ocp_convert (tree type, tree expr, int convtype, int flags,
     }

   /* FIXME remove when moving to c_fully_fold model.  */
-  e = scalar_constant_value (e);
+  if (!CLASS_TYPE_P (type))
+    e = scalar_constant_value (e);
   if (error_operand_p (e))
     return error_mark_node;
Comment 5 Jason Merrill 2015-07-15 14:24:48 UTC
(In reply to Patrick Palka from comment #4)
> Reverting the change made to ocp_convert in r217814 fixes it:
> 
> diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
> index 13bc1f7..6d4bd9a 100644
> --- a/gcc/cp/cvt.c
> +++ b/gcc/cp/cvt.c
> @@ -687,7 +687,8 @@ ocp_convert (tree type, tree expr, int convtype, int
> flags,
>      }
> 
>    /* FIXME remove when moving to c_fully_fold model.  */
> -  e = scalar_constant_value (e);
> +  if (!CLASS_TYPE_P (type))
> +    e = scalar_constant_value (e);
>    if (error_operand_p (e))
>      return error_mark_node;

This fix is OK.
Comment 6 Patrick Palka 2015-07-25 23:16:16 UTC
Author: ppalka
Date: Sat Jul 25 23:15:44 2015
New Revision: 226228

URL: https://gcc.gnu.org/viewcvs?rev=226228&root=gcc&view=rev
Log:
Fix PR c++/66857

gcc/cp/ChangeLog:

	PR c++/66857
	* cvt.c (ocp_convert): Don't call scalar_constant_value when
	converting to a class type.

gcc/testsuite/ChangeLog:

	PR c++/66857
	* g++.dg/init/pr66857.C: New test.


Added:
    trunk/gcc/testsuite/g++.dg/init/pr66857.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cvt.c
    trunk/gcc/testsuite/ChangeLog
Comment 7 Jason Merrill 2015-08-14 16:33:42 UTC
Author: jason
Date: Fri Aug 14 16:33:10 2015
New Revision: 226897

URL: https://gcc.gnu.org/viewcvs?rev=226897&root=gcc&view=rev
Log:
Fix PR c++/66857

gcc/cp/ChangeLog:

	PR c++/66857
	* cvt.c (ocp_convert): Don't call scalar_constant_value when
	converting to a class type.

gcc/testsuite/ChangeLog:

	PR c++/66857
	* g++.dg/init/pr66857.C: New test.

Added:
    branches/gcc-5-branch/gcc/testsuite/g++.dg/init/pr66857.C
Modified:
    branches/gcc-5-branch/gcc/cp/ChangeLog
    branches/gcc-5-branch/gcc/cp/cvt.c
Comment 8 Jason Merrill 2015-08-14 16:33:52 UTC
Fixed.