Re: [PATCH] c++: bogus warning w/ deduction guide in anon ns [PR106604]

2023-08-11 Thread Jason Merrill via Gcc-patches

On 8/11/23 09:54, Patrick Palka wrote:

On Thu, 10 Aug 2023, Jason Merrill wrote:


On 8/10/23 16:40, Patrick Palka wrote:

On Thu, 10 Aug 2023, Jason Merrill wrote:


On 8/10/23 12:09, Patrick Palka wrote:

Booststrapped and regtested on x86_64-pc-linux-gnu, does this look OK
for
trunk and perhaps 13?

-- >8 --

We shouldn't issue a "declared static but never defined" warning
for a deduction guide (declared in an anonymous namespace).

PR c++/106604

gcc/cp/ChangeLog:

* decl.cc (wrapup_namespace_globals): Don't issue a
-Wunused-function warning for a deduction guide.


Maybe instead of special casing this here we could set DECL_INITIAL on
deduction guides so they look defined?


That seems to work, but it requires some tweaks in duplicate_decls to keep
saying "declared" instead of "defined" when diagnosing a deduction guide
redeclaration.  I'm not sure which approach is preferable?


I'm not sure it matters which we say; the restriction that you can't repeat a
deduction guide makes it more like a definition anyway (even if [basic.def]
disagrees).  Is the diagnostic worse apart from that word?


Ah, makes sense.  So we can also remove the special case for them in the
redeclaration checking code after we give them a dummy DECL_INITIAL.
Like so?


OK, thanks.


Here's a before/after for the diagnostic with the below patch:

Before

src/gcc/testsuite/g++.dg/cpp1z/class-deduction74.C:11:1: error: deduction guide 
‘S()-> S’ redeclared
11 | S() -> S; // { dg-error "redefinition" }
   | ^
src/gcc/testsuite/g++.dg/cpp1z/class-deduction74.C:10:1: note: ‘S()-> S’ 
previously declared here
10 | S() -> S; // { dg-message "previously defined here|old 
declaration" }
   | ^

After

src/gcc/testsuite/g++.dg/cpp1z/class-deduction74.C:11:1: error: redefinition of 
‘S()-> S’
11 | S() -> S; // { dg-error "redefinition" }
   | ^
src/gcc/testsuite/g++.dg/cpp1z/class-deduction74.C:10:1: note: ‘S()-> S’ 
previously defined here
    10 | S() -> S; // { dg-message "previously defined here|old 
declaration" }
   | ^

-- >8 --

Subject: [PATCH] c++: bogus warning w/ deduction guide in anon ns [PR106604]

Here we're unintentionally issuing a "declared static but never defined"
warning for a deduction guide declared in an anonymous namespace.
This patch fixes this by giving deduction guides a dummy DECL_INITIAL,
which suppresses the warning and also allows us to simplify redeclaration
checking for them.

Co-authored-by: Jason Merrill 

PR c++/106604

gcc/cp/ChangeLog:

* decl.cc (redeclaration_error_message): Remove special handling
for deduction guides.
(grokfndecl): Give deduction guides a dummy DECL_INITIAL.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/class-deduction74.C: Expect "defined" instead
of "declared" in diagnostics for a repeated deduction guide.
* g++.dg/cpp1z/class-deduction116.C: New test.
---
  gcc/cp/decl.cc  | 14 ++
  gcc/testsuite/g++.dg/cpp1z/class-deduction116.C |  8 
  gcc/testsuite/g++.dg/cpp1z/class-deduction74.C  | 14 +++---
  3 files changed, 21 insertions(+), 15 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction116.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 792ab330dd0..3ada5516c58 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -3297,10 +3297,6 @@ redeclaration_error_message (tree newdecl, tree olddecl)
}
}
  
-  if (deduction_guide_p (olddecl)

- && deduction_guide_p (newdecl))
-   return G_("deduction guide %q+D redeclared");
-
/* [class.compare.default]: A definition of a comparison operator as
 defaulted that appears in a class shall be the first declaration of
 that function.  */
