[PATCH] c++: Fix resolving the address of overloaded pmf [PR96647]

Patrick Palka ppalka@redhat.com
Tue Sep 8 13:17:11 GMT 2020


On Mon, 31 Aug 2020, Jason Merrill wrote:

> On 8/28/20 12:45 PM, Patrick Palka wrote:
> > (Removing libstdc++@gcc.gnu.org from CC list)
> > 
> > On Fri, 28 Aug 2020, Patrick Palka wrote:
> > > In resolve_address_of_overloaded_function, currently only the second
> > > pass over the overload set (which considers just the function templates
> > > in the overload set) checks constraints and performs return type
> > > deduction when necessary.  But as the testcases below show, we need to
> > > do this when considering non-template functions during the first pass,
> > > too.
> > > 
> > > Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
> > > 
> > > gcc/cp/ChangeLog:
> > > 
> > > 	PR c++/96647
> > > 	* class.c (resolve_address_of_overloaded_function): Also check
> > > 	constraints and perform return type deduction when considering
> > > 	non-template functions in the overload set.
> > > 
> > > gcc/testsuite/ChangeLog:
> > > 
> > > 	PR c++/96647
> > > 	* g++.dg/cpp0x/auto-96647.C: New test.
> > > 	* g++.dg/cpp2a/concepts-fn6.C: New test.
> > > ---
> > >   gcc/cp/class.c                            | 16 ++++++++++++++++
> > >   gcc/testsuite/g++.dg/cpp0x/auto-96647.C   | 10 ++++++++++
> > >   gcc/testsuite/g++.dg/cpp2a/concepts-fn6.C | 10 ++++++++++
> > >   3 files changed, 36 insertions(+)
> > >   create mode 100644 gcc/testsuite/g++.dg/cpp0x/auto-96647.C
> > >   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-fn6.C
> 
> > > +	if (undeduced_auto_decl (fn))
> > > +	  {
> > > +	    /* Force instantiation to do return type deduction.  */
> > > +	    ++function_depth;
> > > +	    instantiate_decl (fn, /*defer*/false, /*class*/false);
> > > +	    --function_depth;
> 
> How about maybe_instantiate_decl instead of this hunk?  This looks like it
> could call instantiate_decl for a non-template function, which is wrong.

Good point.  We even ICE on the testcase error9.C below when using
instantiate_decl here, since we indeed end up calling it for the
non-specialization f(bool).

Does the following look OK?

-- >8 --

Subject: [PATCH] c++: Fix resolving the address of overloaded pmf [PR96647]

In resolve_address_of_overloaded_function, currently only the second
pass over the overload set (which considers just the function templates
in the overload set) checks constraints and performs return type
deduction when necessary.  But as the testcases below show, we need to
do the same when considering non-template functions during the first
pass.

gcc/cp/ChangeLog:

	PR c++/96647
	* class.c (resolve_address_of_overloaded_function): Check
	constraints_satisfied_p and perform return-type deduction via
	maybe_instantiate_decl when considering non-template functions
	in the overload set.
	* cp-tree.h (maybe_instantiate_decl): Declare.
	* decl2.c (maybe_instantiate_decl): Remove static.

gcc/testsuite/ChangeLog:

	PR c++/96647
	* g++.dg/cpp0x/auto-96647.C: New test.
	* g++.dg/cpp0x/error9: New test.
	* g++.dg/cpp2a/concepts-fn6.C: New test.
---
 gcc/cp/class.c                            | 13 +++++++++++++
 gcc/cp/cp-tree.h                          |  1 +
 gcc/cp/decl2.c                            |  3 +--
 gcc/testsuite/g++.dg/cpp0x/auto-96647.C   | 10 ++++++++++
 gcc/testsuite/g++.dg/cpp0x/error9.C       |  6 ++++++
 gcc/testsuite/g++.dg/cpp2a/concepts-fn6.C | 10 ++++++++++
 6 files changed, 41 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/auto-96647.C
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/error9.C
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-fn6.C

diff --git a/gcc/cp/class.c b/gcc/cp/class.c
index 3479b8207d2..c9a1f753d56 100644
--- a/gcc/cp/class.c
+++ b/gcc/cp/class.c
@@ -8286,6 +8286,19 @@ resolve_address_of_overloaded_function (tree target_type,
 	     one, or vice versa.  */
 	  continue;
 
+	/* Constraints must be satisfied. This is done before
+	   return type deduction since that instantiates the
+	   function. */
+	if (!constraints_satisfied_p (fn))
+	  continue;
+
+	if (undeduced_auto_decl (fn))
+	  {
+	    /* Force instantiation to do return type deduction.  */
+	    maybe_instantiate_decl (fn);
+	    require_deduced_type (fn);
+	  }
+
 	/* In C++17 we need the noexcept-qualifier to compare types.  */
 	if (flag_noexcept_type
 	    && !maybe_instantiate_noexcept (fn, complain))
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 708de83eb46..78739411755 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -6905,6 +6905,7 @@ extern void do_type_instantiation		(tree, tree, tsubst_flags_t);
 extern bool always_instantiate_p		(tree);
 extern bool maybe_instantiate_noexcept		(tree, tsubst_flags_t = tf_warning_or_error);
 extern tree instantiate_decl			(tree, bool, bool);
+extern void maybe_instantiate_decl		(tree);
 extern int comp_template_parms			(const_tree, const_tree);
 extern bool template_heads_equivalent_p		(const_tree, const_tree);
 extern bool builtin_pack_fn_p			(tree);
diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c
index 33c83773d33..50a042e8070 100644
--- a/gcc/cp/decl2.c
+++ b/gcc/cp/decl2.c
@@ -81,7 +81,6 @@ static void import_export_class (tree);
 static tree get_guard_bits (tree);
 static void determine_visibility_from_class (tree, tree);
 static bool determine_hidden_inline (tree);
-static void maybe_instantiate_decl (tree);
 
 /* A list of static class variables.  This is needed, because a
    static class variable can be declared inside the class without
@@ -5386,7 +5385,7 @@ possibly_inlined_p (tree decl)
    them instantiated for reduction clauses which inline them by hand
    directly.  */
 
-static void
+void
 maybe_instantiate_decl (tree decl)
 {
   if (DECL_LANG_SPECIFIC (decl)
diff --git a/gcc/testsuite/g++.dg/cpp0x/auto-96647.C b/gcc/testsuite/g++.dg/cpp0x/auto-96647.C
new file mode 100644
index 00000000000..314b2a16ac2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/auto-96647.C
@@ -0,0 +1,10 @@
+// PR c++/96647
+// { dg-do compile { target c++11 } }
+
+template<typename>
+struct Base {
+  auto f(int) { }
+  auto f(char) { }
+};
+
+void (Base<void>::*ptr)(int) = &Base<void>::f;
diff --git a/gcc/testsuite/g++.dg/cpp0x/error9.C b/gcc/testsuite/g++.dg/cpp0x/error9.C
new file mode 100644
index 00000000000..b712c7d849a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/error9.C
@@ -0,0 +1,6 @@
+// { dg-do compile { target c++11 } }
+
+void f(int) { }
+auto f(bool) { return f(true); } // { dg-error "auto" }
+
+void (*ptr)(int) = &f;
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-fn6.C b/gcc/testsuite/g++.dg/cpp2a/concepts-fn6.C
new file mode 100644
index 00000000000..3d7941658d4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-fn6.C
@@ -0,0 +1,10 @@
+// PR c++/96647
+// { dg-do compile { target c++20 } }
+
+template<typename T>
+struct Base {
+  auto f(int) { }
+  auto f(int) requires T::fail { static_assert(T::fail); }
+};
+
+void (Base<void>::*ptr)(int) = &Base<void>::f;
-- 
2.28.0.450.g3a238e539b



More information about the Gcc-patches mailing list