public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++: Diagnose a deduction guide in a wrong scope [PR91759]
Date: Tue, 17 Mar 2020 22:06:15 +0100	[thread overview]
Message-ID: <20200317210615.GK2156@tucnak> (raw)
In-Reply-To: <10abd205-1301-d32f-ad47-8d2c6b5d8d34@redhat.com>

On Tue, Mar 17, 2020 at 03:54:57PM -0400, Jason Merrill via Gcc-patches wrote:
> How about always diagnosing this here, and moving the deduction guide code
> in grokfndecl up above set_decl_namespace to avoid a duplicate diagnostic?

I've tried that as:
--- gcc/cp/decl.c.jj	2020-03-16 22:56:46.787172869 +0100
+++ gcc/cp/decl.c	2020-03-17 21:29:17.812732717 +0100
@@ -9506,6 +9506,27 @@ grokfndecl (tree ctype,
       inlinep &= ~8;
     }
 
+  if (deduction_guide_p (decl))
+    {
+      if (!DECL_NAMESPACE_SCOPE_P (decl))
+	{
+	  error_at (location, "deduction guide %qD must be declared at "
+		    "namespace scope", decl);
+	  return NULL_TREE;
+	}
+      tree type = TREE_TYPE (DECL_NAME (decl));
+      if (CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
+	{
+	  error_at (location, "deduction guide %qD must be declared in the "
+			      "same scope as %qT", decl, type);
+	  inform (location_of (type), "  declared here");
+	  return NULL_TREE;
+	}
+      if (funcdef_flag)
+	error_at (location,
+		  "deduction guide %qD must not have a function body", decl);
+    }
+
   /* If this decl has namespace scope, set that up.  */
   if (in_namespace)
     set_decl_namespace (decl, in_namespace, friendp);
@@ -9636,20 +9657,8 @@ grokfndecl (tree ctype,
 	}
     }
 
