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 C76D43858C41 for ; Wed, 20 Dec 2023 17:00:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C76D43858C41 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 C76D43858C41 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=1703091615; cv=none; b=HwcujE92k8uOPtqKQIF3jJ/6VuyUBoY1nRT7OScTCqnCDpt2lQYB37pPHlK3DblCOfdhSAb0ri9UnVcQEwklnPRDJDfuAhS2wXv3cskARtgMcvZ1cZdaXt+urMXY+mpIDnakTrdNGIRmyuo8w0nOhfHJlmsdZb+Lq9s+j8M5AxM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703091615; c=relaxed/simple; bh=QpV2Pw+tYvNU0e7KUEhtNX2tEKQJqzO58UnF+bOqLPc=; h=DKIM-Signature:From:Date:To:Subject:Message-ID:MIME-Version; b=krUAGW1JJoPaVVUpE5Iy1ySVDnuR7zsAJgt3PZAh6oPVUDVKzQBdTsj1N/tJKWHjapNHI+VaTEnFstD9mS/xn73mHFtk0CXFGoQUJcKXB8z8bQ8SuS/bi71FNN7QnXXHQLcvv0u7XRkEFgirdEx9l3pVId/NlBGjKUeVJ1qz3rc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1703091612; 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: in-reply-to:in-reply-to:references:references; bh=jTNUUP8b7W0z+f4BUjhelMIcoyE7VV108CdGq4NXxtA=; b=Cvldk04xU8w0/h7d63qTxbJdPfB3oXRtYxVIT4A0k1Xr7x27Pk46FCVil++kmOYPoMFyGN H2+KEn0vktE+oRo0EBulW1Y5BHPhtlWZ4nzYfO2SEaLYwh+DMTBcci6RfZ1eLyKfO2gj9P JKWyHb/6Yjv0z6osrUMmJsOBd/o+HdY= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-650-WRU7bYUdNjub-ImqbYmizw-1; Wed, 20 Dec 2023 12:00:10 -0500 X-MC-Unique: WRU7bYUdNjub-ImqbYmizw-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-67ee87ff6bfso63294326d6.3 for ; Wed, 20 Dec 2023 09:00:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703091609; x=1703696409; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=jTNUUP8b7W0z+f4BUjhelMIcoyE7VV108CdGq4NXxtA=; b=fG8xeIeqXfyHh5bmnkmsurT6vBHSH68w2ZmRsyI8BFkzObFRHqVgipucPGkXw/fiFw Be+AFor/Rgt3x/sNGE2HO7uMoZrCxjVI2nmRtd9z0jAx5Bv6wdSOAY2nzWRV9ZZr6Utq 23apFZlrTwBbf+nnRn28IOxWkI5uBkmAC/4VK3PIFpCxHdjPgCvQUJAd360pw2cGgmtR CahMoEA5OfUEL3EiowJGkEzf0bnOmcHSKVTbf9hLWbZ33J7lihXfHhqQztjG7tTDG64l w/wCO2u+Hlyt3CiDrUQSCXwvJMEXUR1EG9eTvZr4JR8Az4zrSOYpj7ccL91fR0j8Occ5 e27A== X-Gm-Message-State: AOJu0YwMa9jMxUgKGHtEUTBLrwhIbbr6jJQwSymw4jPOu06duN7ekb1X f3+eBXkPk9g8A0vs+mTfZNW7po3UVn6h4Km/BiJmzzLUkhRRamOScffvuuPSq2qZNcPOosdqBVr UoH+UqPxtYz9q/fyk3yHdMHh1zw== X-Received: by 2002:a05:6214:1cc1:b0:67a:a36:d7 with SMTP id g1-20020a0562141cc100b0067a0a3600d7mr18176490qvd.2.1703091609350; Wed, 20 Dec 2023 09:00:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IGHxKBBaI5HTE+aMbmPI4qUO752fbSLibInxma0rEANHfQ+XzRW5PVz3ODi38kRbt1B9qIiCQ== X-Received: by 2002:a05:6214:1cc1:b0:67a:a36:d7 with SMTP id g1-20020a0562141cc100b0067a0a3600d7mr18176470qvd.2.1703091608908; Wed, 20 Dec 2023 09:00:08 -0800 (PST) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id bj25-20020a05620a191900b0077f04cce6e5sm61371qkb.14.2023.12.20.09.00.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 09:00:08 -0800 (PST) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Wed, 20 Dec 2023 12:00:07 -0500 (EST) To: Jason Merrill cc: Patrick Palka , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] c++: implicit guides should inherit class constraints [PR104873] In-Reply-To: <74840bc2-b868-befb-0ec3-810cf57281db@redhat.com> Message-ID: <58878cf5-8f64-7c6b-3b4d-6af929c0bfd0@idea> References: <20220401151741.2182149-1-ppalka@redhat.com> <74840bc2-b868-befb-0ec3-810cf57281db@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-13.8 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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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 Fri, 1 Apr 2022, Jason Merrill wrote: > On 4/1/22 11:17, Patrick Palka wrote: > > An implicit guide already inherits the (rewritten) constraints of the > > constructor. Thus it seems natural that the guide must also inherit > > the constraints of the class template, since a constructor's constraints > > might assume the class's constraints are satisfied, and therefore > > checking these two sets of constraints "out of order" may result in hard > > errors as in the first testcase below. > > > This patch makes implicit guides inherit the constraints of the class > > template (even for unconstrained constructors, and even for the copy > > deduction candidate). > > > > In passing, this patch gives implicit guides a trailing return type > > since that's how they're depicted in the standard (e.g. > > [over.match.class.deduct]/6); this changes the order of substitution > > into implicit guides in a probably negligible way, especially now that > > they inherit the class constraints. > > > > The parameter_mapping_equivalent_p change is to avoid an ICE in the last > > testcase below (described within), reduced from a cmcstl2 testsuite ICE. > > > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look like > > the right approach? > > I don't think so, given the testcases below. > > Maybe fn_type_unification should check formation of the return type of a > deduction guide before constraints? In general, whichever order you do things > in, it'll be wrong for some testcase or other. Makes sense.. Though I notice the resolution of CWG2628 goes the direction of propagating the class's constraints and seems to have been approved in November. I wonder how that discussion went wrt your alternative resolution of first checking the return type during deduction? Shall we follow suit with the approved resolution (and therefore this patch) for GCC 14? > > The broader subject of constraints and deduction guides should be raised with > CWG in general (https://github.com/cplusplus/CWG/issues/new/choose) > > > PR c++/104873 > > > > gcc/cp/ChangeLog: > > > > * constraint.cc (parameter_mapping_equivalent_p): Relax assert > > to expect equivalence not identity of template parameters. > > * pt.cc (build_deduction_guide): Propagate the class's > > constraints to the deduction guide. Set TYPE_HAS_LATE_RETURN_TYPE > > on the function type. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp2a/concepts-ctad5.C: New test. > > * g++.dg/cpp2a/concepts-ctad6.C: New test. > > * g++.dg/cpp2a/concepts-ctad6a.C: New test. > > * g++.dg/cpp2a/concepts-ctad7.C: New test. > > --- > > gcc/cp/constraint.cc | 2 +- > > gcc/cp/pt.cc | 26 ++++++++++++++++++ > > gcc/testsuite/g++.dg/cpp2a/concepts-ctad5.C | 29 ++++++++++++++++++++ > > gcc/testsuite/g++.dg/cpp2a/concepts-ctad6.C | 19 +++++++++++++ > > gcc/testsuite/g++.dg/cpp2a/concepts-ctad6a.C | 19 +++++++++++++ > > gcc/testsuite/g++.dg/cpp2a/concepts-ctad7.C | 26 ++++++++++++++++++ > > 6 files changed, 120 insertions(+), 1 deletion(-) > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-ctad5.C > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-ctad6.C > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-ctad6a.C > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-ctad7.C > > > > diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc > > index 94f6222b436..6cbb182dda2 100644 > > --- a/gcc/cp/constraint.cc > > +++ b/gcc/cp/constraint.cc > > @@ -604,7 +604,7 @@ parameter_mapping_equivalent_p (tree t1, tree t2) > > tree map2 = ATOMIC_CONSTR_MAP (t2); > > while (map1 && map2) > > { > > - gcc_checking_assert (TREE_VALUE (map1) == TREE_VALUE (map2)); > > + gcc_checking_assert (cp_tree_equal (TREE_VALUE (map1), TREE_VALUE > > (map2))); > > tree arg1 = TREE_PURPOSE (map1); > > tree arg2 = TREE_PURPOSE (map2); > > if (!template_args_equal (arg1, arg2)) > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > > index 75ed9a34018..966e6d90d3a 100644 > > --- a/gcc/cp/pt.cc > > +++ b/gcc/cp/pt.cc > > @@ -29261,6 +29261,10 @@ build_deduction_guide (tree type, tree ctor, tree > > outer_args, tsubst_flags_t com > > /* Discard the 'this' parameter. */ > > fparms = FUNCTION_ARG_CHAIN (ctor); > > fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor)); > > + /* The guide's constraints consist of the class template's > > constraints > > + followed by the constructor's rewritten constraints. We start > > + with the constructor's constraints (since we need to rewrite them), > > + and prepend the class template's constraints later. */ > > ci = get_constraints (ctor); > > loc = DECL_SOURCE_LOCATION (ctor); > > explicit_p = DECL_NONCONVERTING_P (ctor); > > @@ -29362,6 +29366,27 @@ build_deduction_guide (tree type, tree ctor, tree > > outer_args, tsubst_flags_t com > > return error_mark_node; > > } > > + /* Prepend the class template's constraints to the constructor's > > rewritten > > + constraints (if any). */ > > + if (tree class_ci = get_constraints (CLASSTYPE_TI_TEMPLATE (type))) > > + { > > + if (outer_args) > > + { > > + /* FIXME: As above. */ > > + ++processing_template_decl; > > + class_ci = tsubst_constraint_info (class_ci, outer_args, > > + complain, ctor); > > + --processing_template_decl; > > + } > > + if (ci) > > + ci = build_constraints (combine_constraint_expressions > > + (CI_TEMPLATE_REQS (class_ci), > > + CI_TEMPLATE_REQS (ci)), > > + CI_DECLARATOR_REQS (ci)); > > + else > > + ci = copy_node (class_ci); > > + } > > + > > if (!memtmpl) > > { > > /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */ > > @@ -29371,6 +29396,7 @@ build_deduction_guide (tree type, tree ctor, tree > > outer_args, tsubst_flags_t com > > } > > tree fntype = build_function_type (type, fparms); > > + TYPE_HAS_LATE_RETURN_TYPE (fntype) = true; > > tree ded_fn = build_lang_decl_loc (loc, > > FUNCTION_DECL, > > dguide_name (type), fntype); > > diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-ctad5.C > > b/gcc/testsuite/g++.dg/cpp2a/concepts-ctad5.C > > new file mode 100644 > > index 00000000000..5990088f1db > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-ctad5.C > > @@ -0,0 +1,29 @@ > > +// PR c++/104873 > > +// { dg-do compile { target c++20 } } > > +// Verify implicit guides inherit the constraints of the class template. > > + > > +template > > +struct A { > > + static_assert(!__is_same(T, void)); > > + static constexpr bool value = true; > > +}; > > + > > +template requires (!__is_same(T, void)) > > +struct B { > > + B(T*) requires A::value; // #1 > > + // implicit guide: > > + // template requires (!__is_same(T, void)) && A::value > > + // B(T*) -> B; > > + > > + B(T); > > + // implicit guide: > > + // template requires (!__is_same(T, void)) > > + // B(T) -> B; > > +}; > > + > > +void* p; > > +using type = decltype(B(p)); // previously hard error during dguide > > overload > > + // resolution because #1's implicit guide would > > + // inherit only the constructor's constraints and > > + // not also the class's. > > +using type = B; > > diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-ctad6.C > > b/gcc/testsuite/g++.dg/cpp2a/concepts-ctad6.C > > new file mode 100644 > > index 00000000000..10bb86df6a0 > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-ctad6.C > > @@ -0,0 +1,19 @@ > > +// PR c++/104873 > > +// { dg-do compile { target c++20 } } > > + > > +template concept C = true; > > + > > +template requires C > > +struct S { > > + S(...); > > +}; > > +// synthesized copy deduction candidate: > > +// template requires C > > +// S(S) -> S; > > + > > +template S(S) -> S; // #1 > > + > > +using type = decltype(S(S())); // The deduction candidate (which > > inherits > > + // the class's constraints) is preferred > > + // over #1 because it's more constrained. > > This seems like a regression: Presumably people write deduction guides because > they want them to be used, not to have them silently ignored because the class > is constrained. > > > +using type = S; > > diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-ctad6a.C > > b/gcc/testsuite/g++.dg/cpp2a/concepts-ctad6a.C > > new file mode 100644 > > index 00000000000..f9d1d6ec11e > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-ctad6a.C > > @@ -0,0 +1,19 @@ > > +// PR c++/104873 > > +// { dg-do compile { target c++20 } } > > + > > +template concept C = true; > > + > > +template requires C > > +struct S { > > + S(T); // #1 > > + // implicit guide: > > + // template requires C > > + // S -> S; > > +}; > > + > > +template S(T) -> S>; // #2 > > + > > +using type = decltype(S(0)); // The implicit guide for #1 (which inherits > > the > > + // class's constraints) is preferred over #2 > > + // because it's more constrained. > > Likewise. > > > +using type = S; > > diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-ctad7.C > > b/gcc/testsuite/g++.dg/cpp2a/concepts-ctad7.C > > new file mode 100644 > > index 00000000000..4e1b1abfb94 > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-ctad7.C > > @@ -0,0 +1,26 @@ > > +// { dg-do compile { target c++20 } } > > + > > +template requires __is_same(T, int) > > +struct A; > > + > > +// When checking constraints on the class template, we > > +// normalize __is_same(T, int) and cache the resulting atom. > > +A *p; > > + > > +template requires __is_same(T, int) > > +struct A { > > + A(T); > > + // implicit guide: > > + // template requires __is_same(T, int) > > + // A(T) -> A; > > +}; > > + > > +// When checking constraints on the implicit guide, we normalize > > +// the same expression again with an equivalent but not identical > > +// set of template parameters (those from the definition vs from > > +// the forward declaration). We should notice that that the > > +// resulting atom is the same as the one we cached earlier, but > > +// instead we crashed due to an overly strict assert in > > +// parameter_mapping_equivalent_p that demanded identity instead > > +// of equivalence of the template parameters in the mapping. > > +A a = 1; > >