From patchwork Thu Feb 29 11:50:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 208330 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp335739dyb; Thu, 29 Feb 2024 03:51:36 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVyDFCiTS0mDb8dkCLREBfPbEUHbXB21LMYMX5aMqJnhw6+6v29s1sa91cW7/8SkB7DQt5QMfCa8nXW+Gb6Vrccqx0Y3w== X-Google-Smtp-Source: AGHT+IF9KmwHTr+K5wn4iwvD0OtHSsA8lGzYrwDtm0fX3ALaEggS6UE52tqoNe2Zi49Qx+rziOkh X-Received: by 2002:ac8:5bcd:0:b0:42e:586d:1234 with SMTP id b13-20020ac85bcd000000b0042e586d1234mr1910734qtb.28.1709207495881; Thu, 29 Feb 2024 03:51:35 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709207495; cv=pass; d=google.com; s=arc-20160816; b=WgPACaFszhT43jcNXf7Zhm7vY9x6mNWf9LdV8EA0gEI8ntkWOD+sPHwoThPVWiU0N8 rX02Jp6ikv8parTcMDjBuvSUdoGHL6IBToj70rtJdFTAt2JO1C9NrzesxJXb8AqFJzcx MFAwrbQnDz3igH1td6rqrns/3kfe4MYWnhjNUTbmxpH3Yg0eoGU222hrvO8AwvU2TTEH 4vdW4I9Gag6NE3nviONbk1vleFGn35ecvc0eiGqdriZ3SjPsZkNSmdj82t4KnA/KqjXl bxLN84WNN7IyqgPMmAYyiD8hRFuFN5/IwhU1t9LGM95EvxvJkZW1LEGs29B71jNnXfmL yPuA== 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 :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=jgukZqBEJ8vpEIUihjA/tOX2Mf7f9KbwhikvHDwnv+0=; fh=0xZT+NBKSeH8qOu04/61f1ZGePpF4jF/gxp331YE14k=; b=MA5IDl5JyHA082kUe5XtNxl/IJxpy0Nq5jcfxqeW7VgjWQQ2I1QmzjH5RsJm1YWCGk VM+8/SND9n3tbqxKFYI2JTa1GjmYxoOzHztV6VYI0dyoTTBO4g9r4nFSd3fhTrGVASoh I+FET9UljGQ0Koy46P+9h438tk/Lnv2U81JTovkAab1cvhJZvO1LwuYrgpjWljGFcU5u WTCXa77J9kmex03bn+Jr8K6qE71pGnU5RjoYzelEhBpc8NHB/LzZBQEGqxBrGiIZx3D2 hobUCMYpDYHd/HHLtPmTPdyWZAgB3umCHe/ov2efefIBNbOM1tnTaujQkcne4rWn0KYR ACyQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=a003QMlq; 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 ks15-20020a056214310f00b00686a0c905e1si1126545qvb.463.2024.02.29.03.51.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 03:51:35 -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=a003QMlq; 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 665B73858416 for ; Thu, 29 Feb 2024 11:51:35 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id 676A83858C52 for ; Thu, 29 Feb 2024 11:50:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 676A83858C52 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 676A83858C52 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709207460; cv=none; b=GvieQEieSvIoS0ylkgnGeIpQocCtXvjzZ7SRnPW3g9AQ/dRobI456E5ddbGw0ONVIm5fW5zrjKM+kADV1RTOzVI+F+Hm/h0kTREmTXdolGCfTFmDTEQyD7R9iUIrg9A3+CNO8N4SSFl/Wn8zSYyHu/VMnujir6VOpcgvTAACuQc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709207460; c=relaxed/simple; bh=4LF0GsOpvLuTmItqi9SVRad1reOcPuojorHd7XWDYB8=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=txbyQnNPRzaS02zG3jtQmYGMaQUXHgqcae8Jry1dPyWlebGBDXo0+Q/bOnsMOcRuhsbLnFzcFkgcRDxL67PSaIaJn5rVaGatD2TYCAwFAElFNp4OdPSNZ6JFuzYJhLw8kLBCvvdPHMjxNAbtFWv3kCuWZkYLrZoNQeqp6FMocsU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709207452; 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; bh=jgukZqBEJ8vpEIUihjA/tOX2Mf7f9KbwhikvHDwnv+0=; b=a003QMlqx5lDIYSw9RefDQtz84sA2TEvPFEzdPNV7Sg+5EdERFAOD22McopoZIHlZjheaH dHJ/+4bFsxSk0p5H8obrkHtcyv+W6lPXSImg1xUCJgoo+UeooDfBQLyjAy5VtwVfUuNj0t dtK5k7TZz7xjLCa5jSh1OSGgK68Hs58= 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-81-7IdZsWleNRmEiOhl9yU-Ow-1; Thu, 29 Feb 2024 06:50:50 -0500 X-MC-Unique: 7IdZsWleNRmEiOhl9yU-Ow-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 62E2629AA3B2 for ; Thu, 29 Feb 2024 11:50:50 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 25686492BE2; Thu, 29 Feb 2024 11:50:50 +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 41TBolHP1803920 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 29 Feb 2024 12:50:48 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 41TBol2h1803919; Thu, 29 Feb 2024 12:50:47 +0100 Date: Thu, 29 Feb 2024 12:50:47 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Fix up decltype of non-dependent structured binding decl in template [PR92687] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 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_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.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: 1792233959070911541 X-GMAIL-MSGID: 1792233959070911541 Hi! 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. The following patch fixes it by remembering from cp_finish_decomp in the processing_template_decl case whether the structured binding uses std::tuple_{size,element} or not in a flag, which then finish_decltype_type can use. Rather than wasting a lang_decl_base bit on it or growing the size of lang_decl_decomp for it, I chose to abuse the ARRAY_REF operands; the ARRAY_REF in that case is completely artificial, will never be emitted (when the cp_finish_decomp is called on the instantiated version of it with !processing_template_decl, DECL_VALUE_EXPR/DECL_HAS_VALUE_EXPR_P is cleared), so I chose to use size_zero_node for the TREE_OPERAND (array_ref, 2) as a flag this structured binding is the tuple case (per ARRAY_REF documentation the third operand is an optional copy of TYPE_MIN_VALUE of the index type; and AFAIK everything in the C++ FE uses NULL there, it is mainly there for Ada (and the 4th argument for arrays of non-constant length elements), but all these ARRAY_REFs aren't even folded or something similar). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 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); 2024-02-29 Jakub Jelinek PR c++/92687 * decl.cc (cp_finish_decomp): If processing_template_decl, remember whether std::tuple_{size,element} will be used or not in third operand of DECL_VALUE_EXPR ARRAY_REF. * semantics.cc (finish_decltype_type): Use that if ptds.saved to see if lookup_decomp_type should be used. * g++.dg/cpp1z/decomp59.C: New test. Jakub --- gcc/cp/decl.cc.jj 2024-02-28 08:41:18.486493565 +0100 +++ gcc/cp/decl.cc 2024-02-28 15:10:47.555186301 +0100 @@ -9384,6 +9384,7 @@ cp_finish_decomp (tree decl, cp_decomp * tree eltype = NULL_TREE; unsigned HOST_WIDE_INT eltscnt = 0; + bool tuple_p = false; if (TREE_CODE (type) == ARRAY_TYPE) { tree nelts; @@ -9535,6 +9536,7 @@ cp_finish_decomp (tree decl, cp_decomp * of the individual variables. If those will be read, we'll mark the underlying decl as read at that point. */ DECL_READ_P (decl) = save_read; + tuple_p = true; } else if (TREE_CODE (type) == UNION_TYPE) { @@ -9607,14 +9609,25 @@ cp_finish_decomp (tree decl, cp_decomp * } if (processing_template_decl) { + /* For non-dependent structured bindings using std::tuple_size + and std::tuple_element, remember that in third operand of + the DECL_VALUE_EXPR ARRAY_REF for finish_decltype_type purposes. */ 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); + tuple_p ? size_zero_node : NULL_TREE, + NULL_TREE); SET_DECL_VALUE_EXPR (v[i], a); DECL_HAS_VALUE_EXPR_P (v[i]) = 1; } + else + { + tree vexpr = DECL_VALUE_EXPR (v[i]); + gcc_checking_assert (TREE_CODE (vexpr) == ARRAY_REF + && TREE_OPERAND (vexpr, 0) == decl); + TREE_OPERAND (vexpr, 2) = tuple_p ? size_zero_node : NULL_TREE; + } } } --- gcc/cp/semantics.cc.jj 2024-02-10 09:38:25.871982914 +0100 +++ gcc/cp/semantics.cc 2024-02-28 15:08:01.026496157 +0100 @@ -11804,7 +11804,22 @@ finish_decltype_type (tree expr, bool id access expression). */ if (DECL_DECOMPOSITION_P (expr)) { - if (DECL_HAS_VALUE_EXPR_P (expr)) + bool non_tuple_p = DECL_HAS_VALUE_EXPR_P (expr); + if (non_tuple_p && ptds.saved) + { + /* cp_finish_decl sets DECL_VALUE_EXPR on all + DECL_DECOMPOSITION_P decls, not just ones for + array, struct, vector, complex etc. subobject proxies. + The ARRAY_REFs for the tuple cases are marked with + non-NULL third argument. */ + tree vexpr = DECL_VALUE_EXPR (expr); + if (TREE_CODE (vexpr) == ARRAY_REF + && DECL_P (TREE_OPERAND (vexpr, 0)) + && DECL_DECOMPOSITION_P (TREE_OPERAND (vexpr, 0)) + && TREE_OPERAND (vexpr, 2) == size_zero_node) + non_tuple_p = false; + } + if (non_tuple_p) /* Expr is an array or struct subobject proxy, handle bit-fields properly. */ return unlowered_expr_type (expr); --- gcc/testsuite/g++.dg/cpp1z/decomp59.C.jj 2024-02-28 15:14:38.846978129 +0100 +++ gcc/testsuite/g++.dg/cpp1z/decomp59.C 2024-02-28 15:22:45.611226413 +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 (); +}