aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Martin <simartin@users.sourceforge.net>2007-10-12 18:43:33 +0000
committerSimon Martin <simartin@gcc.gnu.org>2007-10-12 18:43:33 +0000
commita9a81e7d6a50fc249f22af07504e9620f93d6037 (patch)
treeeabcc4bcf303645f96501855cd21ddaeac0d8c68
parent8d9597e148163a10bad52f7206a223bb236a20d7 (diff)
downloadgcc-a9a81e7d6a50fc249f22af07504e9620f93d6037.zip
gcc-a9a81e7d6a50fc249f22af07504e9620f93d6037.tar.gz
gcc-a9a81e7d6a50fc249f22af07504e9620f93d6037.tar.bz2
re PR c++/26698 (g++ accepts const-incorrect code due to conversion function)
gcc/cp/ 2007-10-12 Simon Martin <simartin@users.sourceforge.net> 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-12 Simon Martin <simartin@users.sourceforge.net> PR c++/26698 * g++.dg/conversion/op4.C: New test. From-SVN: r129274
-rw-r--r--gcc/cp/ChangeLog8
-rw-r--r--gcc/cp/call.c16
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/g++.dg/conversion/op4.C19
4 files changed, 47 insertions, 1 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 075e209..bc25402 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,11 @@
+2007-10-12 Simon Martin <simartin@users.sourceforge.net>
+
+ 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-12 Paolo Carlini <pcarlini@suse.de>
* pt.c (tsubst): Use template_parm_level_and_index.
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index cf3aea7..6ca0a95 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -2601,7 +2601,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 0b3aa07..6b0c2f8 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2007-10-12 Simon Martin <simartin@users.sourceforge.net>
+
+ PR c++/26698
+ * g++.dg/conversion/op4.C: New test.
+
2007-10-12 Richard Sandiford <rsandifo@nildram.co.uk>
* g++.dg/torture/pr33572.C (main): Allow argc to be zero.
diff --git a/gcc/testsuite/g++.dg/conversion/op4.C b/gcc/testsuite/g++.dg/conversion/op4.C
new file mode 100644
index 0000000..164bbcd
--- /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" } */
+}