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.133.124]) by sourceware.org (Postfix) with ESMTPS id 6EB5B3858C00 for ; Sun, 5 Feb 2023 02:02:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6EB5B3858C00 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=1675562570; 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=Q6FRhLskamxVJBSD04JqjP9L387hIdPOAhYS4WgLRRA=; b=iHGjyIitD5boxUSNPMkkzAcdM4C+aYNBjzQby2clrvRHJZnVF1jG+TEJKo0N0E0YNopLf8 bDCJ/+dzgRHMMGfaemHb7TTAHF9Xvp8NaOXQFyp9hgDpbF3Boe/BNNxeNjvqLkPb1Hxm78 5yJ3ii/WW27KgE3BiNxrn20kM/VdhnM= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-74-Ylab6u3JM3-k8KNLHcQRjg-1; Sat, 04 Feb 2023 21:02:49 -0500 X-MC-Unique: Ylab6u3JM3-k8KNLHcQRjg-1 Received: by mail-qk1-f198.google.com with SMTP id x14-20020a05620a14ae00b0072f7f0f356bso3004737qkj.1 for ; Sat, 04 Feb 2023 18:02:48 -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:references:cc:to:from :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Q6FRhLskamxVJBSD04JqjP9L387hIdPOAhYS4WgLRRA=; b=FIcL8OY9er1s+o1ua2KlKqEUatWtegsPXPK6Vq9x0JTbmXZQZybUGRWJ7rxH1Uke+9 mxecZWuiQTkE8zafZdEonOPIMgMFz3RsQqMVT+VDd3XdGR7HZ5PGqjz3P5z2ieq4iI2Y 1ICJBMuimIZTd9WkQDBQjp2UiL1JN3fWQ6eFFvYI2vjWVtysXXuaWWj36Ce/NFLRHAJg HTwrQByDoFAA/aZ0az7u2cqC4lZA7TwL/urGvL5laHe17ZaAbb6dgimBHZP87o9SLUOx 8XBcvfzmXoJD2Lhq8n2f/VnFXHRlspX2yHoG4Lw17mBzaqwi/2ZSDmT7ZpYmVhwt7oGK +ZKA== X-Gm-Message-State: AO0yUKWMbrGzdkmlKTY+cTlspA28+FB+ejPe+EEA02KS3/J14jXlHGhT APdy5k4hgsipqoH3L9WhvKHDQHV1gpXndvNZ3vQ93MRSG3hiSW+Kdx/AvK78WP1gdL3EB3igoWQ 6fHJQL89G6OP1ZQRscg== X-Received: by 2002:a05:622a:254:b0:3b1:4a8:4665 with SMTP id c20-20020a05622a025400b003b104a84665mr29146280qtx.62.1675562568254; Sat, 04 Feb 2023 18:02:48 -0800 (PST) X-Google-Smtp-Source: AK7set+S9pDuEL0HjrGhsjOdlKtRlshev8uH65fOpM8JfvNnpAR/GtidRzgopNKZD8sVka7AnkeEsQ== X-Received: by 2002:a05:622a:254:b0:3b1:4a8:4665 with SMTP id c20-20020a05622a025400b003b104a84665mr29146245qtx.62.1675562567865; Sat, 04 Feb 2023 18:02:47 -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 a27-20020a05620a103b00b0070736988c10sm4696587qkk.110.2023.02.04.18.02.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 04 Feb 2023 18:02:47 -0800 (PST) Message-ID: <03f75252-861b-528e-df42-6a88adddb388@redhat.com> Date: Sat, 4 Feb 2023 18:02:46 -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] From: Jason Merrill 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> In-Reply-To: <0e0bc838-466e-20ea-4523-a075be14ef76@redhat.com> 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.3 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/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. 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 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" } >>>> +} >>> >>> >> >