From patchwork Fri Jun 14 14:18:09 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: 1947934 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=FYxuRpFx; 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 4W11c40YQTz20X9 for ; Sat, 15 Jun 2024 00:19:00 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3070C3882657 for ; Fri, 14 Jun 2024 14:18:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by sourceware.org (Postfix) with ESMTPS id B81E6388206F for ; Fri, 14 Jun 2024 14:18:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B81E6388206F 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 B81E6388206F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::22f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718374713; cv=none; b=uQ0469yQ6QOgdmENIq/82YjpvtH7U84FFcstLc6yKuHv9zpV1eRE3YqHSy0p7XOVrM2zVwJGTaUDOI7bSUdUL8DmcnL6Xt44S3YIOG65l+p4F158w4aGhiwP3+u3wkOhwi2xVpGaLY/HtkvTevN0Q7wJK1qXHWo6dtYD3x2DbvU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718374713; c=relaxed/simple; bh=bXOhteFKxuE6NpwLhMEZhlHfPgibsyH8LLEWZZf8yNc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=UEESzDBRCliCPXVePA6UiKRdmFycYCeA/b9ZNIJZsLVv3ZgMgSFPVNXWY+2ClMPwQ9KQkau+uIQ7ce2j6zmJ2gR/PhSsFMDqgCAquRirBWz1wm7eZmg5VsOkMkNDn7sVEifDgQx2Q+D4gMWrPuzsQdkXVFo8mRWauWaLwjRzdN0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lj1-x22f.google.com with SMTP id 38308e7fff4ca-2e95a75a90eso23065361fa.2 for ; Fri, 14 Jun 2024 07:18:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718374701; x=1718979501; 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=7PgucRyHM6mPXBKycQ3V44F1FCNZDFTYsNnSm3DHIw8=; b=FYxuRpFx3h1AQHo//IFfXNngZSdT30nC295KpKF7Fqj2eAA+A+SjvIdibV+ykNA4/l Nt/tFuu1736VwHtgEXQ2sUbNaYtPbeUl7Wql6uKshzBlskLJ1eS3eFyVbSb8McB3TCW3 nJKfSdPtJ1ncJQvbL3SK2BtxybVYGd1/57fdIjezg2wrtTKg08WtDHYs93ddxsdyDOpj Eio5hYSj11AFoq922f38EsM0syV/WzIbE+T32BxzAV95vU7v8KnZtB3sueU7JAjQEypi 0zRSieML3zTOcePpabVdBPGCZjxm1ND7KIOTY0FHYv0avH1UXIfDNLs/PmqXIc3wgfLM ceag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718374701; x=1718979501; 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=7PgucRyHM6mPXBKycQ3V44F1FCNZDFTYsNnSm3DHIw8=; b=g+3mrK53BJR7ay2SfEHatd+U5faKP6q+1cfwAO5EVxUqF4rXavcT5J88CM378C4WIi Vy5FmfriXYS7ahSrOp8SAXqBKJ4+aS97rSI5g6q90sgidtRDGf3CLLyjwKj26xQzoMne iYxYxejjInw9AZbSKtZrCe+I7Q8DJSLxDDWZJMgExk52T/k9f7ywt95Lrjz4THP/jkoJ p1okNm4RTuYxsQTA8YNtbpRqnK360YuqM5ucz+0mwniSRuxisY9+sO+9KEYs/n2etuP7 m65AuaBBOjJaeiDyrUVMTzjeCodUFFJbq3NoWJoUCLKm4G0Uhn1dJTe7gIejDN1sC2S0 64Bg== X-Gm-Message-State: AOJu0Ywrvhhdg/IEfBFcW2RapD+adRTt58jpsladqKQ/Or+mYXNthOe/ nrBXZ/1cDyeFn8PWr+sqYxQWWgcaSPALzX1muDwKmBsNgXuYlvUmVpDVDTVV7zw= X-Google-Smtp-Source: AGHT+IGewmzFocXCEE36+1rYIJ8CchbrsWUW0wESMWQjTagH225ur2xQ33z98nEDqRitpnzlLbAlhA== X-Received: by 2002:a2e:9f13:0:b0:2ec:1a1e:d571 with SMTP id 38308e7fff4ca-2ec1a1edab2mr8747381fa.51.1718374700577; Fri, 14 Jun 2024 07:18:20 -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 5b1f17b1804b1-422d0be1424sm67676385e9.12.2024.06.14.07.18.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 07:18:20 -0700 (PDT) From: Seyed Sajad Kahani To: gcc-patches@gcc.gnu.org Cc: ppalka@redhat.com, jason@redhat.com, Seyed Sajad Kahani Subject: [PATCH v5] c++: fix constained auto deduction in templ spec scopes [PR114915] Date: Fri, 14 Jun 2024 15:18:09 +0100 Message-ID: <20240614141809.946977-1-sska1377@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-10.8 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, 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 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). This patch resolves PR114915 by replacing the logic that fills in the missing levels in do_auto_deduction in cp/pt.cc. The new approach now trims targs if the depth of targs is deeper than desired (this will only happen in specific contexts), and still fills targs with empty layers if it has fewer depths than expected. PR c++/114915 gcc/cp/ChangeLog: * pt.cc (do_auto_deduction): Handle excess outer template arguments during constrained auto satisfaction. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-placeholder14.C: New test. * g++.dg/cpp2a/concepts-placeholder15.C: New test. * g++.dg/cpp2a/concepts-placeholder16.C: New test. --- gcc/cp/pt.cc | 20 ++++++++--- .../g++.dg/cpp2a/concepts-placeholder14.C | 19 +++++++++++ .../g++.dg/cpp2a/concepts-placeholder15.C | 26 +++++++++++++++ .../g++.dg/cpp2a/concepts-placeholder16.C | 33 +++++++++++++++++++ 4 files changed, 94 insertions(+), 4 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 32640f8e9..2206d9ffe 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -31253,6 +31253,19 @@ do_auto_deduction (tree type, tree init, tree auto_node, full_targs = add_outermost_template_args (tmpl, full_targs); full_targs = add_to_template_args (full_targs, targs); + int want = TEMPLATE_TYPE_ORIG_LEVEL (auto_node); + int have = TMPL_ARGS_DEPTH (full_targs); + + if (want < have) + { + /* If a constrained auto is declared in an explicit specialization. */ + gcc_assert (context == adc_variable_type || context == adc_return_type + || context == adc_decomp_type); + tree trimmed_full_args + = get_innermost_template_args (full_targs, want); + full_targs = trimmed_full_args; + } + /* HACK: Compensate for callers not always communicating all levels of outer template arguments by filling in the outermost missing levels with dummy levels before checking satisfaction. We'll still crash @@ -31260,11 +31273,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))) + if (want > have) { - tree dummy_levels = make_tree_vec (missing_levels); - for (int i = 0; i < missing_levels; ++i) + tree dummy_levels = make_tree_vec (want - have); + for (int i = 0; i < want - have; ++i) TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0); full_targs = add_to_template_args (dummy_levels, full_targs); } 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..b507e4165 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-placeholder15.C @@ -0,0 +1,26 @@ +// 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; + +template +struct S {}; + +template +int y = 0; + +template +C auto y> = 'c'; + +int main() { + if (y> != 'c') + return 1; + 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; +}