@@ -3355,10 +3351,6 @@ redeclaration_error_message (tree newdecl, tree olddecl)
}
}
  
-  if (deduction_guide_p (olddecl)

- && deduction_guide_p (newdecl))
-   return G_("deduction guide %q+D redeclared");
-
/* Core issue #226 (C++11):
  
 If a friend function template declaration specifies a

@@ -10352,6 +10344,12 @@ grokfndecl (tree ctype,
DECL_CXX_DESTRUCTOR_P (decl) = 1;
DECL_NAME (decl) = dtor_identifier;
break;
+case sfk_deduction_guide:
+  /* Give deduction guides a definition even though they don't really
+have one: the restriction that you can't repeat a deduction guide
+makes them more like a definition anyway.  */
+  DECL_INITIAL (decl) = void_node;
+  break;
  default:
break;
  }
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction116.C 
b/gcc/testsuite/g++.dg/cpp1z/class-deduction116.C
new file mode 10064

Re: [PATCH] c++: bogus warning w/ deduction guide in anon ns [PR106604]

2023-08-11 Thread Patrick Palka via Gcc-patches
On Thu, 10 Aug 2023, Jason Merrill wrote:

> On 8/10/23 16:40, Patrick Palka wrote:
> > On Thu, 10 Aug 2023, Jason Merrill wrote:
> > 
> > > On 8/10/23 12:09, Patrick Palka wrote:
> > > > Booststrapped and regtested on x86_64-pc-linux-gnu, does this look OK
> > > > for
> > > > trunk and perhaps 13?
> > > > 
> > > > -- >8 --
> > > > 
> > > > We shouldn't issue a "declared static but never defined" warning
> > > > for a deduction guide (declared in an anonymous namespace).
> > > > 
> > > > PR c++/106604
> > > > 
> > > > gcc/cp/ChangeLog:
> > > > 
> > > > * decl.cc (wrapup_namespace_globals): Don't issue a
> > > > -Wunused-function warning for a deduction guide.
> > > 
> > > Maybe instead of special casing this here we could set DECL_INITIAL on
> > > deduction guides so they look defined?
> > 
> > That seems to work, but it requires some tweaks in duplicate_decls to keep
> > saying "declared" instead of "defined" when diagnosing a deduction guide
> > redeclaration.  I'm not sure which approach is preferable?
> 
> I'm not sure it matters which we say; the restriction that you can't repeat a
> deduction guide makes it more like a definition anyway (even if [basic.def]
> disagrees).  Is the diagnostic worse apart from that word?

Ah, makes sense.  So we can also remove the special case for them in the
redeclaration checking code after we give them a dummy DECL_INITIAL.
Like so?

Here's a before/after for the diagnostic with the below patch:

Before

src/gcc/testsuite/g++.dg/cpp1z/class-deduction74.C:11:1: error: deduction guide 
‘S()-> S’ redeclared
   11 | S() -> S; // { dg-error "redefinition" }
  | ^
src/gcc/testsuite/g++.dg/cpp1z/class-deduction74.C:10:1: note: ‘S()-> S’ 
previously declared here
   10 | S() -> S; // { dg-message "previously defined here|old 
declaration" }
  | ^

After

src/gcc/testsuite/g++.dg/cpp1z/class-deduction74.C:11:1: error: redefinition of 
‘S()-> S’
   11 | S() -> S; // { dg-error "redefinition" }
  | ^
src/gcc/testsuite/g++.dg/cpp1z/class-deduction74.C:10:1: note: ‘S()-> S’ 
previously defined here
   10 | S() -> S; // { dg-message "previously defined here|old 
declaration" }
  | ^

-- >8 --

Subject: [PATCH] c++: bogus warning w/ deduction guide in anon ns [PR106604]

