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 A1B263858D1E for ; Wed, 22 May 2024 02:59:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A1B263858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A1B263858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716346746; cv=none; b=GMZ3FppOu4VUS1nweLAQW/sCZiG95yzrPG7Vla5NKCWxyWt1+R71yl6PutJ0DrxqizqYmY3bDtq+7Y/PV8shSEefMh60Zwl4PcZiR7RKQv9OIHy9Bhhz/XeQbUOJQqkiECybb/Bw2tO5m0hLUF9Eta3e1BX6c/xSy4QV552Wnvc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716346746; c=relaxed/simple; bh=0ZlmhQwQtzfIMdJ0icWz6XAume5dg5WAFjOHQ9osSDs=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=G/9NQNBOO7VVcMQ5OL2YUTxKA4fKPqAfw5kH/BPjkQoA2G58jd0wMju/4Z2sLS62Klyh50KNHtUcot6bFTPZFv803gIZzQTGy1a66+GMWuuMsMYG3SIbmip8eDPDiu4XW43xjwb1654wYYbBwy8GgmfCenELvEo2s+I1B7bhXDA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716346743; 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=AeyBs8bdmT91PnLbF7/QGFMJEv5tzTbZjo08QsBTTVQ=; b=Qo6jHdWTcxDSyUxphUXh7EiM8sNfwAtVBqnN6hvuHBtn4U1C6ew8r7MkT5f6aiubQMKebW dOb1eG89j41WHJttcDoJl3aZTBQCWdYtu8OLD+D3F5vjnBkbptyOXDMKXAKDk3kfQmDZwI KgU83sTYJtxVxHlJNMzard/YLRt0ZNg= 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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-624-WRdAMxyfMzasZVHPTXnRVA-1; Tue, 21 May 2024 22:59:02 -0400 X-MC-Unique: WRdAMxyfMzasZVHPTXnRVA-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-6ab6d255faeso28026186d6.1 for ; Tue, 21 May 2024 19:59:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716346741; x=1716951541; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=AeyBs8bdmT91PnLbF7/QGFMJEv5tzTbZjo08QsBTTVQ=; b=SxPVzl2L5dJjkn5baKq1pmkCrLoAL6ieiz8QwkUvjjzsR7P9J/8WnAEATu0E58IomL lPsMqktMk2Ezoczn5A93JH5pZKO8kUkLwzlyMIqyYd8k0Oh14ix00zjoYKxc94mtrTtT 2ATJ4IiniQO/+Zka9TWqCb8frD3oHB1W83T1PVOGZofd9dbuHo/mckaV2vj2sZI5ixZR vVb847S9dP0lvK1ge2PNHmq9Gyfyvg3bwcxSx+GQGicX6e9Z1c2jK21YJwKzu2uEOMfQ 84WMk6wYUV3qCkjG37y7GIYW5ezsj03gXnCg9RDmKPsVyuzn8sICrAH/OMzI7ZmpFa/X DLvQ== X-Gm-Message-State: AOJu0YwJTUBqhZBTqy9QsYXyljbubW2SSSNm/8zmFolNvX8OVmzLRQ/U PnTVgxaV6mSDohaOAngYXcklVcmSwf7vn6A9S9urrkW3BjW9xR/2cdlJ/+VdwD/PTXVHEEeNuGX dt+FOaqxFb8Azx610CbfeXkpkj5KXoZlYr1XWkRim1uhMN4Jv/uF4mzA= X-Received: by 2002:a05:6214:440a:b0:6a0:5f8f:d753 with SMTP id 6a1803df08f44-6ab80900c42mr7861846d6.49.1716346741371; Tue, 21 May 2024 19:59:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE5sWtb9fH2nJmFA5LYAOjTLteik4TVxfDRpv+DJmZP24uut1SSKlFSsETipY6hD7knuhdr4A== X-Received: by 2002:a05:6214:440a:b0:6a0:5f8f:d753 with SMTP id 6a1803df08f44-6ab80900c42mr7861676d6.49.1716346740874; Tue, 21 May 2024 19:59:00 -0700 (PDT) Received: from [192.168.1.130] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6a15f1857f9sm129212736d6.49.2024.05.21.19.58.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 May 2024 19:58:57 -0700 (PDT) Message-ID: Date: Tue, 21 May 2024 22:58:55 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] c++: canonicity of fn types w/ complex eh specs [PR115159] To: Patrick Palka Cc: gcc-patches@gcc.gnu.org References: <20240521193629.4129787-1-ppalka@redhat.com> <42726cf3-3bf8-499a-b455-d0181f0b8d3b@redhat.com> <3356d9ff-9c60-a215-427c-9deaf6fb5024@idea> <9c0b3ede-9457-aa5c-0934-b82f7a44291f@idea> From: Jason Merrill In-Reply-To: <9c0b3ede-9457-aa5c-0934-b82f7a44291f@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: 7bit X-Spam-Status: No, score=-12.3 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_H4,RCVD_IN_MSPIKE_WL,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 5/21/24 21:55, Patrick Palka wrote: > On Tue, 21 May 2024, Jason Merrill wrote: > >> On 5/21/24 17:27, Patrick Palka wrote: >>> On Tue, 21 May 2024, Jason Merrill wrote: >>> >>>> On 5/21/24 15:36, Patrick Palka wrote: >>>>> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look >>>>> OK for trunk? >>>>> >>>>> Alternatively, I considered fixing this by incrementing >>>>> comparing_specializations around the call to comp_except_specs in >>>>> cp_check_qualified_type, but generally for types whose identity >>>>> depends on whether comparing_specializations is set we need to >>>>> use structural equality anyway IIUC. >>>> >>>> Why not both? >>> >>> I figured the latter change isn't necessary/observable since >>> comparing_specializations would only make a difference for complex >>> exception specifications, and with this patch we won't even call >>> cp_check_qualified_type on a complex eh spec. >> >> My concern is that if we're building a function type multiple times with the >> same noexcept-spec, this patch would mean creating multiple equivalent >> function types instead of reusing one already created for the same function. >> >>>>> + bool complex_p = (cr && cr != noexcept_true_spec >>>>> + && !UNPARSED_NOEXCEPT_SPEC_P (cr)); >>>> >>>> Why treat unparsed specs differently from parsed ones? >>> >>> Unparsed specs are unique according to cp_tree_equal, so in turn >>> function types with unparsed specs are unique, so it should be safe to >>> treat such types as canonical. I'm not sure if this optimization >>> matters though; I'm happy to remove this case. >> >> The idea that this optimization could make a difference raised the concern >> above. > > Aha, makes sense. To that end it seems we could strengthen the ce_exact > in comp_except_specs to require == instead of cp_tree_equal equality > when comparing two noexcept-specs; the only ce_exact callers are > cp_check_qualified_type and cxx_type_hash_eq, which should be fine with > that strengthening. This way, we at least do try to reuse a variant if > the (complex or unparsed) noexcept-spec is exactly the same. Sounds good. Given that, we probably still want to move the canonical_eh_spec up in build_cp_fntype_variant, and pass that to cp_check_qualified_type? > Like so? > > -- >8 -- > > Subject: [PATCH] c++: canonicity of fn types w/ complex eh specs [PR115159] > > Here the member functions QList::g and QList::h are given the same > function type since their exception specifications are equivalent > according to cp_tree_equal. In doing so however this means that the > type of QList::h refers to a function parameter from QList::g, which > ends up confusing modules streaming. > > I'm not sure if modules can be fixed to handle this situation, but > regardless it seems weird in principle that a function parameter can > escape in such a way. The analogous situation with a trailing return > type and decltype > > auto g(QList &other) -> decltype(f(other)); > auto h(QList &other) -> decltype(f(other)); > > behaves better because we don't canonicalize decltype, and so the > function types of g and h are non-canonical and therefore not shared. > > In light of this, it seems natural to treat function types with complex > eh specs as non-canonical as well so that each such function declaration > is given a unique function/method type node. The main benefit of type > canonicalization is to speed up repeated type comparisons, but it should > rare for us to repeatedly compare two otherwise compatible function > types with complex exception specifications, so foregoing canonicalization > should not cause any problems. > > To that end, this patch strengthens the ce_exact case of comp_except_specs > to require identity instead of equivalence of the exception specification > so that build_cp_fntype_variant doesn't reuse a variant when it shouldn't. > And in build_cp_fntype_variant we need to use structural equality for types > with a complex eh spec. In turn we could simplify the code responsible > for adjusting unparsed eh spec variants. > > PR c++/115159 > > gcc/cp/ChangeLog: > > * tree.cc (build_cp_fntype_variant): Always use structural > equality for types with a complex exception specification. > (fixup_deferred_exception_variants): Always use structural > equality for adjusted variants. > * typeck.cc (comp_except_specs): Require == instead of > cp_tree_equal for noexcept-spec comparison in the ce_exact case. > > gcc/testsuite/ChangeLog: > > * g++.dg/modules/noexcept-2_a.H: New test. > * g++.dg/modules/noexcept-2_b.C: New test. > --- > gcc/cp/tree.cc | 47 +++++---------------- > gcc/cp/typeck.cc | 4 +- > gcc/testsuite/g++.dg/modules/noexcept-2_a.H | 24 +++++++++++ > gcc/testsuite/g++.dg/modules/noexcept-2_b.C | 4 ++ > 4 files changed, 41 insertions(+), 38 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/modules/noexcept-2_a.H > create mode 100644 gcc/testsuite/g++.dg/modules/noexcept-2_b.C > > diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc > index 9d37d255d8d..93a64322418 100644 > --- a/gcc/cp/tree.cc > +++ b/gcc/cp/tree.cc > @@ -2794,7 +2794,12 @@ build_cp_fntype_variant (tree type, cp_ref_qualifier rqual, > /* Canonicalize the exception specification. */ > tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE; > > - if (TYPE_STRUCTURAL_EQUALITY_P (type)) > + /* Always use structural equality for function types with a complex > + exception specification since their identity may depend on e.g. > + whether comparing_specializations is set. */ > + bool complex_eh_spec_p = (cr && cr != noexcept_true_spec > + && !UNPARSED_NOEXCEPT_SPEC_P (cr)); > + if (TYPE_STRUCTURAL_EQUALITY_P (type) || complex_eh_spec_p) > /* Propagate structural equality. */ > SET_TYPE_STRUCTURAL_EQUALITY (v); > else if (TYPE_CANONICAL (type) != type || cr != raises || late) > @@ -2812,55 +2817,23 @@ build_cp_fntype_variant (tree type, cp_ref_qualifier rqual, > /* TYPE is a function or method type with a deferred exception > specification that has been parsed to RAISES. Fixup all the type > variants that are affected in place. Via decltype &| noexcept > - tricks, the unparsed spec could have escaped into the type system. > - The general case is hard to fixup canonical types for. */ > + tricks, the unparsed spec could have escaped into the type system. */ > > void > fixup_deferred_exception_variants (tree type, tree raises) > { > tree original = TYPE_RAISES_EXCEPTIONS (type); > - tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE; > > gcc_checking_assert (UNPARSED_NOEXCEPT_SPEC_P (original)); > > - /* Though sucky, this walk will process the canonical variants > - first. */ > - tree prev = NULL_TREE; > for (tree variant = TYPE_MAIN_VARIANT (type); > - variant; prev = variant, variant = TYPE_NEXT_VARIANT (variant)) > + variant; variant = TYPE_NEXT_VARIANT (variant)) > if (TYPE_RAISES_EXCEPTIONS (variant) == original) > { > gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type)); > > - if (!TYPE_STRUCTURAL_EQUALITY_P (variant)) > - { > - cp_cv_quals var_quals = TYPE_QUALS (variant); > - cp_ref_qualifier rqual = type_memfn_rqual (variant); > - > - /* If VARIANT would become a dup (cp_check_qualified_type-wise) > - of an existing variant in the variant list of TYPE after its > - exception specification has been parsed, elide it. Otherwise, > - build_cp_fntype_variant could use it, leading to "canonical > - types differ for identical types." */ > - tree v = TYPE_MAIN_VARIANT (type); > - for (; v; v = TYPE_NEXT_VARIANT (v)) > - if (cp_check_qualified_type (v, variant, var_quals, > - rqual, cr, false)) > - { > - /* The main variant will not match V, so PREV will never > - be null. */ > - TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant); > - break; > - } > - TYPE_RAISES_EXCEPTIONS (variant) = raises; > - > - if (!v) > - v = build_cp_fntype_variant (TYPE_CANONICAL (variant), > - rqual, cr, false); > - TYPE_CANONICAL (variant) = TYPE_CANONICAL (v); > - } > - else > - TYPE_RAISES_EXCEPTIONS (variant) = raises; > + SET_TYPE_STRUCTURAL_EQUALITY (variant); > + TYPE_RAISES_EXCEPTIONS (variant) = raises; > > if (!TYPE_DEPENDENT_P (variant)) > /* We no longer know that it's not type-dependent. */ > diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc > index 75b696e32e0..d535746fd43 100644 > --- a/gcc/cp/typeck.cc > +++ b/gcc/cp/typeck.cc > @@ -1227,7 +1227,9 @@ comp_except_specs (const_tree t1, const_tree t2, int exact) > if ((t1 && TREE_PURPOSE (t1)) > || (t2 && TREE_PURPOSE (t2))) > return (t1 && t2 > - && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))); > + && (exact == ce_exact > + ? TREE_PURPOSE (t1) == TREE_PURPOSE (t2) > + : cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))); > > if (t1 == NULL_TREE) /* T1 is ... */ > return t2 == NULL_TREE || exact == ce_derived; > diff --git a/gcc/testsuite/g++.dg/modules/noexcept-2_a.H b/gcc/testsuite/g++.dg/modules/noexcept-2_a.H > new file mode 100644 > index 00000000000..b7144f42d7e > --- /dev/null > +++ b/gcc/testsuite/g++.dg/modules/noexcept-2_a.H > @@ -0,0 +1,24 @@ > +// PR c++/115159 > +// { dg-additional-options -fmodule-header } > +// { dg-module-cmi {} } > + > +struct QDebug; > + > +template void f(T); > + > +template struct QList { > + QDebug g(QList &other) noexcept(noexcept(f(other))); > + QDebug h(QList &other) noexcept(noexcept(f(other))); > +}; > + > +struct QObjectData { > + QList children; > +}; > + > +struct QIODevice { > + QObjectData d_ptr; > +}; > + > +struct QDebug { > + QDebug(QIODevice); > +}; > diff --git a/gcc/testsuite/g++.dg/modules/noexcept-2_b.C b/gcc/testsuite/g++.dg/modules/noexcept-2_b.C > new file mode 100644 > index 00000000000..d34c63add10 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/modules/noexcept-2_b.C > @@ -0,0 +1,4 @@ > +// PR c++/115159 > +// { dg-additional-options "-fmodules-ts -fno-module-lazy" } > + > +import "noexcept-2_a.H";