From patchwork Fri May 7 16:33:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1475592 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=kJRawI5g; dkim-atps=neutral Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FcGHX3mfFz9sXV for ; Sat, 8 May 2021 02:34:14 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6C1F93857C75; Fri, 7 May 2021 16:34:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6C1F93857C75 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1620405252; bh=Jrkmmq+ref7FH7fjK2MiBDwVi/WOb0jhPx1jJ6tXysY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=kJRawI5gPvWrVJ7SjfNfnQveIQaAwpqlvr03FE11PIq1COC8H6EqmTYCsgkzSpKnS ODv5T02bYcXJVKRTHbIKPiIqluuMAnvXT38gHqo18UKYjEWKJTuUH3MBR/cBCyXMpy HPFtcZ4AcOP03fUpPbqgVeqSwvJOu01U+6W1Zut4= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 8DE8C3857C75 for ; Fri, 7 May 2021 16:34:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8DE8C3857C75 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-465-ZEISZH_3Pp6_5D33BWTDyQ-1; Fri, 07 May 2021 12:34:03 -0400 X-MC-Unique: ZEISZH_3Pp6_5D33BWTDyQ-1 Received: by mail-qk1-f197.google.com with SMTP id u9-20020a05620a4549b02902e956c2a3c8so6539464qkp.20 for ; Fri, 07 May 2021 09:34:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Jrkmmq+ref7FH7fjK2MiBDwVi/WOb0jhPx1jJ6tXysY=; b=uaLXbGGeRwBwDl01vlX/oSIhgZOwzsbW5Lj3Emk/sRrxr8u1nSpVbtohW2qdclGFYv 3LxRB5PC7EuCb6SP99LiaTACmm3vj8HPNUcQZEBveqVnPYP3HFnzXK3bQWraCdOdIaDz XtnMz3SOZSwMUPBox7mEP7T8lqthqifSU+dbmBvg/iimTGcDk6xuVzXquFyrcA2LZ1ze i3Q9CsjKSAUkV9nRiu7ZL29gMjpFilhADcyJkssK0GuNyRDnEFpr2zo7n7danWbeKfWt F1Ib63Pu9BNsB3oYE/Ro/mgBctdqcqCIDx/t8udt+iNN0hdE126hvLV+I0Gk3iTVtf4I KLcQ== X-Gm-Message-State: AOAM532Bxq5vWO3hKsPPYUbP584rRpyO9PxP9n0Fde5yoWdRTMpqjqus NmgBpAiwU6pgeMCHXhzKZdq9DftQKikPp4XSgd0kGNXhqxiNlYTNWWZpFsvHII8bPqZHlTN58dR VMRq87m8vvLRdBniaSXjKe4JcCnBpcYVYNBLSQBcV3t0blyfSvhJIMMDvi3L/aezlZgg= X-Received: by 2002:a37:7e03:: with SMTP id z3mr10742816qkc.367.1620405243019; Fri, 07 May 2021 09:34:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzn4smL44HYLyrFQcKo9K/xTXPdFySNZ/XihhVEN8OYX4MOXQqpYhg8hrjqGE6F88j/HEyjmQ== X-Received: by 2002:a37:7e03:: with SMTP id z3mr10742783qkc.367.1620405242635; Fri, 07 May 2021 09:34:02 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id f12sm5177630qti.63.2021.05.07.09.34.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 May 2021 09:34:02 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: argument pack expansion inside constraint [PR100138] Date: Fri, 7 May 2021 12:33:59 -0400 Message-Id: <20210507163359.3079556-1-ppalka@redhat.com> X-Mailer: git-send-email 2.31.1.442.g7e39198978 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-15.0 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_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_SBL, URIBL_SBL_A autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" This PR is about CTAD but the underlying problems are more general; CTAD is a good trigger for them because of the necessary substitution into constraints that deduction guide generation entails. In the testcase below, when generating the implicit deduction guide for the constrained constructor template for A, we substitute the generic flattening map 'tsubst_args' into the constructor's constraints. During this substitution, tsubst_pack_expansion returns a rebuilt pack expansion for sizeof...(xs), but it's neglecting to carry over the PACK_EXPANSION_LOCAL_P (and PACK_EXPANSION_SIZEOF_P) flag from the original tree to the rebuilt one. The flag is otherwise unset on the original tree[1] but set for the rebuilt tree from make_pack_expansion only because we're doing the CTAD at function scope (inside main). This leads us to crash when substituting into the pack expansion during satisfaction because we don't have local_specializations set up (it'd be set up for us if PACK_EXPANSION_LOCAL_P is unset) Similarly, when substituting into a constraint we need to set cp_unevaluated since constraints are unevaluated operands. This avoids a crash during CTAD for C below. [1]: Although the original pack expansion is in a function context, I guess it makes sense that PACK_EXPANSION_LOCAL_P is unset for it because we can't rely on local specializations (which are formed when substituting into the function declaration) during satisfaction. Bootstrapped and regtested on x86_64-pc-linux-gnu, also tested on cmcstl2 and range-v3, does this look OK for trunk? gcc/cp/ChangeLog: PR c++/100138 * constraint.cc (tsubst_constraint): Set up cp_unevaluated. (satisfy_atom): Set up iloc_sentinel before calling cxx_constant_value. * pt.c (tsubst_pack_expansion): When returning a rebuilt pack expansion, carry over PACK_EXPANSION_LOCAL_P and PACK_EXPANSION_SIZEOF_P from the original pack expansion. gcc/testsuite/ChangeLog: PR c++/100138 * g++.dg/cpp2a/concepts-ctad4.C: New test. --- gcc/cp/constraint.cc | 6 ++++- gcc/cp/pt.c | 2 ++ gcc/testsuite/g++.dg/cpp2a/concepts-ctad4.C | 25 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-ctad4.C diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index 0709695fd08..30fccc46678 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -2747,6 +2747,7 @@ tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl) /* We also don't want to evaluate concept-checks when substituting the constraint-expressions of a declaration. */ processing_constraint_expression_sentinel s; + cp_unevaluated u; tree expr = tsubst_expr (t, args, complain, in_decl, false); return expr; } @@ -3005,7 +3006,10 @@ satisfy_atom (tree t, tree args, sat_info info) /* Compute the value of the constraint. */ if (info.noisy ()) - result = cxx_constant_value (result); + { + iloc_sentinel ils (EXPR_LOCATION (result)); + result = cxx_constant_value (result); + } else { result = maybe_constant_value (result, NULL_TREE, diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 36a8cb5df5d..0d27dd1af65 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -13203,6 +13203,8 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, else result = tsubst (pattern, args, complain, in_decl); result = make_pack_expansion (result, complain); + PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t); + PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t); if (PACK_EXPANSION_AUTO_P (t)) { /* This is a fake auto... pack expansion created in add_capture with diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-ctad4.C b/gcc/testsuite/g++.dg/cpp2a/concepts-ctad4.C new file mode 100644 index 00000000000..95a3a22dd04 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-ctad4.C @@ -0,0 +1,25 @@ +// PR c++/100138 +// { dg-do compile { target c++20 } } + +template +struct A { + A(T, auto... xs) requires (sizeof...(xs) != 0) { } +}; + +constexpr bool f(...) { return true; } + +template +struct B { + B(T, auto... xs) requires (f(xs...)); // { dg-error "constant expression" } +}; + +template +struct C { + C(T, auto x) requires (f(x)); // { dg-error "constant expression" } +}; + +int main() { + A x{1, 2}; // { dg-bogus "" } + B y{1, 2}; // { dg-error "deduction|no match" } + C z{1, 2}; // { dg-error "deduction|no match" } +}