Fix conversions for built-in operator overloading candidates.
authorJason Merrill <jason@redhat.com>
Tue, 5 Nov 2019 23:53:53 +0000 (18:53 -0500)
committerJason Merrill <jason@gcc.gnu.org>
Tue, 5 Nov 2019 23:53:53 +0000 (18:53 -0500)
While working on C++20 operator<=>, I noticed that build_new_op_1 was doing
too much conversion when a built-in candidate was selected; the standard
says it should only perform user-defined conversions, and then leave the
normal operator semantics to handle any standard conversions.  This is
important for operator<=> because a comparison of two different unscoped
enums is ill-formed; if we promote the enums to int here, cp_build_binary_op
never gets to see the original operand types, so we can't give the error.

I'm also disabling -Wmaybe-uninitialized for expmed.c to avoid the bootstrap
failure from the last time I applied this patch.

* call.c (build_new_op_1): Don't apply any standard conversions to
the operands of a built-in operator.  Don't suppress conversions in
cp_build_unary_op.
* typeck.c (cp_build_unary_op): Do integral promotions for enums.

PR tree-optimization/91825
* expmed.c: Reduce -Wmaybe-uninitialized to warning.

From-SVN: r277864

gcc/ChangeLog
gcc/cp/ChangeLog
gcc/cp/call.c
gcc/cp/typeck.c
gcc/expmed.c

index 4312d12506149a7d64d2ede5003c408115a82727..8ee23dd5986befbc054c8d53801e098003510449 100644 (file)
@@ -1,3 +1,8 @@
+2019-10-26  Jason Merrill  <jason@redhat.com>
+
+       PR tree-optimization/91825
+       * expmed.c: Reduce -Wmaybe-uninitialized to warning.
+
 2019-11-05  Jim Wilson  <jimw@sifive.com>
 
        PR middle-end/92263
index 2085319bbc996fda0d16165383e820120be16fec..560896a1dd8154b6968b182b57f88764a85701ad 100644 (file)
@@ -1,3 +1,10 @@
+2019-09-15  Jason Merrill  <jason@redhat.com>
+
+       * call.c (build_new_op_1): Don't apply any standard conversions to
+       the operands of a built-in operator.  Don't suppress conversions in
+       cp_build_unary_op.
+       * typeck.c (cp_build_unary_op): Do integral promotions for enums.
+
 2019-11-04  Jason Merrill  <jason@redhat.com>
 
        Use vec instead of raw array for built-in candidates.
index 2a1da3cc4e1ece2a89dc065084475a07d835513d..390a4c581e2c922f587b80a3db9973cbd8f6314f 100644 (file)
@@ -6156,41 +6156,40 @@ build_new_op_1 (const op_location_t &loc, enum tree_code code, int flags,
              break;
            }
 
-         /* We need to strip any leading REF_BIND so that bitfields
-            don't cause errors.  This should not remove any important
-            conversions, because builtins don't apply to class
-            objects directly.  */
+         /* "If a built-in candidate is selected by overload resolution, the
+            operands of class type are converted to the types of the
+            corresponding parameters of the selected operation function,
+            except that the second standard conversion sequence of a
+            user-defined conversion sequence (12.3.3.1.2) is not applied."  */
          conv = cand->convs[0];
-         if (conv->kind == ck_ref_bind)
-           conv = next_conversion (conv);
-         arg1 = convert_like (conv, arg1, complain);
+         if (conv->user_conv_p)
+           {
+             while (conv->kind != ck_user)
+               conv = next_conversion (conv);
+             arg1 = convert_like (conv, arg1, complain);
+           }
 
          if (arg2)
            {
              conv = cand->convs[1];
-             if (conv->kind == ck_ref_bind)
-               conv = next_conversion (conv);
-             else
-               arg2 = decay_conversion (arg2, complain);
-
-             /* We need to call warn_logical_operator before
-                converting arg2 to a boolean_type, but after
-                decaying an enumerator to its value.  */
-             if (complain & tf_warning)
-               warn_logical_operator (loc, code, boolean_type_node,
-                                      code_orig_arg1, arg1,
-                                      code_orig_arg2, arg2);
-
-             arg2 = convert_like (conv, arg2, complain);
+             if (conv->user_conv_p)
+               {
+                 while (conv->kind != ck_user)
+                   conv = next_conversion (conv);
+                 arg2 = convert_like (conv, arg2, complain);
+               }
            }
+
          if (arg3)
            {
              conv = cand->convs[2];
-             if (conv->kind == ck_ref_bind)
-               conv = next_conversion (conv);
-             convert_like (conv, arg3, complain);
+             if (conv->user_conv_p)
+               {
+                 while (conv->kind != ck_user)
+                   conv = next_conversion (conv);
+                 arg3 = convert_like (conv, arg3, complain);
+               }
            }
-
        }
     }
 
@@ -6258,7 +6257,7 @@ build_new_op_1 (const op_location_t &loc, enum tree_code code, int flags,
     case REALPART_EXPR:
     case IMAGPART_EXPR:
     case ABS_EXPR:
-      return cp_build_unary_op (code, arg1, candidates != 0, complain);
+      return cp_build_unary_op (code, arg1, false, complain);
 
     case ARRAY_REF:
       return cp_build_array_ref (input_location, arg1, arg2, complain);
index bf2502a3686dc3e4e11080624f53c237273f3169..50240537938aef00086bd3832727503ffdfe9a72 100644 (file)
@@ -6298,7 +6298,7 @@ cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
                       : _("wrong type argument to unary plus"));
        else
          {
-           if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
+           if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
              arg = cp_perform_integral_promotions (arg, complain);
 
            /* Make sure the result is not an lvalue: a unary plus or minus
@@ -6323,7 +6323,7 @@ cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
                                                   | WANT_VECTOR_OR_COMPLEX,
                                                   arg, true)))
        errstring = _("wrong type argument to bit-complement");
-      else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
+      else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
        {
          /* Warn if the expression has boolean value.  */
          if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
index f1975fe33fe6ace731be3ef424928d6c1cf5fe33..ff8554b156202c2e1f95644d3628f928bdcaacc8 100644 (file)
@@ -18,6 +18,8 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+/* Work around tree-optimization/91825.  */
+#pragma GCC diagnostic warning "-Wmaybe-uninitialized"
 
 #include "config.h"
 #include "system.h"