From patchwork Wed May 1 22:52:17 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: 1930362 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=jNgRZ5FU; 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 4VVC5P6zCQz1ydX for ; Thu, 2 May 2024 08:52:56 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 51B60384AB4F for ; Wed, 1 May 2024 22:52:54 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id 078713858D34 for ; Wed, 1 May 2024 22:52:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 078713858D34 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 078713858D34 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::429 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714603956; cv=none; b=tin9TwIEClxcOWPC3HSwb6KPGCH+mc39K3OojICB+8cJ1agYfnSnIXZtpFIOw0uQMUVc15/WGiG+r1MBuE98SqRICvS91quurJop36yjRG99n+vAqs6c+ZPydpQMDozSPtG9uJ4UN5kEMmHRgrJJZEWm77sBKxJRtWcOnpZjznM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714603956; c=relaxed/simple; bh=nJ02oKBFDWeR/123uqpgoTEeSXPUS72ZjIhmftosZIg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=AVtFzhC50lAMEUyBIGKzs2NImBN2oLFBASB53QZlbugjVwLqWNAFt0QZK58XsIVpBsHe/nhCyh3wdZGo65If/eWq+Z1NhJB3QKyk8nQ+B1VT3zhQRwbk45hA/b9PVJ1nV00z8zuyMq+llsFXLmG8MisG0l7Yg3XefcdguwOaKhc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-34d7a32bdd3so1387789f8f.0 for ; Wed, 01 May 2024 15:52:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714603952; x=1715208752; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=5Cmdvvb6MtrrGqN/3GUc5ouY+H3a5JAyI+VyojIF5J8=; b=jNgRZ5FUknc+fMfv/QybQf4xOXQAg6daH0TZI9En3/Tz5NVlnIzsjBJp0Rn6szn+uV hDv6z6mWkWN569Z2DSUi89xoBXsiwYP1Webqzn3rxhbsaoNxu1yM5jah9Iz7q8EqLsAy JHEMSerw6lDnvDYs6HSbmBF3/0FlVqGG6c6A8SO1hxzvSGYYz8CMbZryiwKw8Bwsfxd2 /aMUb3KldEY56tAmHb4EWjL/qASc8B6QE0ymozH4fWZ+SQGLtsEnvQc0FPRrmb2/ycrF ngzHKp9idP6KZTP7oX/H/Fmc0nbYThrvf58fOnKRVDrM9G89/f5Bp9Cr4jTBMWTK+gX5 EFKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714603952; x=1715208752; 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=5Cmdvvb6MtrrGqN/3GUc5ouY+H3a5JAyI+VyojIF5J8=; b=aUBifspEyVhsY0RhJTnNLCmX/9VwANe3znIX5R28+LHXR2hha3zSYLJYCx68kx7CQp 8xz2h7R1/aVx7ZpDGE8dCrPvEiJUAUqdpo78vay1tfjSXjEBHFg/iDcZrsq3KdGY6of5 fkh8fYKbxc3gRVP2dp3hwPpGeB9tsVIgkEKTK/sGbQZqM9fVk7QIcSNGSRmSJfV4LF0u I41goMz4PzFQSKQGEu1v6H5fl/Bd4fZDZ8yFw5eqbqX4URX/mP7KCGiCIaepc43OVQjU UehEx8LGWmAP3ihUs+PtlDR7EIEGZT2z8NFUXfiXhUBDAPjMBpgxkmBJGg/GD+kw2jYD OjcQ== X-Gm-Message-State: AOJu0YzB8RT+DQBRsdG/i1zHkMBzxDJTF2aSZ+gqIIqOioyL52ObtWXW zR0j+aO5JslZTrI3BWGg49eu9bCBvWUrljjBSnoMZ8Pfz8krM8OIEhfNPMl4 X-Google-Smtp-Source: AGHT+IGQnNfRbLjz4gNQTUvjIALVt0hjbaJO7Z4dV6O2IkCCAcf6r9yY0qH+eyseNA+V0HiASDus+A== X-Received: by 2002:a5d:678f:0:b0:34c:5429:497e with SMTP id v15-20020a5d678f000000b0034c5429497emr2538712wru.8.1714603951959; Wed, 01 May 2024 15:52:31 -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 u17-20020adfeb51000000b00347321735a6sm35422110wrn.66.2024.05.01.15.52.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 15:52:31 -0700 (PDT) From: Seyed Sajad Kahani To: gcc-patches@gcc.gnu.org Cc: Seyed Sajad Kahani Subject: [PATCH] Fix auto deduction for template specialization scopes [114915]. Date: Wed, 1 May 2024 23:52:17 +0100 Message-ID: <20240501225217.59069-1-sska1377@gmail.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Spam-Status: No, score=-9.7 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, 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 When deducing auto for `adc_return_type`, `adc_variable_type`, and `adc_decomp_type` contexts (at the usage time), we try to resolve the outermost template arguments to be used for satisfaction. This is done by one of the following, depending on the scope: 1. Checking the `DECL_TEMPLATE_INFO` of the current function scope and extracting DECL_TI_ARGS from it for function scope deductions (pt.cc:31236). 2. Checking the `DECL_TEMPLATE_INFO` of the declaration (alongside with other conditions) for non-function scope variable declaration deductions (decl.cc:8527). Then, we do not retrieve the deeper layers of the template arguments; instead, we fill the missing levels with dummy levels (pt.cc:31260). The problem (that is shown in PR114915) is that we do not consider the case where the deduction happens in a template specialization scope. In this case, the type is not dependent on the outermost template arguments (which are the specialization arguments). Yet, we still resolve the outermost template arguments, and then the number of layers in the template arguments exceeds the number of levels in the type. This causes the missing levels to be negative. This leads to the rejection of valid code and ICEs (like segfault) in the release mode. In the debug mode, it is possible to show as an assertion failure (when creating a tree_vec with a negative size). The code that generates the issue is added to the test suite as `g++.dg/cpp2a/concepts-placeholder14.C`. This patch fixes the issue by checking that the template usage, whose arguments are going to be used for satisfaction, is not a partial or explicit specialization (and therefore it is an implicit or explicit instantiation). This check is done in the two only places that affect the `outer_targs` for the mentioned contexts. One might ask why this is not implemented as a simple `missing_level > 0` check. The reason is that the recovery from the negative `missing_levels` will not be easy, and it is not clear how to recover from it. Therefore, it is better to prevent it from happening. --- gcc/cp/decl.cc | 1 + gcc/cp/pt.cc | 16 ++++++++++----- .../g++.dg/cpp2a/concepts-placeholder14.C | 20 +++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-placeholder14.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 65ab64885..7e51f926e 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -8527,6 +8527,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node) && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl) + && DECL_USE_TEMPLATE (decl) != 2 && !DECL_FUNCTION_SCOPE_P (decl)) /* The outer template arguments might be needed for satisfaction. (For function scope variables, do_auto_deduction will obtain the diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 3b2106dd3..fd646d873 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. @@ -31235,8 +31236,11 @@ do_auto_deduction (tree type, tree init, tree auto_node, if (tree fn = current_function_decl) if (DECL_TEMPLATE_INFO (fn) || LAMBDA_FUNCTION_P (fn)) { - outer_targs = DECL_TEMPLATE_INFO (fn) - ? DECL_TI_ARGS (fn) : NULL_TREE; + outer_targs = NULL_TREE; + if (DECL_TEMPLATE_INFO (fn) && DECL_USE_TEMPLATE (fn) != 2) + { + outer_targs = DECL_TI_ARGS (fn); + } if (LAMBDA_FUNCTION_P (fn)) { /* As in satisfy_declaration_constraints. */ @@ -31260,8 +31264,10 @@ 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 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..4a98ec7b3 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder14.C @@ -0,0 +1,20 @@ +// PR c++/114915 +// { dg-do compile { target c++20 } } + +template +concept TheConcept = __is_same(T, int); + +template +void f() { + TheConcept auto x = 1; +} + +template<> +void f() { + TheConcept auto x = 1; +} + +int main() { + f(); + return 0; +}