From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by sourceware.org (Postfix) with ESMTP id 52C0B39450DC for ; Mon, 6 Apr 2020 15:45:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 52C0B39450DC Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-301-_S_fPoQuNNqg4vYtZEb9Ig-1; Mon, 06 Apr 2020 11:45:32 -0400 X-MC-Unique: _S_fPoQuNNqg4vYtZEb9Ig-1 Received: by mail-qv1-f70.google.com with SMTP id bp6so121838qvb.17 for ; Mon, 06 Apr 2020 08:45:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=fpC3MwWPmDWg2ECfq2gf3Tjevh3q0y4r7zF1nbiEsGs=; b=NwpaRMVzpA8WwMiOulug8YmgCcABiwAlmHWZQm39SJnUrSqtXBVX/S7CitEf3jTFPh faGCQ/lXI0MQ5Gahgo8J8KGU2wW9MAuPzi3p0ck7HWb2c/lZuw1z4JLQyZeCtnvcWchH 4P/ISAYa+TZCDN1jcZqUbckq+85pb22UHBmXv2y0hnTctM7diSLSpiPTz5UaI+PZnWGI h1PeErEe8OAsqytHKHnBB87XjF0a/mir1c5XNlRWH6FfbSU8gqBDaRk7mdVtdtEIpJsn mTymQKLKVndLA1QtgwmcCt8LhyX8AZs8WD9hsp1W55A/YlT1SMrlE47/CMyCILeGsdCO 9oIg== X-Gm-Message-State: AGi0PubkhsQa+JYcEv5SOOu6tLnafJ9jyOircsJLVZQAnLXTL50oaObP 00IZUVEM5Q9E78uwF2sJHfIAyowD2PS4lnnrRyS+iNtyCpXHE9Hw4b27AT4Nr0EYD4ZtQ/Hlv4e CrGrC9m3kf9G40XLHgw== X-Received: by 2002:ae9:e30c:: with SMTP id v12mr20813407qkf.405.1586187929058; Mon, 06 Apr 2020 08:45:29 -0700 (PDT) X-Google-Smtp-Source: APiQypL5vA3DdcxTnROk9el6jXw76klmOxwH3mH/XamVl7t/k/C6gM2ETru+06TGwOC8xpai1llxHQ== X-Received: by 2002:ae9:e30c:: with SMTP id v12mr20813349qkf.405.1586187928192; Mon, 06 Apr 2020 08:45:28 -0700 (PDT) Received: from [192.168.1.130] (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id p1sm14945097qkf.73.2020.04.06.08.45.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Apr 2020 08:45:27 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Mon, 6 Apr 2020 11:45:26 -0400 (EDT) To: Jason Merrill cc: Patrick Palka , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] c++: Fix ICE in tsubst_default_argument [PR92010] In-Reply-To: Message-ID: References: <20200323012105.3692086-1-ppalka@redhat.com> <0c702b38-1fca-d0ac-c1ee-f7e080d9367c@redhat.com> User-Agent: Alpine 2.22 (DEB 413 2020-03-19) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-28.6 required=5.0 tests=BAYES_00, BODY_8BITS, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 06 Apr 2020 15:45:36 -0000 On Wed, 1 Apr 2020, Jason Merrill wrote: > On 4/1/20 6:29 PM, Jason Merrill wrote: > > On 3/31/20 3:50 PM, Patrick Palka wrote: > > > On Tue, 31 Mar 2020, Jason Merrill wrote: > > >=20 > > > > On 3/30/20 6:46 PM, Patrick Palka wrote: > > > > > On Mon, 30 Mar 2020, Jason Merrill wrote: > > > > > > On 3/30/20 3:58 PM, Patrick Palka wrote: > > > > > > > On Thu, 26 Mar 2020, Jason Merrill wrote: > > > > > > >=20 > > > > > > > > On 3/22/20 9:21 PM, Patrick Palka wrote: > > > > > > > > > This patch relaxes an assertion in tsubst_default_argumen= t > > > > > > > > > that > > > > > > > > > exposes > > > > > > > > > a > > > > > > > > > latent > > > > > > > > > bug in how we substitute an array type into a cv-qualifie= d > > > > > > > > > wildcard > > > > > > > > > function > > > > > > > > > parameter type.=C2=A0 Concretely, the latent bug is that = given the > > > > > > > > > function > > > > > > > > > template > > > > > > > > >=20 > > > > > > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 template void = foo(const T t); > > > > > > > > >=20 > > > > > > > > > one would expect the type of foo to be void(const > > > > > > > > > int*), but > > > > > > > > > we > > > > > > > > > (seemingly prematurely) strip function parameter types of > > > > > > > > > their > > > > > > > > > top-level > > > > > > > > > cv-qualifiers when building the function's TYPE_ARG_TYPES= , and > > > > > > > > > instead > > > > > > > > > end > > > > > > > > > up > > > > > > > > > obtaining void(int*) as the type of foo after > > > > > > > > > substitution > > > > > > > > > and > > > > > > > > > decaying. > > > > > > > > >=20 > > > > > > > > > We still however correctly substitute into and decay the > > > > > > > > > formal > > > > > > > > > parameter > > > > > > > > > type, > > > > > > > > > obtaining const int* as the type of t after substitution.= =C2=A0 But > > > > > > > > > this > > > > > > > > > then > > > > > > > > > leads > > > > > > > > > to us tripping over the assert in tsubst_default_argument= that > > > > > > > > > verifies > > > > > > > > > the > > > > > > > > > formal parameter type and the function type are consisten= t. > > > > > > > > >=20 > > > > > > > > > Assuming it's too late at this stage to fix the substitut= ion > > > > > > > > > bug, we > > > > > > > > > can > > > > > > > > > still > > > > > > > > > relax the assertion like so.=C2=A0 Tested on x86_64-pc-li= nux-gnu, > > > > > > > > > does > > > > > > > > > this > > > > > > > > > look > > > > > > > > > OK? > > > > > > > >=20 > > > > > > > > This is core issues 1001/1322, which have not been resolved= .=C2=A0 > > > > > > > > Clang > > > > > > > > does > > > > > > > > the > > > > > > > > substitution the way you suggest; EDG rejects the testcase > > > > > > > > because the > > > > > > > > two > > > > > > > > substitutions produce different results.=C2=A0 I think it w= ould make > > > > > > > > sense > > > > > > > > to > > > > > > > > follow the EDG behavior until this issue is actually resolv= ed. > > > > > > >=20 > > > > > > > Here is what I have so far towards that end.=C2=A0 When subst= ituting > > > > > > > into the > > > > > > > PARM_DECLs of a function decl, we now additionally check if t= he > > > > > > > aforementioned Core issues are relevant and issue a (fatal) > > > > > > > diagnostic > > > > > > > if so.=C2=A0 This patch checks this in tsubst_decl > > > > > > > rather > > > > > > > than in tsubst_function_decl for efficiency reasons, so that = we > > > > > > > don't > > > > > > > have to perform another traversal over the DECL_ARGUMENTS / > > > > > > > TYPE_ARG_TYPES just to implement this check. > > > > > >=20 > > > > > > Hmm, this seems like writing more complicated code for a very > > > > > > marginal > > > > > > optimization; how many function templates have so many paramete= rs > > > > > > that > > > > > > walking > > > > > > over them once to compare types will have any effect on compile > > > > > > time? > > > > >=20 > > > > > Good point... though I just tried implementing this check in > > > > > tsubst_function_decl, and it seems it might be just as complicate= d to > > > > > implement it there instead, at least if we want to handle functio= n > > > > > parameter packs correctly. > > > > >=20 > > > > > If we were to implement this check in tsubst_function_decl, then = since > > > > > we have access to the instantiated function, it would presumably > > > > > suffice > > > > > to compare its substituted DECL_ARGUMENTS with its substituted > > > > > TYPE_ARG_TYPES to see if they're consistent.=C2=A0 Doing so would= certainly > > > > > catch the original testcase, i.e. > > > > >=20 > > > > > =C2=A0=C2=A0=C2=A0 template > > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 void foo(const T); > > > > > =C2=A0=C2=A0=C2=A0 int main() { foo(0); } > > > > >=20 > > > > > because the DECL_ARGUMENTS of foo would be {const int*} an= d its > > > > > TYPE_ARG_TYPES would be {int*}.=C2=A0 But apparently it doesn't c= atch the > > > > > corresponding testcase that uses a function parameter pack, i.e. > > > > >=20 > > > > > =C2=A0=C2=A0=C2=A0 template > > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 void foo(const Ts...); > > > > > =C2=A0=C2=A0=C2=A0 int main() { foo(0); } > > > > >=20 > > > > > because it turns out we don't strip top-level cv-qualifiers from > > > > > function parameter packs from TYPE_ARG_TYPES at declaration time,= as > > > > > we > > > > > do with regular function parameters.=C2=A0 So in this second test= case both > > > > > DECL_ARGUMENTS and TYPE_ARG_TYPES of foo would be {const i= nt*}, > > > > > and yet we would (presumably) want to reject this instantiation t= oo. > > > > >=20 > > > > > So it seems comparing TYPE_ARG_TYPES and DECL_ARGUMENTS from > > > > > tsubst_function_decl would not suffice, and we would still need t= o do > > > > > a > > > > > variant of the trick that's done in this patch, i.e. substitute i= nto > > > > > each dependent parameter type stripped of its top-level cv-qualif= iers, > > > > > to see if these cv-qualifiers make a material difference in the > > > > > resulting function type.=C2=A0 Or maybe there's yet another way t= o detect > > > > > this? > > > >=20 > > > > I think let's go ahead with comparing TYPE_ARG_TYPES and DECL_ARGUM= ENTS; > > > > the > > > > problem comes when they disagree.=C2=A0 If we're handling pack expa= nsions > > > > wrong, > > > > that's a separate issue. > > >=20 > > > Hm, comparing TYPE_ARG_TYPES and DECL_ARGUMENTS for compatibility see= ms > > > to be exposing a latent bug with how we handle lambdas that appear in > > > function parameter types.=C2=A0 Take g++.dg/cpp2a/lambda-uneval3.C fo= r > > > example: > > >=20 > > > =C2=A0=C2=A0=C2=A0=C2=A0 template void spam(decltype([]{}) = (*s)[sizeof(T)]) {} > > > =C2=A0=C2=A0=C2=A0=C2=A0 int main() { spam(nullptr); } > > >=20 > > > According to tsubst_function_decl in current trunk, the type of the > > > function paremeter 's' of spam according to its TYPE_ARG_TYPES = is > > > =C2=A0=C2=A0=C2=A0=C2=A0 struct ._anon_4[1] * > > > and according to its DECL_ARGUMENTS the type of 's' is > > > =C2=A0=C2=A0=C2=A0=C2=A0 struct ._anon_5[1] * > > >=20 > > > The disagreement happens because we call tsubst_lambda_expr twice dur= ing > > > substitution and thereby generate two distinct lambda types, one when > > > substituting into the TYPE_ARG_TYPES and another when substituting in= to > > > the DECL_ARGUMENTS.=C2=A0 I'm not sure how to work around this > > > bug/false-positive.. > >=20 > > Oof. > >=20 > > I think probably the right answer is to rebuild TYPE_ARG_TYPES from > > DECL_ARGUMENTS if they don't match. >=20 > ...and treat that as a resolution of 1001/1322, so not giving an error. Is something like this what you have in mind? Bootstrap and testing in progress. -- >8 -- Subject: [PATCH] c++: Rebuild function type when it disagrees with formal parameter types [PR92010] gcc/cp/ChangeLog: =09Core issues 1001 and 1322 =09PR c++/92010 =09* pt.c (maybe_rebuild_function_type): New function. =09(tsubst_function_decl): Use it. gcc/testsuite/ChangeLog: =09Core issues 1001 and 1322 =09PR c++/92010 =09* g++.dg/cpp2a/lambda-uneval11.c: New test. =09* g++.dg/template/array33.C: New test. =09* g++.dg/template/array34.C: New test. =09* g++.dg/template/defarg22.C: New test. --- gcc/cp/pt.c | 55 +++++++++++++++++ gcc/testsuite/g++.dg/cpp2a/lambda-uneval11.C | 10 ++++ gcc/testsuite/g++.dg/template/array33.C | 63 ++++++++++++++++++++ gcc/testsuite/g++.dg/template/array34.C | 63 ++++++++++++++++++++ gcc/testsuite/g++.dg/template/defarg22.C | 13 ++++ 5 files changed, 204 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-uneval11.C create mode 100644 gcc/testsuite/g++.dg/template/array33.C create mode 100644 gcc/testsuite/g++.dg/template/array34.C create mode 100644 gcc/testsuite/g++.dg/template/defarg22.C diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 041ce35a31c..fc0df790c0f 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -13475,6 +13475,59 @@ lookup_explicit_specifier (tree v) return *explicit_specifier_map->get (v); } =20 +/* Check if the function type of DECL, a FUNCTION_DECL, agrees with the ty= pe of + each of its formal parameters. If there is a disagreement then rebuild + DECL's function type according to its formal parameter types, as part o= f a + resolution for Core issues 1001/1322. */ + +static void +maybe_rebuild_function_decl_type (tree decl) +{ + bool function_type_needs_rebuilding =3D false; + if (tree parm_list =3D FUNCTION_FIRST_USER_PARM (decl)) + { + tree parm_type_list =3D FUNCTION_FIRST_USER_PARMTYPE (decl); + while (parm_type_list && parm_type_list !=3D void_list_node) +=09{ +=09 tree parm_type =3D TREE_VALUE (parm_type_list); +=09 tree formal_parm_type_unqual =3D strip_top_quals (TREE_TYPE (parm_lis= t)); +=09 if (!same_type_p (parm_type, formal_parm_type_unqual)) +=09 { +=09 function_type_needs_rebuilding =3D true; +=09 break; +=09 } + +=09 parm_list =3D DECL_CHAIN (parm_list); +=09 parm_type_list =3D TREE_CHAIN (parm_type_list); +=09} + } + + if (!function_type_needs_rebuilding) + return; + + const tree new_arg_types =3D copy_list (TYPE_ARG_TYPES (TREE_TYPE (decl)= )); + + tree parm_list =3D FUNCTION_FIRST_USER_PARM (decl); + tree old_parm_type_list =3D FUNCTION_FIRST_USER_PARMTYPE (decl); + tree new_parm_type_list =3D skip_artificial_parms_for (decl, new_arg_typ= es); + while (old_parm_type_list && old_parm_type_list !=3D void_list_node) + { + tree *new_parm_type =3D &TREE_VALUE (new_parm_type_list); + tree formal_parm_type_unqual =3D strip_top_quals (TREE_TYPE (parm_li= st)); + if (!same_type_p (*new_parm_type, formal_parm_type_unqual)) +=09*new_parm_type =3D formal_parm_type_unqual; + + if (TREE_CHAIN (old_parm_type_list) =3D=3D void_list_node) +=09TREE_CHAIN (new_parm_type_list) =3D void_list_node; + parm_list =3D DECL_CHAIN (parm_list); + old_parm_type_list =3D TREE_CHAIN (old_parm_type_list); + new_parm_type_list =3D TREE_CHAIN (new_parm_type_list); + } + + TREE_TYPE (decl) + =3D build_function_type (TREE_TYPE (TREE_TYPE (decl)), new_arg_types); +} + /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */ =20 static tree @@ -13665,6 +13718,8 @@ tsubst_function_decl (tree t, tree args, tsubst_fla= gs_t complain, DECL_ARGUMENTS (r) =3D parms; DECL_RESULT (r) =3D NULL_TREE; =20 + maybe_rebuild_function_decl_type (r); + TREE_STATIC (r) =3D 0; TREE_PUBLIC (r) =3D TREE_PUBLIC (t); DECL_EXTERNAL (r) =3D 1; diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-uneval11.C b/gcc/testsuite/g= ++.dg/cpp2a/lambda-uneval11.C new file mode 100644 index 00000000000..a04262494c7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-uneval11.C @@ -0,0 +1,10 @@ +// PR c++/92010 +// { dg-do compile { target c++2a } } + +template void spam(decltype([]{}) (*s)[sizeof(T)] =3D nullptr) +{ } + +void foo() +{ + spam(); +} diff --git a/gcc/testsuite/g++.dg/template/array33.C b/gcc/testsuite/g++.dg= /template/array33.C new file mode 100644 index 00000000000..0aa587351b4 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/array33.C @@ -0,0 +1,63 @@ +// Verify that top-level cv-qualifiers on parameter types are considered +// when determining the function type of an instantiated function template= . +// This resolves a part of Core issues 1001/1322. +// { dg-do compile } +// { dg-additional-options "-Wno-volatile" } + +template +void foo0(T t =3D 0); + +template +void foo1(const T =3D 0); + +template +void foo2(volatile T t =3D 0); + +template +void foo3(const volatile T t =3D 0); + +#if __cplusplus >=3D 201103L +#define SA(X) static_assert(X,#X) +SA(__is_same(decltype(foo0), void(char*))); +SA(__is_same(decltype(foo0), void(const char*))); +SA(__is_same(decltype(foo0), void(volatile char*))); +SA(__is_same(decltype(foo0), void(const volatile ch= ar*))); + +SA(__is_same(decltype(foo1), void(const char*))); +SA(__is_same(decltype(foo1), void(const char*))); +SA(__is_same(decltype(foo1), void(const volatile char*)))= ; +SA(__is_same(decltype(foo1), void(const volatile ch= ar*))); + +SA(__is_same(decltype(foo2), void(volatile char*))); +SA(__is_same(decltype(foo2), void(const volatile char*))); +SA(__is_same(decltype(foo2), void(volatile char*))); +SA(__is_same(decltype(foo2), void(const volatile ch= ar*))); + +SA(__is_same(decltype(foo3), void(const volatile char*))); +SA(__is_same(decltype(foo3), void(const volatile char*))); +SA(__is_same(decltype(foo3), void(const volatile char*)))= ; +SA(__is_same(decltype(foo3), void(const volatile ch= ar*))); +#endif + +int main() +{ + foo0(); + foo0(); + foo0(); + foo0(); + + foo1(); + foo1(); + foo1(); + foo1(); + + foo2(); + foo2(); + foo2(); + foo2(); + + foo3(); + foo3(); + foo3(); + foo3(); +} diff --git a/gcc/testsuite/g++.dg/template/array34.C b/gcc/testsuite/g++.dg= /template/array34.C new file mode 100644 index 00000000000..38c06401974 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/array34.C @@ -0,0 +1,63 @@ +// Verify that top-level cv-qualifiers on parameter types are considered +// when determining the function type of an instantiated function template= . +// This resolves a part of Core issues 1001/1322. +// { dg-do compile { target c++11 } } +// { dg-additional-options "-Wno-volatile" } + +template +void foo0(Ts... t); + +template +void foo1(const Ts... t); + +template +void foo2(volatile Ts... t); + +template +void foo3(const volatile Ts... t); + +#if __cplusplus >=3D 201103L +#define SA(X) static_assert(X,#X) +SA(__is_same(decltype(foo0), void(char*))); +SA(__is_same(decltype(foo0), void(const char*))); +SA(__is_same(decltype(foo0), void(volatile char*))); +SA(__is_same(decltype(foo0), void(const volatile ch= ar*))); + +SA(__is_same(decltype(foo1), void(const char*))); +SA(__is_same(decltype(foo1), void(const char*))); +SA(__is_same(decltype(foo1), void(const volatile char*)))= ; +SA(__is_same(decltype(foo1), void(const volatile ch= ar*))); + +SA(__is_same(decltype(foo2), void(volatile char*))); +SA(__is_same(decltype(foo2), void(const volatile char*))); +SA(__is_same(decltype(foo2), void(volatile char*))); +SA(__is_same(decltype(foo2), void(const volatile ch= ar*))); + +SA(__is_same(decltype(foo3), void(const volatile char*))); +SA(__is_same(decltype(foo3), void(const volatile char*))); +SA(__is_same(decltype(foo3), void(const volatile char*)))= ; +SA(__is_same(decltype(foo3), void(const volatile ch= ar*))); +#endif + +int main() +{ + foo0(0); + foo0(0); + foo0(0); + foo0(0); + + foo1(0); + foo1(0); + foo1(0); + foo1(0); + + foo2(0); + foo2(0); + foo2(0); + foo2(0); + + foo3(0); + foo3(0); + foo3(0); + foo3(0); +} diff --git a/gcc/testsuite/g++.dg/template/defarg22.C b/gcc/testsuite/g++.d= g/template/defarg22.C new file mode 100644 index 00000000000..599061cedb0 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/defarg22.C @@ -0,0 +1,13 @@ +// PR c++/92010 +// { dg-do compile { target c++11 } } + +template +void foo(const T t =3D "; ") +{ +} + +int main() +{ + foo (); +} + --=20 2.26.0.106.g9fadedd637