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 8AFB93858C62 for ; Mon, 28 Nov 2022 20:16:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8AFB93858C62 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=1669666612; 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; bh=QJ+53T6ypkAZv1U87ZGw8n6+NjOuwcO+gyf6c+mfcZc=; b=bENXmt4WrqMywBzzY3nXmQwWdpy1Awxp8EhWDNHlSBrgv0+2MF9tSbE202ExDDxQeOQnDz DweJOZfGHu+ki+dcf4ZYZIh1/dpvp0t9AXY/IzeAyncVATjYtK3gAPv5+pQLMZyhjVrAxW IbQYiWWPRazA5hWcOHTPrSATH4UjVx0= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-401-B8gY4I3gPtSrBKy_IbHE7g-1; Mon, 28 Nov 2022 15:16:51 -0500 X-MC-Unique: B8gY4I3gPtSrBKy_IbHE7g-1 Received: by mail-qt1-f199.google.com with SMTP id hg24-20020a05622a611800b003a66175d924so16519095qtb.1 for ; Mon, 28 Nov 2022 12:16:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QJ+53T6ypkAZv1U87ZGw8n6+NjOuwcO+gyf6c+mfcZc=; b=u+qNmSHNrtoipUTodksuCl/f+JQf6zlipw6BWQHMGb+YZU+Ne1iVCwSW6JmUFcupQb omFMA6FaKkokiX9kmJHn+ov0/aHG4M9oybfxjkRpzu+Rts62l4J9CgZjnIKIkcgbzEMU 8UAxK2yJS6hNG2AoB/JzmurYECxSLfkPNe/HrEvquX9V9W1ArZGgMjQZhf3udCzDTVcR Sb0Nx6tzNcP1Au8ZL6gnZqPM0FOwUrgrW4tHYwpJWhFNrjPKyTv/DtoIURWudxs1XoHG 2Toel9csfOozdWhEJKgUkyM6ZECkmQv8dO4Tva7/WFn02Ga42XBMOsEXLCi8rMKfNYAv fgVw== X-Gm-Message-State: ANoB5plVakNiZ0pbzZoePaIuvykUfI+/+bg5z8/5di/IpDB1xMxIqgnx riyFMMLcwU3mMWgXqDsWuUilM6fkHtyQ/OaaPq7bIcLMiF+Xwjzfgba02RDuNahHXWNrX+auDgB +ufzhf6SIG28hWI2D+2CaHbrmvDi+MMAIXF20pgKPd79q22AR68KLEHWHsf956pZHBpc= X-Received: by 2002:ac8:7eeb:0:b0:3a5:2615:5694 with SMTP id r11-20020ac87eeb000000b003a526155694mr34036242qtc.429.1669666609638; Mon, 28 Nov 2022 12:16:49 -0800 (PST) X-Google-Smtp-Source: AA0mqf78NfSxRpE/S6xKuFgs4FTpNcp8nIvLROuc4C8MUygu8stXeAvY1o47vuBlkcPwoB9L0yEblQ== X-Received: by 2002:ac8:7eeb:0:b0:3a5:2615:5694 with SMTP id r11-20020ac87eeb000000b003a526155694mr34036211qtc.429.1669666609275; Mon, 28 Nov 2022 12:16:49 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id y18-20020a05620a25d200b006eeb3165565sm9092396qko.80.2022.11.28.12.16.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Nov 2022 12:16:48 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: explicit specialization and trailing requirements [PR107864] Date: Mon, 28 Nov 2022 15:16:47 -0500 Message-Id: <20221128201647.484582-1-ppalka@redhat.com> X-Mailer: git-send-email 2.39.0.rc0.33.g815c1e8202 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.7 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_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: Here we're crashing when using an explicit specialization of a function template with trailing requirements ultimately because decls_match (called indirectly from register_specialization) returns false since the template has trailing requirements whereas the specialization doesn't. In r12-2230-gddd25bd1a7c8f4, we fixed a similar issue concerning template requirements instead of trailing requirements. We could just extend this fix to ignore trailing requirement mismatches for explicit specializations as well, but it seems cleaner to just propagate constraints from the specialized template to the explicit specialization so that decls_match will naturally return true. And it looks like determine_specialization already does this, albeit inconsistently (only for non-template member functions of a class template as in cpp2a/concepts-explicit-spec4.C). So this patch makes determine_specialization consistently propagate constraints from the specialized template to the specialization, which obviates the function_requirements_equivalent_p special case added by r12-2230. In passing use add_outermost_template_args instead of open coding it. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? Also tested on range-v3 and cmcstl2. PR c++/107864 gcc/cp/ChangeLog: * decl.cc (function_requirements_equivalent_p): Don't check DECL_TEMPLATE_SPECIALIZATION. * pt.cc (determine_specialization): Propagate constraints when specializing a function template too. Simplify by using add_outermost_template_args. gcc/testsuite/ChangeLog: * g++.dg/concepts/explicit-spec1a.C: New test. --- gcc/cp/decl.cc | 4 +--- gcc/cp/pt.cc | 21 +++++++++---------- .../g++.dg/concepts/explicit-spec1a.C | 11 ++++++++++ 3 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/g++.dg/concepts/explicit-spec1a.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 544efdc9914..238e72f90da 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -956,9 +956,7 @@ static bool function_requirements_equivalent_p (tree newfn, tree oldfn) { /* In the concepts TS, the combined constraints are compared. */ - if (cxx_dialect < cxx20 - && (DECL_TEMPLATE_SPECIALIZATION (newfn) - <= DECL_TEMPLATE_SPECIALIZATION (oldfn))) + if (cxx_dialect < cxx20) { tree ci1 = get_constraints (oldfn); tree ci2 = get_constraints (newfn); diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index fbf498ad16a..e677e9d1b38 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -2482,17 +2482,16 @@ determine_specialization (tree template_id, } /* It was a specialization of a template. */ - targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); - if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) - { - *targs_out = copy_node (targs); - SET_TMPL_ARGS_LEVEL (*targs_out, - TMPL_ARGS_DEPTH (*targs_out), - TREE_PURPOSE (templates)); - } - else - *targs_out = TREE_PURPOSE (templates); - return TREE_VALUE (templates); + tree tmpl = TREE_VALUE (templates); + targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)); + targs = add_outermost_template_args (targs, TREE_PURPOSE (templates)); + *targs_out = targs; + + /* Propagate the template's constraints to the declaration. */ + if (tsk != tsk_template) + set_constraints (decl, get_constraints (tmpl)); + + return tmpl; } /* Returns a chain of parameter types, exactly like the SPEC_TYPES, diff --git a/gcc/testsuite/g++.dg/concepts/explicit-spec1a.C b/gcc/testsuite/g++.dg/concepts/explicit-spec1a.C new file mode 100644 index 00000000000..ec678740cb8 --- /dev/null +++ b/gcc/testsuite/g++.dg/concepts/explicit-spec1a.C @@ -0,0 +1,11 @@ +// A version of explicit-spec1.C where the template g has trailing instead of +// template requirements. +// PR c++/107864 +// { dg-do compile { target concepts } } + +template concept C = __is_class(T); +struct Y { int n; } y; +template void g(T) requires C { } +int called; +template<> void g(Y) { called = 3; } +int main() { g(y); } -- 2.39.0.rc0.33.g815c1e8202