From patchwork Fri Aug 16 16:00:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1973302 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=aJAoZCE6; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wlmth49ksz1yfB for ; Sat, 17 Aug 2024 02:00:57 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2DF40385F028 for ; Fri, 16 Aug 2024 16:00:55 +0000 (GMT) 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 ESMTP id 48D53385E836 for ; Fri, 16 Aug 2024 16:00:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 48D53385E836 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 48D53385E836 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723824019; cv=none; b=Ev2TqRUhdg/bSmnHef0I9lu1XZUIDPbpqBLVsVKnYwzNuNYDAiGgh1cRk9n3fsqyrBUryNfgruNiaXEfUDPjtepKnu1YHPPie3UJ8tN2/JcBMcrY/J/4H+b3UuWm6+0Ej+miF6XC5Ctx9GHA6fIah3GhJKQ5NGkcov+38vgK4i4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723824019; c=relaxed/simple; bh=k3tqjIs4igqV2S6tZrkenq0Ef0tgQUDVcVIi3GzoZSY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=tmEJW03COLb4CDZWXzPHKB4HY3F1ZicH7cHxFLmBMkDKAlFiC52Snb1Hv3YMXLesQGy1cHIyrG+kMwBBFO2D6A+Rjvr0X9SgA4Wo8BFqjLyQOxVFNsFkMRVCuKyhxHatQEsARQrGVI7sHthdQ/PioIJmLIdXG0VDV6/yf9l/Ddk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723824011; 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=ZnmyZb8ZsZsqyRaHUWHa0sP0izQLZKdxoyiF8FHPV0U=; b=aJAoZCE64jyCo8jI3QvcaH6k4lFr02zW+9hcz8zif0YxxHloeKDg+UeURiCWHHozPN3HWq RF+PWO1uRJT0ikVGlo4P9pkPDFHEEI4jesctVRFtiLMkirg8G8PRWki5FGLMjyoCngHbiB 7onrNiUi7R/U199rhQOEUKXY6eCN2Yk= Received: from mail-yb1-f198.google.com (mail-yb1-f198.google.com [209.85.219.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-619-ZlftZ1MOPD6nlQUUIqMouA-1; Fri, 16 Aug 2024 12:00:10 -0400 X-MC-Unique: ZlftZ1MOPD6nlQUUIqMouA-1 Received: by mail-yb1-f198.google.com with SMTP id 3f1490d57ef6-e03b3f48c65so3309412276.0 for ; Fri, 16 Aug 2024 09:00:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723824007; x=1724428807; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZnmyZb8ZsZsqyRaHUWHa0sP0izQLZKdxoyiF8FHPV0U=; b=UkNj7120q3qwEpVk5G+2QlrfPLN9I9j68yZT5Q0ZSM0JvyyZsokDUCESfrF+5nhx13 l4qurhwO/bZLFrSrNNVvaeDv+Ua8I2unLJ/Uq0BheonRVyVFVBFaKwXwpCSZq/m4NzNx 4AZoeE0AEKw26B+ru6WRcdanGaqi/hL1p90HAmFDPFA7chGMoWNwKU6LEilc6Ocwavnj 7xKQ+e98HdPWTZFK96w5lNjBttuPpZJcmmlBPkwn04m8omWdxftSPIk+7jCetUNGnVew vezG8CrUDmWinV8k1SxRDEuc3FV2wfcpnFOSrzKOuAbbyXljwbS62rym0eiJayR3Q0HY Hj7Q== X-Gm-Message-State: AOJu0YzaDh5X8VI5/DfqlS188SDbvL6g5MIpL31W6BISB8rKONJlXB5p izD+9Ze9eAT+HEQI5LqX4vbji3RZUoBCKn1tU1emzxhy9xfPcUV3tGcR7l1HE8Q/PnsxB15YutY BNLq7xnZ55yAKi8xDvZAD4YWE3PGphYQICbduw1hXE+YuXQmCIU4BH/zhauw1HKzzWGfeWebbl9 fnKQVoinNBuxQeP4GFctAjAyhkVlo8MDRL9FnY X-Received: by 2002:a25:aa01:0:b0:e13:cfa6:7fa6 with SMTP id 3f1490d57ef6-e13cfa682b6mr214615276.45.1723824006555; Fri, 16 Aug 2024 09:00:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF7jDGANk50k9TB/s2ANZnDQ0DLFi/17niUm+VD+DPm8F+9/qXgIoKi3x61kyIb4Ay1Ctlrrg== X-Received: by 2002:a25:aa01:0:b0:e13:cfa6:7fa6 with SMTP id 3f1490d57ef6-e13cfa682b6mr214476276.45.1723824004392; Fri, 16 Aug 2024 09:00:04 -0700 (PDT) Received: from localhost.localdomain (ool-18bb2a2e.dyn.optonline.net. [24.187.42.46]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6bf6ff0fbb8sm18661006d6.144.2024.08.16.09.00.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2024 09:00:04 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: default targ eligibility refinement [PR101463] Date: Fri, 16 Aug 2024 12:00:01 -0400 Message-ID: <20240816160001.1826916-1-ppalka@redhat.com> X-Mailer: git-send-email 2.46.0.164.g477ce5ccd6 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.5 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org > > Here during default template argument substitution we wrongly consider > > the (substituted) default arguments v and vt as value-dependent[1] > > which ultimately leads to deduction failure for the calls. > > > > The bogus value_dependent_expression_p result aside, I noticed > > type_unification_real during default targ substitution keeps track of > > whether all previous targs are known and non-dependent, as is the case > > for these calls. And in such cases it should be safe to avoid checking > > dependence of the substituted default targ and just assume it's not. > > This patch implements this optimization, which lets us accept both > > testcases by sidestepping the value_dependent_expression_p issue > > altogether. > > Hmm, maybe instead of substituting and asking if it's dependent, we should > specifically look for undeduced parameters. Makes sense, like so? Bootstrapped and regtested on x86_64-pc-linux-gnu. PR c++/101463 gcc/cp/ChangeLog: * pt.cc (type_unification_real): Directly look for undeduced parameters in the default argument instead of substituting and asking if it's dependent. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/nontype6.C: New test. * g++.dg/cpp1z/nontype6a.C: New test. --- gcc/cp/pt.cc | 41 ++++++++++++++------------ gcc/testsuite/g++.dg/cpp1z/nontype6.C | 24 +++++++++++++++ gcc/testsuite/g++.dg/cpp1z/nontype6a.C | 25 ++++++++++++++++ 3 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/nontype6.C create mode 100644 gcc/testsuite/g++.dg/cpp1z/nontype6a.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 8725a5eeb3f..ad0f73c2f43 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -23607,28 +23607,31 @@ type_unification_real (tree tparms, is important if the default argument contains something that might be instantiation-dependent like access (87480). */ processing_template_decl_sentinel s (!any_dependent_targs); - tree substed = NULL_TREE; - if (saw_undeduced == 1 && !any_dependent_targs) + + tree used_tparms = NULL_TREE; + if (saw_undeduced == 1) { - /* First instatiate in template context, in case we still - depend on undeduced template parameters. */ - ++processing_template_decl; - substed = tsubst_template_arg (arg, full_targs, complain, - NULL_TREE); - --processing_template_decl; - if (substed != error_mark_node - && !uses_template_parms (substed)) - /* We replaced all the tparms, substitute again out of - template context. */ - substed = NULL_TREE; + tree tparms_list = build_tree_list (size_int (1), tparms); + used_tparms = find_template_parameters (arg, tparms_list); + for (; used_tparms; used_tparms = TREE_CHAIN (used_tparms)) + { + int level, index; + template_parm_level_and_index (TREE_VALUE (used_tparms), + &level, &index); + if (TREE_VEC_ELT (targs, index) == NULL_TREE) + break; + } } - if (!substed) - substed = tsubst_template_arg (arg, full_targs, complain, - NULL_TREE); - if (!uses_template_parms (substed)) - arg = convert_template_argument (parm, substed, full_targs, - complain, i, NULL_TREE); + if (!used_tparms) + { + /* All template parameters used within this default argument + are deduced, so we can use it. */ + arg = tsubst_template_arg (arg, full_targs, complain, + NULL_TREE); + arg = convert_template_argument (parm, arg, full_targs, + complain, i, NULL_TREE); + } else if (saw_undeduced == 1) arg = NULL_TREE; else if (!any_dependent_targs) diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype6.C b/gcc/testsuite/g++.dg/cpp1z/nontype6.C new file mode 100644 index 00000000000..06cd234cc61 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/nontype6.C @@ -0,0 +1,24 @@ +// PR c++/101463 +// { dg-do compile { target c++17 } } + +int a; + +int& v = a; + +template +void f(int) { } + +template +void g(T) { } + +template +int& vt = a; + +template> +void h(T) { } + +int main() { + f(0); + g(0); + h(0); +} diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype6a.C b/gcc/testsuite/g++.dg/cpp1z/nontype6a.C new file mode 100644 index 00000000000..8bc40a0505c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/nontype6a.C @@ -0,0 +1,25 @@ +// PR c++/101463 +// A version of nontype6.C where v and vt are constexpr. +// { dg-do compile { target c++17 } } + +int a; + +constexpr int& v = a; + +template +void f(int) { } + +template +void g(T) { } + +template +constexpr int& vt = a; + +template> +void h(T) { } + +int main() { + f(0); + g(0); + h(0); +}