Bug 10931

Summary: [3.3/3.4 regression] valid conversion static_cast<const unsigned int&>(lvalue-of-type-int) is rejected
Product: gcc Reporter: Tim Bagot <timb>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: gcc-bugs, giovannibajo
Priority: P2 Keywords: rejects-valid
Version: 3.3   
Target Milestone: 3.3.1   
Host: i386-linux Target: i386-linux
Build: i386-linux Known to work:
Known to fail: Last reconfirmed: 2003-05-22 22:27:55

Description Tim Bagot 2003-05-22 12:35:09 UTC
This code:

void foo(int x)
{
    static_cast<const unsigned int&>(x);
}

gets:

test.cpp:3: error: non-lvalue in unary `&'

AFAICT there is nothing wrong with it, and it compiles fine before 3.3. (And 
what unary `&'?)
Comment 1 Giovanni Bajo 2003-05-22 22:27:55 UTC
Confirmed. The code is illegal because the conversion is invalid (basically, 
you can't bind an int variable to an unsigned int reference), but the 
diagnostic is totally confusing.
Comment 2 Tim Bagot 2003-05-23 08:37:53 UTC
I don't think it is illegal, because the reference is to const unsigned int, not
unsigned int. Looking at [8.5.3/5], the initialiser is an lvalue but the types
are (as you point out) not reference compatible, and the initialiser does not 
have a class type; therefore the second top-level case applies: '- Otherwise 
the reference shall be to a non-volatile const type', which does hold. The 
initialiser is not an rvalue, so the final case applies: 'a temporary of 
type "cv1 T1" is created and initialised from the initialiser expression using 
the rules for a non-reference copy initialisation (8.5). The reference is then 
bound to the temporary.'

In fact, initialising a named reference does work, fortunately providing a 
workaround:

const unsigned int& y = x;
Comment 3 Giovanni Bajo 2003-05-29 18:26:17 UTC
This issue was discussed on comp.std.c++ here:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-
8&threadm=rpcadv8bdp0b5pgfo19831t7cu8v6vl4qc%404ax.com&rnum=1&prev=/&frame=on

It appears to be a bug in GCC 3.3/3.4 and a regression with respect to 3.2. 
It's no more a diagnostic issue.
Comment 4 GCC Commits 2003-06-26 01:03:26 UTC
Subject: Bug 10931

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	mmitchel@gcc.gnu.org	2003-06-26 00:07:10

Modified files:
	gcc/cp         : ChangeLog typeck.c cp-tree.h call.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/g++.dg/expr: static_cast1.C 

Log message:
	PR c++/10931
	* g++.dg/expr/static_cast1.C: New test.
	
	PR c++/10931
	* call.c (convert_like): Pass issue_conversion_warnings.
	(convert_like_with_context): Likewise.
	(convert_like_real): Add issue_conversion_warnings parameter.
	(perform_direct_initialization_if_possible): New function.
	* cp-tree.h (perform_direct_initialization_if_possible): Declare it.
	* typeck.c (check_for_casting_away_constness): New function.
	(build_static_cast): Rewrite.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3455&r2=1.3456
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck.c.diff?cvsroot=gcc&r1=1.467&r2=1.468
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/cp-tree.h.diff?cvsroot=gcc&r1=1.859&r2=1.860
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/call.c.diff?cvsroot=gcc&r1=1.391&r2=1.392
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2798&r2=1.2799
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/expr/static_cast1.C.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 5 Mark Mitchell 2003-06-26 01:09:18 UTC
Fixed in GCC 3.3.1, GCC 3.4.
Comment 6 GCC Commits 2003-06-26 04:27:01 UTC
Subject: Bug 10931

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_3-branch
Changes by:	mmitchel@gcc.gnu.org	2003-06-26 01:05:47

Modified files:
	gcc/cp         : ChangeLog call.c cp-tree.h search.c typeck.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/g++.dg/abi: mangle4.C 
	gcc/testsuite/g++.dg/lookup: scoped1.C 
	gcc/testsuite/g++.old-deja/g++.jason: access8.C 
	gcc/testsuite/g++.old-deja/g++.martin: pmf1.C 
Added files:
	gcc/testsuite/g++.dg/expr: static_cast1.C 
	gcc/testsuite/g++.dg/rtti: dyncast1.C 

Log message:
	PR c++/10990
	* search.c (lookup_base_r): Rely on accessible_p, rather than
	trying to emulate that logic here.
	
	PR c++/10931
	* call.c (convert_like): Pass issue_conversion_warnings.
	(convert_like_with_context): Likewise.
	(convert_like_real): Add issue_conversion_warnings parameter.
	(perform_direct_initialization_if_possible): New function.
	* cp-tree.h (perform_direct_initialization_if_possible): Declare it.
	* typeck.c (check_for_casting_away_constness): New function.
	(build_static_cast): Rewrite.
	
	PR c++/10990
	* g++.dg/rtti/dyncast1.C: New test.
	* g++.dg/abi/mangle4.C: Correct base-specifier access.
	* g++.dg/lookup/scoped1.C: Remove XFAIL.
	* g++.old-deja/g++.martin/pmf1.C: Correct base-specifier access.
	
	PR c++/10931
	* g++.dg/expr/static_cast1.C: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.3076.2.163&r2=1.3076.2.164
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/call.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.341.2.23&r2=1.341.2.24
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/cp-tree.h.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.776.2.23&r2=1.776.2.24
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/search.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.243.2.10&r2=1.243.2.11
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.436.2.8&r2=1.436.2.9
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.2261.2.202&r2=1.2261.2.203
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/abi/mangle4.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.1&r2=1.1.46.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/expr/static_cast1.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/lookup/scoped1.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.1&r2=1.1.28.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/rtti/dyncast1.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.old-deja/g++.jason/access8.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.4&r2=1.4.20.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.old-deja/g++.martin/pmf1.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.1&r2=1.1.64.1