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 EB3343858D3C for ; Mon, 17 Jan 2022 18:48:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EB3343858D3C Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-607-6wKCPc-gOtG8CdV_LZg9nw-1; Mon, 17 Jan 2022 13:48:51 -0500 X-MC-Unique: 6wKCPc-gOtG8CdV_LZg9nw-1 Received: by mail-qk1-f199.google.com with SMTP id b141-20020a376793000000b0047910c74c9dso12952845qkc.4 for ; Mon, 17 Jan 2022 10:48:51 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=wpKezeSe9UuBdC5z4yrK1zjw+cAXuNlaXPuKhzzF8GM=; b=2ndSnlzSCZADGE+QaJhJLlQr4Yavq1hU2fniYbiM/0AZy7vlii4JGKpQsBXL9zXmXk SCDV50/RmzqKwbRh+sIy/tfVdCHLUpmGR8CiZ/3yx0VSiWbl+lwA9xf7izm53nXVOEsD 0dn9ODBWgwcaXAiD001FYu+wFxuECSAgEIkH317xHHkjZNjH43OJM1td9lv7g1dpoq6M dLRfIQilZoXZkyWTWGPHxP76kKevlTUWao+PvkILvEeIs7/MHuyzQu12UYzc/DvkwGO0 UxzuoyvrrCY5xTXLzTW9qslrBaqS/oC90z2z2Gmb0jxQ0MaUxLNyObIrVhgZtBuT0Ws3 cn4Q== X-Gm-Message-State: AOAM530W9Uv6tMxvIh9Kqfj0SWiStZlPMqv5EapGYMMOwpC2r/dS72uJ 34GP9Srx537p6KbQ8rVsgY6h6gn6Fti00xiQ8Vn1w4sCWd66zYVi2OVWV5BRqSnDoOLHvJvsphX P1T3x6vxcalaXzr5hXw== X-Received: by 2002:a05:620a:208e:: with SMTP id e14mr13106407qka.227.1642445330525; Mon, 17 Jan 2022 10:48:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJxlXCYaZwggq3s27OOrFgTr6teyneZD5pvtOd3PcYxdceCC6y6r86JLZwD6sm0bSMlqF8ab6w== X-Received: by 2002:a05:620a:208e:: with SMTP id e14mr13106393qka.227.1642445330143; Mon, 17 Jan 2022 10:48:50 -0800 (PST) Received: from [192.168.1.149] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id z5sm8806953qti.57.2022.01.17.10.48.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 17 Jan 2022 10:48:49 -0800 (PST) Message-ID: <1ef32dc7-8b16-9fa4-7c7c-649632be6768@redhat.com> Date: Mon, 17 Jan 2022 13:48:48 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH] c++: ICE with noexcept and canonical types [PR101715] To: Marek Polacek , Nathan Sidwell , GCC Patches References: <20220115002249.366484-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20220115002249.366484-1-polacek@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: 7bit X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 17 Jan 2022 18:48:54 -0000 On 1/14/22 19:22, Marek Polacek wrote: > This is a "canonical types differ for identical types" ICE, which started > with r11-4682. It's a bit tricky to explain. Consider: > > template struct S { > S bar() noexcept(T::value); // #1 > S foo() noexcept(T::value); // #2 > }; > > template S S::foo() noexcept(T::value) {} // #3 > > We ICE because #3 and #2 have the same type, but their canonical types > differ: TYPE_CANONICAL (#3) == #2 but TYPE_CANONICAL (#2) == #1. > > The member functions #1 and #2 have the same type. However, since their > noexcept-specifier is deferred, when parsing them, we create a variant for > both of them, because DEFERRED_PARSE cannot be compared. In other words, > build_cp_fntype_variant's > > tree v = TYPE_MAIN_VARIANT (type); > for (; v; v = TYPE_NEXT_VARIANT (v)) > if (cp_check_qualified_type (v, type, type_quals, rqual, raises, late)) > return v; > > will *not* find an existing variant when creating a method_type for #2, so we > have to create a new one. > > But then we perform delayed parsing and call fixup_deferred_exception_variants > for #1 and #2. f_d_e_v will replace TYPE_RAISES_EXCEPTIONS with the newly > parsed noexcept-specifier. It also sets TYPE_CANONICAL (#2) to #1. Both > noexcepts turned out to be the same, so now we have two equivalent variants in > the list! I.e., > > +-----------------+ +-----------------+ +-----------------+ > | main | | #2 | | #1 | > | S S::(S*) |----->| S S::(S*) |----->| S S::(S*) |----->NULL > | - | | noex(T::value) | | noex(T::value) | > +-----------------+ +-----------------+ +-----------------+ > > Then we get to #3. As for #1 and #2, grokdeclarator calls build_memfn_type, > which ends up calling build_cp_fntype_variant, which will use the loop > above to look for an existing variant. The first one that matches > cp_check_qualified_type will be used, so we use #2 rather than #1, and the > TYPE_CANONICAL mismatch follows. Hopefully that makes sense. Why doesn't the TYPE_CANONICAL (v) == v check prevent this? > As for the fix, I didn't think I could rewrite the method_type #2 with #1 > because the type may have escaped via decltype. So my approach is to > elide #2 from the list, so when looking for a matching variant, we always > find #1 (#2 remains live though, which admittedly sounds sort of dodgy). > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/11? > > PR c++/101715 > > gcc/cp/ChangeLog: > > * tree.c (fixup_deferred_exception_variants): Remove duplicate > variants after parsing the exception specifications. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp0x/noexcept72.C: New test. > * g++.dg/cpp0x/noexcept73.C: New test. > --- > gcc/cp/tree.c | 16 +++++++++++++++- > gcc/testsuite/g++.dg/cpp0x/noexcept72.C | 21 +++++++++++++++++++++ > gcc/testsuite/g++.dg/cpp0x/noexcept73.C | 13 +++++++++++++ > 3 files changed, 49 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept72.C > create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept73.C > > diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c > index 7f7de86b4e8..2efad49e7c1 100644 > --- a/gcc/cp/tree.c > +++ b/gcc/cp/tree.c > @@ -2804,8 +2804,9 @@ fixup_deferred_exception_variants (tree type, tree raises) > > /* Though sucky, this walk will process the canonical variants > first. */ > + tree prev = NULL_TREE; > for (tree variant = TYPE_MAIN_VARIANT (type); > - variant; variant = TYPE_NEXT_VARIANT (variant)) > + variant; prev = variant, variant = TYPE_NEXT_VARIANT (variant)) > if (TYPE_RAISES_EXCEPTIONS (variant) == original) > { > gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type)); > @@ -2827,6 +2828,19 @@ fixup_deferred_exception_variants (tree type, tree raises) > v = build_cp_fntype_variant (TYPE_CANONICAL (variant), > rqual, cr, false); > TYPE_CANONICAL (variant) = v; > + > + /* If VARIANT became a duplicate (cp_check_qualified_type-wise) > + of an existing variant in the variant list of TYPE after we > + have parsed its exception specification, elide it. Otherwise, > + build_cp_fntype_variant would use it, leading to "canonical > + types differ for identical types." */ > + for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v)) > + if (v != variant > + /* The main variant will not have TYPE_RAISES_EXCEPTIONS > + so PREV should never be null. */ > + && cp_check_qualified_type (v, variant, var_quals, > + rqual, cr, false)) > + TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant); > } > else > TYPE_RAISES_EXCEPTIONS (variant) = raises; > diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept72.C b/gcc/testsuite/g++.dg/cpp0x/noexcept72.C > new file mode 100644 > index 00000000000..f1455b3b46b > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept72.C > @@ -0,0 +1,21 @@ > +// PR c++/101715 > +// { dg-do compile { target c++11 } } > + > +template struct S { > + S bar() noexcept(T::value); // #1 > + S foo() noexcept(T::value); // #2 > +}; > + > +template S S::foo() noexcept(T::value) {} // #3 > + > +template struct S2 { > + S2 bar1() noexcept(T::value); > + S2 bar2() noexcept(T::value); > + S2 bar3() noexcept(T::value); > + S2 bar4() noexcept(T::value); > + S2 bar5() noexcept(T::value); > + S2 baz() noexcept(T::value2); > + S2 foo() noexcept(T::value); > +}; > + > +template S2 S2::foo() noexcept(T::value) {} > diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept73.C b/gcc/testsuite/g++.dg/cpp0x/noexcept73.C > new file mode 100644 > index 00000000000..24524f3592a > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept73.C > @@ -0,0 +1,13 @@ > +// PR c++/101715 > +// { dg-do compile { target c++11 } } > + > +template struct S { }; > + > +template > +struct A > +{ > + A& foo(A&&) noexcept((S::value)); > + A& assign(A&&) noexcept((S::value)); > +}; > +template > +A& A::foo(A&&) noexcept((S::value)) {} > > base-commit: 952b7dbb418198f86d7829aaf9d7f9fc7714a8b3