From: Simon Martin Date: Sat, 13 Oct 2007 06:04:57 +0000 (+0000) Subject: re PR c++/26698 (g++ accepts const-incorrect code due to conversion function) X-Git-Tag: prereleases/gcc-4.2.3-rc1~190 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=59a85808a9d5e8c167086e02250929943ce82f8e;p=thirdparty%2Fgcc.git re PR c++/26698 (g++ accepts const-incorrect code due to conversion function) gcc/cp/ 2007-10-13 Simon Martin PR c++/26698 * call.c (build_user_type_conversion_1): Do not consider conversion functions to convert a (possibly cv-qualified) object to the (possibly cv-qualified) same object type (or a reference to it), to a (possibly cv-qualified) base class of that type (or a reference to it). gcc/testsuite/ 2007-10-13 Simon Martin PR c++/26698 * g++.dg/conversion/op4.C: New test. From-SVN: r129282 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 5a8ecbcbe35b..e884dc47c69b 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,11 @@ +2007-10-13 Simon Martin + + PR c++/26698 + * call.c (build_user_type_conversion_1): Do not consider conversion + functions to convert a (possibly cv-qualified) object to the (possibly + cv-qualified) same object type (or a reference to it), to a (possibly + cv-qualified) base class of that type (or a reference to it). + 2007-10-09 Jason Merrill PR c++/32470 diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 891ebb96829c..244cab1c1f34 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -2550,7 +2550,21 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags) ctors = lookup_fnfields (totype, complete_ctor_identifier, 0); if (IS_AGGR_TYPE (fromtype)) - conv_fns = lookup_conversions (fromtype); + { + tree to_nonref = non_reference (totype); + if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) || + (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype) + && DERIVED_FROM_P (to_nonref, fromtype))) + { + /* [class.conv.fct] A conversion function is never used to + convert a (possibly cv-qualified) object to the (possibly + cv-qualified) same object type (or a reference to it), to a + (possibly cv-qualified) base class of that type (or a + reference to it)... */ + } + else + conv_fns = lookup_conversions (fromtype); + } candidates = 0; flags |= LOOKUP_NO_CONVERSION; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 7ffdf38aec72..6de4856501f7 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2007-10-13 Simon Martin + + PR c++/26698 + * g++.dg/conversion/op4.C: New test. + 2007-10-12 Zdenek Dvorak PR tree-optimization/33714 diff --git a/gcc/testsuite/g++.dg/conversion/op4.C b/gcc/testsuite/g++.dg/conversion/op4.C new file mode 100644 index 000000000000..164bbcdc4131 --- /dev/null +++ b/gcc/testsuite/g++.dg/conversion/op4.C @@ -0,0 +1,19 @@ +/* PR c++/26698 */ +/* { dg-do "compile" } */ + +struct X { + int x; + X (int i = 0) : x (i) {} + operator X& (void) const { + return *(new X); + } +}; + +void add_one (X & ref) { /* { dg-error "in passing argument" } */ + ++ ref.x; +} + +void foo() { + X const a (2); + add_one(a); /* { dg-error "invalid initialization of reference of type" } */ +}