-  if (deduction_guide_p (decl))
-    {
-      if (!DECL_NAMESPACE_SCOPE_P (decl))
-	{
-	  error_at (location, "deduction guide %qD must be declared at "
-		    "namespace scope", decl);
-	  return NULL_TREE;
-	}
-      if (funcdef_flag)
-	error_at (location,
-		  "deduction guide %qD must not have a function body", decl);
-    }
-  else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
-	   && !grok_op_properties (decl, /*complain=*/true))
+  if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
+      && !grok_op_properties (decl, /*complain=*/true))
     return NULL_TREE;
   else if (UDLIT_OPER_P (DECL_NAME (decl)))
     {
--- gcc/testsuite/g++.dg/cpp1z/class-deduction9.C.jj	2020-01-12 11:54:37.126402652 +0100
+++ gcc/testsuite/g++.dg/cpp1z/class-deduction9.C	2020-03-17 21:29:53.842207083 +0100
@@ -10,7 +10,7 @@ namespace N {
 }
 
 template <class T>
-N::A(T) -> N::A<T>;	  // { dg-error "should have been declared inside .N" }
+N::A(T) -> N::A<T>;	  // { dg-error "must be declared in the same scope as" }
 
 namespace N {
   template <class T>
--- gcc/testsuite/g++.dg/cpp1z/class-deduction72.C.jj	2020-03-17 21:26:00.566610331 +0100
+++ gcc/testsuite/g++.dg/cpp1z/class-deduction72.C	2020-03-17 21:26:00.566610331 +0100
@@ -0,0 +1,11 @@
+// PR c++/91759
+// { dg-do compile { target c++17 } }
+
+namespace N {
+  template <typename T>
+  struct X{ X(int); };	// { dg-message "declared here" }
+}
+
+using N::X;
+
+X(int) -> X<int>;	// { dg-error "must be declared in the same scope as" }

but that fails with
Excess errors:
/usr/src/gcc/obj/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_pair.h:459:40: error: deduction guide 'pair(_T1, _T2)-> std::pair<_T1, _T2>' must be declared in the same scope as 'std::pair<_T1, _T2>'
/usr/src/gcc/obj/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_vector.h:1873:5: error: deduction guide 'vector(_InputIterator, _InputIterator, _Allocator)-> std::vector<_ValT, _Allocator>' must be declared in the same scope as 'std::vector<_Tp, _Alloc>'
on various testcases.

I've tried next:
--- gcc/cp/decl.c.jj	2020-03-16 22:56:46.787172869 +0100
+++ gcc/cp/decl.c	2020-03-17 22:00:13.897648589 +0100
@@ -9506,6 +9506,36 @@ grokfndecl (tree ctype,
       inlinep &= ~8;
     }
 
+  if (deduction_guide_p (decl))
+    {
+      if (!DECL_NAMESPACE_SCOPE_P (decl))
+	{
+	  error_at (location, "deduction guide %qD must be declared at "
+		    "namespace scope", decl);
+	  return NULL_TREE;
+	}
+      tree type = TREE_TYPE (DECL_NAME (decl));
+      tree ctx = NULL_TREE;
+      if (in_namespace)
+	ctx = ORIGINAL_NAMESPACE (in_namespace);
+      else if (!ctype)
+	ctx = current_decl_namespace ();
+      if (ctx)
+	ctx = FROB_CONTEXT (ctx);
+      if (SCOPE_FILE_SCOPE_P (ctx))
+	ctx = global_namespace;
+      if (CP_TYPE_CONTEXT (type) != ctx)
+	{
+	  error_at (location, "deduction guide %qD must be declared in the "
+			      "same scope as %qT", decl, type);
+	  inform (location_of (type), "  declared here");
+	  return NULL_TREE;
+	}
+      if (funcdef_flag)
+	error_at (location,
+		  "deduction guide %qD must not have a function body", decl);
+    }
+
   /* If this decl has namespace scope, set that up.  */
   if (in_namespace)
     set_decl_namespace (decl, in_namespace, friendp);
@@ -9636,20 +9666,8 @@ grokfndecl (tree ctype,
 	}
     }
 
-  if (deduction_guide_p (decl))
-    {
-      if (!DECL_NAMESPACE_SCOPE_P (decl))
-	{
-	  error_at (location, "deduction guide %qD must be declared at "
-		    "namespace scope", decl);
-	  return NULL_TREE;
-	}
-      if (funcdef_flag)
-	error_at (location,
-		  "deduction guide %qD must not have a function body", decl);
-    }
-  else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
-	   && !grok_op_properties (decl, /*complain=*/true))
+  if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
+      && !grok_op_properties (decl, /*complain=*/true))
     return NULL_TREE;
   else if (UDLIT_OPER_P (DECL_NAME (decl)))
     {
--- gcc/testsuite/g++.dg/cpp1z/class-deduction72.C.jj	2020-03-17 21:26:00.566610331 +0100
+++ gcc/testsuite/g++.dg/cpp1z/class-deduction72.C	2020-03-17 21:26:00.566610331 +0100
@@ -0,0 +1,11 @@
+// PR c++/91759
+// { dg-do compile { target c++17 } }
+
+namespace N {
+  template <typename T>
+  struct X{ X(int); };	// { dg-message "declared here" }
+}
+
+using N::X;
+
+X(int) -> X<int>;	// { dg-error "must be declared in the same scope as" }
--- gcc/testsuite/g++.dg/cpp1z/class-deduction9.C.jj	2020-01-12 11:54:37.126402652 +0100
+++ gcc/testsuite/g++.dg/cpp1z/class-deduction9.C	2020-03-17 21:29:53.842207083 +0100
@@ -10,7 +10,7 @@ namespace N {
 }
 
 template <class T>
-N::A(T) -> N::A<T>;	  // { dg-error "should have been declared inside .N" }
+N::A(T) -> N::A<T>;	  // { dg-error "must be declared in the same scope as" }
 
 namespace N {
   template <class T>

but that FAILs the class-deduction9.C test with the adjustment,
reports the older "should have been"... error instead, because the computed
ctx is the N namespace, which is also what set_decl_namespace sets, but then
it fails some lookup and emits this message.

	Jakub


  reply	other threads:[~2020-03-17 21:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-17  8:49 Jakub Jelinek
2020-03-17 19:54 ` Jason Merrill
2020-03-17 21:06   ` Jakub Jelinek [this message]
2020-03-17 21:36     ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200317210615.GK2156@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).