Message ID | 20230204203126.782976-1-ppalka@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> 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 02FF0385482C for <patchwork@sourceware.org>; Sat, 4 Feb 2023 20:34:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 02FF0385482C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675542865; bh=OtSrfe2aeKd+WaHwaWf5AapfJ7I/UBJ+OVhRsVRsLcY=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=JKdeBDgIBR54huV+xDQx9+waVuFNdn4p+Hi/FDhzB4YfB5/ByPqc3LDKpCDHdM+01 eNSJtqBcnjT1vtGc7Bx+GaOOFaNM0Cc2G2dnG8SETN69SiOY/htEknw0KX7muXOIHq WCElda/aNKSe/rIgzgr8lNuTpoxZqizfWX0pSXxI= 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 A3C33385B510 for <gcc-patches@gcc.gnu.org>; Sat, 4 Feb 2023 20:33:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A3C33385B510 Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-131-ei6-D7SWOfG3XmOkO6hNsw-1; Sat, 04 Feb 2023 15:31:30 -0500 X-MC-Unique: ei6-D7SWOfG3XmOkO6hNsw-1 Received: by mail-qt1-f197.google.com with SMTP id c16-20020ac85190000000b003b841d1118aso4351712qtn.17 for <gcc-patches@gcc.gnu.org>; Sat, 04 Feb 2023 12:31:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=OtSrfe2aeKd+WaHwaWf5AapfJ7I/UBJ+OVhRsVRsLcY=; b=R6GMVTdfp7jZnYPbxTjDAQWVneN5YbaVroNzM2bDnplt4I1LI5sCJOb4YHMCzDvMcV VaT12sHGQ2rDJH0BCfxvvAl5Zm7D41wGnCBZ3HIZwlrO0c3ulVkDvsYJ57mDJ3Z6JETG CSrqoD08BTGzOpVG7EG2tSSZlbH9Vx8TBZxIFOy0F8GNKpql+8I3E96nVdtRtvDMnRcL gxJIxniZgdfLk70QNvLkyGwyMKUV85x/8U+NhmM6iPbtgeJO1kjkKpfdl7bDI6pEkKH7 Xi4rqluN+XsZ80ARjcpRHkDSEK/b1PRG/q+SKDKSKI5M9I71yYsicaQEmTHjwFvcKCaB lXpw== X-Gm-Message-State: AO0yUKVNvv/RW9ye0WWXKtCBJ6xfz0O0DWALku+E4kBMM6w+RKXBMT6c 9Tm5rOK5dxSSwjGNrUk+eI0ZKJYIaXQj4ywvv7DhAZHbqoHp+3H8ilsanW9kasgSfTstOEwapqM FB5nL8LksdzaRE2OEuH9W3eMrLlikvOi0ZbJMDQCwbsnlKArQ/nWer3vdhsGdsJn8oi8= X-Received: by 2002:a0c:c3c5:0:b0:56b:524:72cb with SMTP id p5-20020a0cc3c5000000b0056b052472cbmr4420451qvi.18.1675542688964; Sat, 04 Feb 2023 12:31:28 -0800 (PST) X-Google-Smtp-Source: AK7set+HHEyll7br0cBMDqnDcifgj+fDCprh+CKNaD64vnZQWcsSPFpw0Alo7CR+A9Ji1YYV3rDtCw== X-Received: by 2002:a0c:c3c5:0:b0:56b:524:72cb with SMTP id p5-20020a0cc3c5000000b0056b052472cbmr4420430qvi.18.1675542688550; Sat, 04 Feb 2023 12:31:28 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id bs11-20020a05620a470b00b0071b1fe18746sm4304193qkb.63.2023.02.04.12.31.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Feb 2023 12:31:27 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka <ppalka@redhat.com> Subject: [PATCH] c++: equivalence of non-dependent calls [PR107461] Date: Sat, 4 Feb 2023 15:31:26 -0500 Message-Id: <20230204203126.782976-1-ppalka@redhat.com> X-Mailer: git-send-email 2.39.1.388.g2fc9e9ca3c MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Patrick Palka via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Patrick Palka <ppalka@redhat.com> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
c++: equivalence of non-dependent calls [PR107461]
|
|
Commit Message
Patrick Palka
Feb. 4, 2023, 8:31 p.m. UTC
After r13-5684-g59e0376f607805 the (pruned) callee of a non-dependent CALL_EXPR is a bare FUNCTION_DECL rather than ADDR_EXPR of FUNCTION_DECL. This innocent change revealed that cp_tree_equal doesn't first check dependentness of a CALL_EXPR before treating the callee as a dependent name, which manifests as us incorrectly accepting the first two testcases below and rejecting the third: * In the first testcase, cp_tree_equal incorrectly returns true for the two non-dependent CALL_EXPRs f(0) and f(0) (whose CALL_EXPR_FN are different FUNCTION_DECLs) and so we treat #2 as a redeclaration of #1. * Same issue in the second testcase, for f<int*>() and f<char>(). * In the third testcase, cp_tree_equal incorrectly returns true for f<int>() and f<void(*)(int)>() which causes us to conflate the two dependent specializations A<decltype(f<int>()(U()))> and A<decltype(f<void(*)(int)>()(U()))>, leading to a bogus error. This patch fixes this by making called_fns_equal treat two callees as dependent names only if the CALL_EXPRs in question are dependent. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/12? Patch generated with -w to ignore noisy whitespace changes. PR c++/107461 gcc/cp/ChangeLog: * pt.cc (iterative_hash_template_arg) <case CALL_EXPR>: Treat the callee as a dependent name only if the CALL_EXPR is dependent. * tree.cc (called_fns_equal): Take two CALL_EXPRs instead of CALL_EXPR_FNs thereof. As above. (cp_tree_equal) <case CALL_EXPR>: Adjust call to called_fns_equal. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/overload5.C: New test. * g++.dg/cpp0x/overload5a.C: New test. * g++.dg/cpp0x/overload6.C: New test. --- gcc/cp/pt.cc | 1 + gcc/cp/tree.cc | 33 ++++++++++++++----------- gcc/testsuite/g++.dg/cpp0x/overload5.C | 12 +++++++++ gcc/testsuite/g++.dg/cpp0x/overload5a.C | 10 ++++++++ gcc/testsuite/g++.dg/cpp0x/overload6.C | 16 ++++++++++++ 5 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5a.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload6.C
Comments
On 2/4/23 15:31, Patrick Palka wrote: > After r13-5684-g59e0376f607805 the (pruned) callee of a non-dependent > CALL_EXPR is a bare FUNCTION_DECL rather than ADDR_EXPR of FUNCTION_DECL. > This innocent change revealed that cp_tree_equal doesn't first check > dependentness of a CALL_EXPR before treating the callee as a dependent > name, which manifests as us incorrectly accepting the first two > testcases below and rejecting the third: > > * In the first testcase, cp_tree_equal incorrectly returns true for > the two non-dependent CALL_EXPRs f(0) and f(0) (whose CALL_EXPR_FN > are different FUNCTION_DECLs) and so we treat #2 as a redeclaration > of #1. > > * Same issue in the second testcase, for f<int*>() and f<char>(). > > * In the third testcase, cp_tree_equal incorrectly returns true for > f<int>() and f<void(*)(int)>() which causes us to conflate the two > dependent specializations A<decltype(f<int>()(U()))> and > A<decltype(f<void(*)(int)>()(U()))>, leading to a bogus error. > > This patch fixes this by making called_fns_equal treat two callees as > dependent names only if the CALL_EXPRs in question are dependent. > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk/12? Patch generated with -w to ignore noisy whitespace changes. > > PR c++/107461 > > gcc/cp/ChangeLog: > > * pt.cc (iterative_hash_template_arg) <case CALL_EXPR>: Treat > the callee as a dependent name only if the CALL_EXPR is > dependent. > * tree.cc (called_fns_equal): Take two CALL_EXPRs instead of > CALL_EXPR_FNs thereof. As above. > (cp_tree_equal) <case CALL_EXPR>: Adjust call to called_fns_equal. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp0x/overload5.C: New test. > * g++.dg/cpp0x/overload5a.C: New test. > * g++.dg/cpp0x/overload6.C: New test. > --- > gcc/cp/pt.cc | 1 + > gcc/cp/tree.cc | 33 ++++++++++++++----------- > gcc/testsuite/g++.dg/cpp0x/overload5.C | 12 +++++++++ > gcc/testsuite/g++.dg/cpp0x/overload5a.C | 10 ++++++++ > gcc/testsuite/g++.dg/cpp0x/overload6.C | 16 ++++++++++++ > 5 files changed, 58 insertions(+), 14 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5.C > create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5a.C > create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload6.C > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > index 255332dc0c1..c9360240cd2 100644 > --- a/gcc/cp/pt.cc > +++ b/gcc/cp/pt.cc > @@ -1841,6 +1841,7 @@ iterative_hash_template_arg (tree arg, hashval_t val) > case CALL_EXPR: > { > tree fn = CALL_EXPR_FN (arg); > + if (TREE_TYPE (arg) == NULL_TREE) How about changing dependent_name to take the CALL_EXPR rather than the CALL_EXPR_FN? That would mean some changes to write_expression to move the dependent_name handling into the CALL_EXPR handling, but that doesn't seem like a bad thing. Other callers seem like a trivial change. > if (tree name = dependent_name (fn)) > { > if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) > diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc > index c1da868732b..3a57e71b76e 100644 > --- a/gcc/cp/tree.cc > +++ b/gcc/cp/tree.cc > @@ -3870,16 +3870,21 @@ decl_internal_context_p (const_tree decl) > return !TREE_PUBLIC (decl); > } > > -/* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two > - CALL_EXPRS. Return whether they are equivalent. */ > +/* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs. > + Return whether their CALL_EXPR_FNs are equivalent. */ > > static bool > called_fns_equal (tree t1, tree t2) > +{ > + tree fn1 = CALL_EXPR_FN (t1); > + tree fn2 = CALL_EXPR_FN (t2); > + if (TREE_TYPE (t1) == NULL_TREE > + && TREE_TYPE (t2) == NULL_TREE) > { > /* Core 1321: dependent names are equivalent even if the overload sets > are different. But do compare explicit template arguments. */ > - tree name1 = dependent_name (t1); > - tree name2 = dependent_name (t2); > + tree name1 = dependent_name (fn1); > + tree name2 = dependent_name (fn2); > if (name1 || name2) > { > tree targs1 = NULL_TREE, targs2 = NULL_TREE; > @@ -3891,19 +3896,19 @@ called_fns_equal (tree t1, tree t2) > of whether the function was named with a qualified- or unqualified-id. > Until that's fixed, check that we aren't looking at overload sets from > different scopes. */ > - if (is_overloaded_fn (t1) && is_overloaded_fn (t2) > - && (DECL_CONTEXT (get_first_fn (t1)) > - != DECL_CONTEXT (get_first_fn (t2)))) > + if (is_overloaded_fn (fn1) && is_overloaded_fn (fn2) > + && (DECL_CONTEXT (get_first_fn (fn1)) > + != DECL_CONTEXT (get_first_fn (fn2)))) > return false; > > - if (TREE_CODE (t1) == TEMPLATE_ID_EXPR) > - targs1 = TREE_OPERAND (t1, 1); > - if (TREE_CODE (t2) == TEMPLATE_ID_EXPR) > - targs2 = TREE_OPERAND (t2, 1); > + if (TREE_CODE (fn1) == TEMPLATE_ID_EXPR) > + targs1 = TREE_OPERAND (fn1, 1); > + if (TREE_CODE (fn2) == TEMPLATE_ID_EXPR) > + targs2 = TREE_OPERAND (fn2, 1); > return cp_tree_equal (targs1, targs2); > } > - else > - return cp_tree_equal (t1, t2); > + } > + return cp_tree_equal (fn1, fn2); > } > > bool comparing_override_contracts; > @@ -4037,7 +4042,7 @@ cp_tree_equal (tree t1, tree t2) > if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2)) > return false; > > - if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) > + if (!called_fns_equal (t1, t2)) > return false; > > call_expr_arg_iterator iter1, iter2; > diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5.C b/gcc/testsuite/g++.dg/cpp0x/overload5.C > new file mode 100644 > index 00000000000..e05b1594f51 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/overload5.C > @@ -0,0 +1,12 @@ > +// PR c++/107461 > +// { dg-do compile { target c++11 } } > + > +int f(...); > +template<class T> decltype(T() + f(0)) g(); // #1 > + > +char f(int); > +template<class T> decltype(T() + f(0)) g(); // #2, distinct from #1 > + > +int main() { > + g<int>(); // { dg-error "ambiguous" } > +} > diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5a.C b/gcc/testsuite/g++.dg/cpp0x/overload5a.C > new file mode 100644 > index 00000000000..037114f199c > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/overload5a.C > @@ -0,0 +1,10 @@ > +// PR c++/107461 > +// { dg-do compile { target c++11 } } > + > +template<class T> T f(); > +template<class T> decltype(T() + f<int*>()) g(); // #1 > +template<class T> decltype(T() + f<char>()) g(); // #2, distinct from #1 > + > +int main() { > + g<int>(); // { dg-error "ambiguous" } > +} > diff --git a/gcc/testsuite/g++.dg/cpp0x/overload6.C b/gcc/testsuite/g++.dg/cpp0x/overload6.C > new file mode 100644 > index 00000000000..1fbee0501de > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/overload6.C > @@ -0,0 +1,16 @@ > +// PR c++/107461 > +// { dg-do compile { target c++11 } } > + > +template<class T> T f(); > + > +template<class> struct A { }; > + > +template<class T> struct B { > + template<class U, class = A<decltype(f<T>()(U()))>> > + static void g(U); > +}; > + > +int main() { > + B<int> b; > + B<void(*)(int)>::g(0); // { dg-bogus "no match" } > +}
On Sat, 4 Feb 2023, Jason Merrill wrote: > On 2/4/23 15:31, Patrick Palka wrote: > > After r13-5684-g59e0376f607805 the (pruned) callee of a non-dependent > > CALL_EXPR is a bare FUNCTION_DECL rather than ADDR_EXPR of FUNCTION_DECL. > > This innocent change revealed that cp_tree_equal doesn't first check > > dependentness of a CALL_EXPR before treating the callee as a dependent > > name, which manifests as us incorrectly accepting the first two > > testcases below and rejecting the third: > > > > * In the first testcase, cp_tree_equal incorrectly returns true for > > the two non-dependent CALL_EXPRs f(0) and f(0) (whose CALL_EXPR_FN > > are different FUNCTION_DECLs) and so we treat #2 as a redeclaration > > of #1. > > > > * Same issue in the second testcase, for f<int*>() and f<char>(). > > > > * In the third testcase, cp_tree_equal incorrectly returns true for > > f<int>() and f<void(*)(int)>() which causes us to conflate the two > > dependent specializations A<decltype(f<int>()(U()))> and > > A<decltype(f<void(*)(int)>()(U()))>, leading to a bogus error. > > > > This patch fixes this by making called_fns_equal treat two callees as > > dependent names only if the CALL_EXPRs in question are dependent. > > > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > > trunk/12? Patch generated with -w to ignore noisy whitespace changes. > > > > PR c++/107461 > > > > gcc/cp/ChangeLog: > > > > * pt.cc (iterative_hash_template_arg) <case CALL_EXPR>: Treat > > the callee as a dependent name only if the CALL_EXPR is > > dependent. > > * tree.cc (called_fns_equal): Take two CALL_EXPRs instead of > > CALL_EXPR_FNs thereof. As above. > > (cp_tree_equal) <case CALL_EXPR>: Adjust call to called_fns_equal. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp0x/overload5.C: New test. > > * g++.dg/cpp0x/overload5a.C: New test. > > * g++.dg/cpp0x/overload6.C: New test. > > --- > > gcc/cp/pt.cc | 1 + > > gcc/cp/tree.cc | 33 ++++++++++++++----------- > > gcc/testsuite/g++.dg/cpp0x/overload5.C | 12 +++++++++ > > gcc/testsuite/g++.dg/cpp0x/overload5a.C | 10 ++++++++ > > gcc/testsuite/g++.dg/cpp0x/overload6.C | 16 ++++++++++++ > > 5 files changed, 58 insertions(+), 14 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5.C > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5a.C > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload6.C > > > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > > index 255332dc0c1..c9360240cd2 100644 > > --- a/gcc/cp/pt.cc > > +++ b/gcc/cp/pt.cc > > @@ -1841,6 +1841,7 @@ iterative_hash_template_arg (tree arg, hashval_t val) > > case CALL_EXPR: > > { > > tree fn = CALL_EXPR_FN (arg); > > + if (TREE_TYPE (arg) == NULL_TREE) > > How about changing dependent_name to take the CALL_EXPR rather than the > CALL_EXPR_FN? That would mean some changes to write_expression to move the > dependent_name handling into the CALL_EXPR handling, but that doesn't seem > like a bad thing. Other callers seem like a trivial change. Indeed changing dependent_name seems best, but I'm worried about such a refactoring to write_expression causing unintended mangling changes at this stage. Because it seems the CALL_EXPR case of write_expression isn't the user of the dependent_name branch of write_expression, at least according to the following patch which causes us to ICE on mangle{37,57,58,76}.C: diff --git a/gcc/cp/mangle.cc b/gcc/cp/mangle.cc index f2cda3be2cf..700857f8f3c 100644 --- a/gcc/cp/mangle.cc +++ b/gcc/cp/mangle.cc @@ -3450,6 +3450,7 @@ write_expression (tree expr) } else if (dependent_name (expr)) { + gcc_unreachable (); tree name = dependent_name (expr); if (IDENTIFIER_ANY_OP_P (name)) { @@ -3554,7 +3555,19 @@ write_expression (tree expr) && type_dependent_expression_p_push (expr)) fn = OVL_NAME (fn); - write_expression (fn); + if (tree name = dependent_name (fn)) + { + if (IDENTIFIER_ANY_OP_P (name)) + { + if (abi_version_at_least (16)) + write_string ("on"); + if (abi_warn_or_compat_version_crosses (16)) + G.need_abi_warning = 1; + } + write_unqualified_id (name); + } + else + write_expression (fn); } for (i = 0; i < call_expr_nargs (expr); ++i) And since the CALL_EXPR case of write_expression looks through an ADDR_EXPR callee before recursing, IIUC the refactoring would need to make dependent_name look through an ADDR_EXPR callee as well, which seems like a desirable/correct change but I'm worried that might have unintended consequences as well. > > > if (tree name = dependent_name (fn)) > > { > > if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) > > diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc > > index c1da868732b..3a57e71b76e 100644 > > --- a/gcc/cp/tree.cc > > +++ b/gcc/cp/tree.cc > > @@ -3870,16 +3870,21 @@ decl_internal_context_p (const_tree decl) > > return !TREE_PUBLIC (decl); > > } > > -/* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two > > - CALL_EXPRS. Return whether they are equivalent. */ > > +/* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs. > > + Return whether their CALL_EXPR_FNs are equivalent. */ > > static bool > > called_fns_equal (tree t1, tree t2) > > +{ > > + tree fn1 = CALL_EXPR_FN (t1); > > + tree fn2 = CALL_EXPR_FN (t2); > > + if (TREE_TYPE (t1) == NULL_TREE > > + && TREE_TYPE (t2) == NULL_TREE) > > { > > /* Core 1321: dependent names are equivalent even if the overload > > sets > > are different. But do compare explicit template arguments. */ > > - tree name1 = dependent_name (t1); > > - tree name2 = dependent_name (t2); > > + tree name1 = dependent_name (fn1); > > + tree name2 = dependent_name (fn2); > > if (name1 || name2) > > { > > tree targs1 = NULL_TREE, targs2 = NULL_TREE; > > @@ -3891,19 +3896,19 @@ called_fns_equal (tree t1, tree t2) > > of whether the function was named with a qualified- or > > unqualified-id. > > Until that's fixed, check that we aren't looking at overload sets > > from > > different scopes. */ > > - if (is_overloaded_fn (t1) && is_overloaded_fn (t2) > > - && (DECL_CONTEXT (get_first_fn (t1)) > > - != DECL_CONTEXT (get_first_fn (t2)))) > > + if (is_overloaded_fn (fn1) && is_overloaded_fn (fn2) > > + && (DECL_CONTEXT (get_first_fn (fn1)) > > + != DECL_CONTEXT (get_first_fn (fn2)))) > > return false; > > - if (TREE_CODE (t1) == TEMPLATE_ID_EXPR) > > - targs1 = TREE_OPERAND (t1, 1); > > - if (TREE_CODE (t2) == TEMPLATE_ID_EXPR) > > - targs2 = TREE_OPERAND (t2, 1); > > + if (TREE_CODE (fn1) == TEMPLATE_ID_EXPR) > > + targs1 = TREE_OPERAND (fn1, 1); > > + if (TREE_CODE (fn2) == TEMPLATE_ID_EXPR) > > + targs2 = TREE_OPERAND (fn2, 1); > > return cp_tree_equal (targs1, targs2); > > } > > - else > > - return cp_tree_equal (t1, t2); > > + } > > + return cp_tree_equal (fn1, fn2); > > } > > bool comparing_override_contracts; > > @@ -4037,7 +4042,7 @@ cp_tree_equal (tree t1, tree t2) > > if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2)) > > return false; > > - if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) > > + if (!called_fns_equal (t1, t2)) > > return false; > > call_expr_arg_iterator iter1, iter2; > > diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5.C > > b/gcc/testsuite/g++.dg/cpp0x/overload5.C > > new file mode 100644 > > index 00000000000..e05b1594f51 > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp0x/overload5.C > > @@ -0,0 +1,12 @@ > > +// PR c++/107461 > > +// { dg-do compile { target c++11 } } > > + > > +int f(...); > > +template<class T> decltype(T() + f(0)) g(); // #1 > > + > > +char f(int); > > +template<class T> decltype(T() + f(0)) g(); // #2, distinct from #1 > > + > > +int main() { > > + g<int>(); // { dg-error "ambiguous" } > > +} > > diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5a.C > > b/gcc/testsuite/g++.dg/cpp0x/overload5a.C > > new file mode 100644 > > index 00000000000..037114f199c > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp0x/overload5a.C > > @@ -0,0 +1,10 @@ > > +// PR c++/107461 > > +// { dg-do compile { target c++11 } } > > + > > +template<class T> T f(); > > +template<class T> decltype(T() + f<int*>()) g(); // #1 > > +template<class T> decltype(T() + f<char>()) g(); // #2, distinct from #1 > > + > > +int main() { > > + g<int>(); // { dg-error "ambiguous" } > > +} > > diff --git a/gcc/testsuite/g++.dg/cpp0x/overload6.C > > b/gcc/testsuite/g++.dg/cpp0x/overload6.C > > new file mode 100644 > > index 00000000000..1fbee0501de > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp0x/overload6.C > > @@ -0,0 +1,16 @@ > > +// PR c++/107461 > > +// { dg-do compile { target c++11 } } > > + > > +template<class T> T f(); > > + > > +template<class> struct A { }; > > + > > +template<class T> struct B { > > + template<class U, class = A<decltype(f<T>()(U()))>> > > + static void g(U); > > +}; > > + > > +int main() { > > + B<int> b; > > + B<void(*)(int)>::g(0); // { dg-bogus "no match" } > > +} > >
On 2/4/23 20:08, Patrick Palka wrote: > On Sat, 4 Feb 2023, Jason Merrill wrote: > >> On 2/4/23 15:31, Patrick Palka wrote: >>> After r13-5684-g59e0376f607805 the (pruned) callee of a non-dependent >>> CALL_EXPR is a bare FUNCTION_DECL rather than ADDR_EXPR of FUNCTION_DECL. >>> This innocent change revealed that cp_tree_equal doesn't first check >>> dependentness of a CALL_EXPR before treating the callee as a dependent >>> name, which manifests as us incorrectly accepting the first two >>> testcases below and rejecting the third: >>> >>> * In the first testcase, cp_tree_equal incorrectly returns true for >>> the two non-dependent CALL_EXPRs f(0) and f(0) (whose CALL_EXPR_FN >>> are different FUNCTION_DECLs) and so we treat #2 as a redeclaration >>> of #1. >>> >>> * Same issue in the second testcase, for f<int*>() and f<char>(). >>> >>> * In the third testcase, cp_tree_equal incorrectly returns true for >>> f<int>() and f<void(*)(int)>() which causes us to conflate the two >>> dependent specializations A<decltype(f<int>()(U()))> and >>> A<decltype(f<void(*)(int)>()(U()))>, leading to a bogus error. >>> >>> This patch fixes this by making called_fns_equal treat two callees as >>> dependent names only if the CALL_EXPRs in question are dependent. >>> >>> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for >>> trunk/12? Patch generated with -w to ignore noisy whitespace changes. >>> >>> PR c++/107461 >>> >>> gcc/cp/ChangeLog: >>> >>> * pt.cc (iterative_hash_template_arg) <case CALL_EXPR>: Treat >>> the callee as a dependent name only if the CALL_EXPR is >>> dependent. >>> * tree.cc (called_fns_equal): Take two CALL_EXPRs instead of >>> CALL_EXPR_FNs thereof. As above. >>> (cp_tree_equal) <case CALL_EXPR>: Adjust call to called_fns_equal. >>> >>> gcc/testsuite/ChangeLog: >>> >>> * g++.dg/cpp0x/overload5.C: New test. >>> * g++.dg/cpp0x/overload5a.C: New test. >>> * g++.dg/cpp0x/overload6.C: New test. >>> --- >>> gcc/cp/pt.cc | 1 + >>> gcc/cp/tree.cc | 33 ++++++++++++++----------- >>> gcc/testsuite/g++.dg/cpp0x/overload5.C | 12 +++++++++ >>> gcc/testsuite/g++.dg/cpp0x/overload5a.C | 10 ++++++++ >>> gcc/testsuite/g++.dg/cpp0x/overload6.C | 16 ++++++++++++ >>> 5 files changed, 58 insertions(+), 14 deletions(-) >>> create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5.C >>> create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5a.C >>> create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload6.C >>> >>> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc >>> index 255332dc0c1..c9360240cd2 100644 >>> --- a/gcc/cp/pt.cc >>> +++ b/gcc/cp/pt.cc >>> @@ -1841,6 +1841,7 @@ iterative_hash_template_arg (tree arg, hashval_t val) >>> case CALL_EXPR: >>> { >>> tree fn = CALL_EXPR_FN (arg); >>> + if (TREE_TYPE (arg) == NULL_TREE) >> >> How about changing dependent_name to take the CALL_EXPR rather than the >> CALL_EXPR_FN? That would mean some changes to write_expression to move the >> dependent_name handling into the CALL_EXPR handling, but that doesn't seem >> like a bad thing. Other callers seem like a trivial change. > > Indeed changing dependent_name seems best, but I'm worried about such a > refactoring to write_expression causing unintended mangling changes at > this stage. Because it seems the CALL_EXPR case of write_expression > isn't the user of the dependent_name branch of write_expression, at > least according to the following patch which causes us to ICE on > mangle{37,57,58,76}.C: Yeah, I tried the same thing. Maybe for GCC 13 better to add a new function rather than change the current one. > diff --git a/gcc/cp/mangle.cc b/gcc/cp/mangle.cc > index f2cda3be2cf..700857f8f3c 100644 > --- a/gcc/cp/mangle.cc > +++ b/gcc/cp/mangle.cc > @@ -3450,6 +3450,7 @@ write_expression (tree expr) > } > else if (dependent_name (expr)) > { > + gcc_unreachable (); > tree name = dependent_name (expr); > if (IDENTIFIER_ANY_OP_P (name)) > { > @@ -3554,7 +3555,19 @@ write_expression (tree expr) > && type_dependent_expression_p_push (expr)) > fn = OVL_NAME (fn); > > - write_expression (fn); > + if (tree name = dependent_name (fn)) > + { > + if (IDENTIFIER_ANY_OP_P (name)) > + { > + if (abi_version_at_least (16)) > + write_string ("on"); > + if (abi_warn_or_compat_version_crosses (16)) > + G.need_abi_warning = 1; > + } > + write_unqualified_id (name); > + } > + else > + write_expression (fn); > } > > for (i = 0; i < call_expr_nargs (expr); ++i) > > And since the CALL_EXPR case of write_expression looks through an > ADDR_EXPR callee before recursing, IIUC the refactoring would need to > make dependent_name look through an ADDR_EXPR callee as well, which > seems like a desirable/correct change but I'm worried that might have > unintended consequences as well. > >> >>> if (tree name = dependent_name (fn)) >>> { >>> if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) >>> diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc >>> index c1da868732b..3a57e71b76e 100644 >>> --- a/gcc/cp/tree.cc >>> +++ b/gcc/cp/tree.cc >>> @@ -3870,16 +3870,21 @@ decl_internal_context_p (const_tree decl) >>> return !TREE_PUBLIC (decl); >>> } >>> -/* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two >>> - CALL_EXPRS. Return whether they are equivalent. */ >>> +/* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs. >>> + Return whether their CALL_EXPR_FNs are equivalent. */ >>> static bool >>> called_fns_equal (tree t1, tree t2) >>> +{ >>> + tree fn1 = CALL_EXPR_FN (t1); >>> + tree fn2 = CALL_EXPR_FN (t2); >>> + if (TREE_TYPE (t1) == NULL_TREE >>> + && TREE_TYPE (t2) == NULL_TREE) >>> { >>> /* Core 1321: dependent names are equivalent even if the overload >>> sets >>> are different. But do compare explicit template arguments. */ >>> - tree name1 = dependent_name (t1); >>> - tree name2 = dependent_name (t2); >>> + tree name1 = dependent_name (fn1); >>> + tree name2 = dependent_name (fn2); >>> if (name1 || name2) >>> { >>> tree targs1 = NULL_TREE, targs2 = NULL_TREE; >>> @@ -3891,19 +3896,19 @@ called_fns_equal (tree t1, tree t2) >>> of whether the function was named with a qualified- or >>> unqualified-id. >>> Until that's fixed, check that we aren't looking at overload sets >>> from >>> different scopes. */ >>> - if (is_overloaded_fn (t1) && is_overloaded_fn (t2) >>> - && (DECL_CONTEXT (get_first_fn (t1)) >>> - != DECL_CONTEXT (get_first_fn (t2)))) >>> + if (is_overloaded_fn (fn1) && is_overloaded_fn (fn2) >>> + && (DECL_CONTEXT (get_first_fn (fn1)) >>> + != DECL_CONTEXT (get_first_fn (fn2)))) >>> return false; >>> - if (TREE_CODE (t1) == TEMPLATE_ID_EXPR) >>> - targs1 = TREE_OPERAND (t1, 1); >>> - if (TREE_CODE (t2) == TEMPLATE_ID_EXPR) >>> - targs2 = TREE_OPERAND (t2, 1); >>> + if (TREE_CODE (fn1) == TEMPLATE_ID_EXPR) >>> + targs1 = TREE_OPERAND (fn1, 1); >>> + if (TREE_CODE (fn2) == TEMPLATE_ID_EXPR) >>> + targs2 = TREE_OPERAND (fn2, 1); >>> return cp_tree_equal (targs1, targs2); >>> } >>> - else >>> - return cp_tree_equal (t1, t2); >>> + } >>> + return cp_tree_equal (fn1, fn2); >>> } >>> bool comparing_override_contracts; >>> @@ -4037,7 +4042,7 @@ cp_tree_equal (tree t1, tree t2) >>> if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2)) >>> return false; >>> - if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) >>> + if (!called_fns_equal (t1, t2)) >>> return false; >>> call_expr_arg_iterator iter1, iter2; >>> diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5.C >>> b/gcc/testsuite/g++.dg/cpp0x/overload5.C >>> new file mode 100644 >>> index 00000000000..e05b1594f51 >>> --- /dev/null >>> +++ b/gcc/testsuite/g++.dg/cpp0x/overload5.C >>> @@ -0,0 +1,12 @@ >>> +// PR c++/107461 >>> +// { dg-do compile { target c++11 } } >>> + >>> +int f(...); >>> +template<class T> decltype(T() + f(0)) g(); // #1 >>> + >>> +char f(int); >>> +template<class T> decltype(T() + f(0)) g(); // #2, distinct from #1 >>> + >>> +int main() { >>> + g<int>(); // { dg-error "ambiguous" } >>> +} >>> diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5a.C >>> b/gcc/testsuite/g++.dg/cpp0x/overload5a.C >>> new file mode 100644 >>> index 00000000000..037114f199c >>> --- /dev/null >>> +++ b/gcc/testsuite/g++.dg/cpp0x/overload5a.C >>> @@ -0,0 +1,10 @@ >>> +// PR c++/107461 >>> +// { dg-do compile { target c++11 } } >>> + >>> +template<class T> T f(); >>> +template<class T> decltype(T() + f<int*>()) g(); // #1 >>> +template<class T> decltype(T() + f<char>()) g(); // #2, distinct from #1 >>> + >>> +int main() { >>> + g<int>(); // { dg-error "ambiguous" } >>> +} >>> diff --git a/gcc/testsuite/g++.dg/cpp0x/overload6.C >>> b/gcc/testsuite/g++.dg/cpp0x/overload6.C >>> new file mode 100644 >>> index 00000000000..1fbee0501de >>> --- /dev/null >>> +++ b/gcc/testsuite/g++.dg/cpp0x/overload6.C >>> @@ -0,0 +1,16 @@ >>> +// PR c++/107461 >>> +// { dg-do compile { target c++11 } } >>> + >>> +template<class T> T f(); >>> + >>> +template<class> struct A { }; >>> + >>> +template<class T> struct B { >>> + template<class U, class = A<decltype(f<T>()(U()))>> >>> + static void g(U); >>> +}; >>> + >>> +int main() { >>> + B<int> b; >>> + B<void(*)(int)>::g(0); // { dg-bogus "no match" } >>> +} >> >> >
On 2/4/23 20:41, Jason Merrill wrote: > On 2/4/23 20:08, Patrick Palka wrote: >> On Sat, 4 Feb 2023, Jason Merrill wrote: >> >>> On 2/4/23 15:31, Patrick Palka wrote: >>>> After r13-5684-g59e0376f607805 the (pruned) callee of a non-dependent >>>> CALL_EXPR is a bare FUNCTION_DECL rather than ADDR_EXPR of >>>> FUNCTION_DECL. >>>> This innocent change revealed that cp_tree_equal doesn't first check >>>> dependentness of a CALL_EXPR before treating the callee as a dependent >>>> name, which manifests as us incorrectly accepting the first two >>>> testcases below and rejecting the third: >>>> >>>> * In the first testcase, cp_tree_equal incorrectly returns true for >>>> the two non-dependent CALL_EXPRs f(0) and f(0) (whose CALL_EXPR_FN >>>> are different FUNCTION_DECLs) and so we treat #2 as a >>>> redeclaration >>>> of #1. >>>> >>>> * Same issue in the second testcase, for f<int*>() and f<char>(). >>>> >>>> * In the third testcase, cp_tree_equal incorrectly returns true for >>>> f<int>() and f<void(*)(int)>() which causes us to conflate the two >>>> dependent specializations A<decltype(f<int>()(U()))> and >>>> A<decltype(f<void(*)(int)>()(U()))>, leading to a bogus error. >>>> >>>> This patch fixes this by making called_fns_equal treat two callees as >>>> dependent names only if the CALL_EXPRs in question are dependent. >>>> >>>> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK >>>> for >>>> trunk/12? Patch generated with -w to ignore noisy whitespace changes. >>>> >>>> PR c++/107461 >>>> >>>> gcc/cp/ChangeLog: >>>> >>>> * pt.cc (iterative_hash_template_arg) <case CALL_EXPR>: Treat >>>> the callee as a dependent name only if the CALL_EXPR is >>>> dependent. >>>> * tree.cc (called_fns_equal): Take two CALL_EXPRs instead of >>>> CALL_EXPR_FNs thereof. As above. >>>> (cp_tree_equal) <case CALL_EXPR>: Adjust call to called_fns_equal. >>>> >>>> gcc/testsuite/ChangeLog: >>>> >>>> * g++.dg/cpp0x/overload5.C: New test. >>>> * g++.dg/cpp0x/overload5a.C: New test. >>>> * g++.dg/cpp0x/overload6.C: New test. >>>> --- >>>> gcc/cp/pt.cc | 1 + >>>> gcc/cp/tree.cc | 33 >>>> ++++++++++++++----------- >>>> gcc/testsuite/g++.dg/cpp0x/overload5.C | 12 +++++++++ >>>> gcc/testsuite/g++.dg/cpp0x/overload5a.C | 10 ++++++++ >>>> gcc/testsuite/g++.dg/cpp0x/overload6.C | 16 ++++++++++++ >>>> 5 files changed, 58 insertions(+), 14 deletions(-) >>>> create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5.C >>>> create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5a.C >>>> create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload6.C >>>> >>>> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc >>>> index 255332dc0c1..c9360240cd2 100644 >>>> --- a/gcc/cp/pt.cc >>>> +++ b/gcc/cp/pt.cc >>>> @@ -1841,6 +1841,7 @@ iterative_hash_template_arg (tree arg, >>>> hashval_t val) >>>> case CALL_EXPR: >>>> { >>>> tree fn = CALL_EXPR_FN (arg); >>>> + if (TREE_TYPE (arg) == NULL_TREE) >>> >>> How about changing dependent_name to take the CALL_EXPR rather than the >>> CALL_EXPR_FN? That would mean some changes to write_expression to >>> move the >>> dependent_name handling into the CALL_EXPR handling, but that doesn't >>> seem >>> like a bad thing. Other callers seem like a trivial change. >> >> Indeed changing dependent_name seems best, but I'm worried about such a >> refactoring to write_expression causing unintended mangling changes at >> this stage. Because it seems the CALL_EXPR case of write_expression >> isn't the user of the dependent_name branch of write_expression, at >> least according to the following patch which causes us to ICE on >> mangle{37,57,58,76}.C: > > Yeah, I tried the same thing. Maybe for GCC 13 better to add a new > function rather than change the current one. mangle76 seems like a bug where we're producing (and testing for) the wrong mangling -- mangling (*this). that doesn't exist in the source. clang gets it right. mangle5{7,8} has the right mangling, we're just using dependent_name to mangle function names that aren't dependent names (because they're template arguments in both cases, and qualified in the latter). >> diff --git a/gcc/cp/mangle.cc b/gcc/cp/mangle.cc >> index f2cda3be2cf..700857f8f3c 100644 >> --- a/gcc/cp/mangle.cc >> +++ b/gcc/cp/mangle.cc >> @@ -3450,6 +3450,7 @@ write_expression (tree expr) >> } >> else if (dependent_name (expr)) >> { >> + gcc_unreachable (); >> tree name = dependent_name (expr); >> if (IDENTIFIER_ANY_OP_P (name)) >> { >> @@ -3554,7 +3555,19 @@ write_expression (tree expr) >> && type_dependent_expression_p_push (expr)) >> fn = OVL_NAME (fn); >> - write_expression (fn); >> + if (tree name = dependent_name (fn)) >> + { >> + if (IDENTIFIER_ANY_OP_P (name)) >> + { >> + if (abi_version_at_least (16)) >> + write_string ("on"); >> + if (abi_warn_or_compat_version_crosses (16)) >> + G.need_abi_warning = 1; >> + } >> + write_unqualified_id (name); >> + } >> + else >> + write_expression (fn); >> } >> for (i = 0; i < call_expr_nargs (expr); ++i) >> >> And since the CALL_EXPR case of write_expression looks through an >> ADDR_EXPR callee before recursing, IIUC the refactoring would need to >> make dependent_name look through an ADDR_EXPR callee as well, which >> seems like a desirable/correct change but I'm worried that might have >> unintended consequences as well. >> >>> >>>> if (tree name = dependent_name (fn)) >>>> { >>>> if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) >>>> diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc >>>> index c1da868732b..3a57e71b76e 100644 >>>> --- a/gcc/cp/tree.cc >>>> +++ b/gcc/cp/tree.cc >>>> @@ -3870,16 +3870,21 @@ decl_internal_context_p (const_tree decl) >>>> return !TREE_PUBLIC (decl); >>>> } >>>> -/* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs >>>> of two >>>> - CALL_EXPRS. Return whether they are equivalent. */ >>>> +/* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs. >>>> + Return whether their CALL_EXPR_FNs are equivalent. */ >>>> static bool >>>> called_fns_equal (tree t1, tree t2) >>>> +{ >>>> + tree fn1 = CALL_EXPR_FN (t1); >>>> + tree fn2 = CALL_EXPR_FN (t2); >>>> + if (TREE_TYPE (t1) == NULL_TREE >>>> + && TREE_TYPE (t2) == NULL_TREE) >>>> { >>>> /* Core 1321: dependent names are equivalent even if the >>>> overload >>>> sets >>>> are different. But do compare explicit template arguments. */ >>>> - tree name1 = dependent_name (t1); >>>> - tree name2 = dependent_name (t2); >>>> + tree name1 = dependent_name (fn1); >>>> + tree name2 = dependent_name (fn2); >>>> if (name1 || name2) >>>> { >>>> tree targs1 = NULL_TREE, targs2 = NULL_TREE; >>>> @@ -3891,19 +3896,19 @@ called_fns_equal (tree t1, tree t2) >>>> of whether the function was named with a qualified- or >>>> unqualified-id. >>>> Until that's fixed, check that we aren't looking at >>>> overload sets >>>> from >>>> different scopes. */ >>>> - if (is_overloaded_fn (t1) && is_overloaded_fn (t2) >>>> - && (DECL_CONTEXT (get_first_fn (t1)) >>>> - != DECL_CONTEXT (get_first_fn (t2)))) >>>> + if (is_overloaded_fn (fn1) && is_overloaded_fn (fn2) >>>> + && (DECL_CONTEXT (get_first_fn (fn1)) >>>> + != DECL_CONTEXT (get_first_fn (fn2)))) >>>> return false; >>>> - if (TREE_CODE (t1) == TEMPLATE_ID_EXPR) >>>> - targs1 = TREE_OPERAND (t1, 1); >>>> - if (TREE_CODE (t2) == TEMPLATE_ID_EXPR) >>>> - targs2 = TREE_OPERAND (t2, 1); >>>> + if (TREE_CODE (fn1) == TEMPLATE_ID_EXPR) >>>> + targs1 = TREE_OPERAND (fn1, 1); >>>> + if (TREE_CODE (fn2) == TEMPLATE_ID_EXPR) >>>> + targs2 = TREE_OPERAND (fn2, 1); >>>> return cp_tree_equal (targs1, targs2); >>>> } >>>> - else >>>> - return cp_tree_equal (t1, t2); >>>> + } >>>> + return cp_tree_equal (fn1, fn2); >>>> } >>>> bool comparing_override_contracts; >>>> @@ -4037,7 +4042,7 @@ cp_tree_equal (tree t1, tree t2) >>>> if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2)) >>>> return false; >>>> - if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) >>>> + if (!called_fns_equal (t1, t2)) >>>> return false; >>>> call_expr_arg_iterator iter1, iter2; >>>> diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5.C >>>> b/gcc/testsuite/g++.dg/cpp0x/overload5.C >>>> new file mode 100644 >>>> index 00000000000..e05b1594f51 >>>> --- /dev/null >>>> +++ b/gcc/testsuite/g++.dg/cpp0x/overload5.C >>>> @@ -0,0 +1,12 @@ >>>> +// PR c++/107461 >>>> +// { dg-do compile { target c++11 } } >>>> + >>>> +int f(...); >>>> +template<class T> decltype(T() + f(0)) g(); // #1 >>>> + >>>> +char f(int); >>>> +template<class T> decltype(T() + f(0)) g(); // #2, distinct from #1 >>>> + >>>> +int main() { >>>> + g<int>(); // { dg-error "ambiguous" } >>>> +} >>>> diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5a.C >>>> b/gcc/testsuite/g++.dg/cpp0x/overload5a.C >>>> new file mode 100644 >>>> index 00000000000..037114f199c >>>> --- /dev/null >>>> +++ b/gcc/testsuite/g++.dg/cpp0x/overload5a.C >>>> @@ -0,0 +1,10 @@ >>>> +// PR c++/107461 >>>> +// { dg-do compile { target c++11 } } >>>> + >>>> +template<class T> T f(); >>>> +template<class T> decltype(T() + f<int*>()) g(); // #1 >>>> +template<class T> decltype(T() + f<char>()) g(); // #2, distinct >>>> from #1 >>>> + >>>> +int main() { >>>> + g<int>(); // { dg-error "ambiguous" } >>>> +} >>>> diff --git a/gcc/testsuite/g++.dg/cpp0x/overload6.C >>>> b/gcc/testsuite/g++.dg/cpp0x/overload6.C >>>> new file mode 100644 >>>> index 00000000000..1fbee0501de >>>> --- /dev/null >>>> +++ b/gcc/testsuite/g++.dg/cpp0x/overload6.C >>>> @@ -0,0 +1,16 @@ >>>> +// PR c++/107461 >>>> +// { dg-do compile { target c++11 } } >>>> + >>>> +template<class T> T f(); >>>> + >>>> +template<class> struct A { }; >>>> + >>>> +template<class T> struct B { >>>> + template<class U, class = A<decltype(f<T>()(U()))>> >>>> + static void g(U); >>>> +}; >>>> + >>>> +int main() { >>>> + B<int> b; >>>> + B<void(*)(int)>::g(0); // { dg-bogus "no match" } >>>> +} >>> >>> >> >
On Sat, 4 Feb 2023, Jason Merrill wrote: > On 2/4/23 20:41, Jason Merrill wrote: > > On 2/4/23 20:08, Patrick Palka wrote: > > > On Sat, 4 Feb 2023, Jason Merrill wrote: > > > > > > > On 2/4/23 15:31, Patrick Palka wrote: > > > > > After r13-5684-g59e0376f607805 the (pruned) callee of a non-dependent > > > > > CALL_EXPR is a bare FUNCTION_DECL rather than ADDR_EXPR of > > > > > FUNCTION_DECL. > > > > > This innocent change revealed that cp_tree_equal doesn't first check > > > > > dependentness of a CALL_EXPR before treating the callee as a dependent > > > > > name, which manifests as us incorrectly accepting the first two > > > > > testcases below and rejecting the third: > > > > > > > > > > * In the first testcase, cp_tree_equal incorrectly returns true for > > > > > the two non-dependent CALL_EXPRs f(0) and f(0) (whose > > > > > CALL_EXPR_FN > > > > > are different FUNCTION_DECLs) and so we treat #2 as a > > > > > redeclaration > > > > > of #1. > > > > > > > > > > * Same issue in the second testcase, for f<int*>() and f<char>(). > > > > > > > > > > * In the third testcase, cp_tree_equal incorrectly returns true for > > > > > f<int>() and f<void(*)(int)>() which causes us to conflate the > > > > > two > > > > > dependent specializations A<decltype(f<int>()(U()))> and > > > > > A<decltype(f<void(*)(int)>()(U()))>, leading to a bogus error. > > > > > > > > > > This patch fixes this by making called_fns_equal treat two callees as > > > > > dependent names only if the CALL_EXPRs in question are dependent. > > > > > > > > > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK > > > > > for > > > > > trunk/12? Patch generated with -w to ignore noisy whitespace changes. > > > > > > > > > > PR c++/107461 > > > > > > > > > > gcc/cp/ChangeLog: > > > > > > > > > > * pt.cc (iterative_hash_template_arg) <case CALL_EXPR>: Treat > > > > > the callee as a dependent name only if the CALL_EXPR is > > > > > dependent. > > > > > * tree.cc (called_fns_equal): Take two CALL_EXPRs instead of > > > > > CALL_EXPR_FNs thereof. As above. > > > > > (cp_tree_equal) <case CALL_EXPR>: Adjust call to called_fns_equal. > > > > > > > > > > gcc/testsuite/ChangeLog: > > > > > > > > > > * g++.dg/cpp0x/overload5.C: New test. > > > > > * g++.dg/cpp0x/overload5a.C: New test. > > > > > * g++.dg/cpp0x/overload6.C: New test. > > > > > --- > > > > > gcc/cp/pt.cc | 1 + > > > > > gcc/cp/tree.cc | 33 > > > > > ++++++++++++++----------- > > > > > gcc/testsuite/g++.dg/cpp0x/overload5.C | 12 +++++++++ > > > > > gcc/testsuite/g++.dg/cpp0x/overload5a.C | 10 ++++++++ > > > > > gcc/testsuite/g++.dg/cpp0x/overload6.C | 16 ++++++++++++ > > > > > 5 files changed, 58 insertions(+), 14 deletions(-) > > > > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5.C > > > > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5a.C > > > > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload6.C > > > > > > > > > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > > > > > index 255332dc0c1..c9360240cd2 100644 > > > > > --- a/gcc/cp/pt.cc > > > > > +++ b/gcc/cp/pt.cc > > > > > @@ -1841,6 +1841,7 @@ iterative_hash_template_arg (tree arg, hashval_t > > > > > val) > > > > > case CALL_EXPR: > > > > > { > > > > > tree fn = CALL_EXPR_FN (arg); > > > > > + if (TREE_TYPE (arg) == NULL_TREE) > > > > > > > > How about changing dependent_name to take the CALL_EXPR rather than the > > > > CALL_EXPR_FN? That would mean some changes to write_expression to move > > > > the > > > > dependent_name handling into the CALL_EXPR handling, but that doesn't > > > > seem > > > > like a bad thing. Other callers seem like a trivial change. > > > > > > Indeed changing dependent_name seems best, but I'm worried about such a > > > refactoring to write_expression causing unintended mangling changes at > > > this stage. Because it seems the CALL_EXPR case of write_expression > > > isn't the user of the dependent_name branch of write_expression, at > > > least according to the following patch which causes us to ICE on > > > mangle{37,57,58,76}.C: > > > > Yeah, I tried the same thing. Maybe for GCC 13 better to add a new function > > rather than change the current one. Sounds good, like so? Only regtested so far. Full bootstrap and regtest running on x86_64-pc-linux-gnu. -- >8 -- Subject: [PATCH] c++: equivalence of non-dependent calls [PR107461] After r13-5684-g59e0376f607805 the (pruned) callee of a non-dependent CALL_EXPR is a bare FUNCTION_DECL rather than ADDR_EXPR of FUNCTION_DECL. This innocent change revealed that cp_tree_equal doesn't first check dependentness of a CALL_EXPR before treating a FUNCTION_DECL callee as a dependent name, which manifests as us incorrectly accepting the first two testcases below and rejecting the third: * In the first testcase, cp_tree_equal incorrectly returns true for the two non-dependent CALL_EXPRs f(0) and f(0) (whose CALL_EXPR_FN are different FUNCTION_DECLs) and so we treat #2 as a redeclaration of #1. * Same issue in the second testcase, for f<int*>() and f<char>(). * In the third testcase, cp_tree_equal incorrectly returns true for f<int>() and f<void(*)(int)>() which causes us to conflate the two dependent specializations A<decltype(f<int>()(U()))> and A<decltype(f<void(*)(int)>()(U()))>, leading to a bogus error. This patch fixes this by making called_fns_equal treat two callees as dependent names only if the overall CALL_EXPRs are dependent, via a new convenience function call_expr_dependent_name that is like dependent_name but also checks dependence of the overall CALL_EXPR. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/12? Patch generated with -w to ignore noisy whitespace changes. PR c++/107461 gcc/cp/ChangeLog: * cp-tree.h (call_expr_dependent_name): Declare. * pt.cc (iterative_hash_template_arg) <case CALL_EXPR>: Use call_expr_dependent_name instead of dependent_name. * tree.cc (call_expr_dependent_name): Define. (called_fns_equal): Adjust to take two CALL_EXPRs instead of CALL_EXPR_FNs thereof. Use call_expr_dependent_name instead of dependent_name. (cp_tree_equal) <case CALL_EXPR>: Adjust call to called_fns_equal. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/overload5.C: New test. * g++.dg/cpp0x/overload5a.C: New test. * g++.dg/cpp0x/overload6.C: New test. --- gcc/cp/cp-tree.h | 1 + gcc/cp/pt.cc | 2 +- gcc/cp/tree.cc | 24 +++++++++++++++++++----- gcc/testsuite/g++.dg/cpp0x/overload5.C | 12 ++++++++++++ gcc/testsuite/g++.dg/cpp0x/overload5a.C | 10 ++++++++++ gcc/testsuite/g++.dg/cpp0x/overload6.C | 16 ++++++++++++++++ 6 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5a.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload6.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 00b2bffc85c..ef601182d4b 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7902,6 +7902,7 @@ extern tree lookup_maybe_add (tree fns, tree lookup, extern int is_overloaded_fn (tree) ATTRIBUTE_PURE; extern bool really_overloaded_fn (tree) ATTRIBUTE_PURE; extern tree dependent_name (tree); +extern tree call_expr_dependent_name (tree); extern tree maybe_get_fns (tree) ATTRIBUTE_PURE; extern tree get_fns (tree) ATTRIBUTE_PURE; extern tree get_first_fn (tree) ATTRIBUTE_PURE; diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 255332dc0c1..9f3fc1fa089 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -1841,7 +1841,7 @@ iterative_hash_template_arg (tree arg, hashval_t val) case CALL_EXPR: { tree fn = CALL_EXPR_FN (arg); - if (tree name = dependent_name (fn)) + if (tree name = call_expr_dependent_name (arg)) { if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val); diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index c1da868732b..880bd4f9bcf 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -2608,6 +2608,18 @@ dependent_name (tree x) return NULL_TREE; } +/* Like dependent_name, but takes the overall CALL_EXPR and checks its + dependence. */ + +tree +call_expr_dependent_name (tree x) +{ + if (TREE_TYPE (x) != NULL_TREE) + /* X isn't dependent, so its callee isn't a dependent name. */ + return NULL_TREE; + return dependent_name (CALL_EXPR_FN (x)); +} + /* Returns true iff X is an expression for an overloaded function whose type cannot be known without performing overload resolution. */ @@ -3870,16 +3882,18 @@ decl_internal_context_p (const_tree decl) return !TREE_PUBLIC (decl); } -/* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two - CALL_EXPRS. Return whether they are equivalent. */ +/* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs. + Return whether their CALL_EXPR_FNs are equivalent. */ static bool called_fns_equal (tree t1, tree t2) { /* Core 1321: dependent names are equivalent even if the overload sets are different. But do compare explicit template arguments. */ - tree name1 = dependent_name (t1); - tree name2 = dependent_name (t2); + tree name1 = call_expr_dependent_name (t1); + tree name2 = call_expr_dependent_name (t2); + t1 = CALL_EXPR_FN (t1); + t2 = CALL_EXPR_FN (t2); if (name1 || name2) { tree targs1 = NULL_TREE, targs2 = NULL_TREE; @@ -4037,7 +4051,7 @@ cp_tree_equal (tree t1, tree t2) if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2)) return false; - if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) + if (!called_fns_equal (t1, t2)) return false; call_expr_arg_iterator iter1, iter2; diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5.C b/gcc/testsuite/g++.dg/cpp0x/overload5.C new file mode 100644 index 00000000000..e05b1594f51 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/overload5.C @@ -0,0 +1,12 @@ +// PR c++/107461 +// { dg-do compile { target c++11 } } + +int f(...); +template<class T> decltype(T() + f(0)) g(); // #1 + +char f(int); +template<class T> decltype(T() + f(0)) g(); // #2, distinct from #1 + +int main() { + g<int>(); // { dg-error "ambiguous" } +} diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5a.C b/gcc/testsuite/g++.dg/cpp0x/overload5a.C new file mode 100644 index 00000000000..037114f199c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/overload5a.C @@ -0,0 +1,10 @@ +// PR c++/107461 +// { dg-do compile { target c++11 } } + +template<class T> T f(); +template<class T> decltype(T() + f<int*>()) g(); // #1 +template<class T> decltype(T() + f<char>()) g(); // #2, distinct from #1 + +int main() { + g<int>(); // { dg-error "ambiguous" } +} diff --git a/gcc/testsuite/g++.dg/cpp0x/overload6.C b/gcc/testsuite/g++.dg/cpp0x/overload6.C new file mode 100644 index 00000000000..1fbee0501de --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/overload6.C @@ -0,0 +1,16 @@ +// PR c++/107461 +// { dg-do compile { target c++11 } } + +template<class T> T f(); + +template<class> struct A { }; + +template<class T> struct B { + template<class U, class = A<decltype(f<T>()(U()))>> + static void g(U); +}; + +int main() { + B<int> b; + B<void(*)(int)>::g(0); // { dg-bogus "no match" } +}
On 2/5/23 09:57, Patrick Palka wrote: > On Sat, 4 Feb 2023, Jason Merrill wrote: > >> On 2/4/23 20:41, Jason Merrill wrote: >>> On 2/4/23 20:08, Patrick Palka wrote: >>>> On Sat, 4 Feb 2023, Jason Merrill wrote: >>>> >>>>> On 2/4/23 15:31, Patrick Palka wrote: >>>>>> After r13-5684-g59e0376f607805 the (pruned) callee of a non-dependent >>>>>> CALL_EXPR is a bare FUNCTION_DECL rather than ADDR_EXPR of >>>>>> FUNCTION_DECL. >>>>>> This innocent change revealed that cp_tree_equal doesn't first check >>>>>> dependentness of a CALL_EXPR before treating the callee as a dependent >>>>>> name, which manifests as us incorrectly accepting the first two >>>>>> testcases below and rejecting the third: >>>>>> >>>>>> * In the first testcase, cp_tree_equal incorrectly returns true for >>>>>> the two non-dependent CALL_EXPRs f(0) and f(0) (whose >>>>>> CALL_EXPR_FN >>>>>> are different FUNCTION_DECLs) and so we treat #2 as a >>>>>> redeclaration >>>>>> of #1. >>>>>> >>>>>> * Same issue in the second testcase, for f<int*>() and f<char>(). >>>>>> >>>>>> * In the third testcase, cp_tree_equal incorrectly returns true for >>>>>> f<int>() and f<void(*)(int)>() which causes us to conflate the >>>>>> two >>>>>> dependent specializations A<decltype(f<int>()(U()))> and >>>>>> A<decltype(f<void(*)(int)>()(U()))>, leading to a bogus error. >>>>>> >>>>>> This patch fixes this by making called_fns_equal treat two callees as >>>>>> dependent names only if the CALL_EXPRs in question are dependent. >>>>>> >>>>>> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK >>>>>> for >>>>>> trunk/12? Patch generated with -w to ignore noisy whitespace changes. >>>>>> >>>>>> PR c++/107461 >>>>>> >>>>>> gcc/cp/ChangeLog: >>>>>> >>>>>> * pt.cc (iterative_hash_template_arg) <case CALL_EXPR>: Treat >>>>>> the callee as a dependent name only if the CALL_EXPR is >>>>>> dependent. >>>>>> * tree.cc (called_fns_equal): Take two CALL_EXPRs instead of >>>>>> CALL_EXPR_FNs thereof. As above. >>>>>> (cp_tree_equal) <case CALL_EXPR>: Adjust call to called_fns_equal. >>>>>> >>>>>> gcc/testsuite/ChangeLog: >>>>>> >>>>>> * g++.dg/cpp0x/overload5.C: New test. >>>>>> * g++.dg/cpp0x/overload5a.C: New test. >>>>>> * g++.dg/cpp0x/overload6.C: New test. >>>>>> --- >>>>>> gcc/cp/pt.cc | 1 + >>>>>> gcc/cp/tree.cc | 33 >>>>>> ++++++++++++++----------- >>>>>> gcc/testsuite/g++.dg/cpp0x/overload5.C | 12 +++++++++ >>>>>> gcc/testsuite/g++.dg/cpp0x/overload5a.C | 10 ++++++++ >>>>>> gcc/testsuite/g++.dg/cpp0x/overload6.C | 16 ++++++++++++ >>>>>> 5 files changed, 58 insertions(+), 14 deletions(-) >>>>>> create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5.C >>>>>> create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5a.C >>>>>> create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload6.C >>>>>> >>>>>> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc >>>>>> index 255332dc0c1..c9360240cd2 100644 >>>>>> --- a/gcc/cp/pt.cc >>>>>> +++ b/gcc/cp/pt.cc >>>>>> @@ -1841,6 +1841,7 @@ iterative_hash_template_arg (tree arg, hashval_t >>>>>> val) >>>>>> case CALL_EXPR: >>>>>> { >>>>>> tree fn = CALL_EXPR_FN (arg); >>>>>> + if (TREE_TYPE (arg) == NULL_TREE) >>>>> >>>>> How about changing dependent_name to take the CALL_EXPR rather than the >>>>> CALL_EXPR_FN? That would mean some changes to write_expression to move >>>>> the >>>>> dependent_name handling into the CALL_EXPR handling, but that doesn't >>>>> seem >>>>> like a bad thing. Other callers seem like a trivial change. >>>> >>>> Indeed changing dependent_name seems best, but I'm worried about such a >>>> refactoring to write_expression causing unintended mangling changes at >>>> this stage. Because it seems the CALL_EXPR case of write_expression >>>> isn't the user of the dependent_name branch of write_expression, at >>>> least according to the following patch which causes us to ICE on >>>> mangle{37,57,58,76}.C: >>> >>> Yeah, I tried the same thing. Maybe for GCC 13 better to add a new function >>> rather than change the current one. > > Sounds good, like so? Only regtested so far. Full bootstrap and > regtest running on x86_64-pc-linux-gnu. > > -- >8 -- > > Subject: [PATCH] c++: equivalence of non-dependent calls [PR107461] > > After r13-5684-g59e0376f607805 the (pruned) callee of a non-dependent > CALL_EXPR is a bare FUNCTION_DECL rather than ADDR_EXPR of FUNCTION_DECL. > This innocent change revealed that cp_tree_equal doesn't first check > dependentness of a CALL_EXPR before treating a FUNCTION_DECL callee as a > dependent name, which manifests as us incorrectly accepting the first > two testcases below and rejecting the third: > > * In the first testcase, cp_tree_equal incorrectly returns true for > the two non-dependent CALL_EXPRs f(0) and f(0) (whose CALL_EXPR_FN > are different FUNCTION_DECLs) and so we treat #2 as a redeclaration > of #1. > > * Same issue in the second testcase, for f<int*>() and f<char>(). > > * In the third testcase, cp_tree_equal incorrectly returns true for > f<int>() and f<void(*)(int)>() which causes us to conflate the two > dependent specializations A<decltype(f<int>()(U()))> and > A<decltype(f<void(*)(int)>()(U()))>, leading to a bogus error. > > This patch fixes this by making called_fns_equal treat two callees as > dependent names only if the overall CALL_EXPRs are dependent, via a new > convenience function call_expr_dependent_name that is like dependent_name > but also checks dependence of the overall CALL_EXPR. > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk/12? Patch generated with -w to ignore noisy whitespace changes. OK, thanks. > PR c++/107461 > > gcc/cp/ChangeLog: > > * cp-tree.h (call_expr_dependent_name): Declare. > * pt.cc (iterative_hash_template_arg) <case CALL_EXPR>: Use > call_expr_dependent_name instead of dependent_name. > * tree.cc (call_expr_dependent_name): Define. > (called_fns_equal): Adjust to take two CALL_EXPRs instead of > CALL_EXPR_FNs thereof. Use call_expr_dependent_name instead > of dependent_name. > (cp_tree_equal) <case CALL_EXPR>: Adjust call to called_fns_equal. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp0x/overload5.C: New test. > * g++.dg/cpp0x/overload5a.C: New test. > * g++.dg/cpp0x/overload6.C: New test. > --- > gcc/cp/cp-tree.h | 1 + > gcc/cp/pt.cc | 2 +- > gcc/cp/tree.cc | 24 +++++++++++++++++++----- > gcc/testsuite/g++.dg/cpp0x/overload5.C | 12 ++++++++++++ > gcc/testsuite/g++.dg/cpp0x/overload5a.C | 10 ++++++++++ > gcc/testsuite/g++.dg/cpp0x/overload6.C | 16 ++++++++++++++++ > 6 files changed, 59 insertions(+), 6 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5.C > create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload5a.C > create mode 100644 gcc/testsuite/g++.dg/cpp0x/overload6.C > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > index 00b2bffc85c..ef601182d4b 100644 > --- a/gcc/cp/cp-tree.h > +++ b/gcc/cp/cp-tree.h > @@ -7902,6 +7902,7 @@ extern tree lookup_maybe_add (tree fns, tree lookup, > extern int is_overloaded_fn (tree) ATTRIBUTE_PURE; > extern bool really_overloaded_fn (tree) ATTRIBUTE_PURE; > extern tree dependent_name (tree); > +extern tree call_expr_dependent_name (tree); > extern tree maybe_get_fns (tree) ATTRIBUTE_PURE; > extern tree get_fns (tree) ATTRIBUTE_PURE; > extern tree get_first_fn (tree) ATTRIBUTE_PURE; > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > index 255332dc0c1..9f3fc1fa089 100644 > --- a/gcc/cp/pt.cc > +++ b/gcc/cp/pt.cc > @@ -1841,7 +1841,7 @@ iterative_hash_template_arg (tree arg, hashval_t val) > case CALL_EXPR: > { > tree fn = CALL_EXPR_FN (arg); > - if (tree name = dependent_name (fn)) > + if (tree name = call_expr_dependent_name (arg)) > { > if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) > val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val); > diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc > index c1da868732b..880bd4f9bcf 100644 > --- a/gcc/cp/tree.cc > +++ b/gcc/cp/tree.cc > @@ -2608,6 +2608,18 @@ dependent_name (tree x) > return NULL_TREE; > } > > +/* Like dependent_name, but takes the overall CALL_EXPR and checks its > + dependence. */ > + > +tree > +call_expr_dependent_name (tree x) > +{ > + if (TREE_TYPE (x) != NULL_TREE) > + /* X isn't dependent, so its callee isn't a dependent name. */ > + return NULL_TREE; > + return dependent_name (CALL_EXPR_FN (x)); > +} > + > /* Returns true iff X is an expression for an overloaded function > whose type cannot be known without performing overload > resolution. */ > @@ -3870,16 +3882,18 @@ decl_internal_context_p (const_tree decl) > return !TREE_PUBLIC (decl); > } > > -/* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two > - CALL_EXPRS. Return whether they are equivalent. */ > +/* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs. > + Return whether their CALL_EXPR_FNs are equivalent. */ > > static bool > called_fns_equal (tree t1, tree t2) > { > /* Core 1321: dependent names are equivalent even if the overload sets > are different. But do compare explicit template arguments. */ > - tree name1 = dependent_name (t1); > - tree name2 = dependent_name (t2); > + tree name1 = call_expr_dependent_name (t1); > + tree name2 = call_expr_dependent_name (t2); > + t1 = CALL_EXPR_FN (t1); > + t2 = CALL_EXPR_FN (t2); > if (name1 || name2) > { > tree targs1 = NULL_TREE, targs2 = NULL_TREE; > @@ -4037,7 +4051,7 @@ cp_tree_equal (tree t1, tree t2) > if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2)) > return false; > > - if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) > + if (!called_fns_equal (t1, t2)) > return false; > > call_expr_arg_iterator iter1, iter2; > diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5.C b/gcc/testsuite/g++.dg/cpp0x/overload5.C > new file mode 100644 > index 00000000000..e05b1594f51 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/overload5.C > @@ -0,0 +1,12 @@ > +// PR c++/107461 > +// { dg-do compile { target c++11 } } > + > +int f(...); > +template<class T> decltype(T() + f(0)) g(); // #1 > + > +char f(int); > +template<class T> decltype(T() + f(0)) g(); // #2, distinct from #1 > + > +int main() { > + g<int>(); // { dg-error "ambiguous" } > +} > diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5a.C b/gcc/testsuite/g++.dg/cpp0x/overload5a.C > new file mode 100644 > index 00000000000..037114f199c > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/overload5a.C > @@ -0,0 +1,10 @@ > +// PR c++/107461 > +// { dg-do compile { target c++11 } } > + > +template<class T> T f(); > +template<class T> decltype(T() + f<int*>()) g(); // #1 > +template<class T> decltype(T() + f<char>()) g(); // #2, distinct from #1 > + > +int main() { > + g<int>(); // { dg-error "ambiguous" } > +} > diff --git a/gcc/testsuite/g++.dg/cpp0x/overload6.C b/gcc/testsuite/g++.dg/cpp0x/overload6.C > new file mode 100644 > index 00000000000..1fbee0501de > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/overload6.C > @@ -0,0 +1,16 @@ > +// PR c++/107461 > +// { dg-do compile { target c++11 } } > + > +template<class T> T f(); > + > +template<class> struct A { }; > + > +template<class T> struct B { > + template<class U, class = A<decltype(f<T>()(U()))>> > + static void g(U); > +}; > + > +int main() { > + B<int> b; > + B<void(*)(int)>::g(0); // { dg-bogus "no match" } > +}
On Sat, Feb 04, 2023 at 06:02:46PM -0800, Jason Merrill via Gcc-patches wrote: > On 2/4/23 20:41, Jason Merrill wrote: > > On 2/4/23 20:08, Patrick Palka wrote: > > > On Sat, 4 Feb 2023, Jason Merrill wrote: > > > > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > > > > > index 255332dc0c1..c9360240cd2 100644 > > > > > --- a/gcc/cp/pt.cc > > > > > +++ b/gcc/cp/pt.cc > > > > > @@ -1841,6 +1841,7 @@ iterative_hash_template_arg (tree arg, > > > > > hashval_t val) > > > > > case CALL_EXPR: > > > > > { > > > > > tree fn = CALL_EXPR_FN (arg); > > > > > + if (TREE_TYPE (arg) == NULL_TREE) > > > > > > > > How about changing dependent_name to take the CALL_EXPR rather than the > > > > CALL_EXPR_FN? That would mean some changes to write_expression > > > > to move the > > > > dependent_name handling into the CALL_EXPR handling, but that > > > > doesn't seem > > > > like a bad thing. Other callers seem like a trivial change. > > > > > > Indeed changing dependent_name seems best, but I'm worried about such a > > > refactoring to write_expression causing unintended mangling changes at > > > this stage. Because it seems the CALL_EXPR case of write_expression > > > isn't the user of the dependent_name branch of write_expression, at > > > least according to the following patch which causes us to ICE on > > > mangle{37,57,58,76}.C: > > > > Yeah, I tried the same thing. Maybe for GCC 13 better to add a new > > function rather than change the current one. > > mangle76 seems like a bug where we're producing (and testing for) the wrong > mangling -- mangling (*this). that doesn't exist in the source. clang gets > it right. Yes, this is https://gcc.gnu.org/PR98756. > mangle5{7,8} has the right mangling, we're just using dependent_name to > mangle function names that aren't dependent names (because they're template > arguments in both cases, and qualified in the latter). Marek
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 255332dc0c1..c9360240cd2 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -1841,6 +1841,7 @@ iterative_hash_template_arg (tree arg, hashval_t val) case CALL_EXPR: { tree fn = CALL_EXPR_FN (arg); + if (TREE_TYPE (arg) == NULL_TREE) if (tree name = dependent_name (fn)) { if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index c1da868732b..3a57e71b76e 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -3870,16 +3870,21 @@ decl_internal_context_p (const_tree decl) return !TREE_PUBLIC (decl); } -/* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two - CALL_EXPRS. Return whether they are equivalent. */ +/* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs. + Return whether their CALL_EXPR_FNs are equivalent. */ static bool called_fns_equal (tree t1, tree t2) +{ + tree fn1 = CALL_EXPR_FN (t1); + tree fn2 = CALL_EXPR_FN (t2); + if (TREE_TYPE (t1) == NULL_TREE + && TREE_TYPE (t2) == NULL_TREE) { /* Core 1321: dependent names are equivalent even if the overload sets are different. But do compare explicit template arguments. */ - tree name1 = dependent_name (t1); - tree name2 = dependent_name (t2); + tree name1 = dependent_name (fn1); + tree name2 = dependent_name (fn2); if (name1 || name2) { tree targs1 = NULL_TREE, targs2 = NULL_TREE; @@ -3891,19 +3896,19 @@ called_fns_equal (tree t1, tree t2) of whether the function was named with a qualified- or unqualified-id. Until that's fixed, check that we aren't looking at overload sets from different scopes. */ - if (is_overloaded_fn (t1) && is_overloaded_fn (t2) - && (DECL_CONTEXT (get_first_fn (t1)) - != DECL_CONTEXT (get_first_fn (t2)))) + if (is_overloaded_fn (fn1) && is_overloaded_fn (fn2) + && (DECL_CONTEXT (get_first_fn (fn1)) + != DECL_CONTEXT (get_first_fn (fn2)))) return false; - if (TREE_CODE (t1) == TEMPLATE_ID_EXPR) - targs1 = TREE_OPERAND (t1, 1); - if (TREE_CODE (t2) == TEMPLATE_ID_EXPR) - targs2 = TREE_OPERAND (t2, 1); + if (TREE_CODE (fn1) == TEMPLATE_ID_EXPR) + targs1 = TREE_OPERAND (fn1, 1); + if (TREE_CODE (fn2) == TEMPLATE_ID_EXPR) + targs2 = TREE_OPERAND (fn2, 1); return cp_tree_equal (targs1, targs2); } - else - return cp_tree_equal (t1, t2); + } + return cp_tree_equal (fn1, fn2); } bool comparing_override_contracts; @@ -4037,7 +4042,7 @@ cp_tree_equal (tree t1, tree t2) if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2)) return false; - if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) + if (!called_fns_equal (t1, t2)) return false; call_expr_arg_iterator iter1, iter2; diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5.C b/gcc/testsuite/g++.dg/cpp0x/overload5.C new file mode 100644 index 00000000000..e05b1594f51 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/overload5.C @@ -0,0 +1,12 @@ +// PR c++/107461 +// { dg-do compile { target c++11 } } + +int f(...); +template<class T> decltype(T() + f(0)) g(); // #1 + +char f(int); +template<class T> decltype(T() + f(0)) g(); // #2, distinct from #1 + +int main() { + g<int>(); // { dg-error "ambiguous" } +} diff --git a/gcc/testsuite/g++.dg/cpp0x/overload5a.C b/gcc/testsuite/g++.dg/cpp0x/overload5a.C new file mode 100644 index 00000000000..037114f199c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/overload5a.C @@ -0,0 +1,10 @@ +// PR c++/107461 +// { dg-do compile { target c++11 } } + +template<class T> T f(); +template<class T> decltype(T() + f<int*>()) g(); // #1 +template<class T> decltype(T() + f<char>()) g(); // #2, distinct from #1 + +int main() { + g<int>(); // { dg-error "ambiguous" } +} diff --git a/gcc/testsuite/g++.dg/cpp0x/overload6.C b/gcc/testsuite/g++.dg/cpp0x/overload6.C new file mode 100644 index 00000000000..1fbee0501de --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/overload6.C @@ -0,0 +1,16 @@ +// PR c++/107461 +// { dg-do compile { target c++11 } } + +template<class T> T f(); + +template<class> struct A { }; + +template<class T> struct B { + template<class U, class = A<decltype(f<T>()(U()))>> + static void g(U); +}; + +int main() { + B<int> b; + B<void(*)(int)>::g(0); // { dg-bogus "no match" } +}