From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [216.205.24.74]) by sourceware.org (Postfix) with ESMTP id 61E6F385B834 for ; Mon, 30 Mar 2020 20:43:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 61E6F385B834 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-39-ibVyZ1t5Nke7-7TFkDbm6Q-1; Mon, 30 Mar 2020 16:43:00 -0400 X-MC-Unique: ibVyZ1t5Nke7-7TFkDbm6Q-1 Received: by mail-qk1-f198.google.com with SMTP id b21so16198779qkl.14 for ; Mon, 30 Mar 2020 13:43:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=1rojOgyCaZ6fjeYoNwC/vtYRzIdkYJeiJ1W1G1IWArQ=; b=L1RjdtRFKhJuhbDZ7FQpITof59zXmqP0FD01/w4mh6VDkMF1ER26I7PIELoWujPnVq W8+fwCw1abm5BklIwG4B4mQRtBjYDUPlMBuvKZoxPB4vVHJoVdxv/rq8kuxHOAQESM0X 5KXmAdzj0FCPGUjqdEMyCOqkdFoW7oL+DFtreftK06B/3v/mGUmsJ+L9+LoorXXNg3JQ VEW72KDufPf6LZHNOoYW5LXgS/dqE//ZvhbqIpD5kzUZFDo8FWu152O/mtVCVzX6WJdB bqI6fyW3oeia4qujZaXV0gZp3nHkRY16HIluO7r0b+IaSJhcx5APy+LQ1NMPZumyR/jA Ndcg== X-Gm-Message-State: ANhLgQ198obBCm1OfQDZn7yF2eo+MMu4TFW41K89Aq8lOe6F0favMj3v ZY+K1Tpr4CBKM3Lw1O1EVnULqombPT0OQG60QISfm2Z7NDZn0VWlGoS+xYGR0H8T9XKeZo59715 13FDbAmtSSkO9POsdwg== X-Received: by 2002:a0c:be13:: with SMTP id k19mr13180517qvg.153.1585600979361; Mon, 30 Mar 2020 13:42:59 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsI2fOXxgJRrO5x2S30NmYiDx16aELiCRryCLuTne9/6Veg2NATfAv3nOuxMzTR3ImqDv9Ysg== X-Received: by 2002:a0c:be13:: with SMTP id k19mr13180480qvg.153.1585600978701; Mon, 30 Mar 2020 13:42:58 -0700 (PDT) Received: from [192.168.1.148] (209-6-216-142.s141.c3-0.smr-cbr1.sbo-smr.ma.cable.rcncustomer.com. [209.6.216.142]) by smtp.gmail.com with ESMTPSA id c12sm11378408qkg.30.2020.03.30.13.42.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Mar 2020 13:42:58 -0700 (PDT) Subject: Re: [PATCH] c++: Fix ICE in tsubst_default_argument [PR92010] To: Patrick Palka Cc: gcc-patches@gcc.gnu.org References: <20200323012105.3692086-1-ppalka@redhat.com> From: Jason Merrill Message-ID: <0c702b38-1fca-d0ac-c1ee-f7e080d9367c@redhat.com> Date: Mon, 30 Mar 2020 16:42:57 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.0 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-31.9 required=5.0 tests=BAYES_00, 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 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, 30 Mar 2020 20:43:04 -0000 On 3/30/20 3:58 PM, Patrick Palka wrote: > On Thu, 26 Mar 2020, Jason Merrill wrote: > >> On 3/22/20 9:21 PM, Patrick Palka wrote: >>> This patch relaxes an assertion in tsubst_default_argument that exposes a >>> latent >>> bug in how we substitute an array type into a cv-qualified wildcard function >>> parameter type. Concretely, the latent bug is that given the function >>> template >>> >>> template void foo(const T t); >>> >>> 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. >>> >>> We still however correctly substitute into and decay the formal parameter >>> type, >>> obtaining const int* as the type of t after substitution. 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 consistent. >>> >>> Assuming it's too late at this stage to fix the substitution bug, we can >>> still >>> relax the assertion like so. Tested on x86_64-pc-linux-gnu, does this look >>> OK? >> >> This is core issues 1001/1322, which have not been resolved. Clang does the >> substitution the way you suggest; EDG rejects the testcase because the two >> substitutions produce different results. I think it would make sense to >> follow the EDG behavior until this issue is actually resolved. > > Here is what I have so far towards that end. When substituting into the > PARM_DECLs of a function decl, we now additionally check if the > aforementioned Core issues are relevant and issue a (fatal) diagnostic > if so. 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. Hmm, this seems like writing more complicated code for a very marginal optimization; how many function templates have so many parameters that walking over them once to compare types will have any effect on compile time? > Is something like this what you have in mind? > > -- >8 -- > > Subject: [PATCH] c++: Reject some instantiations that depend on resolution of > Core issues 1001/1322 > > gcc/cp/ChangeLog: > > Core issues 1001 and 1322 > PR c++/92010 > * pt.c (tsubst_decl) : Detect and reject the case where > the function type depends on whether we strip top-level qualifiers from > the type of T before or after substitution. > > gcc/testsuite/ChangeLog: > > Core issues 1001 and 1322 > PR c++/92010 > * g++.dg/template/array33.C: New test. > * g++.dg/template/array34.C: New test. > --- > gcc/cp/pt.c | 70 ++++++++++++++++++++++++- > gcc/testsuite/g++.dg/template/array33.C | 39 ++++++++++++++ > gcc/testsuite/g++.dg/template/array34.C | 63 ++++++++++++++++++++++ > 3 files changed, 171 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/g++.dg/template/array33.C > create mode 100644 gcc/testsuite/g++.dg/template/array34.C > > diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c > index 8564eb11df4..fd99053df36 100644 > --- a/gcc/cp/pt.c > +++ b/gcc/cp/pt.c > @@ -14072,9 +14072,77 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) > /* We're dealing with a normal parameter. */ > type = tsubst (TREE_TYPE (t), args, complain, in_decl); > > + const int type_quals = cp_type_quals (type); > + > + /* Determine whether the function type after substitution into the > + type of the function parameter T depends on the resolution of > + Core issues 1001/1322, which have not been resolved. In > + particular, the following detects the case where the resulting > + function type depends on whether we strip top-level qualifiers > + from the type of T before or after substitution. > + > + This can happen only when the dependent type of T is a > + cv-qualified wildcard type, and substitution yields a > + (cv-qualified) array type before array-to-pointer conversion. */ > + tree unqual_expanded_types = NULL_TREE; > + if (TREE_CODE (type) == ARRAY_TYPE > + && (type_quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) > + && DECL_FUNCTION_SCOPE_P (t) > + && !DECL_TEMPLATE_PARM_P (t)) > + { > + tree type_pattern = (PACK_EXPANSION_P (TREE_TYPE (t)) > + ? PACK_EXPANSION_PATTERN (TREE_TYPE (t)) > + : TREE_TYPE (t)); > + if (WILDCARD_TYPE_P (type_pattern) > + && cv_qualified_p (type_pattern)) > + { > + /* Substitute into the corresponding wildcard type that is > + stripped of its own top-level cv-qualifiers. */ > + tree unqual_type; > + if (PACK_EXPANSION_P (TREE_TYPE (t))) > + { > + if (unqual_expanded_types == NULL_TREE) > + { > + tree unqual_pack_expansion > + = copy_node (TREE_TYPE (t)); > + PACK_EXPANSION_PATTERN (unqual_pack_expansion) > + = cv_unqualified (type_pattern); > + unqual_expanded_types > + = tsubst_pack_expansion (unqual_pack_expansion, > + args, tf_none, in_decl); > + } > + unqual_type = TREE_VEC_ELT (unqual_expanded_types, i); > + } > + else > + { > + tree unqual_type_pattern = cv_unqualified (type_pattern); > + unqual_type = tsubst (unqual_type_pattern, args, > + tf_none, in_decl); > + } > + /* Check if the top-level cv-qualifiers on the wildcard type > + make a difference in the resulting type. */ > + int unqual_type_quals = cp_type_quals (unqual_type); > + if (type_quals & ~unqual_type_quals > + & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) > + { > + sorry ("the type of function %qD after substitution " > + "depends on the resolution of Core issues " > + "1001 and 1332", > + DECL_NAME (DECL_CONTEXT (t))); > + if (PACK_EXPANSION_P (TREE_TYPE (t))) > + inform (input_location, "when substituting into the " > + "function parameter pack %q#D", t); > + else > + inform (input_location, "when substituting into the " > + "function parameter %q#D", t); > + type = error_mark_node; > + } > + } > + } > + > type = type_decays_to (type); > TREE_TYPE (r) = type; > - cp_apply_type_quals_to_decl (cp_type_quals (type), r); > + cp_apply_type_quals_to_decl (type_quals, r); > > if (DECL_INITIAL (r)) > { > diff --git a/gcc/testsuite/g++.dg/template/array33.C b/gcc/testsuite/g++.dg/template/array33.C > new file mode 100644 > index 00000000000..20b005bc865 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/template/array33.C > @@ -0,0 +1,39 @@ > +// Reject some instantiations whose result depend on the resolution of // > +// Core issues 1001 and 1332, which are not yet resolved. > +// { dg-do compile } > +// { dg-additional-options "-Wno-volatile" } > + > +template > +void foo0(T t = 0); // { dg-bogus "" } > + > +template > +void foo1(const T = 0); // { dg-message "sorry, unimplemented" } > + > +template > +void foo2(volatile T t = 0); // { dg-message "sorry, unimplemented" } > + > +template > +void foo3(const volatile T t = 0); // { dg-message "sorry, unimplemented" } > + > +int main() > +{ > + foo0(); // { dg-bogus "" } > + foo0(); // { dg-bogus "" } > + foo0(); // { dg-bogus "" } > + foo0(); // { dg-bogus "" } > + > + foo1(); // { dg-message "required from here" } > + foo1(); // { dg-bogus "" } > + foo1(); // { dg-message "required from here" } > + foo1(); // { dg-bogus "" } > + > + foo2(); // { dg-message "required from here" } > + foo2(); // { dg-message "required from here" } > + foo2(); // { dg-bogus "" } > + foo2(); // { dg-bogus "" } > + > + foo3(); // { dg-message "required from here" } > + foo3(); // { dg-message "required from here" } > + foo3(); // { dg-message "required from here" } > + foo3(); // { dg-bogus "" } > +} > diff --git a/gcc/testsuite/g++.dg/template/array34.C b/gcc/testsuite/g++.dg/template/array34.C > new file mode 100644 > index 00000000000..316ea2f6407 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/template/array34.C > @@ -0,0 +1,63 @@ > +// Reject some instantiations whose result depend on the resolution of > +// Core issues 1001 and 1332, which are not yet resolved. > +// { dg-do compile { target c++11 } } > +// { dg-additional-options "-Wno-volatile" } > + > +template > +void foo0(Ts... t); // { dg-bogus "" } > + > +template > +void foo1(const Ts... t); // { dg-message "sorry, unimplemented" } > + > +template > +void foo2(volatile Ts... t); // { dg-message "sorry, unimplemented" } > + > +template > +void foo3(const volatile Ts... t); // { dg-message "sorry, unimplemented" } > + > +template > +void bar0() > +{ > + foo0(0, 0, 0); > +} > + > +template > +void bar1() > +{ > + foo1(0, 0, 0); > +} > + > +template > +void bar2() > +{ > + foo2(0, 0, 0); > +} > + > +template > +void bar3() > +{ > + foo3(0, 0, 0); > +} > + > +int main() > +{ > + bar0(); // { dg-bogus "" } > + bar0(); // { dg-bogus "" } > + bar0(); // { dg-bogus "" } > + bar0(); // { dg-bogus "" } > + > + bar1(); // { dg-message "required from here" } > + bar1(); // { dg-bogus "" } > + bar1(); // { dg-message "required from here" } > + bar1(); // { dg-bogus "" } > + > + bar2(); // { dg-message "required from here" } > + bar2(); // { dg-message "required from here" } > + bar2(); // { dg-bogus "" } > + bar2(); // { dg-bogus "" } > + > + bar3(); // { dg-message "required from here" } > + bar3(); // { dg-message "required from here" } > + bar3(); // { dg-message "required from here" } > + bar3(); // { dg-bogus "" } > +} >