c++: Missing SFINAE with lookup_fnfields [PR78446]
authorPatrick Palka <ppalka@redhat.com>
Thu, 14 May 2020 16:56:18 +0000 (12:56 -0400)
committerPatrick Palka <ppalka@redhat.com>
Thu, 14 May 2020 16:56:18 +0000 (12:56 -0400)
Here we're failing to do SFINAE in build_op_call when looking up the
class's operator() via lookup_fnfields, which calls lookup_member always
with complain=tf_warning_or_error; from there we would complain
about an ambiguous lookup for operator().

This patch fixes this by adding a tsubst_flags_t parameter to
lookup_fnfields and adjusting all its callers appropriately.

gcc/cp/ChangeLog:

PR c++/78446
* call.c (build_op_call): Pass complain to lookup_fnfields.
(build_special_member_call): Likewise.
* class.c (type_requires_array_cookie): Pass tf_warning_or_error
to lookup_fnfields.
* cp-tree.h (lookup_fnfields): Add tsubst_flags_t parameter.
* except.c (build_throw): Pass tf_warning_or_error to
lookup_fnfields.
* init.c (build_new_1): Pass complain to lookup_fnfields.
* method.c (locate_fn_flags): Likewise.
* name-lookup.c (lookup_name_real_1): Pass tf_warning_or_error
to lookup_fnfields.
* pt.c (tsubst_baselink): Pass complain to lookup_fnfields.
* search.c (lookup_fnfields): New 'complain' parameter.  Pass it
to lookup_member.

gcc/testsuite/ChangeLog:

PR c++/78446
* g++.dg/template/sfinae31.C: New test.

12 files changed:
gcc/cp/ChangeLog
gcc/cp/call.c
gcc/cp/class.c
gcc/cp/cp-tree.h
gcc/cp/except.c
gcc/cp/init.c
gcc/cp/method.c
gcc/cp/name-lookup.c
gcc/cp/pt.c
gcc/cp/search.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/template/sfinae31.C [new file with mode: 0644]

index ba0d9e691cac5fcdc931baff1b82736ec4010350..437495458e2a2ae230377c3b92a7b0de3d56948b 100644 (file)
@@ -1,3 +1,21 @@
+2020-05-14  Patrick Palka  <ppalka@redhat.com>
+
+       PR c++/78446
+       * call.c (build_op_call): Pass complain to lookup_fnfields.
+       (build_special_member_call): Likewise.
+       * class.c (type_requires_array_cookie): Pass tf_warning_or_error
+       to lookup_fnfields.
+       * cp-tree.h (lookup_fnfields): Add tsubst_flags_t parameter.
+       * except.c (build_throw): Pass tf_warning_or_error to
+       lookup_fnfields.
+       * init.c (build_new_1): Pass complain to lookup_fnfields.
+       * method.c (locate_fn_flags): Likewise.
+       * name-lookup.c (lookup_name_real_1): Pass tf_warning_or_error
+       to lookup_fnfields.
+       * pt.c (tsubst_baselink): Pass complain to lookup_fnfields.
+       * search.c (lookup_fnfields): New 'complain' parameter.  Pass it
+       to lookup_member.
+
 2020-05-14  Nathan Sidwell  <nathan@acm.org>
 
        * parser.c (cp_parser_diagnose_invalid_typename): Mention
index 940e65da6319dfbda70a115ea6c2a0b32edcfb49..c5871974eb155daa1d62ce9927c939a6b35c2b6b 100644 (file)
@@ -4792,7 +4792,7 @@ build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
 
   if (TYPE_BINFO (type))
     {
-      fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1);
+      fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain);
       if (fns == error_mark_node)
        return error_mark_node;
     }
@@ -5978,7 +5978,7 @@ add_operator_candidates (z_candidate **candidates,
   tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE;
   if (CLASS_TYPE_P (arg1_type))
     {
-      tree fns = lookup_fnfields (arg1_type, fnname, 1);
+      tree fns = lookup_fnfields (arg1_type, fnname, 1, complain);
       if (fns == error_mark_node)
        return error_mark_node;
       if (fns)
@@ -6785,7 +6785,7 @@ build_op_delete_call (enum tree_code code, tree addr, tree size,
 
        Therefore, we ask lookup_fnfields to complain about ambiguity.  */
     {
-      fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
+      fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain);
       if (fns == error_mark_node)
        return error_mark_node;
     }
@@ -9806,7 +9806,7 @@ build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
        }
     }
 
-  fns = lookup_fnfields (binfo, name, 1);
+  fns = lookup_fnfields (binfo, name, 1, complain);
 
   /* When making a call to a constructor or destructor for a subobject
      that uses virtual base classes, pass down a pointer to a VTT for
index 15f490da3fca0b3b5434051751adfde527207ea8..bab15524a606aac2914a0e4809943e6973a5e8c7 100644 (file)
@@ -5644,7 +5644,7 @@ type_requires_array_cookie (tree type)
      a cookie.  */
   fns = lookup_fnfields (TYPE_BINFO (type),
                         ovl_op_identifier (false, VEC_DELETE_EXPR),
-                        /*protect=*/0);
+                        /*protect=*/0, tf_warning_or_error);
   /* If there are no `operator []' members, or the lookup is
      ambiguous, then we don't need a cookie.  */
   if (!fns || fns == error_mark_node)
