From patchwork Fri Mar 1 07:55:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 208683 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp922905dyb; Thu, 29 Feb 2024 23:56:37 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVzMblBriLyUAomvaAVecDfamTuXFR8Hr+fABP56BN2q2iCuLjsoZanJUJkjdkaJit8xIH8KWUUlLQSHcTfCSuDpBw3dw== X-Google-Smtp-Source: AGHT+IE/B5TkYwuGDH/9hJjAotgBcmZaNBQnc60UjNh2P1X3ZguFeP2u00ZgZ8VXE5u205YWFIEb X-Received: by 2002:ae9:e011:0:b0:787:f42e:595d with SMTP id m17-20020ae9e011000000b00787f42e595dmr864388qkk.28.1709279797085; Thu, 29 Feb 2024 23:56:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709279797; cv=pass; d=google.com; s=arc-20160816; b=ZV+7Sgjkizw/ZlqD/MfiXgV9BS0kDf94JPJilIizK2+H4dXV0Lq2xI3pgoHnJysACu h1O/Hg73d+9fHshqoLF5e6SVCUApzhIS9wIpU8J//IX/hOH45mDbfL3mBlx60Wod1ipR ZjbGe44oNvOL1azA2m+B30O/MAjBWdUoI1EFmIlnHr8VBM0Fns9BNDeVshU/27Huz9kV JTZAbvHNBpfNBHd7d1ujYqEAt+6OvXGn3POD4vaL8Mv5Z7AumUWkImGCT1S6AXPgGZQ4 3DOtMa6aE020EBaGW3V6SJo6cj4mIy3Fm7kGayTIMjU4v2cVppMqThKJJ1rMR5gU/J8w fMoA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition:in-reply-to :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=ItCgBHkEezxwwiFePwsmQXAXMjcEdtOtEIyF39w5Elk=; fh=0xZT+NBKSeH8qOu04/61f1ZGePpF4jF/gxp331YE14k=; b=jz5fL0YqmJ8U190nEBDlkqjqe3f6+8zfwESc0VwRasHjm4BDiCQs4b9xb0k8huBvRB zMHsSfAP4y7xUr5VCOen1f0KcV1YeLmNAa2f14UjcZMRlsfuYsX46gGkimYo0zTYiFSe RfhgdUwP7rnjzwnf/y+0Mq6TqoHPg/6vvLvd81e+kAuADXrrPZ8iI/TmVRkz+XbyFADV 31P/n4vOqhWgtfocdmoQEUpPVQWCN8DvsopMVFj8NB1v89uIGytFPERw/HTEPgzrZUe8 QXxUwDXlnCkSFrIUN1gcQToQ82oX8nBRmSPvMPQcH2k6x5fVqQh/BFkeQDKGi2xDECqU RwlQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Vu8omm9N; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id i29-20020a05620a0a1d00b00787b401d224si3030656qka.363.2024.02.29.23.56.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 23:56:37 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=Vu8omm9N; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C6C513858409 for ; Fri, 1 Mar 2024 07:56:36 +0000 (GMT) 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 7F77A3858C53 for ; Fri, 1 Mar 2024 07:55:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7F77A3858C53 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7F77A3858C53 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709279749; cv=none; b=oVrOksaQWBcPEJlLcIIbaG8s1OzM5YD/aN/NMHsBWyxBZMfE4RIIPYbK2ZtAFs/26DDk809r9BAYIutY99yQ4ieqKfLv6DQIxMKjSC/MjoRvUVDUzh+iQtc+3Fy/2/bEFnSO5w1cdwbvyMguOUEGel0c1ThuZRJEFcumjQ+6yOo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709279749; c=relaxed/simple; bh=zWLrr5V219XTrduWe33aeUevTcEoHkUMJThYheOYRZI=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=AzKRxyLqZmtKFBw4CbDDuzET98ZoctASyzksRUQsCwXxk1cUUHlJ7HmLH30LZw6pTeNS4kYGdYWa3b95scfikXxsxFw8IlnrD1VaFJ+QuCyo96DS8U/YkuwKiSTAcFciD+H9bVQo9j7tAho/uI5oAzaac4crNjsEGECtO5XuZE4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709279747; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=ItCgBHkEezxwwiFePwsmQXAXMjcEdtOtEIyF39w5Elk=; b=Vu8omm9NlmDVHV+tEoexc+4amQRyDyYn9oKPihz14VSqSbF1MFixh8vTfb2oHaAiezSJ75 Fl14gTqfXWgP9CyqHvyleDzG5yTaIl/qeYQXr6mjf4ATMIgVX5cTtpcEk8EjqWsMQMQxhC 8Ni1o7/GZp4zhZ/Th9RuSjodf6ILK1E= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-424-xSPvegesPXKH7pM0zcXRNA-1; Fri, 01 Mar 2024 02:55:45 -0500 X-MC-Unique: xSPvegesPXKH7pM0zcXRNA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0B0D31C0782D for ; Fri, 1 Mar 2024 07:55:45 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.226.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C4C721BDB1; Fri, 1 Mar 2024 07:55:44 +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 4217th6u506007 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 1 Mar 2024 08:55:43 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4217th1V506006; Fri, 1 Mar 2024 08:55:43 +0100 Date: Fri, 1 Mar 2024 08:55:43 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++, v2: Fix up decltype of non-dependent structured binding decl in template [PR92687] Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 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, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792309772617439490 X-GMAIL-MSGID: 1792309772617439490 On Thu, Feb 29, 2024 at 12:50:47PM +0100, Jakub Jelinek wrote: > finish_decltype_type uses DECL_HAS_VALUE_EXPR_P (expr) check for > DECL_DECOMPOSITION_P (expr) to determine if it is > array/struct/vector/complex etc. subobject proxy case vs. structured > binding using std::tuple_{size,element}. > For non-templates or when templates are already instantiated, that works > correctly, finalized DECL_DECOMPOSITION_P non-base vars indeed have > DECL_VALUE_EXPR in the former case and don't have it in the latter. > It works fine for dependent structured bindings as well, cp_finish_decomp in > that case creates DECLTYPE_TYPE tree and defers the handling until > instantiation. > As the testcase shows, this doesn't work for the non-dependent structured > binding case in templates, because DECL_HAS_VALUE_EXPR_P is set in that case > always; cp_finish_decomp ends with: > if (processing_template_decl) > { > for (unsigned int i = 0; i < count; i++) > if (!DECL_HAS_VALUE_EXPR_P (v[i])) > { > tree a = build_nt (ARRAY_REF, decl, size_int (i), > NULL_TREE, NULL_TREE); > SET_DECL_VALUE_EXPR (v[i], a); > DECL_HAS_VALUE_EXPR_P (v[i]) = 1; > } > } > and those artificial ARRAY_REFs are used in various places during > instantiation to find out what base the DECL_DECOMPOSITION_P VAR_DECLs > have and their positions. > Another option would be to change > tree > lookup_decomp_type (tree v) > { > - return *decomp_type_table->get (v); > + if (tree *slot = decomp_type_table->get (v)) > + return *slot; > + return NULL_TREE; > } > > and in finish_decl_decomp either just in the ptds.saved case or always > try to lookup_decomp_type, if it returns non-NULL, return what it returned, > otherwise return unlowered_expr_type (expr). I guess it would be cleaner, > I thought it would be more costly due to the hash table lookup, but now that > I think about it again, DECL_VALUE_EXPR is a hash table lookup as well. > So maybe then > + if (ptds.saved) > + { > + gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (expr)); > + /* DECL_HAS_VALUE_EXPR_P is always set if > + processing_template_decl. If lookup_decomp_type > + returns non-NULL, it is the tuple case. */ > + if (tree ret = lookup_decomp_type (expr)) > + return ret; > + } > if (DECL_HAS_VALUE_EXPR_P (expr)) > /* Expr is an array or struct subobject proxy, handle > bit-fields properly. */ > return unlowered_expr_type (expr); > else > /* Expr is a reference variable for the tuple case. */ > return lookup_decomp_type (expr); Here is a variant of the patch which does that. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? Or the other version, or adding some flag to the DECL_DECOMPOSITION_P decls? 2024-03-01 Jakub Jelinek PR c++/92687 * decl.cc (lookup_decomp_type): Return NULL_TREE if decomp_type_table doesn't have entry for V. * semantics.cc (finish_decltype_type): If ptds.saved, assert DECL_HAS_VALUE_EXPR_P is true and decide on tuple vs. non-tuple based on if lookup_decomp_type is NULL or not. * g++.dg/cpp1z/decomp59.C: New test. Jakub --- gcc/cp/decl.cc.jj 2024-02-28 23:20:01.004751204 +0100 +++ gcc/cp/decl.cc 2024-02-29 20:03:11.087218176 +0100 @@ -9262,7 +9262,9 @@ static GTY((cache)) decl_tree_cache_map tree lookup_decomp_type (tree v) { - return *decomp_type_table->get (v); + if (tree *slot = decomp_type_table->get (v)) + return *slot; + return NULL_TREE; } /* Mangle a decomposition declaration if needed. Arguments like --- gcc/cp/semantics.cc.jj 2024-02-28 22:57:08.101800588 +0100 +++ gcc/cp/semantics.cc 2024-02-29 20:04:51.936880622 +0100 @@ -11804,6 +11804,15 @@ finish_decltype_type (tree expr, bool id access expression). */ if (DECL_DECOMPOSITION_P (expr)) { + if (ptds.saved) + { + gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (expr)); + /* DECL_HAS_VALUE_EXPR_P is always set if + processing_template_decl. If lookup_decomp_type + returns non-NULL, it is the tuple case. */ + if (tree ret = lookup_decomp_type (expr)) + return ret; + } if (DECL_HAS_VALUE_EXPR_P (expr)) /* Expr is an array or struct subobject proxy, handle bit-fields properly. */ --- gcc/testsuite/g++.dg/cpp1z/decomp59.C.jj 2024-02-29 20:02:17.467929327 +0100 +++ gcc/testsuite/g++.dg/cpp1z/decomp59.C 2024-02-29 20:02:17.467929327 +0100 @@ -0,0 +1,63 @@ +// PR c++/92687 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +namespace std { + template struct tuple_size; + template struct tuple_element; +} + +struct A { + int i; + template int& get() { return i; } +}; + +template<> struct std::tuple_size { static const int value = 2; }; +template struct std::tuple_element { using type = int; }; + +template +struct is_reference { + static const bool value = false; +}; + +template +struct is_reference +{ + static const bool value = true; +}; + +template +struct is_reference +{ + static const bool value = true; +}; + +template +void +foo () +{ + auto [x, y] = A {}; // { dg-warning "structured bindings only available with" "" { target c++14_down } } + static_assert (!is_reference::value, ""); +} + +void +bar () +{ + auto [x, y] = A {}; // { dg-warning "structured bindings only available with" "" { target c++14_down } } + static_assert (!is_reference::value, ""); +} + +template +void +baz () +{ + auto [x, y] = T {}; // { dg-warning "structured bindings only available with" "" { target c++14_down } } + static_assert (!is_reference::value, ""); +} + +void +qux () +{ + foo<0> (); + baz (); +}