Bug 102033 - template function signature incorrectly drops top-level cv-qualifiers causing template specialization failing to match
Summary: template function signature incorrectly drops top-level cv-qualifiers causing...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.2.0
: P3 normal
Target Milestone: 12.0
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
: 102034 102038 102042 (view as bug list)
Depends on:
Blocks: 24666
  Show dependency treegraph
 
Reported: 2021-08-24 09:25 UTC by qingzhe huang
Modified: 2021-12-09 04:45 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-08-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description qingzhe huang 2021-08-24 09:25:38 UTC
The following snippet code fails with similar reason as PR101402, but it cannot be fixed with similar way of using *resolve_typename_type* without further improvement of itself.

template<class TA>
struct A{
        template<class TB>
        using Type=TB[3];
};
template<class TA, class TB>
void f(const typename A<TA>::template Type<TB>){}
template <>
void f<int, char>(const typename A<int>::template Type<char>){}


error: template-id 'f<int, char>' for 'void f(const char*)' does not match any template declaration
    9 | void f<int, char>(const typename A<int>::template Type<char>){}
      |      ^~~~~~~~~~~~
array-using.cpp:7:6: note: candidate is: 'template<class TA, class TB> void f(typename A<TA>::Type<TB>)'
    7 | void f(const typename A<TA>::template Type<TB>){}
      |      ^
Comment 1 Andrew Pinski 2021-08-24 09:33:32 UTC
Confirmed, looks like an array is not decaying to a pointer issue or decaying too early really.
Comment 2 Andrew Pinski 2021-08-24 09:42:24 UTC
*** Bug 102034 has been marked as a duplicate of this bug. ***
Comment 3 qingzhe huang 2021-08-24 10:12:53 UTC
I can give tons of similar cases with even more complicated template levels combined with using/typedefs/default arguments. i.e.

template<class TA>
struct A{
        template<class TB>
        struct B{
                using TB_Alias=TB;
                template<class TC=TB_Alias>
                struct C{
                        typedef TC Arr3[3];
                };
        };
};
template<class TA, class TB>
void f(const typename A<TA>::template B<TB>::template C<>::Arr3){}
template <>
void f<int, char>(const typename A<int>::template B<char>::template C<>::Arr3){}

The fix are all can be done similarly at point to calculating function parameter "cv-qualifier" value by passing its result into call *cp_build_qualified_type* at  decl.c:grokparms.

for example:

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 3414cbdc876..473c7b7d75c 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -14493,7 +14493,16 @@ grokparms (tree parmlist, tree *parms)
 
          /* Top-level qualifiers on the parameters are
             ignored for function types.  */
-         type = cp_build_qualified_type (type, 0);
+         int type_quals = 0;
+         /* Top-level qualifiers are reserved for array type. PR101402 */
+         if (TREE_CODE (type) == TYPENAME_TYPE)
+         {
+            tree resolved_type = resolve_typename_type(type, false);
+            if (resolved_type && TREE_CODE(resolved_type) == ARRAY_TYPE)
+               type_quals = CP_TYPE_CONST_P(type);
+         }
+         type = cp_build_qualified_type (type, type_quals);
+
          if (TREE_CODE (type) == METHOD_TYPE)
            {
              error ("parameter %qD invalidly declared method type", decl);



However, original *resolve_typename_type* function is not working well to cover all cases. I only fixed one "non-recursive" case with 

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 020a4bf2f6d..1944bf4a6ea 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -27989,7 +27989,12 @@ resolve_typename_type (tree type, bool only_current_p)
 
   /* If we failed to resolve it, return the original typename.  */
   if (!result)
-    return type;
+  {
+         if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
+                         && TREE_CODE (decl) == TEMPLATE_DECL)
+                 return TREE_TYPE(decl);
+         return type;
+  }
 
   /* If lookup found a typename type, resolve that too.  */
   if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))



And then I give up because it requires almost a new *resolve_typename_type* function to cover all possible cases. And I have no GCC write access and the code is extremely difficult to maintain.
Comment 4 Jonathan Wakely 2021-08-24 11:28:14 UTC
> And I have no GCC write access and the code is extremely difficult to maintain.

We don't just give people write access as soon as they propose a patch (does any serious software proejct do that?!)

The contribution process is documented at https://gcc.gnu.org/contribute.html#patches and write access policies at https://gcc.gnu.org/gitwrite.html#policies
Comment 5 Jonathan Wakely 2021-08-24 13:39:02 UTC
*** Bug 102038 has been marked as a duplicate of this bug. ***
Comment 6 Jonathan Wakely 2021-08-24 15:29:45 UTC
*** Bug 102042 has been marked as a duplicate of this bug. ***
Comment 7 GCC Commits 2021-10-15 21:00:41 UTC
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:79802c5dcc043a515f429bb2bec7573b8537c32a

commit r12-4453-g79802c5dcc043a515f429bb2bec7573b8537c32a
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Sep 28 10:02:04 2021 -0400

    c++: array cv-quals and template specialization [PR101402]
    
    PRs 101402, 102033, etc. demonstrated that the fix for PR92010 wasn't
    handling all cases of the CWG1001/1322 issue with parameter type qual
    stripping and arrays with templates.  The problem turned out to be in
    determine_specialization, which did an extra substitution without the 92010
    fix and then complained that the result didn't match.
    
    But just removing that wrong/redundant code meant that we were accepting
    specializations with different numbers of parameters, because the code in
    fn_type_unification that compares types in this case wasn't checking for
    length mismatch.
    
    After fixing that, I realized that fn_type_unification couldn't tell the
    difference between variadic and non-variadic function types, because the
    args array doesn't include the terminal void we use to indicate non-variadic
    function type.  So I added it, and made the necessary adjustments.
    
    Thanks to qingzhe "nick" huang <nickhuang99@hotmail.com> for the patch that
    led me to dig more into this, and the extensive testcases.
    
            PR c++/51851
            PR c++/101402
            PR c++/102033
            PR c++/102034
            PR c++/102039
            PR c++/102044
    
    gcc/cp/ChangeLog:
    
            * pt.c (determine_specialization): Remove redundant code.
            (fn_type_unification): Check for mismatched length.
            (type_unification_real): Ignore terminal void.
            (get_bindings): Don't stop at void_list_node.
            * class.c (resolve_address_of_overloaded_function): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/template/fnspec2.C: New test.
            * g++.dg/template/parm-cv1.C: New test.
            * g++.dg/template/parm-cv2.C: New test.
            * g++.dg/template/parm-cv3.C: New test.
Comment 8 qingzhe huang 2021-10-25 09:02:08 UTC
fixed by patch under PR101402