From patchwork Fri Mar 3 14:58:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 63968 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp453022wrd; Fri, 3 Mar 2023 06:59:12 -0800 (PST) X-Google-Smtp-Source: AK7set9wlr3cqPJwDfYr2Xk5b654IMRwBJNpEf3TqDYQrFJRGh/3u19AlCwBMOdzGu3AS2OAvRy1 X-Received: by 2002:a17:906:1e97:b0:8af:2fa1:619e with SMTP id e23-20020a1709061e9700b008af2fa1619emr6096764ejj.18.1677855552599; Fri, 03 Mar 2023 06:59:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677855552; cv=none; d=google.com; s=arc-20160816; b=scEzm4s/vVxJvKhZElO4FaD8P4sMpfNPYrrf4sq2WhCjkt5iIdbQ3RS+7L3GryBLvW Usq6akhpXa1WHtyxXkqnPfs7ddjSWs4bf5fWRqxb806vDhzqzziAoXcJKK3Qbg3hXZ0u 0tFqhu28w8QPANmOpUShkFI8xSxfklP4tNRFGYhWLuOLzNTQ3Tax0zMyNiRxQ+Jt3zWh 4Z1XjWh712YfjZRBa2nTkN1K/QrO77/4Xg4i/+RW9Rsb/jgV3FXS9S+8/FOW4noAsGPq p3mL9oHacnF6hQoDb+7eVohVX4tFSqnd8VRZksZMyJf37QkvXevSVrrD8XHToyqaLLu/ I1sA== 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-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=0oTjCM4pRcEppdq3ZDEprnYHTNLO87kE30WuOBuP5VM=; b=VdqIEZr8J8ipUGkbyiryh/CgqkT6U4W4+yLgsLttoOj8p2V0Ch3XBgwQCCOlYmxZGZ nlojAhYtz1ohNmpiaW+V09DwCmo8UUTc7uRnQhvtHctmlgSnhvG6OMw143u4R/sLKJxm 85GFSmySv68r9YcmPhia6Pkr1h+NrbS9r0ms9BlCG0IPCVJMIfyl5k/DT7YBMTGsAPtA msQwXIxYpm4OE2mLLpGC4UUPAyYIm01uScgUF8mG+yMCeGicRY7NcfTLSRru/EN6B/N6 QqUw4GN1/2nXYn6lstDMMhQcPfk//zDirtr8+E6xeHH25RJA2jQGs/G2KG33+saU6TpP vvBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=tXBCPmw1; 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 r5-20020aa7c145000000b004c0862e3d09si3021819edp.138.2023.03.03.06.59.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 06:59:12 -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=@gcc.gnu.org header.s=default header.b=tXBCPmw1; 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 8F8FD3858416 for ; Fri, 3 Mar 2023 14:59:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8F8FD3858416 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677855550; bh=0oTjCM4pRcEppdq3ZDEprnYHTNLO87kE30WuOBuP5VM=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=tXBCPmw1BFIShAiOG2n3vpEZPHiNxLX4sT7u5AiiM3OxlN0Qy/TSrAPSAgTpTdxLV vC9XuuqpHQDyV4C8eYEFnEuVbDcMVYSTVspf7/VeC6k/8POrHk3ufJlHJQqAHHqacM B6JMcJ3ad9et9R3jwiXm/0RJs75lTehAV7SQ6V7A= 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 C7B403858D33 for ; Fri, 3 Mar 2023 14:58:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C7B403858D33 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-373-ZqwPKfwgOb63mmvT7RXUcA-1; Fri, 03 Mar 2023 09:58:25 -0500 X-MC-Unique: ZqwPKfwgOb63mmvT7RXUcA-1 Received: by mail-qv1-f72.google.com with SMTP id e9-20020a0cf749000000b00571f1377451so1560161qvo.2 for ; Fri, 03 Mar 2023 06:58:25 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677855504; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0oTjCM4pRcEppdq3ZDEprnYHTNLO87kE30WuOBuP5VM=; b=BXwUfIE8Nd69iTfs0qldRgRdLI87psky+Bdl/dX/BJIUPwFLIqvN3wz/SpidhaOJsk K7yEBAHlc2UGeufoi4+sqUl+93jNVFF9Hz/VuRX5ooNpHHpTw+edgomDqy1Q8xTkqvzO xJZ6+xzwmSraXbDzh7mni/eSCzGjrURJ+uUVFZ0bhI0EHsHPhxkbbLTf7B1IqU6ilT1v EIAHkAExav6RthqvW2NOTpcU/+q5jn9D+raI2ywxNhwI3jqyt0foQ/0f3uI7RjWS2nXa FSFfcq3wfqBopg8FayiQkf83FcS1Dr11ouG90RFonrPEtNJ2bWlu5X9m93WlmFj7NvsO 40Fg== X-Gm-Message-State: AO0yUKWVD09m+Ju+xWKYWsfOoqi9bLmiDlpUzLMh1nVhaYBgn9mBzJZ+ YZvVcVvI6Uw/8W4nWY4SPcKFk6ft87xPOX0yuGjLp76pj6aVOo9zd943EPvGdrmg2okRWTUnNOt twgfJp+/2Kur2GoTN/O+IoJZFocIS2FC7/LTpnIjVWHu5aJ5huWl7CIGGTjOYrFLW3Zi1splE X-Received: by 2002:ac8:5f07:0:b0:3bf:d5e4:308c with SMTP id x7-20020ac85f07000000b003bfd5e4308cmr3229935qta.46.1677855504184; Fri, 03 Mar 2023 06:58:24 -0800 (PST) X-Received: by 2002:ac8:5f07:0:b0:3bf:d5e4:308c with SMTP id x7-20020ac85f07000000b003bfd5e4308cmr3229907qta.46.1677855503846; Fri, 03 Mar 2023 06:58:23 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id j26-20020ac874da000000b003b82489d8acsm1867450qtr.21.2023.03.03.06.58.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 06:58:23 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: thinko in extract_local_specs [PR108998] Date: Fri, 3 Mar 2023 09:58:21 -0500 Message-Id: <20230303145821.1081489-1-ppalka@redhat.com> X-Mailer: git-send-email 2.40.0.rc0.57.g454dfcbddf MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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 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: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka 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?1759359064325793643?= X-GMAIL-MSGID: =?utf-8?q?1759359064325793643?= In order to fix PR100295, r13-4730-g18499b9f848707 attempted to make extract_local_specs walk the given pattern twice, ignoring unevaluated operands the first time around so that we prefer to process a local specialization in an evaluated context if it appears in one (we process a local specialization once even if it appears multiple times in the pattern). But there's a thinko in the patch, namely that we don't actually walk the pattern twice, because we reuse the visited set for the second walk (to avoid processing a local specialization twice), and the root node (and any nodes leading up to an unevaluated operand) is considered visited already. So the patch effectively made extract_local_specs ignore unevaluated operands altogether, which this testcase demonstrates isn't quite safe (extract_local_specs never sees 'aa' and we don't save its local specialization, so we later try to specialize 'aa' on the spot with the args {{int},{42}} which causes us to nonsensically substitute its auto with 42.) This patch fixes this by walking only the trees we skipped over during the first walk the second time around. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/12? PR c++/108998 gcc/cp/ChangeLog: * pt.cc (el_data::skipped_trees): New data member. (extract_locals_r): Push to skipped_trees any unevaluated contexts that we skipped over. (extract_local_specs): During the second walk, consider only the trees in skipped_trees. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/lambda-generic11.C: New test. --- gcc/cp/pt.cc | 10 +++++++++- gcc/testsuite/g++.dg/cpp2a/lambda-generic11.C | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-generic11.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index ba1b3027513..85136df1730 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -13052,6 +13052,8 @@ public: tsubst_flags_t complain; /* True iff we don't want to walk into unevaluated contexts. */ bool skip_unevaluated_operands = false; + /* The unevaluated contexts that we avoided walking. */ + auto_vec skipped_trees; el_data (tsubst_flags_t c) : extra (NULL_TREE), complain (c) {} @@ -13066,6 +13068,7 @@ extract_locals_r (tree *tp, int *walk_subtrees, void *data_) if (data.skip_unevaluated_operands && unevaluated_p (TREE_CODE (*tp))) { + data.skipped_trees.safe_push (*tp); *walk_subtrees = 0; return NULL_TREE; } @@ -13168,8 +13171,13 @@ extract_local_specs (tree pattern, tsubst_flags_t complain) context). */ data.skip_unevaluated_operands = true; cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited); + /* Now walk the unevaluated contexts we skipped the first time around. */ data.skip_unevaluated_operands = false; - cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited); + for (tree t : data.skipped_trees) + { + data.visited.remove (t); + cp_walk_tree (&t, extract_locals_r, &data, &data.visited); + } return data.extra; } diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-generic11.C b/gcc/testsuite/g++.dg/cpp2a/lambda-generic11.C new file mode 100644 index 00000000000..418650699e3 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-generic11.C @@ -0,0 +1,13 @@ +// PR c++/108999 +// { dg-do compile { target c++20 } } + +template +void ice(T a) { + auto aa = a; + auto lambda = []() { + if constexpr (sizeof(aa) + I != 42) {} + }; + lambda.template operator()<0>(); +} + +template void ice(int);