From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by sourceware.org (Postfix) with ESMTPS id D2828384AB76 for ; Tue, 25 Jun 2024 05:00:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D2828384AB76 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=quicinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D2828384AB76 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719291655; cv=none; b=HgWWeW4dMGGryfElpZEmOk9ahOzS9hUi5tEuooxs/zqPkKNvPYihk7Ye8ArIBzquV1ZLVl0+UPaN0yE3TiznzF9GjNduvXZMJ7RaRoJDR7dget6UZtYhlHLOoUlJhIPKvhBmfVEgMXHG/ILsMLvcqtBEC/2V8aaRsg3Hm62vSmM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719291655; c=relaxed/simple; bh=Kl7WRALG9tcN52oaJ1tlLn4am0aC8984lasRBha7LS0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=vV2FoMj1dpj0YVGunjjFwEZp/9ZL4XlzbEiqjrj3H/JNL//dCN9X2AR1u8Xb2GgvjIEefC1YNBIa1KPr55KycrrZUyjk+nFPTdJqzuXOSqgfrE5dmtYt2gkzB+PG00s8Sj0ELNHKaWpPwcdhdaEO4EMkjkJWd1N/apFoQMTxqaQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45OK270D014011 for ; Tue, 25 Jun 2024 05:00:52 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:message-id :mime-version:subject:to; s=qcppdkim1; bh=Oo4loXb2eUeUgagkhIgUrO cclwrA+qsx8EZrKhQZgcI=; b=Me0LIPySeyfVQg6CtQLyqUSUv6Ry2yBwhzJr4e 7v2OL03aK7jkkej9mQ3Q8pBHJLplP+bw+TcnY5s3vgPoAjK+852ochGHTDYEAbaA JYmD3lA+Dy6LDT/HeUbNIVmOEY4h56CiWcPzG5G7kPRzxQ+/L6JUVNN1BZU5JqQa vTCJTCTpuwUZn4jbsQc/JkkrCjhBgf2cKybbg3s5l4UFGwsVRBXkmql2xO19GNjg 7DYyCckD2kAGfriDmNOVyz3JhL+4TzuA1qLmXenKUp7/UX5BaZu6Yik+foEyAJWx 2D//LA3SIuy8RwGFd3tloa2Q/hoRz1ZAZoqRNnAQ8BJNN1wA== Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3ywnxgwc69-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 25 Jun 2024 05:00:51 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA01.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 45P50oQe026116 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 25 Jun 2024 05:00:51 GMT Received: from hu-apinski-lv.qualcomm.com (10.49.16.6) by nasanex01c.na.qualcomm.com (10.45.79.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Mon, 24 Jun 2024 22:00:50 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] c++: structured bindings and lookup of tuple_size/tuple_element [PR115605] Date: Mon, 24 Jun 2024 22:00:40 -0700 Message-ID: <20240625050040.3812713-1-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01a.na.qualcomm.com (10.47.209.196) To nasanex01c.na.qualcomm.com (10.45.79.139) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: 0MCdik6LTsXYn98X-5z7GSb-6VqEgwfp X-Proofpoint-ORIG-GUID: 0MCdik6LTsXYn98X-5z7GSb-6VqEgwfp X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-06-25_02,2024-06-24_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 clxscore=1015 suspectscore=0 spamscore=0 lowpriorityscore=0 adultscore=0 mlxscore=0 malwarescore=0 priorityscore=1501 mlxlogscore=999 impostorscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2406140001 definitions=main-2406250037 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: The problem here is even though we pass std namespace to lookup_template_class as the context, it will look at the current scope for the name too. The fix is to lookup the qualified name first and then use that for lookup_template_class. This is how std::initializer_list is handled in listify. Note g++.dg/cpp1z/decomp22.C testcase now fails correctly with an error, that tuple_size is not in the std namespace. I copied a fixed up testcase into g++.dg/cpp1z/decomp62.C. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR c++/115605 gcc/cp/ChangeLog: * decl.cc (get_tuple_size): Call lookup_qualified_name before calling lookup_template_class. (get_tuple_element_type): Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/decomp22.C: Expect an error * g++.dg/cpp1z/decomp61.C: New test. * g++.dg/cpp1z/decomp62.C: Copied from decomp22.C and wrap tuple_size/tuple_element inside std namespace. Signed-off-by: Andrew Pinski --- gcc/cp/decl.cc | 16 +++++--- gcc/testsuite/g++.dg/cpp1z/decomp22.C | 2 +- gcc/testsuite/g++.dg/cpp1z/decomp61.C | 53 +++++++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp1z/decomp62.C | 23 ++++++++++++ 4 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/decomp61.C create mode 100644 gcc/testsuite/g++.dg/cpp1z/decomp62.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 03deb1493a4..81dde4d51a3 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -9195,10 +9195,13 @@ get_tuple_size (tree type) { tree args = make_tree_vec (1); TREE_VEC_ELT (args, 0) = type; - tree inst = lookup_template_class (tuple_size_identifier, args, + tree std_tuple_size = lookup_qualified_name (std_node, tuple_size_identifier); + if (std_tuple_size == error_mark_node) + return NULL_TREE; + tree inst = lookup_template_class (std_tuple_size, args, /*in_decl*/NULL_TREE, - /*context*/std_node, - tf_none); + /*context*/NULL_TREE, + tf_warning_or_error); inst = complete_type (inst); if (inst == error_mark_node || !COMPLETE_TYPE_P (inst) @@ -9224,9 +9227,12 @@ get_tuple_element_type (tree type, unsigned i) tree args = make_tree_vec (2); TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i); TREE_VEC_ELT (args, 1) = type; - tree inst = lookup_template_class (tuple_element_identifier, args, + tree std_tuple_elem = lookup_qualified_name (std_node, tuple_element_identifier); + if (std_tuple_elem == error_mark_node) + return NULL_TREE; + tree inst = lookup_template_class (std_tuple_elem, args, /*in_decl*/NULL_TREE, - /*context*/std_node, + /*context*/NULL_TREE, tf_warning_or_error); return make_typename_type (inst, type_identifier, none_type, tf_warning_or_error); diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp22.C b/gcc/testsuite/g++.dg/cpp1z/decomp22.C index 9e6b8df486a..4131486e292 100644 --- a/gcc/testsuite/g++.dg/cpp1z/decomp22.C +++ b/gcc/testsuite/g++.dg/cpp1z/decomp22.C @@ -17,5 +17,5 @@ int foo (C t) { auto[x0] = t; // { dg-warning "structured bindings only available with" "" { target c++14_down } } - return x0; + return x0; /* { dg-error "cannot convert" } */ } diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp61.C b/gcc/testsuite/g++.dg/cpp1z/decomp61.C new file mode 100644 index 00000000000..874844b2c61 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/decomp61.C @@ -0,0 +1,53 @@ +// PR c++/115605 +// { dg-do compile { target c++17 } } +// { dg-options "" } + +using size_t = decltype(sizeof(0)); + +namespace std +{ + template + struct tuple_size; + template + struct tuple_element; +} + +struct mytuple +{ + int t; + template + int &get() + { + return t; + } +}; + +namespace std +{ + template<> + struct tuple_size + { + static constexpr int value = 3; + }; + template + struct tuple_element + { + using type = int; + }; +} + +/* The tuple_size/tuple_element lookup should only be from std and not + from the current scope so these 2 functions should work. */ +int foo() { + int const tuple_size = 5; + mytuple array; + auto [a, b, c] = array; + return c; +} +int foo1() { + int const tuple_element = 5; + mytuple array; + auto [a, b, c] = array; + return c; +} + diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp62.C b/gcc/testsuite/g++.dg/cpp1z/decomp62.C new file mode 100644 index 00000000000..694f3263bd8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/decomp62.C @@ -0,0 +1,23 @@ +// PR c++/79205 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +template struct B; +template struct B { int b; }; +template struct C { B<0, E...> c; C (C &) = default; C (C &&); }; +namespace std { + template struct tuple_size; + template <> struct tuple_size> { static constexpr int value = 1; }; + template struct tuple_element; + template + struct tuple_element<0, C> { typedef int type; }; +} +template +int &&get (C &&); + +int +foo (C t) +{ + auto[x0] = t; // { dg-warning "structured bindings only available with" "" { target c++14_down } } + return x0; +} -- 2.43.0