From patchwork Mon Nov 28 20:16:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 26946 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp5921238wrr; Mon, 28 Nov 2022 12:17:37 -0800 (PST) X-Google-Smtp-Source: AA0mqf79c+P1Ek8Ng8uv61Bw3VmXZK0em1f8CecPSrObzEUZtdPKng9TTznfLxCxloXvShe7hPUy X-Received: by 2002:a17:906:d7ad:b0:7bc:e98e:2d50 with SMTP id pk13-20020a170906d7ad00b007bce98e2d50mr15183414ejb.743.1669666657820; Mon, 28 Nov 2022 12:17:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669666657; cv=none; d=google.com; s=arc-20160816; b=rayb+fpC3l8IOWG3PE4dmTZMblVHb2b+p5r4xZgCPcHSl9Mon51UNKQGugCh1vJUm3 kOa2/7aNao3C74NX+T5kMR5/EOrof3JjdErX8Ox/kd2H7U9wFLfq3WvrioGVYqxdj1kA QYnwESjnekNXZvlZBN0vnudDD/QePOYdlLV44Yt1j/7fyqFY0YVrJctv1wXeLUxdZ/k/ Ga/CwkzA2CF7YhnDb6AG72MO0S/v/kEkav6osJwaeHjTB+qJcoW5zNvpz5jnl6yMNADc tBNdAKpXBOhJ27MsxwWAjxkkJ+aKR08iJEJtoa9MlrziP2QPijz7elKe5Hqh/GdvQQeu ewaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=QJ+53T6ypkAZv1U87ZGw8n6+NjOuwcO+gyf6c+mfcZc=; b=V86feETEfqGic1qiqrj8Qo7pJhDIfP2i2DHbSrLpeXH6dYoP+AuB3/DoiTERc+CTRp Yk/gHlmVLJkC8MC4uSwHbrVMnl+9ulB3JK+SUoU/yMfyGoWeXD2NgidDopnp6nn/OiTB Fsn3/aPWFHqREnASp/2LBUk9ePfj4tw3vCgB6GohWuV9UZx3K2vgo3yMHlBfLxsHVWAt NR9Fk6r/F+ve+4ZGIpRLU7QeIbj5mmcwEbnocuZFElCzGQbETf0X+HVUCguS83upOeUa M5udc2LgQ6cwJ6FrNmoMvmxshjHxpxqN+/f1lQoKlVUM2YaXKo/MWvbXDF+oAP+jidgz PUKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=pquz2Qbe; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id xd9-20020a170907078900b0078df2f22f80si10600409ejb.647.2022.11.28.12.17.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Nov 2022 12:17:37 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=pquz2Qbe; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 95DD13855160 for ; Mon, 28 Nov 2022 20:17:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 95DD13855160 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669666656; bh=QJ+53T6ypkAZv1U87ZGw8n6+NjOuwcO+gyf6c+mfcZc=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=pquz2Qbe39xz3PmqioFMr2WauyPMGMxp3YVvNwAY1bYzK7vDkj4i+7EZECAAZNOdS NXFNrcOhzV/R63+NXJKqQhOu6YQabSPjM2LQHl7sN6QeW9ulvm1Fz7X8fFVswHk2gK 8EHA6P7GTLO6fHtjNxN/fD9B3yHxYolXYqcflFws= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 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-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) 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 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 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-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750772385777326288?= X-GMAIL-MSGID: =?utf-8?q?1750772385777326288?= 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); }