From patchwork Tue May 9 08:22:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 91449 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2709764vqo; Tue, 9 May 2023 01:23:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6zV03NSrByiwy//tqQY+dkdFvzNa+4WOtikyPl9ujbdXmTt71KfIrqBhyKuP43evDoSY+o X-Received: by 2002:aa7:d50a:0:b0:50b:d4e8:317f with SMTP id y10-20020aa7d50a000000b0050bd4e8317fmr10910396edq.13.1683620603572; Tue, 09 May 2023 01:23:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683620603; cv=none; d=google.com; s=arc-20160816; b=MFlkmJme93Ipp5o9/qI0qOtdwn5H51fJd0/HyZfUk040VNAPrF+QHElJdQRQU6cGQy unMbY1aw4nvUpTSK19E7SFZamypx/6w8TyNvlHeOWJqsJro/3S29zrJBC1lHhAiE5aZe LYd1SPLYFYI944L+5FFUTi5yXy3l9Bt+V1hAKgxtYeC1H3p7q+fpCIQGJBR1OpHgTzQI i6NCQCrdRpjAv3Illz88mwytjxLJR2wGMtfebSVmVxRZh61GkTQ0xMCpnCnWESoJSgci 5arRbhwhRhfPftbTnenAtRwy8WP9g+8Nm8wS3yljGuyxE7PzsJNGf7F3iz3vPs19QY9p cmsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=SnOfge/tOyRvzLNHGV2OF9ZBEAjp3+UIMwQWNjqNGZg=; b=vAIqq2IYSR6pYD0aKdYwHwXbLBz/FoKtU/2BlP+JtbeO2bqs4B7uCyYwnKaZ19ABi6 RpCw7RpKYJ8uFzCCELrTPX+awYlAjGHXO+7GQ7t+YTG/dnamV7LCjmJSy2A/mRFd+naP Y95depLDDlo3gOU68JgnsFuRSfWKfTWHVF/abqwzyAFy3eRxBkKmqEiDFvhF8YSA8MSR O0dkQR8qCCdHrbe2NbDBDxx3bTBeXV0+/u8CYzU85TeYwm982Io6WFOyGxQiKmo8Kkqw dtPpopT+AY1mCpt0PH94JDOIGuoPJTwPxhbXOTQUdvOl6DvbXbs4zVOrY4Cbe6Y1acRk 67Cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Hb5aoY8o; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id o20-20020aa7d3d4000000b0050c9581ffa0si935317edr.192.2023.05.09.01.23.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 01:23:23 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Hb5aoY8o; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3BC5E3858C36 for ; Tue, 9 May 2023 08:23:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3BC5E3858C36 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683620602; bh=SnOfge/tOyRvzLNHGV2OF9ZBEAjp3+UIMwQWNjqNGZg=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Hb5aoY8oNqRUZDya8W42hO1cdS+fw3q8VbLa3cYCQXwMlR57U+UYMiLuo31CixNON RoyzNCTHID4thO3M9qdel+fuIvlAaHzlgknbV5VWKvXis3xS/ZXvoAkEzMoCwJXVzH cR3e1V5YErR6PHJfEMeQHUx8pgQI8qvy9Z4IogVo= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 2BFB83858C53 for ; Tue, 9 May 2023 08:22:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2BFB83858C53 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-338-lmFlRarYP124e7qvJdw3zA-1; Tue, 09 May 2023 04:22:13 -0400 X-MC-Unique: lmFlRarYP124e7qvJdw3zA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 207FB1C0878B for ; Tue, 9 May 2023 08:22:13 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.156]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B99A71121314; Tue, 9 May 2023 08:22:12 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 3498MAYc1638640 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 9 May 2023 10:22:10 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3498M9ka1638639; Tue, 9 May 2023 10:22:09 +0200 Date: Tue, 9 May 2023 10:22:09 +0200 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Reject pack expansion of assume attribute [PR109756] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765404158177185467?= X-GMAIL-MSGID: =?utf-8?q?1765404158177185467?= Hi! http://eel.is/c++draft/dcl.attr#grammar-4 says "In an attribute-list, an ellipsis may appear only if that attribute's specification permits it." and doesn't explicitly permit it on any standard attribute. The https://wg21.link/p1774r8 paper which introduced assume attribute says "We could therefore hypothetically permit the assume attribute to directly support pack expansion: template void f() { [[assume(args >= 0)...]]; } However, we do not propose this. It would require substantial additional work for a very rare use case. Note that this can instead be expressed with a fold expression, which is equivalent to the above and works out of the box without any extra effort: template void f() { [[assume(((args >= 0) && ...))]]; } ", but as the testcase shows, GCC 13+ ICEs on assume attribute followed by ... if it contains packs. The following patch rejects those instead of ICE and for C++17 or later suggests using fold expressions instead (it doesn't make sense to suggest it for C++14 and earlier when we'd error on the fold expressions). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/13.2? 2023-05-09 Jakub Jelinek PR c++/109756 * gp-gimplify.cc (process_stmt_assume_attribute): Diagnose pack expansion of assume attribute. * g++.dg/cpp23/attr-assume11.C: New test. Jakub --- gcc/cp/cp-gimplify.cc.jj 2023-05-04 12:13:50.791647593 +0200 +++ gcc/cp/cp-gimplify.cc 2023-05-08 13:20:33.191070530 +0200 @@ -3267,6 +3267,16 @@ process_stmt_assume_attribute (tree std_ for (; attr; attr = lookup_attribute ("gnu", "assume", TREE_CHAIN (attr))) { tree args = TREE_VALUE (attr); + if (args && PACK_EXPANSION_P (args)) + { + auto_diagnostic_group d; + error_at (attrs_loc, "pack expansion of %qE attribute", + get_attribute_name (attr)); + if (cxx_dialect >= cxx17) + inform (attrs_loc, "use fold expression in the attribute " + "argument instead"); + continue; + } int nargs = list_length (args); if (nargs != 1) { --- gcc/testsuite/g++.dg/cpp23/attr-assume11.C.jj 2023-05-08 13:19:07.812290213 +0200 +++ gcc/testsuite/g++.dg/cpp23/attr-assume11.C 2023-05-08 13:22:07.746719746 +0200 @@ -0,0 +1,22 @@ +// PR c++/109756 +// { dg-do compile { target c++11 } } + +template +void +foo () +{ + [[assume (1 > 0)...]]; // { dg-error "expansion pattern '\\\(1 > 0\\\)' contains no parameter packs" } + // { dg-warning "attributes at the beginning of statement are ignored" "" { target *-*-* } .-1 } + [[assume (args > 0)...]]; // { dg-error "pack expansion of 'assume' attribute" } + // { dg-message "use fold expression in the attribute argument instead" "" { target c++17 } .-1 } +#if __cpp_fold_expressions >= 201603L + [[assume (((args > 0) && ...))]]; +#endif + [[gnu::assume (1 > 0)...]]; // { dg-error "expansion pattern '\\\(1 > 0\\\)' contains no parameter packs" } + // { dg-warning "attributes at the beginning of statement are ignored" "" { target *-*-* } .-1 } + [[gnu::assume (args > 0)...]]; // { dg-error "pack expansion of 'assume' attribute" } + // { dg-message "use fold expression in the attribute argument instead" "" { target c++17 } .-1 } +#if __cpp_fold_expressions >= 201603L + [[gnu::assume (((args > 0) && ...))]]; +#endif +}