Here I attach [PATCH-v2]. On Sun, Oct 3, 2021 at 7:14 AM Nick Huang <nickhuan...@gmail.com> wrote: > > Hi Jason, > > I made a little improvement of my fix by including template > type parameter to not dropping cv-const because they are similar to dependent > type which you cannot determine whether they are top-level cv-qualifier or not > until instantiation. > > + if (processing_template_decl > + && (TREE_CODE (type) == TYPENAME_TYPE > + || TREE_CODE (type) == DECLTYPE_TYPE > ++ || TREE_CODE (type) == TEMPLATE_TYPE_PARM // this is new > + ) > + ) > > 1. It fix your test case of > template <class T> > struct A{ > void f(T); > }; > template <class T> > void A<T>::f(const T){ } > template<> > void A<int[]>::f(const int*){} > > current GCC mistakenly accepts without considering the gap of missing "const" > between declaration and out of line definition. clang correctly rejects it. > (https://www.godbolt.org/z/qb9Tf99eK) and explains the cause nicely. > My fix also rejects it. > > 2. It also fix a more obvious core1001 issue of free function as my previous > fix > only deals with typename/decltype cases. > template<class T> > void f(const T){} > template<> > void f<int[3]>(const int*){} > > Your comment is appreciated. > > thank you
From 0d096262fc708edea825af713526b1ce1e9e689d Mon Sep 17 00:00:00 2001 From: qingzhe huang <nickhuan...@gmail.com> Date: Sun, 3 Oct 2021 16:00:29 -0400 Subject: [PATCH] Fix core1001/1322 by preserving const MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit
These bugs are considered duplicate cases of PR51851 which has been suspended since 2012, an issue known as "core1001/1322". Considering this background, it deserves a long comment to explain. Many people believed the root cause of this family of bugs is related with the nature of how and when the array type is converted to pointer type during function signature is calculated. This is true, but we may need to go into details to understand the exact reason. There is a pattern for these bugs(PR101402,PR102033,PR102034,PR102039). In the template function declaration, the function parameter is consisted of a "const" followed by a typename-type which is actually an array type. According to standard, function signature is calculated by dropping so- called "top-level-cv-qualifier". As a result, the templater specialization complains no matching to declaration can be found because specialization has const and template function declaration doesn't have const which is dropped as mentioned. Obviously the template function declaration should NOT drop the const. But why? Let's review the procedure of standard first. (https://timsong-cpp.github.io/cppwp/dcl.fct#5.sentence-3) "After determining the type of each parameter, any parameter of type “array of T” or of function type T is adjusted to be “pointer to T”. After producing the list of parameter types, any top-level cv-qualifiers modifying a parameter type are deleted when forming the function type." Please note the action of deleting top-level cv-qualifiers happens at last stage after array type is converted to pointer type. More importantly, there are two conditions: a) Each type must be able to be determined. b) The cv-qualifier must be top-level. Let's analysis if these two conditions can be met one by one. 1) Keyword "typename" indicates inside template it involves dependent name (https://timsong-cpp.github.io/cppwp/n4659/temp.res#2) for which the name lookup can be postponed until template instantiation. Clearly the type of dependent name cannot be determined without name lookup. Then we can NOT proceed to next step until concrete template argument type is determined during specialization. 2) After “array of T” is converted to “pointer to T”, the cv-qualifiers are no longer top-level! Unfortunately in standard there is no definition of "top-level". Mr. Dan Saks's articals (https://www.dansaks.com/articles.shtml) are tremendous help! Especially this wonderful paper (https://www.dansaks.com/articles/ 2000-02%20Top-Level%20cv-Qualifiers%20in%20Function%20Parameters.pdf) discusses this topic in details. In one short sentence, the "const" before array type is NOT top-level-cv-qualifier and should NOT be dropped. So, understanding the root cause makes the fix very clear: Let's NOT drop cv-qualifier for typename-type inside template. Leave this task for template substitution later when template specialization locks template argument types. Similarly inside template, "decltype" may also include dependent name and the best strategy for parser is to preserve all original declaration and postpone the task till template substitution. For template type parameter, it is obvious to follow similar strategy to not drop cv-const and leave it till instantiation. Here is an interesting observation to share. Originally my fix is trying to use function "resolve_typename_type" to see if the "typename-type" is indeed an array type so as to decide whether the const should be dropped. It works for cases of PR101402,PR102033(with a small fix of function), but cannot succeed on cases of PR102034,PR102039. Especially PR102039 is impossible because it depends on template argument. This helps me realize that parser should not do any work if it cannot be 100% successful. All can wait. At last I want to acknowledge other efforts to tackle this core 1001/1322 from PR92010 which is an irreplaceable different approach from this fix by doing rebuilding template function signature during template substitution stage. After all, this fix can only deal with dependent type started with "typename" or "decltype" or template type parameter which is not the case of pr92010. gcc/cp/ChangeLog: PR c++/101402 PR c++/102033 PR c++/102034 PR c++/102039 PR c++/102044 * decl.c (grokparms): Excludes typename,decltype and template type parameter to drop cv-const. gcc/testsuite/ChangeLog: PR c++/101402 PR c++/102033 PR c++/102034 PR c++/102039 PR c++/102044 * g++.dg/parse/pr101402.C: New test. * g++.dg/parse/pr102033.C: New test. * g++.dg/parse/pr102034.C: New test. * g++.dg/parse/pr102039.C: New test. * g++.dg/parse/pr102044.C: New test. Signed-off-by: qingzhe huang <nickhuan...@gmail.com> --- gcc/cp/decl.c | 16 ++++++++++++- gcc/testsuite/g++.dg/parse/pr101402.C | 29 +++++++++++++++++++++++ gcc/testsuite/g++.dg/parse/pr102033.C | 34 +++++++++++++++++++++++++++ gcc/testsuite/g++.dg/parse/pr102034.C | 13 ++++++++++ gcc/testsuite/g++.dg/parse/pr102039.C | 24 +++++++++++++++++++ gcc/testsuite/g++.dg/parse/pr102044.C | 32 +++++++++++++++++++++++++ 6 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/parse/pr101402.C create mode 100644 gcc/testsuite/g++.dg/parse/pr102033.C create mode 100644 gcc/testsuite/g++.dg/parse/pr102034.C create mode 100644 gcc/testsuite/g++.dg/parse/pr102039.C create mode 100644 gcc/testsuite/g++.dg/parse/pr102044.C diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 90111e4c786..dac0c50f886 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -13995,7 +13995,21 @@ 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; + /* Inside template declaration, typename, decltype + and template type parameter indicating dependent + name and cv-qualifier are preserved until template + instantiation. + PR101402/PR102033/PR102034/PR102039/PR102044 */ + if (processing_template_decl + && (TREE_CODE (type) == TYPENAME_TYPE + || TREE_CODE (type) == DECLTYPE_TYPE + || TREE_CODE (type) == TEMPLATE_TYPE_PARM + ) + ) + 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); diff --git a/gcc/testsuite/g++.dg/parse/pr101402.C b/gcc/testsuite/g++.dg/parse/pr101402.C new file mode 100644 index 00000000000..42a9ce3ab78 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/pr101402.C @@ -0,0 +1,29 @@ +/* { dg-do compile } */ +namespace test1{ +template<class T> struct A { + typedef T arr[3]; +}; +template<class T> void f(const typename A<T>::arr) { } +template void f<int>(const A<int>::arr); +} + +namespace test2{ +template <class T> +void f(const T); + +template<> +void f<int[]>(const int*){} +} + +namespace test3{ +template <class T> +struct A{ +void f(T); +}; + +template<class T> +void A<T>::f(const T){} /* { dg-error "no declaration matches" } */ + +template<> +void A<int[3]>::f(const int*){} /* { dg-error "does not match any template declaration" } */ +} diff --git a/gcc/testsuite/g++.dg/parse/pr102033.C b/gcc/testsuite/g++.dg/parse/pr102033.C new file mode 100644 index 00000000000..0d5cc17620f --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/pr102033.C @@ -0,0 +1,34 @@ +/* {dg-do compile } */ +/* {dg-options "-std=c++11" } */ + +namespace test1 +{ +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>){} +} +namespace test2 +{ +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){} +} + diff --git a/gcc/testsuite/g++.dg/parse/pr102034.C b/gcc/testsuite/g++.dg/parse/pr102034.C new file mode 100644 index 00000000000..37fdce52912 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/pr102034.C @@ -0,0 +1,13 @@ +/*{dg-do compile} */ +template<class TA> +struct A{ + template<class TB> + struct B{ + typedef TB Arr3[3]; + }; +}; +template<class TA, class TB> +void f(const typename A<TA>::template B<TB>::Arr3){} +template <> +void f<int, char>(const typename A<int>::B<char>::Arr3){} + diff --git a/gcc/testsuite/g++.dg/parse/pr102039.C b/gcc/testsuite/g++.dg/parse/pr102039.C new file mode 100644 index 00000000000..25d3e77fd74 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/pr102039.C @@ -0,0 +1,24 @@ +namespace test1 +{ +struct A{ + typedef int Arr3[3]; +}; + +template<class T> +void f(const typename T::Arr3){} + +template<> +void f<A>(const int[3]){} +} + +namespace test2 +{ +struct A{ + typedef int Arr3[3]; +}; +template<class T> +void f(const typename T::Arr3){} +template<> +void f<A>(const int*){} +} + diff --git a/gcc/testsuite/g++.dg/parse/pr102044.C b/gcc/testsuite/g++.dg/parse/pr102044.C new file mode 100644 index 00000000000..bef6a920b47 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/pr102044.C @@ -0,0 +1,32 @@ +/* {dg-do compile } */ +/* {dg-options "-std=c++11" } */ +namespace test1 +{ +template<unsigned int N, class T> +void f(const T[N]){} + +template<unsigned int N, class T> +using fPtr=decltype(f<N,T>)*; + +template<unsigned int N, class T> +fPtr<N,T> af[N]={&f<N,T>}; + +template<unsigned int N, class T> +void g(const decltype(af<N,T>)){} + +template<> +void g<1,int>(const fPtr<1,int>[1]){} +} + +namespace test2 +{ +template <class T> +struct A{ +T arr3[3]; +}; +template <class T> +void f(const decltype(A<T>::arr3)){} +template <> +void f<int>(const int[3]){} +} + -- 2.17.1