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 3B7073857C4E for ; Tue, 18 Jan 2022 16:08:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3B7073857C4E Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-413-t62QlVNGOn6OyGq6yHyxuw-1; Tue, 18 Jan 2022 11:08:39 -0500 X-MC-Unique: t62QlVNGOn6OyGq6yHyxuw-1 Received: by mail-qv1-f72.google.com with SMTP id kk29-20020a056214509d00b0041c9228d334so6333338qvb.23 for ; Tue, 18 Jan 2022 08:08:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=mNhKCRNujVq/afY5zZfz7Nc8y0BXB4yo5Gif5YqTloM=; b=2cXLl/UQ7V7cJElw6t7IIlsZGk6TENTyqkM4RRIoYAZuYxmFQQs5wZPV93u/hr400h Y4szv7KDKXskiiFZWHOoKhEKSTnW0qh/PmYipmkxks/S8catYixhw4iTxIEAdRHkVmGP ahwbuWN6LULq+KVKnB7rTxdrHtLs/CHZppRrqtkO+TXXa+CVfgZnzF/Sw4i9bN8E/7Lp mgsKt0wBjq7pTSwFsL8vPgMrriYFAOPkTQimw0Deb+Q3da+D4RdkKLOb5VkXC87d7zsK koE763B6Qxs46lSxkX6OzfvYl00ntaqejxv95GfhcXntEk+MV5NJ+jfvZXNMynDF4lnN ZQlA== X-Gm-Message-State: AOAM533mNiJk9oHFfYLk0ONGlX6eKXBxjfWH+Azq1aFcJM7cRHkn1qF2 0AtO8Tvs1Ty1Tfdl+HCtyCuhv89cFm68PZm6jrzCwCjCkXrdLE3zfOcy49xsN9XPwjqY1AR0Ac2 EEtDcMcksuj3BNjHE6A== X-Received: by 2002:a05:6214:23cd:: with SMTP id hr13mr22884652qvb.7.1642522118986; Tue, 18 Jan 2022 08:08:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJyCgPE5325gwOlPfBbIpmg3NouhAuOUF9q9ryFv2bDWegM9PJGdJhmJCWDcgOPGv5uDwmbHrw== X-Received: by 2002:a05:6214:23cd:: with SMTP id hr13mr22884622qvb.7.1642522118741; Tue, 18 Jan 2022 08:08:38 -0800 (PST) Received: from redhat.com ([2601:184:4780:4310::7156]) by smtp.gmail.com with ESMTPSA id y17sm3067860qtw.0.2022.01.18.08.08.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jan 2022 08:08:38 -0800 (PST) Date: Tue, 18 Jan 2022 11:08:36 -0500 From: Marek Polacek To: Patrick Palka Cc: Jason Merrill , Nathan Sidwell , GCC Patches Subject: Re: [PATCH] c++: ICE with noexcept and canonical types [PR101715] Message-ID: References: <20220115002249.366484-1-polacek@redhat.com> <4b973bd2-1a5a-03ac-18ca-51e3614d3ae1@idea> MIME-Version: 1.0 In-Reply-To: <4b973bd2-1a5a-03ac-18ca-51e3614d3ae1@idea> User-Agent: Mutt/2.1.5 (2021-12-30) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_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: Tue, 18 Jan 2022 16:08:42 -0000 On Sat, Jan 15, 2022 at 09:24:05AM -0500, Patrick Palka wrote: > On Fri, 14 Jan 2022, Marek Polacek via Gcc-patches 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. > > > > 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). > > I wonder about instead making build_cp_fntype_variant set the TYPE_CANONICAL for > #3 to TYPE_CANONICAL(#2) (i.e. #1) instead of to #2? Something like: > > -- >8 -- > > gcc/cp/tree.c | 5 +++-- > 1 file changed, 3 insertions(+), 2 deletions(-) > > diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c > index 7f7de86b4e8..b89135fa121 100644 > --- a/gcc/cp/tree.c > +++ b/gcc/cp/tree.c > @@ -2779,8 +2779,9 @@ build_cp_fntype_variant (tree type, cp_ref_qualifier rqual, > else if (TYPE_CANONICAL (type) != type || cr != raises || late) > /* Build the underlying canonical type, since it is different > from TYPE. */ > - TYPE_CANONICAL (v) = build_cp_fntype_variant (TYPE_CANONICAL (type), > - rqual, cr, false); > + TYPE_CANONICAL (v) > + = TYPE_CANONICAL (build_cp_fntype_variant (TYPE_CANONICAL (type), > + rqual, cr, false)); > else > /* T is its own canonical type. */ > TYPE_CANONICAL (v) = v; Thanks for looking. I can dig that (and verified it works), but it strikes me more as a workaround for the duplicity problem. I also don't see TYPE_CANONICAL (...) = TYPE_CANONICAL (build_cp_fntype_variant (...)) anywhere in the codebase, if that means anything. Marek