From patchwork Wed Nov 3 16:04:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 47000 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1F3EF3858420 for ; Wed, 3 Nov 2021 16:04:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1F3EF3858420 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1635955494; bh=/7n06ONmeYyd0j46IZtFZFx6Nf+y78seiyEIz314M6I=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ko5riXnlhH3vZqRRjkNOp3fCKqtsK/GmxAgS2HzkqnebkWRv4mzmduYILpQh2vGuK H7wNaEQYsqLfYmcWJZ/bTReQRhTS1132A1geOf0G6P63OZssP2iKDK8l6EohmDRw41 tOvZlO6LtVmKjprwtjPYKJHj2kZxJe0Fn0x67JB4= 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 13F3D3858D28 for ; Wed, 3 Nov 2021 16:04:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 13F3D3858D28 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-554-Mtjm-mleOk6z2bgiYKDFcQ-1; Wed, 03 Nov 2021 12:04:23 -0400 X-MC-Unique: Mtjm-mleOk6z2bgiYKDFcQ-1 Received: by mail-qt1-f198.google.com with SMTP id o14-20020a05622a138e00b002a79da5b350so929499qtk.15 for ; Wed, 03 Nov 2021 09:04:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/7n06ONmeYyd0j46IZtFZFx6Nf+y78seiyEIz314M6I=; b=4GyMtrw39WexMtwLvpZgKFpkSNKPFMMM8MMslwZ3b/HUEShrdRpoyM3Pj21O/DqSgt 3mgTrq2jmrc0XV8ranuhpRwUirror7IJ6Vc2BxwuMl7d+o/dBTML21NQL8f4c/oeNsx7 JBnGGXU7NsB8HU/eZYoyYJsqkS/UlcvaJDmC0Fu9hXzsIxPwGCxZEY4+tapRG7ktDHP+ hcABKRea+XSmI70jHs6yg//jmxGFd0kGQ2jc0fwdi5JlmyZpFf4K9VPxEefaRzuqAuND QXs+DYF8oqD+PYJK79Zh62zKvPOusZsnT1unSKwh8LXG9lXkqbXC/EfTLn04zTYxzOro GnjA== X-Gm-Message-State: AOAM532n1C9/Wo8bMyPcPFn8m9Qn7hOiiA7upetQealj8CgZh0xs0QYX 5r0gUPVckBsPYeVCpSdtzvKAmR1Nr13okuC21xbr9lHR9QflBYuMz+imPNmhqnGwwgW/IL7Reu6 u80LQFTaxa+wsc6rq0MYAFFj4e+eIqxTMX+ub0DMrzYBGv+VtStrKXZqMoFdU6RFyZNU= X-Received: by 2002:a05:622a:1aaa:: with SMTP id s42mr37580013qtc.376.1635955461864; Wed, 03 Nov 2021 09:04:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzg0CEnTNjDonmEKiYVLivhYVMbvGXKKMorVeEThtA2NxMVOv89QJxUcaEa/S+CKYibCJ2F6A== X-Received: by 2002:a05:622a:1aaa:: with SMTP id s42mr37579961qtc.376.1635955461451; Wed, 03 Nov 2021 09:04:21 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id bi38sm1782874qkb.115.2021.11.03.09.04.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Nov 2021 09:04:21 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: unqual lookup performed twice w/ template-id ADL [PR102670] Date: Wed, 3 Nov 2021 12:04:19 -0400 Message-Id: <20211103160419.1472321-1-ppalka@redhat.com> X-Mailer: git-send-email 2.34.0.rc0.19.g0cddd84c9f MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-16.0 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_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Here we're incorrectly performing unqualified lookup of 'adl' again at substitution time for the call adl(t) (for which name lookup at parse time found nothing) which causes us to reject the testcase because the second unqualified lookup finds the later-declared variable template 'adl', leading to confusion. Fixed thusly. The testcase concepts-recursive-sat1.C needed to be adjusted use ADL proper instead of relying on this incorrect behavior. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk and perhaps 11 given it's a C++20 bugfix? PR c++/102670 gcc/cp/ChangeLog: * pt.c (tsubst_copy_and_build) : When looking for an identifier callee in the koenig_p case, also look through TEMPLATE_ID_EXPR. Use tsubst_copy to substitute through the template arguments of the template-id. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-recursive-sat1.C: * g++.dg/cpp2a/fn-template23.C: New test. --- gcc/cp/pt.c | 11 +++++- .../g++.dg/cpp2a/concepts-recursive-sat1.C | 15 +++++--- gcc/testsuite/g++.dg/cpp2a/fn-template23.C | 36 +++++++++++++++++++ 3 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/fn-template23.C diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 66040035b2f..40f84648ed2 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -20256,7 +20256,10 @@ tsubst_copy_and_build (tree t, /*done=*/false, /*address_p=*/false); } - else if (koenig_p && identifier_p (function)) + else if (koenig_p + && (identifier_p (function) + || (TREE_CODE (function) == TEMPLATE_ID_EXPR + && identifier_p (TREE_OPERAND (function, 0))))) { /* Do nothing; calling tsubst_copy_and_build on an identifier would incorrectly perform unqualified lookup again. @@ -20269,6 +20272,12 @@ tsubst_copy_and_build (tree t, FIXME but doing that causes c++/15272, so we need to stop using IDENTIFIER_NODE in that situation. */ qualified_p = false; + + if (TREE_CODE (function) == TEMPLATE_ID_EXPR) + /* Use tsubst_copy to substitute through the template arguments + of the template-id without performing unqualified lookup on + the template name. */ + function = tsubst_copy (function, args, complain, in_decl); } else { diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-recursive-sat1.C b/gcc/testsuite/g++.dg/cpp2a/concepts-recursive-sat1.C index 22696c30d81..4c178b77946 100644 --- a/gcc/testsuite/g++.dg/cpp2a/concepts-recursive-sat1.C +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-recursive-sat1.C @@ -3,16 +3,21 @@ template concept Foo = requires(T t) { foo(t); }; // { dg-error "template instantiation depth" } -template - requires Foo -int foo(T t) +namespace ns { - return foo(t); + struct S { }; + + template + requires Foo + int foo(T t) + { + return foo(t); + } } int main(int, char**) { - return foo<1>(1); + return ns::foo<1>(ns::S{}); } // { dg-prune-output "compilation terminated" } diff --git a/gcc/testsuite/g++.dg/cpp2a/fn-template23.C b/gcc/testsuite/g++.dg/cpp2a/fn-template23.C new file mode 100644 index 00000000000..b85d4c96dab --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/fn-template23.C @@ -0,0 +1,36 @@ +// PR c++/102670 +// { dg-do compile { target c++20 } } + +namespace ns { + struct S { }; + + template + constexpr int adl(const S &) { + return I; + } +} + +namespace redirect { + template + concept can_call_adl = requires(T t) { + adl(t); + }; + + template + struct adl_fn { + template T> + constexpr decltype(auto) operator()(T t) const { + return adl(t); + } + }; + + namespace { + template + constexpr inline adl_fn adl{}; + } +} + +int main() { + static_assert(redirect::can_call_adl); + redirect::adl<3>(ns::S{}); +}