https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101783

            Bug ID: 101783
           Summary: unnecessary error when top level cv qualifier is
                    dropped
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nickhuang99 at hotmail dot com
  Target Milestone: ---

Created attachment 51261
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51261&action=edit
When no template specialization is at present, static_assert shows correct
function signature. It would impress user this error unnecessary.

The following code:

template<class T> struct A{
        typedef T& Type; // #0
};
template<class T> void f(const typename A<T>::Type){}  // #1
template <> void f<int>(const typename A<int>::Type){} // #2

will generate error:

error: ‘const’ qualifiers cannot be applied to ‘A<int>::Type’ {aka ‘int&’}

1. This error is unnecessary because the *const* as top level cv qualifier will
be dropped anyway. Considering following correct static assert even without
template specialization of #2 at presence:

static_assert(std::is_same<decltype(f<int>),void(int&)>::value);

The signature of *f<int>* would drop unnecessary const anyway as top-level cv
qualifier according to spec. So, there is no need to generate error.

2. clang gives a warning but still accept it of which I believe appropriate
(https://www.godbolt.org/z/9qWrj5asq). MSVC accept without complaining
(https://www.godbolt.org/z/3coqMnjjq).

3. I have a simple fix for this. By using *tf_keep_type_decl*, it will suppress
this unnecessary error emit and let returning result being validated anyway. In
other words, suppressing this early error doesn't break anything.

--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -4104,7 +4104,7 @@ cp_parser_make_typename_type (cp_parser *parser, tree id,
   if (identifier_p (id))
     {
       result = make_typename_type (parser->scope, id, typename_type,
-                                  /*complain=*/tf_none);
+                                  /*complain=*/tf_keep_type_decl);
       if (result == error_mark_node)
        cp_parser_diagnose_invalid_type_name (parser, id, id_location);
       return result;

Reply via email to