public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Marek Polacek <mpolacek@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-4077] c++: Disable -Wignored-qualifiers for template args [PR107492]
Date: Tue, 15 Nov 2022 22:44:51 +0000 (GMT)	[thread overview]
Message-ID: <20221115224451.3F1073945C11@sourceware.org> (raw)

https://gcc.gnu.org/g:ed1797ddf8285f59a50d9c883beb97705279d980

commit r13-4077-ged1797ddf8285f59a50d9c883beb97705279d980
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Nov 1 11:49:03 2022 -0400

    c++: Disable -Wignored-qualifiers for template args [PR107492]
    
    It seems wrong to issue a -Wignored-qualifiers warning for code like:
    
      static_assert(!is_same_v<void(*)(), const void(*)()>);
    
    because there the qualifier matters.  Likewise in template
    specialization:
    
      template<typename T> struct S { };
      template<> struct S<void(*)()> { };
      template<> struct S<const void(*)()> { }; // OK, not a redefinition
    
    And likewise in other type-id contexts such as trailing-return-type:
    
      auto g() -> const void (*)();
    
    This patch limits the warning to the function declaration context only.
    
            PR c++/107492
    
    gcc/cp/ChangeLog:
    
            * decl.cc (grokdeclarator): Only emit a -Wignored-qualifiers warning
            when funcdecl_p.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/warn/Wignored-qualifiers3.C: New test.

Diff:
---
 gcc/cp/decl.cc                                   |  6 +++++-
 gcc/testsuite/g++.dg/warn/Wignored-qualifiers3.C | 24 ++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index c83fdac4984..0234919ea57 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -13038,7 +13038,11 @@ grokdeclarator (const cp_declarator *declarator,
 
 	    if (type_quals != TYPE_UNQUALIFIED)
 	      {
-		if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
+		/* It's wrong, for instance, to issue a -Wignored-qualifiers
+		   warning for
+		    static_assert(!is_same_v<void(*)(), const void(*)()>);
+		    because there the qualifier matters.  */
+		if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
 		  warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
 			      "qualifiers ignored on function return type");
 		/* [dcl.fct] "A volatile-qualified return type is
diff --git a/gcc/testsuite/g++.dg/warn/Wignored-qualifiers3.C b/gcc/testsuite/g++.dg/warn/Wignored-qualifiers3.C
new file mode 100644
index 00000000000..dedb38fc995
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wignored-qualifiers3.C
@@ -0,0 +1,24 @@
+// PR c++/107492
+// { dg-do compile { target c++14 } }
+// { dg-additional-options "-Wignored-qualifiers" }
+
+// Here the 'const' matters, so don't warn.
+template<typename T> struct S { };
+template<> struct S<void(*)()> { };
+template<> struct S<const void(*)()> { }; // { dg-bogus "ignored" }
+
+template<typename T, typename U> constexpr bool is_same_v = false;
+template<typename T> constexpr bool is_same_v<T, T> = true;
+
+static_assert( ! is_same_v< void(*)(), const void(*)() >, ""); // { dg-bogus "ignored" }
+
+// Here the 'const' matters as well -> don't warn.
+auto g() -> const void (*)(); // { dg-bogus "ignored" }
+auto g() -> const void (*)() { return nullptr; } // { dg-bogus "ignored" }
+
+// Here as well.
+const void (*h)() = static_cast<const void (*)()>(h); // { dg-bogus "ignored" }
+
+// But let's keep the warning here.
+const void f(); // { dg-warning "ignored" }
+const void f() { } // { dg-warning "ignored" }

                 reply	other threads:[~2022-11-15 22:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20221115224451.3F1073945C11@sourceware.org \
    --to=mpolacek@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /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).