From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 136303858D39 for ; Mon, 13 Dec 2021 15:02:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 136303858D39 Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-209-qDI1W-aEP3q4MAi7U7UQ8Q-1; Mon, 13 Dec 2021 10:02:31 -0500 X-MC-Unique: qDI1W-aEP3q4MAi7U7UQ8Q-1 Received: by mail-qv1-f71.google.com with SMTP id 1-20020ad45ba1000000b003bdfcecfe10so23986159qvq.23 for ; Mon, 13 Dec 2021 07:02:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=Hz6DBgtdp3UAXeDCrnpg2JPYmVO1i7la4odhdCKzYts=; b=4GyLlxQuliMs7WsZ+NFX4fqed0/OGUYCvJg7ym5NmvzHE3L9uVSx1z96CqSraVJjcK WnOb4YMzWCoEddAuZ9FSFRDfNt9WlKu8WeVo85wFTics+EP6zbN2hzJqXjNhzKqItH+n D+nkK/C/VY3Cbh85CfTkTbyT7A3kbIJEgT8+TLDErPzSwv9ieah54iLJuR9AVrhaqUrZ Tge3m8Wnn79zOi1bv7z1+3FjsHGAnt3IJIBRw76K8UAdFhsd6mWDBnDlyMzkOC5o3KKD uO9rWPPT6bEtWGd3EV1YV7aBiZDR0MfV+6eyuJHx5OJmnAL9uN8qZHVPAwcptg5n/NSb kBCw== X-Gm-Message-State: AOAM530GnQoJUfS348EJVW4aOWboC9yAhKbBAXksROSVGYr+gAblV8WL 8gQ/RSwWKYKZEkojPqj7FbtiDic3QxVlg155E9RUqaIjngrYwvPkceQ9+lhb36IJTBOp6VqpbfZ CDBmPEbFRpgBezr4r2w== X-Received: by 2002:a05:6214:15c6:: with SMTP id p6mr43571990qvz.12.1639407748251; Mon, 13 Dec 2021 07:02:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJy3yRxRMmjgFRz61//kj388LOwCFOTIxRU2ho3QYf7/liZaOBULa0NsxjfyKlGOvH6XImBNuA== X-Received: by 2002:a05:6214:15c6:: with SMTP id p6mr43571741qvz.12.1639407745543; Mon, 13 Dec 2021 07:02:25 -0800 (PST) Received: from [192.168.1.149] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id w10sm10025893qtj.37.2021.12.13.07.02.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 Dec 2021 07:02:24 -0800 (PST) Message-ID: <020edec6-7812-0367-8605-109ec7c3b9a3@redhat.com> Date: Mon, 13 Dec 2021 10:02:24 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: [PATCH] c++: Allow constexpr decltype(auto) [PR102229] To: Marek Polacek , GCC Patches References: <20211210222909.649695-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20211210222909.649695-1-polacek@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-15.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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, 13 Dec 2021 15:02:35 -0000 On 12/10/21 17:29, Marek Polacek wrote: > My r11-2202 was trying to enforce [dcl.type.auto.deduct]/4, which says > "If the placeholder-type-specifier is of the form type-constraint[opt] > decltype(auto), T shall be the placeholder alone." But this made us > reject 'constexpr decltype(auto)', which, after clarification from CWG, > should be valid. [dcl.type.auto.deduct]/4 is supposed to be a syntactic > constraint, not semantic, so it's OK that the constexpr marks the object > as const. > > As a consequence, checking TYPE_QUALS in do_auto_deduction is too late, > and we have a FIXME there anyway. So in this patch I'm attempting to > detect 'const decltype(auto)' earlier. If I'm going to use TYPE_QUALS, > it needs to happen before we mark the object as const due to constexpr, > that is, before grokdeclarator's > > /* A `constexpr' specifier used in an object declaration declares > the object as `const'. */ > if (constexpr_p && innermost_code != cdk_function) > ... > > Constrained decltype(auto) was a little problem, hence the TYPENAME > check. But in a typename context you can't use decltype(auto) anyway, > I think. I wonder about checking even earlier, like in cp_parser_decl_specifier_seq? > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/11? > > PR c++/102229 > > gcc/cp/ChangeLog: > > * decl.c (check_decltype_auto): New. > (grokdeclarator): Call it. > * pt.c (do_auto_deduction): Don't check decltype(auto) here. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp1y/decltype-auto5.C: New test. > --- > gcc/cp/decl.c | 58 ++++++++++++++------- > gcc/cp/pt.c | 13 ----- > gcc/testsuite/g++.dg/cpp1y/decltype-auto5.C | 35 +++++++++++++ > 3 files changed, 74 insertions(+), 32 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp1y/decltype-auto5.C > > diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c > index 56f80775ca0..196eac287eb 100644 > --- a/gcc/cp/decl.c > +++ b/gcc/cp/decl.c > @@ -11354,6 +11354,33 @@ name_unnamed_type (tree type, tree decl) > gcc_assert (!TYPE_UNNAMED_P (type)); > } > > +/* Check that decltype(auto) was well-formed: only plain decltype(auto) > + is allowed. TYPE might contain a decltype(auto). Returns true if > + there was a problem, false otherwise. */ > + > +static bool > +check_decltype_auto (location_t loc, tree type) > +{ > + if (tree a = type_uses_auto (type)) > + { > + if (AUTO_IS_DECLTYPE (a)) > + { > + if (a != type) > + { > + error_at (loc, "%qT as type rather than plain " > + "%", type); > + return true; > + } > + else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED) > + { > + error_at (loc, "% cannot be cv-qualified"); > + return true; > + } > + } > + } > + return false; > +} > + > /* Given declspecs and a declarator (abstract or otherwise), determine > the name and type of the object declared and construct a DECL node > for it. > @@ -12702,25 +12729,9 @@ grokdeclarator (const cp_declarator *declarator, > "allowed"); > return error_mark_node; > } > - /* Only plain decltype(auto) is allowed. */ > - if (tree a = type_uses_auto (type)) > - { > - if (AUTO_IS_DECLTYPE (a)) > - { > - if (a != type) > - { > - error_at (typespec_loc, "%qT as type rather than " > - "plain %", type); > - return error_mark_node; > - } > - else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED) > - { > - error_at (typespec_loc, "% cannot be " > - "cv-qualified"); > - return error_mark_node; > - } > - } > - } > + > + if (check_decltype_auto (typespec_loc, type)) > + return error_mark_node; > > if (ctype == NULL_TREE > && decl_context == FIELD > @@ -13080,6 +13091,15 @@ grokdeclarator (const cp_declarator *declarator, > > id_loc = declarator ? declarator->id_loc : input_location; > > + if (innermost_code != cdk_function > + /* Don't check this if it can be the artifical decltype(auto) > + we created when building a constraint in a compound-requirement: > + that the type-constraint is plain is going to be checked in > + cp_parser_compound_requirement. */ > + && decl_context != TYPENAME > + && check_decltype_auto (id_loc, type)) > + return error_mark_node; > + > /* A `constexpr' specifier used in an object declaration declares > the object as `const'. */ > if (constexpr_p && innermost_code != cdk_function) > diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c > index 9834baf34db..6d5da17b9d0 100644 > --- a/gcc/cp/pt.c > +++ b/gcc/cp/pt.c > @@ -29912,19 +29912,6 @@ do_auto_deduction (tree type, tree init, tree auto_node, > return error_mark_node; > targs = make_tree_vec (1); > TREE_VEC_ELT (targs, 0) = deduced; > - /* FIXME: These errors ought to be diagnosed at parse time. */ > - if (type != auto_node) > - { > - if (complain & tf_error) > - error ("%qT as type rather than plain %", type); > - return error_mark_node; > - } > - else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED) > - { > - if (complain & tf_error) > - error ("% cannot be cv-qualified"); > - return error_mark_node; > - } > } > else > { > diff --git a/gcc/testsuite/g++.dg/cpp1y/decltype-auto5.C b/gcc/testsuite/g++.dg/cpp1y/decltype-auto5.C > new file mode 100644 > index 00000000000..01cc54fb71d > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp1y/decltype-auto5.C > @@ -0,0 +1,35 @@ > +// PR c++/102229 > +// { dg-do compile { target c++14 } } > + > +struct S { > + constexpr static decltype(auto) x = 42; > + const constexpr static decltype(auto) y = 42; // { dg-error "cannot be cv-qualified" } > + > + constexpr decltype(auto) mfn1 () { return 0; } > + const constexpr decltype(auto) mfn2 () { return 0; } // { dg-error "cannot be cv-qualified" } > +}; > + > +constexpr decltype(auto) i = 42; > +const constexpr decltype(auto) j = 42; // { dg-error "cannot be cv-qualified" } > + > +constexpr decltype(auto) fn() { return 42; } > +const decltype(auto) fn2() { return 42; } // { dg-error "cannot be cv-qualified" } > + > +auto constexpr foo() -> const decltype(auto) // { dg-error "cannot be cv-qualified" } > +{ > + return 0; > +} > + > +#if __cpp_concepts > +template > +concept C = true; > + > +constexpr C decltype(auto) x1 = 0; > +const constexpr C decltype(auto) x2 = 0; // { dg-error "cannot be cv-qualified" "" { target c++20 } } > + > +constexpr C decltype(auto) fn3() { return 0; } > +const constexpr C decltype(auto) fn4() { return 0; } // { dg-error "cannot be cv-qualified" "" { target c++20 } } > +#endif > + > +template // { dg-error "cannot be cv-qualified" } > +void g (); > > base-commit: 1e2eee7b29ed2afbc7edea6f3da7e6c8f70b1a4e