From patchwork Fri Jan 5 20:01:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1883110 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=E3oaRnTi; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4T6Dqq6Xxcz1yP3 for ; Sat, 6 Jan 2024 07:01:43 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ECF26385803B for ; Fri, 5 Jan 2024 20:01:40 +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 ESMTPS id 4EE963858D1E for ; Fri, 5 Jan 2024 20:01:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4EE963858D1E 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 4EE963858D1E 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=1704484884; cv=none; b=QilgAM2ke9zi0qooJqLA+vM7aQ3TEMHv2p/BW93f+qfTaX64+U++qGwIVjBDW5oWft8Zk2l07dAccG9Wub86rmQp9Rn/nYN6knDRepu7vGkqmI+vrjHiePXrH1ziNtzN+XUKkZjGGpyG+KaAALL9Dd2rdYKRIqMbW8k1ZOqlEpM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704484884; c=relaxed/simple; bh=zawKR7DgS33VV3e0mSl4KsgviwpFhf5PFJuDVPjeAL8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Q/C5X9Vih1eEViHy9ADd/NP85HdFEDpRNOcS60A7gAiH0Iwx0D4yKl03cCgIGQ35BqDt4+q1VWVnvoZpgbIbkKCHFyRxi9HmPDHsklkW9i27fvEXWyM1BQHkeNB5GXZo5xSYmxvX38wohFLrHfgD4kYmr0Oa3gK3F6RsnyDuDJg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704484881; 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=bVn5NplYiVfBL2wB7rZayGxhtu4wSBa7C1A7HduCKUU=; b=E3oaRnTikmPDEkZTc0nyNq8c+CXx0XQXHuCsqFRDNN7oyHDwNy11hgtkQ7MAZB15cMiI+4 lJMHlIisBtT8SgUQ/fYFOZoWf49Kin+YLA9eUl5e4+FnRYj1SySF6SJZF68i9zAsRuYw/a owFBUS9cvmX8hBdhCGi0i8uT+QjPfLg= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-477-CXDn1mpuNhyC2hE-cCN0DQ-1; Fri, 05 Jan 2024 15:01:20 -0500 X-MC-Unique: CXDn1mpuNhyC2hE-cCN0DQ-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-7816e45f957so298655985a.1 for ; Fri, 05 Jan 2024 12:01:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704484879; x=1705089679; 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=bVn5NplYiVfBL2wB7rZayGxhtu4wSBa7C1A7HduCKUU=; b=BDOIuQ3t6xbtxU8Yw75sWqQSYdBEdX437uORu8DhmB34J7tjMLayqwDDlUx84/tXWp ZL3oF/TvC4PYQN337kgvS1/SfMQ4m/7XgGqi2Pjamnx7F5ZG1EdReA9ZvLRRLGfXY+3O rpoQVkORfMAo41deiAD6Z5lHrV9wXDsCVlGfd8e4MPQgx398akbdnYH7Y054h0Bq/TaW uEihmr+gr+b8zVoMEdQg0e40PbA/YNgIA9jBnZ2wK/PkOBsXlMFaqDcm8cSYuyYQHzIO 5uoV9gjfCWWw1xQOLwN/SVW40RSSacvLI4FcsNQu9xagDNi/Fg31R2fdFfX54RC1zkR0 7JEg== X-Gm-Message-State: AOJu0YzGKpCNIb+h7hDUZs/JCeVGSRNmMaFyeSYGlA6p3BPlX0/mKaV6 IgtR3eCwLukRPSv5cwC2YDhS8ab73SKmJCKcWiSpB7yUhBCKEtJ172LWL0hcIDcqMZeXAhDgCsS OLk+wO322LZpYc9klpoZzBEEyFMuWi2J3Ney6ZObDQybskyqQJ18nvhwzadxAJEZGLtDV4F+79q SlylFx+3o= X-Received: by 2002:a05:620a:1aa7:b0:783:52b:518f with SMTP id bl39-20020a05620a1aa700b00783052b518fmr2872201qkb.75.1704484879185; Fri, 05 Jan 2024 12:01:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IHGm/CUfAt1YtwIEVToRTtKu5Qakv1ujwaxywkWjjizaSufu84Uaac9oYvX9nP0EvlusCqb1g== X-Received: by 2002:a05:620a:1aa7:b0:783:52b:518f with SMTP id bl39-20020a05620a1aa700b00783052b518fmr2872191qkb.75.1704484878838; Fri, 05 Jan 2024 12:01:18 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id c1-20020a05620a268100b00781baa4db60sm816149qkp.66.2024.01.05.12.01.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 12:01:18 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: reference variable as default targ [PR101463] Date: Fri, 5 Jan 2024 15:01:16 -0500 Message-ID: <20240105200116.1382389-1-ppalka@redhat.com> X-Mailer: git-send-email 2.43.0.254.ga26002b628 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.9 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, 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 Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? -- >8 -- 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. [1]: The reason we consider these reference variables value-dependent is due to a workaround in value_dependent_expression_p: case VAR_DECL: ... else if (TYPE_REF_P (TREE_TYPE (expression))) /* FIXME cp_finish_decl doesn't fold reference initializers. */ return true; ... added by r5-5022-g51d72abe5ea04e. I'm not sure if this workaround is needed anymore, but naively removing it seems safe as far as bootstrap+regtest is concerned (the only change is that we issue more -Wmissing-braces warnings ahead of time in cpp0x/initlist123.C), and lets us accept the first testcase. Unfortunately we still reject the second testcase (in which v and vt are additionally constexpr) for the same reason (bogus value dependence) due to the subsequent check in v_d_e_p: ... /* We have a constexpr variable and we're processing a template. When there's lifetime extension involved (for which finish_compound_literal used to create a temporary), we'll not be able to evaluate the variable until instantiating, so pretend it's value-dependent. */ else if (DECL_DECLARED_CONSTEXPR_P (expression) && !TREE_CONSTANT (expression)) return true; And TREE_CONSTANT isn't set for v and vt because of a workaround in cp_finish_decl: if (decl_maybe_constant_var_p (decl) /* FIXME setting TREE_CONSTANT on refs breaks the back end. */ && !TYPE_REF_P (type)) TREE_CONSTANT (decl) = true; Naively removing this workaround lets us accept the second testcase, but it re-introduces an ICE in g++.dg/opt/pr78373.C. PR c++/101463 gcc/cp/ChangeLog: * pt.cc (type_unification_real): Avoid checking dependence of a substituted default template argument if we can assume it's non-dependent. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/nontype6.C: New test. * g++.dg/cpp1z/nontype6a.C: New test. --- gcc/cp/pt.cc | 9 +++++++-- gcc/testsuite/g++.dg/cpp1z/nontype6.C | 24 ++++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp1z/nontype6a.C | 25 +++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 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 7208c721b0b..b801ce1f18c 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -23304,6 +23304,7 @@ type_unification_real (tree tparms, might be instantiation-dependent like access (87480). */ processing_template_decl_sentinel s (!any_dependent_targs); tree substed = NULL_TREE; + tristate dependent_p = tristate::unknown (); if (saw_undeduced == 1 && !any_dependent_targs) { /* First instatiate in template context, in case we still @@ -23312,8 +23313,9 @@ type_unification_real (tree tparms, substed = tsubst_template_arg (arg, full_targs, complain, NULL_TREE); --processing_template_decl; + dependent_p = uses_template_parms (substed); if (substed != error_mark_node - && !uses_template_parms (substed)) + && dependent_p.is_false ()) /* We replaced all the tparms, substitute again out of template context. */ substed = NULL_TREE; @@ -23321,8 +23323,11 @@ type_unification_real (tree tparms, if (!substed) substed = tsubst_template_arg (arg, full_targs, complain, NULL_TREE); + if (dependent_p.is_unknown ()) + dependent_p = (processing_template_decl + && uses_template_parms (substed)); - if (!uses_template_parms (substed)) + if (dependent_p.is_false ()) arg = convert_template_argument (parm, substed, full_targs, complain, i, NULL_TREE); else if (saw_undeduced == 1) 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); +}