Here we're unintentionally issuing a "declared static but never defined"
warning for a deduction guide declared in an anonymous namespace.
This patch fixes this by giving deduction guides a dummy DECL_INITIAL,
which suppresses the warning and also allows us to simplify redeclaration
checking for them.

Co-authored-by: Jason Merrill 

PR c++/106604

gcc/cp/ChangeLog:

* decl.cc (redeclaration_error_message): Remove special handling
for deduction guides.
(grokfndecl): Give deduction guides a dummy DECL_INITIAL.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/class-deduction74.C: Expect "defined" instead
of "declared" in diagnostics for a repeated deduction guide.
* g++.dg/cpp1z/class-deduction116.C: New test.
---
 gcc/cp/decl.cc  | 14 ++
 gcc/testsuite/g++.dg/cpp1z/class-deduction116.C |  8 
 gcc/testsuite/g++.dg/cpp1z/class-deduction74.C  | 14 +++---
 3 files changed, 21 insertions(+), 15 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction116.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 792ab330dd0..3ada5516c58 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -3297,10 +3297,6 @@ redeclaration_error_message (tree newdecl, tree olddecl)
}
}
 
-  if (deduction_guide_p (olddecl)
- && deduction_guide_p (newdecl))
-   return G_("deduction guide %q+D redeclared");
-
   /* [class.compare.default]: A definition of a comparison operator as
 defaulted that appears in a class shall be the first declaration of
 that function.  */
@@ -3355,10 +3351,6 @@ redeclaration_error_message (tree newdecl, tree olddecl)
}
}
 
-  if (deduction_guide_p (olddecl)
- && deduction_guide_p (newdecl))
-   return G_("deduction guide %q+D redeclared");
-
   /* Core issue #226 (C++11):
 
If a friend function template declaration specifies a
@@ -10352,6 +10344,12 @@ grokfndecl (tree ctype,
   DECL_CXX_DESTRUCTOR_P (decl) = 1;
   DECL_NAME (decl) = dtor_identifier;
   break;
+case sfk_deduction_guide:
+  /* Give deduction guides a definition even though they don't really
+ 

Re: [PATCH] c++: bogus warning w/ deduction guide in anon ns [PR106604]

2023-08-10 Thread Jason Merrill via Gcc-patches

On 8/10/23 16:40, Patrick Palka wrote:

On Thu, 10 Aug 2023, Jason Merrill wrote:


On 8/10/23 12:09, Patrick Palka wrote:

Booststrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk and perhaps 13?

-- >8 --

We shouldn't issue a "declared static but never defined" warning
for a deduction guide (declared in an anonymous namespace).

PR c++/106604

gcc/cp/ChangeLog:

* decl.cc (wrapup_namespace_globals): Don't issue a
-Wunused-function warning for a deduction guide.


Maybe instead of special casing this here we could set DECL_INITIAL on
deduction guides so they look defined?


That seems to work, but it requires some tweaks in duplicate_decls to keep
saying "declared" instead of "defined" when diagnosing a deduction guide
redeclaration.  I'm not sure which approach is preferable?


I'm not sure it matters which we say; the restriction that you can't 
repeat a deduction guide makes it more like a definition anyway (even if 
[basic.def] disagrees).  Is the diagnostic worse apart from that word?


Jason



Re: [PATCH] c++: bogus warning w/ deduction guide in anon ns [PR106604]

2023-08-10 Thread Patrick Palka via Gcc-patches
On Thu, 10 Aug 2023, Jason Merrill wrote:

> On 8/10/23 12:09, Patrick Palka wrote:
> > Booststrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> > trunk and perhaps 13?
> > 
> > -- >8 --
> > 
> > We shouldn't issue a "declared static but never defined" warning
> > for a deduction guide (declared in an anonymous namespace).
> > 
> > PR c++/106604
> > 
> > gcc/cp/ChangeLog:
> > 
> > * decl.cc (wrapup_namespace_globals): Don't issue a
> > -Wunused-function warning for a deduction guide.
> 
> Maybe instead of special casing this here we could set DECL_INITIAL on
> deduction guides so they look defined?

That seems to work, but it requires some tweaks in duplicate_decls to keep
saying "declared" instead of "defined" when diagnosing a deduction guide
redeclaration.  I'm not sure which approach is preferable?

-- >8 --

gcc/cp/ChangeLog:

* decl.cc (duplicate_decls): When diagnosing a redeclared
deduction guide, ensure we say "declared" instead of "defined".
(redeclaration_error_message): Move up deduction guide tests.
(grokfndecl): Set DECL_INITIAL to void_node for a deduction
guide.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/class-deduction116.C: New test.
---
 gcc/cp/decl.cc| 23 +++
 .../g++.dg/cpp1z/class-deduction116.C |  5 
 2 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 2b3fb313166..70dcff7aa8c 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -2025,7 +2025,9 @@ duplicate_decls (tree newdecl, tree olddecl, bool hiding, 
bool was_hidden)
  error_at (newdecl_loc, errmsg, newdecl);
  if (DECL_NAME (olddecl) != NULL_TREE)
inform (olddecl_loc,
-   (DECL_INITIAL (olddecl) && namespace_bindings_p ())
+   (DECL_INITIAL (olddecl)
+&& !deduction_guide_p (olddecl)
+&& namespace_bindings_p ())
? G_("%q#D previously defined here")
: G_("%q#D previously declared here"), olddecl);
  return error_mark_node;
@@ -3271,6 +3273,10 @@ redeclaration_error_message (tree newdecl, tree olddecl)
   /* We'll complain about linkage mismatches in
 warn_extern_redeclared_static.  */
 
