From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 9FEAE3858D32 for ; Sun, 5 Feb 2023 19:30:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9FEAE3858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675625408; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BRt4w88lXhIMm8oUVgcVM0E4Z2fw8zdM4ccRFDOYEF0=; b=CwhmChBtrbfI57NqV4hGzdm+qAy/zpWWiJ9Os+NGX8XW70n3HsEjnIA+dMvQR3CvDK9GUu mjLWo0Zo01l05J8YRv76UUyRSA+zhY7fZS3oBv/yN538oiSGHVQ63+5Gf8u6XVZNcahDS8 3xSB47iJzK5jZU3znhwKowkkxYU9zeI= Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-433-B4Ok8Cw9Ol2mGmD2InalPw-1; Sun, 05 Feb 2023 14:30:06 -0500 X-MC-Unique: B4Ok8Cw9Ol2mGmD2InalPw-1 Received: by mail-pj1-f70.google.com with SMTP id r17-20020a17090aa09100b0021903e75f14so3635519pjp.9 for ; Sun, 05 Feb 2023 11:30:06 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BRt4w88lXhIMm8oUVgcVM0E4Z2fw8zdM4ccRFDOYEF0=; b=qzNeH4XKqCn97+Qx68Z+PXOpcmphWS4B4cAh+oYGixN+s3WasiKmQ73Gzft3h/C5Y3 I8U5EKR/XJAYpAa7OjoPeiZ+pDEyStGfsUjAh962oLuAFvxrZ6IuMgp3JB6BhAxNuD9C CTsKlZYKjYn3UumgImbeVtC+u3UpC2Gv4krIyrXXmxXbQPpP0ikVzeByBI/TIExo+p2s i+iL/b3kh1D3PWLgw95q5wYfdVvCWXhkqQ12BruyuNbOLKSIgFC0dWsLNryGmZZ7VLdX IDmjccq4IUVNp3c6ZzJZwNasKfcS0i0l0t+/9mKklLaQEvmeTD1QDM8YXI0baJI43Ida XxJQ== X-Gm-Message-State: AO0yUKU4YzjxVP/VI0nLaioHVNNlAsHLfRwRlAwX2amCd5WLpmq51Fkq hy15sGuNyFoIhRqbUQBGWy5YgTTnFo9I2o6DPM3HR+VJONWTqulSFwP1IDzAkFkeWe/ufm18sFJ 4MLxSnkqfP56I9EsEew== X-Received: by 2002:a17:902:e14a:b0:189:cef2:88e3 with SMTP id d10-20020a170902e14a00b00189cef288e3mr13089979pla.57.1675625405299; Sun, 05 Feb 2023 11:30:05 -0800 (PST) X-Google-Smtp-Source: AK7set/PB0dWxnU8iwM3x1jgGc3fMMek5/XBbAnp1epXEApG1Fvd4vP0FlXgZEAG/9VMn7jH32Z8wg== X-Received: by 2002:a17:902:e14a:b0:189:cef2:88e3 with SMTP id d10-20020a170902e14a00b00189cef288e3mr13089965pla.57.1675625404745; Sun, 05 Feb 2023 11:30:04 -0800 (PST) Received: from [192.168.0.67] (75-172-69-110.tukw.qwest.net. [75.172.69.110]) by smtp.gmail.com with ESMTPSA id a5-20020a170902710500b001743ba85d39sm5213338pll.110.2023.02.05.11.30.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 05 Feb 2023 11:30:04 -0800 (PST) Message-ID: <9d767b0a-d848-0c08-c1d4-690fc78f3160@redhat.com> Date: Sun, 5 Feb 2023 11:30:02 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH] c++: equivalence of non-dependent calls [PR107461] To: Patrick Palka Cc: gcc-patches@gcc.gnu.org References: <20230204203126.782976-1-ppalka@redhat.com> <3f14f800-4b00-9215-c249-1c7f19d43560@redhat.com> <9b958e6f-6f8b-9183-5277-39f83871dd9d@idea> <0e0bc838-466e-20ea-4523-a075be14ef76@redhat.com> <03f75252-861b-528e-df42-6a88adddb388@redhat.com> <645edbe6-7853-acc1-607d-946c8fba6ea9@idea> From: Jason Merrill In-Reply-To: <645edbe6-7853-acc1-607d-946c8fba6ea9@idea> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00,BODY_8BITS,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,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 List-Id: 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() and f(). >>>>>> >>>>>>    * In the third testcase, cp_tree_equal incorrectly returns true for >>>>>>      f() and f() which causes us to conflate the >>>>>> two >>>>>>      dependent specializations A()(U()))> and >>>>>>      A()(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) : 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) : 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() and f(). > > * In the third testcase, cp_tree_equal incorrectly returns true for > f() and f() which causes us to conflate the two > dependent specializations A()(U()))> and > A()(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) : 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) : 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 decltype(T() + f(0)) g(); // #1 > + > +char f(int); > +template decltype(T() + f(0)) g(); // #2, distinct from #1 > + > +int main() { > + g(); // { 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 T f(); > +template decltype(T() + f()) g(); // #1 > +template decltype(T() + f()) g(); // #2, distinct from #1 > + > +int main() { > + g(); // { 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 T f(); > + > +template struct A { }; > + > +template struct B { > + template()(U()))>> > + static void g(U); > +}; > + > +int main() { > + B b; > + B::g(0); // { dg-bogus "no match" } > +}