From: Marek Polacek Date: Tue, 1 Nov 2022 15:49:03 +0000 (-0400) Subject: c++: Disable -Wignored-qualifiers for template args [PR107492] X-Git-Tag: basepoints/gcc-14~3130 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ed1797ddf8285f59a50d9c883beb97705279d980;p=thirdparty%2Fgcc.git 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); because there the qualifier matters. Likewise in template specialization: template struct S { }; template<> struct S { }; template<> struct S { }; // 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 --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index c83fdac49844..0234919ea572 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); + 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 000000000000..dedb38fc9952 --- /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 struct S { }; +template<> struct S { }; +template<> struct S { }; // { dg-bogus "ignored" } + +template constexpr bool is_same_v = false; +template constexpr bool is_same_v = 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(h); // { dg-bogus "ignored" } + +// But let's keep the warning here. +const void f(); // { dg-warning "ignored" } +const void f() { } // { dg-warning "ignored" }