+  if (deduction_guide_p (olddecl)
+ && deduction_guide_p (newdecl))
+   return G_("deduction guide %q+D redeclared");
+
   /* Defining the same name twice is no good.  */
   if (decl_defined_p (olddecl)
  && decl_defined_p (newdecl))
@@ -3298,10 +3304,6 @@ redeclaration_error_message (tree newdecl, tree olddecl)
}
}
 
-  if (deduction_guide_p (olddecl)
- && deduction_guide_p (newdecl))
-   return G_("deduction guide %q+D redeclared");
-
   /* [class.compare.default]: A definition of a comparison operator as
 defaulted that appears in a class shall be the first declaration of
 that function.  */
@@ -3330,6 +3332,10 @@ redeclaration_error_message (tree newdecl, tree olddecl)
   if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
return NULL;
 
+  if (deduction_guide_p (olddecl)
+ && deduction_guide_p (newdecl))
+   return G_("deduction guide %q+D redeclared");
+
   nt = DECL_TEMPLATE_RESULT (newdecl);
   if (DECL_TEMPLATE_INFO (nt))
nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
@@ -3356,10 +3362,6 @@ redeclaration_error_message (tree newdecl, tree olddecl)
}
}
 
-  if (deduction_guide_p (olddecl)
- && deduction_guide_p (newdecl))
-   return G_("deduction guide %q+D redeclared");
-
   /* Core issue #226 (C++11):
 
If a friend function template declaration specifies a
@@ -10353,6 +10355,9 @@ grokfndecl (tree ctype,
   DECL_CXX_DESTRUCTOR_P (decl) = 1;
   DECL_NAME (decl) = dtor_identifier;
   break;
+case sfk_deduction_guide:
+  DECL_INITIAL (decl) = void_node;
+  break;
 default:
   break;
 }
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction116.C 
b/gcc/testsuite/g++.dg/cpp1z/class-deduction116.C
new file mode 100644
index 000..00f6d5fef41
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction116.C
@@ -0,0 +1,8 @@
+// PR c++/106604
+// { dg-do compile { target c++17 } }
+// { dg-additional-options "-Wunused-function" }
+
+namespace {
+  template struct A { A(...); };
+  A(bool) -> A; // { dg-bogus "never defined" }
+}
-- 
2.42.0.rc1


> 
> > gcc/testsuite/ChangeLog:
> > 
> > * g++.dg/cpp1z/class-deduction116.C: New test.
> > ---
> >   gcc/cp/decl.cc  | 1 +
> >   gcc/testsuite/g++.dg/cpp1z/class-deduction116.C | 8 
> >   2 files changed, 9 insertions(+)
> >   create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction116.C
> > 
> > diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
> > index 792ab330dd

Re: [PATCH] c++: bogus warning w/ deduction guide in anon ns [PR106604]

2023-08-10 Thread Jason Merrill via Gcc-patches

On 8/10/23 12:09, Patrick Palka wrote:

Booststrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk and perhaps 13?

-- >8 --

We shouldn't issue a "declared static but never defined" warning
for a deduction guide (declared in an anonymous namespace).

PR c++/106604

gcc/cp/ChangeLog:

* decl.cc (wrapup_namespace_globals): Don't issue a
-Wunused-function warning for a deduction guide.


Maybe instead of special casing this here we could set DECL_INITIAL on 
deduction guides so they look defined?



gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/class-deduction116.C: New test.
---
  gcc/cp/decl.cc  | 1 +
  gcc/testsuite/g++.dg/cpp1z/class-deduction116.C | 8 
  2 files changed, 9 insertions(+)
  create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction116.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 792ab330dd0..9fe3a0b98fd 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -856,6 +856,7 @@ wrapup_namespace_globals ()
  && !TREE_PUBLIC (decl)
  && !DECL_ARTIFICIAL (decl)
  && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
+ && !deduction_guide_p (decl)
  && !warning_suppressed_p (decl, OPT_Wunused_function))
warning_at (DECL_SOURCE_LOCATION (decl),
OPT_Wunused_function,
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction116.C 
b/gcc/testsuite/g++.dg/cpp1z/class-deduction116.C
new file mode 100644
index 000..00f6d5fef41
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction116.C
@@ -0,0 +1,8 @@
+// PR c++/106604
+// { dg-do compile { target c++17 } }
+// { dg-additional-options "-Wunused-function" }
+
+namespace {
+  template struct A { A(...); };
+  A(bool) -> A; // { dg-bogus "never defined" }
+}




[PATCH] c++: bogus warning w/ deduction guide in anon ns [PR106604]

2023-08-10 Thread Patrick Palka via Gcc-patches
Booststrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk and perhaps 13?

-- >8 --

We shouldn't issue a "declared static but never defined" warning
for a deduction guide (declared in an anonymous namespace).

PR c++/106604

gcc/cp/ChangeLog:

* decl.cc (wrapup_namespace_globals): Don't issue a
-Wunused-function warning for a deduction guide.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/class-deduction116.C: New test.
---
 gcc/cp/decl.cc  | 1 +
 gcc/testsuite/g++.dg/cpp1z/class-deduction116.C | 8 
 2 files changed, 9 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction116.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 792ab330dd0..9fe3a0b98fd 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -856,6 +856,7 @@ wrapup_namespace_globals ()
  && !TREE_PUBLIC (decl)
  && !DECL_ARTIFICIAL (decl)
  && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
+ && !deduction_guide_p (decl)
  && !warning_suppressed_p (decl, OPT_Wunused_function))
warning_at (DECL_SOURCE_LOCATION (decl),
OPT_Wunused_function,
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction116.C 
b/gcc/testsuite/g++.dg/cpp1z/class-deduction116.C
new file mode 100644
index 000..00f6d5fef41
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction116.C
@@ -0,0 +1,8 @@
+// PR c++/106604
+// { dg-do compile { target c++17 } }
+// { dg-additional-options "-Wunused-function" }
+
+namespace {
+  template struct A { A(...); };
+  A(bool) -> A; // { dg-bogus "never defined" }
+}
-- 
2.42.0.rc0.25.ga82fb66fed