From patchwork Sat May 4 15:15:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyed Sajad Kahani X-Patchwork-Id: 1931359 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=HoyANVhd; 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 4VWrpc2qNcz1yb7 for ; Sun, 5 May 2024 01:15:52 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D34E23858283 for ; Sat, 4 May 2024 15:15:49 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by sourceware.org (Postfix) with ESMTPS id 5F1063858D39 for ; Sat, 4 May 2024 15:15:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5F1063858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5F1063858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::336 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714835732; cv=none; b=NjSMo2gndp9Eq8oPaGqgyntgYxzQH4kwwRnJorMZMXkdXXUw8en6J6U1pA2ekx+Y1eY3h4SdmWrIklWkzcUJkYhgH06AbLx6RkDLSs1f783YolF/3cVczbook3klLdh6EIL33gJ+QlR3qdYTt91Ys2c7a1iHRFVb5MRkD/y+ovw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714835732; c=relaxed/simple; bh=lAcuUx4ReaQ31KgSqqdC6AQMdKfhKCeTBrkpH7ZLK3Y=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=sUEbBojIuQEGv4In64NwZVn1HJdVFbOF3z+rFKifYvov9h/GvhUX+BabgUDYn3G3AcfELa7N0PcvCC9wJaFnrR+OGa6uK/PWuHzwth0cDsvIWw8XpzLB+ulNvdMhxlLfzWsxhUKXgDyosPg3KJAM4sNjP4zIuKcE8pSL5uy7mTc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-41ba1ba55e8so4125815e9.1 for ; Sat, 04 May 2024 08:15:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714835729; x=1715440529; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KcmejtxIHI3RJuDLdeGUdgTf5F/V2579F8Z4I3yJKdc=; b=HoyANVhdZIQNFfKXuYf0Lxx6Jo+zokQOBY5DdoQSqpj6TkQ4Vluc06YzWb+PxUscyk F4oo+VkL5LSzcYMNS4pC9yeXz+/M3NffILRvgMzHYPPhV6UGpfoKnCWhh9KTaQ7ao/eg ZLAn8E7pRzdUN84rYXcDdZniqC5EO9aK7vdDb01kfVSkl+ksemI8A9yysFXUt1jYQ0Es 8/9/Ctxveebd4PiqYwo4+ZfScUFrndoiSq31rRTx/UkLXiozLXXtMuFq3VqiWYU3DIyn 5AODZMBdf+it0QG2I7UrcIITP2Pk0K8bHhF6vMFBHIy6QB6fs5sor/01UfwGNdwtOPpv RahQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714835729; x=1715440529; 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=KcmejtxIHI3RJuDLdeGUdgTf5F/V2579F8Z4I3yJKdc=; b=J7O9MeNXGOOhj7HEMbM/xADDspoW0c8noVlKANF+CAj7JFkV3EqlTf2eKFIZAYBHEz iAJIwgu+Ibv9IGko2+rUEK3MFQCTbxX9FJMwZaVSgB06G8IZcnqAcvy8BAv7mCZeunHc CXujv9XM1SPq2IhDN7FXs1w+5CKlwPHuA7Dl1kByYhmR1+YqdV009Eqf5noZX+sLsoE4 sWkvoq5U+dEhhmb4oYcjA67Fq09Edxa0y+tmQgVRtCW+6P08bp3Xaf8e7s1Pcy+LBjxV ZELx2AHdH//PIT8HbOfcI2RBhpDUELZfF3tEhqj+uEqEUDBMEJs8MUQdmdfUz4inKu+k o7Tw== X-Gm-Message-State: AOJu0Yzyws0WdWgGZal3QIgvsj44L1bEtsVLV4I1UGMYOhB2EUck/ML3 BuzSR77bT/+XRogIFHUNY+sl4ECPQszdt3YguvsInaspHV/bvRfxl6oxQCIfs584yw== X-Google-Smtp-Source: AGHT+IHB/E5/QdRdJQUbJZV49Sb/QCC8N2UGsux1r/0aghNIKbkfKUNP7hKPc9myugOAQyVCCftKcQ== X-Received: by 2002:a05:600c:1e8e:b0:419:a1b:d11f with SMTP id be14-20020a05600c1e8e00b004190a1bd11fmr4559485wmb.17.1714835728606; Sat, 04 May 2024 08:15:28 -0700 (PDT) Received: from localhost.localdomain (cpc86130-nfds16-2-0-cust181.8-2.cable.virginm.net. [82.11.162.182]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b0041a1fee2854sm13311090wmq.17.2024.05.04.08.15.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 May 2024 08:15:28 -0700 (PDT) From: Seyed Sajad Kahani To: gcc-patches@gcc.gnu.org Cc: ppalka@redhat.com, jason@redhat.com, Seyed Sajad Kahani Subject: [PATCH v2] Fix auto deduction for template specialization scopes [PR114915] Date: Sat, 4 May 2024 16:15:03 +0100 Message-ID: <20240504151503.661288-1-sska1377@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.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 The limitations of the initial patch (checking specializiation template usage), have been discussed. > I realized that for the case where we have a member function template > of a class template, and a specialization of the enclosing class only > (like below), > > template <> > template > void S::f() { > // some constrained auto > } > > When using S::f, DECL_TEMPLATE_INFO(fn) is non-zero, and > DECL_TEMPLATE_SPECIALIZATION(fn) is zero, while > DECL_TEMPLATE_SPECIALIZATION(DECL_TI_TEMPLATE(fn)) is non-zero. > So it means that the patch will extract DECL_TI_ARGS(fn) as > outer_targs, and it would be while the type of the > constrained auto will be as template ... and will not be > dependent on the parameters of the enclosing class. > This means that again (outer_targs + targs) will have more depth than > auto_node levels. > This means that for the case where the function is not an explicit > specialization, but it is defined in an explicitly specialized scope, > the patch will not work. As described in more detail below, this patch attempts to resolve this issue by trimming full_targs. > > Another more context-unaware approach to fix this might be to only > > use the innermost level from 'full_targs' for satisfaction if > > TEMPLATE_TYPE_ORIG_LEVEL is 1 (which means this constrained auto > > appeared in a context that doesn't have template parameters such as an > > explicit specialization or ordinary non-template function, and so > > only the level corresponding to the deduced type is needed for > > satisfaction.) > > > > Generalizing on that, another approach might be to handle missing_levels < 0 > > by removing -missing_levels from full_targs via get_innermost_template_args. > > But AFAICT it should suffice to handle the TEMPLATE_TYPE_ORIG_LEVEL == 1 > > case specifically. > > I was unable to understand why you think that it might not handle > TEMPLATE_TYPE_ORIG_LEVEL > 1 cases, so I tried to formulate my > reasoning as follows. > > Assuming contexts adc_variable_type, adc_return_type, adc_decomp_type: > For any case where missing_level < 0, it means that the type depends > on fewer levels than the template arguments used to materialize it. > This can only happen when the type is defined in an explicit > specialization scope. This explicit specialization might not occur in > its immediate scope. > Note that partial specialization (while changing the set of > parameters) cannot reduce the number of levels for the type. > Because of the fact that the enclosing scope of any explicit > specialization is explicitly specialized > (https://eel.is/c++draft/temp.expl.spec#16), the type will not depend > on template parameters outside of its innermost explicit specialized > scope. > Assuming that there are no real missing levels, by removing those > levels, missing_level should be = 0. As a result, by roughly doing > > if (missing_levels < 0) { > tree trimmed_full_args = get_innermost_template_args(full_targs, > TEMPLATE_TYPE_ORIG_LEVEL(auto_node)); > full_targs = trimmed_full_args; > } > in pt.cc:31262, where we calculate and check missing_levels, we would > be able to fix the errors. > Note that, for the case where there are real missing levels, we are > putting irrelevant template arguments for the missing levels instead > of make_tree_vec(0). By this change: > - If the type is independent of those missing levels: it works fine either way. > - If the type is dependent on those missing levels: Instead of raising > an ICE, the compiler exhibits undefined behavior. --- gcc/cp/pt.cc | 14 ++++++-- .../g++.dg/cpp2a/concepts-placeholder14.C | 19 +++++++++++ .../g++.dg/cpp2a/concepts-placeholder15.C | 15 +++++++++ .../g++.dg/cpp2a/concepts-placeholder16.C | 33 +++++++++++++++++++ 4 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-placeholder14.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-placeholder15.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-placeholder16.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 3b2106dd3..bdf03a1a7 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -31044,7 +31044,8 @@ unparenthesized_id_or_class_member_access_p (tree init) OUTER_TARGS is used during template argument deduction (context == adc_unify) to properly substitute the result. It's also used in the adc_unify and adc_requirement contexts to communicate the necessary template arguments - to satisfaction. OUTER_TARGS is ignored in other contexts. + to satisfaction. OUTER_TARGS will be used for other contexts if it is a + function scope deduction. Otherwise it is ignored. Additionally for adc_unify contexts TMPL is the template for which TYPE is a template parameter type. @@ -31260,8 +31261,15 @@ do_auto_deduction (tree type, tree init, tree auto_node, these missing levels, but this hack otherwise allows us to handle a large subset of possible constraints (including all non-dependent constraints). */ - if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node) - - TMPL_ARGS_DEPTH (full_targs))) + int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node) + - TMPL_ARGS_DEPTH (full_targs)); + if (missing_levels < 0) + { + tree trimmed_full_args = get_innermost_template_args(full_targs, + TEMPLATE_TYPE_ORIG_LEVEL (auto_node)); + full_targs = trimmed_full_args; + } + if (missing_levels > 0) { tree dummy_levels = make_tree_vec (missing_levels); for (int i = 0; i < missing_levels; ++i) diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder14.C b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder14.C new file mode 100644 index 000000000..fcdbd7608 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder14.C @@ -0,0 +1,19 @@ +// PR c++/114915 +// { dg-do compile { target c++20 } } + +template +concept C = __is_same(T, int); + +template +void f() { +} + +template<> +void f() { + C auto x = 1; +} + +int main() { + f(); + return 0; +} diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder15.C b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder15.C new file mode 100644 index 000000000..b4f73f407 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder15.C @@ -0,0 +1,15 @@ +// PR c++/114915 +// { dg-do compile { target c++20 } } + +template +concept C = __is_same(T, U); + +template +int x = 0; + +template<> +C auto x = 1.0; + +int main() { + return 0; +} diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder16.C b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder16.C new file mode 100644 index 000000000..f808ef1b6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder16.C @@ -0,0 +1,33 @@ +// PR c++/114915 +// { dg-do compile { target c++20 } } + +template +concept C = __is_same(T, U); + +template +struct A +{ + template + void f() { + } +}; + +template<> +template<> +void A::f() { + C auto x = 1; +} + +template<> +template +void A::f() { + C auto x = 1; +} + +int main() { + A a; + a.f(); + A b; + b.f(); + return 0; +}