This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Fix PR c++/26698: g++ accepts const-incorrect code due to conversion function
- From: Simon Martin <simartin at users dot sourceforge dot net>
- To: Mark Mitchell <mark at codesourcery dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Fri, 12 Oct 2007 20:47:58 +0200
- Subject: Re: [PATCH] Fix PR c++/26698: g++ accepts const-incorrect code due to conversion function
- References: <4709385D.6020202@users.sourceforge.net> <470D061C.60803@codesourcery.com>
Hi Mark.
You can simplify this condition a bit. If you use
same_type_ignoring_top_level_qualifiers_p, it will automatically do the
TYPE_MAIN_VARIANT bit for you. And, so will DERIVED_FROM_P, so:
tree to_non_ref = non_reference (totype);
if (same_type_ignoring_top_level_qualifiers_p (to_non_ref, fromtype)
|| (CLASS_TYPE_P (to_non_ref) && CLASS_TYPE_P (fromtype)
&& DERIVED_FROM_P (to_non_ref, fromtype))
should work. OK with that change.
It works indeed. I attach the updated patch, that I've successfully
regtested on i386-apple-darwin8.10.1, and that I will commit into trunk
and the 4.2 branch.
Best regards,
Simon
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).
Index: gcc/cp/call.c
===================================================================
--- gcc/cp/call.c (revision 129214)
+++ gcc/cp/call.c (working copy)
@@ -2601,7 +2601,21 @@ build_user_type_conversion_1 (tree totyp
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;