index f7c11bcf83802a95eeb77a5b3da7adfd61c5e605..827b03dd5d5024b40e80548c07b73812c5671674 100644 (file)
@@ -7056,7 +7056,7 @@ extern tree dcast_base_hint                       (tree, tree);
 extern int accessible_p                                (tree, tree, bool);
 extern int accessible_in_template_p            (tree, tree);
 extern tree lookup_field                       (tree, tree, int, bool);
-extern tree lookup_fnfields                    (tree, tree, int);
+extern tree lookup_fnfields                    (tree, tree, int, tsubst_flags_t);
 extern tree lookup_member                      (tree, tree, int, bool,
                                                 tsubst_flags_t,
                                                 access_failure_info *afi = NULL);
index 7e93c51f9ea94203369be6df1bb55952e3d2fcfd..9e1aa5085d492ce5a2bfcaabd8998bc6de7e1b13 100644 (file)
@@ -821,7 +821,8 @@ build_throw (location_t loc, tree exp)
       if (type_build_dtor_call (TREE_TYPE (object)))
        {
          tree dtor_fn = lookup_fnfields (TYPE_BINFO (TREE_TYPE (object)),
-                                         complete_dtor_identifier, 0);
+                                         complete_dtor_identifier, 0,
+                                         tf_warning_or_error);
          dtor_fn = BASELINK_FUNCTIONS (dtor_fn);
          mark_used (dtor_fn);
          if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (object)))
index d4c883be3136846eef4b5d6b1f2a5f8f36fcdf28..ef4b3c4dc3cc9cf684fa089bf582b53e33d60172 100644 (file)
@@ -3276,7 +3276,7 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
       /* Create the argument list.  */
       vec_safe_insert (*placement, 0, size);
       /* Do name-lookup to find the appropriate operator.  */
-      fns = lookup_fnfields (elt_type, fnname, /*protect=*/2);
+      fns = lookup_fnfields (elt_type, fnname, /*protect=*/2, complain);
       if (fns == NULL_TREE)
        {
          if (complain & tf_error)
index 3f8842bf49482a954b708f4929603eddb40df499..2d31462c90149ead50fdcb5cbd27d7d94e4fe686 100644 (file)
@@ -1646,7 +1646,7 @@ locate_fn_flags (tree type, tree name, tree argtype, int flags,
        }
     }
 
-  fns = lookup_fnfields (binfo, name, 0);
+  fns = lookup_fnfields (binfo, name, 0, complain);
   rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
 
   if (fn && rval == error_mark_node)
index 4928b60758916b9956c7b8993f1d8fefd6579e65..2ff85f1cf5e29864950798b32a0d407858d0fc86 100644 (file)
@@ -6455,7 +6455,8 @@ lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
 
          /* Lookup the conversion operator in the class.  */
          class_type = level->this_entity;
-         operators = lookup_fnfields (class_type, name, /*protect=*/0);
+         operators = lookup_fnfields (class_type, name, /*protect=*/0,
+                                      tf_warning_or_error);
          if (operators)
            return operators;
        }
index 5ca659e9f288b40198cb185451f1122451001e38..61e6fa7484dfa30a036dcdf75dcf55c1b5a0243f 100644 (file)
@@ -16022,7 +16022,8 @@ tsubst_baselink (tree baselink, tree object_type,
        /* Treat as-if non-dependent below.  */
        dependent_p = false;
 
-      baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
+      baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
+                                 complain);
       if (!baselink)
        {
          if ((complain & tf_error)
index d9a2d748428e5b3afc22a4b0bbe9021a4b131af7..b9da2fccb7fefebbedf0ed353b20110b62a0ea83 100644 (file)
@@ -1347,10 +1347,11 @@ lookup_field (tree xbasetype, tree name, int protect, bool want_type)
    return NULL_TREE.  */
 
 tree
-lookup_fnfields (tree xbasetype, tree name, int protect)
+lookup_fnfields (tree xbasetype, tree name, int protect,
+                tsubst_flags_t complain)
 {
   tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
-                            tf_warning_or_error);
+                            complain);
 
   /* Ignore non-functions, but propagate the ambiguity list.  */
   if (!error_operand_p (rval)
index e16946829992985f7236ed69f1d36c08444b8fbb..b9b968a89a14655f3bf156137a9c9a621c560afd 100644 (file)
@@ -1,3 +1,8 @@
+2020-05-14  Patrick Palka  <ppalka@redhat.com>
+
+       PR c++/78446
+       * g++.dg/template/sfinae31.C: New test.
+
 2020-05-14  Uroš Bizjak  <ubizjak@gmail.com>
 
        PR target/95046
diff --git a/gcc/testsuite/g++.dg/template/sfinae31.C b/gcc/testsuite/g++.dg/template/sfinae31.C
new file mode 100644 (file)
index 0000000..b31a5ae
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/78446
+// { dg-do compile { target c++11 } }
+
+struct A { void operator()(); };
+struct B { void operator()(); };
+struct C : A, B {};
+
+template<class T>
+decltype(T()()) foo(int);
+
+template<class> int foo(...);
+
+using type = decltype(foo<C>(0));
+using type = int;