public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] c++: alias with same name as base fn [PR91706]
@ 2021-06-08  3:56 Jason Merrill
  2021-06-08  3:56 ` [pushed] c++: preserve BASELINK from lookup [PR91706] Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Jason Merrill @ 2021-06-08  3:56 UTC (permalink / raw)
  To: gcc-patches

This is a bit complex.  Looking up c<T> in the definition of D::c finds
C::c, OK.  Looking up c in the definition of E finds D::c, OK.  Since the
alias is not dependent, we strip it from the template argument, leaving

using E = A<decltype(c<T>())>;

where 'c' still refers to C::c.  But instantiating E looks up 'c' again and
finds D::c, which isn't a function, and sadness ensues.

I think the bug here is looking up 'c' in D at instantiation time; the
declaration we found before is not dependent.  This seems to happen because
baselink_for_fns gets BASELINK_BINFO wrong; it is supposed to be the base
where lookup found the functions, C in this case.

Tested x86_64-pc-linux-gnu, applying to trunk.

gcc/cp/ChangeLog:

	PR c++/91706
	* semantics.c (baselink_for_fns): Fix BASELINK_BINFO.

gcc/testsuite/ChangeLog:

	PR c++/91706
	* g++.dg/template/lookup17.C: New test.
---
 gcc/cp/semantics.c                       |  6 ++++--
 gcc/testsuite/g++.dg/template/lookup17.C | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/template/lookup17.C

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index d08c1ddabf9..f506a239864 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -3663,8 +3663,10 @@ baselink_for_fns (tree fns)
   cl = currently_open_derived_class (scope);
   if (!cl)
     cl = scope;
-  cl = TYPE_BINFO (cl);
-  return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
+  tree access_path = TYPE_BINFO (cl);
+  tree conv_path = (cl == scope ? access_path
+		    : lookup_base (cl, scope, ba_any, NULL, tf_none));
+  return build_baselink (conv_path, access_path, fns, /*optype=*/NULL_TREE);
 }
 
 /* Returns true iff DECL is a variable from a function outside
diff --git a/gcc/testsuite/g++.dg/template/lookup17.C b/gcc/testsuite/g++.dg/template/lookup17.C
new file mode 100644
index 00000000000..b8571b9f1eb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/lookup17.C
@@ -0,0 +1,18 @@
+// PR c++/91706
+// { dg-do compile { target c++11 } }
+// { dg-additional-options -g }
+
+template <bool> struct A;
+
+struct B { static constexpr bool g = false; };
+
+struct C {
+  template <typename> static B c ();
+};
+
+template <class T> struct D : C {
+  using c = decltype (c<T>());
+  using E = A<c::g>;
+};
+
+D<int> g;

base-commit: 715614ec3ec5390293e508bb190335d28db1fa8b
-- 
2.27.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [pushed] c++: preserve BASELINK from lookup [PR91706]
  2021-06-08  3:56 [pushed] c++: alias with same name as base fn [PR91706] Jason Merrill
@ 2021-06-08  3:56 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2021-06-08  3:56 UTC (permalink / raw)
  To: gcc-patches

In the earlier patch for PR91706 I fixed the BASELINK built by
baselink_for_fns, but since we already had one from lookup, we should keep
that one around instead of stripping it.  The removed hunk in
get_class_binding was a wierdly large amount of code to decide whether to
pull out BASELINK_FUNCTIONS.

Tested x86_64-pc-linux-gnu, applying to trunk.

gcc/cp/ChangeLog:

	PR c++/91706
	* name-lookup.c (get_class_binding): Keep a BASELINK.
	(set_inherited_value_binding_p): Adjust.
	* lambda.c (is_lambda_ignored_entity): Adjust.
	* pt.c (lookup_template_function): Copy a BASELINK before
	modifying it.
---
 gcc/cp/lambda.c      |  6 +++---
 gcc/cp/name-lookup.c | 24 +-----------------------
 gcc/cp/pt.c          |  1 +
 3 files changed, 5 insertions(+), 26 deletions(-)

diff --git a/gcc/cp/lambda.c b/gcc/cp/lambda.c
index 4a1e090ead4..2e9d38bbe83 100644
--- a/gcc/cp/lambda.c
+++ b/gcc/cp/lambda.c
@@ -1338,9 +1338,9 @@ is_lambda_ignored_entity (tree val)
 
   /* None of the lookups that use qualify_lookup want the op() from the
      lambda; they want the one from the enclosing class.  */
-  val = OVL_FIRST (val);
-  if (LAMBDA_FUNCTION_P (val))
-    return true;
+  if (tree fns = maybe_get_fns (val))
+    if (LAMBDA_FUNCTION_P (OVL_FIRST (fns)))
+      return true;
 
   return false;
 }
diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c
index 241ad2b9c32..1be5f3da6d5 100644
--- a/gcc/cp/name-lookup.c
+++ b/gcc/cp/name-lookup.c
@@ -5236,7 +5236,7 @@ set_inherited_value_binding_p (cxx_binding *binding, tree decl,
     {
       tree context;
 
-      if (TREE_CODE (decl) == OVERLOAD)
+      if (is_overloaded_fn (decl))
 	context = ovl_scope (decl);
       else
 	{
@@ -5338,28 +5338,6 @@ get_class_binding (tree name, cp_binding_level *scope)
 				 /*protect=*/2, /*want_type=*/false,
 				 tf_warning_or_error);
 
-  if (value_binding
-      && (TREE_CODE (value_binding) == TYPE_DECL
-	  || DECL_CLASS_TEMPLATE_P (value_binding)
-	  || (TREE_CODE (value_binding) == TREE_LIST
-	      && TREE_TYPE (value_binding) == error_mark_node
-	      && (TREE_CODE (TREE_VALUE (value_binding))
-		  == TYPE_DECL))))
-    /* We found a type binding, even when looking for a non-type
-       binding.  This means that we already processed this binding
-       above.  */
-    ;
-  else if (value_binding)
-    {
-      if (TREE_CODE (value_binding) == TREE_LIST
-	  && TREE_TYPE (value_binding) == error_mark_node)
-	/* NAME is ambiguous.  */
-	;
-      else if (BASELINK_P (value_binding))
-	/* NAME is some overloaded functions.  */
-	value_binding = BASELINK_FUNCTIONS (value_binding);
-    }
-
   /* If we found either a type binding or a value binding, create a
      new binding object.  */
   if (type_binding || value_binding)
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 2ae886d3a39..b0155a9c370 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -9597,6 +9597,7 @@ lookup_template_function (tree fns, tree arglist)
 
   if (BASELINK_P (fns))
     {
+      fns = copy_node (fns);
       BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
 					 unknown_type_node,
 					 BASELINK_FUNCTIONS (fns),

base-commit: 715614ec3ec5390293e508bb190335d28db1fa8b
prerequisite-patch-id: 91247507710e22bb04c3dffc0a3c418152b7dcd0
-- 
2.27.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-06-08  3:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-08  3:56 [pushed] c++: alias with same name as base fn [PR91706] Jason Merrill
2021-06-08  3:56 ` [pushed] c++: preserve BASELINK from lookup [PR91706] Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).