From patchwork Tue Oct 31 18:17:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1857698 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=WHyceae6; 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 4SKdfv1tVdz1yQ5 for ; Wed, 1 Nov 2023 05:18:07 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AAF193857B98 for ; Tue, 31 Oct 2023 18:18:04 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id 58E103858418 for ; Tue, 31 Oct 2023 18:17:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 58E103858418 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 58E103858418 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698776272; cv=none; b=c7iLV87eB1agvciJIhYRMfySqXrZjiBEN8hP/++IrLdvw0MBgT3Zeb/KIbIHDGOE7Y5kmqFaNPDxF2It7iSD8wrd0zte/U+r9d43N3I7JVmWEfsmdC3nNwwj9uRAx992b2rRfhq8DPhmHwrIBqeKRljbxOjZa4Wmo7lSDj/9DdA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698776272; c=relaxed/simple; bh=DnLkko9Qw1HtjTpc+hjYz4TQWNJGzGE7steZ2DwEqPw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=uJpvm4cJuRWseC7aytun4V3cN334Y+SXiL1QHOKmAc/cmTfFCn+meuaRJVll5xNHekfJyWb3AKKgLMqsXBUC9IofqdrP1mj2L6f/boRBkpavFbcZzvEmZ7P3KCYKDkDobjB5ENhBgj+jCbClHYpp0t7YZiaLlLM5JOUJ2BUXQYY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698776262; 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=CrOkFgVgik6iOSLZpYuP/YTHPuy9965bXFnPhUtQnOQ=; b=WHyceae6BMmWc/aO+dwRyz4Yf5aGwkxndn100NWdTqxFuKFvaHv+z4wlzuWOZFUuvpyUZK tsupxPWnO5qRoLou4gYL5eezESqFMDKFcTfT9xe0lTAIKqfMpKPF55to+QQjw6Yoey3SPc 006LF6Zr6orVDWI22O0YdWb7qBrT9zs= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-50-o886eu-YPrStnv8OlNAP_A-1; Tue, 31 Oct 2023 14:17:30 -0400 X-MC-Unique: o886eu-YPrStnv8OlNAP_A-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-41cb6df5c7bso67394691cf.3 for ; Tue, 31 Oct 2023 11:17:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698776249; x=1699381049; 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=CrOkFgVgik6iOSLZpYuP/YTHPuy9965bXFnPhUtQnOQ=; b=ubxwaKeDxOrAap6BXErSCUTEZMlekZ1/QsSk9yh4hWB4yRpKn0+cE0U3KaUs2WWYYS PQsXIm4/cNCahKYP1pnsqf6djVIn4SUm3/mY/hYRoiE2UQ5j/zgNR+8/ImCBF+D1l8TP MalzH9FcA9IZ8E8hdTyy/uArZQJRxxEyF0THCYPN7Ee7o8c4aB31zHju1mAmJ9rEepbs Iy97qMozSauduUFF7/77F/tn01Mg76gqfmsn3eupcLSNeA4gva2jWR6QCJ/sehMbQTan tGkLtvUY8RxNVchSJa99isJ9Q22an3x1lUaImATYkeWt8y7vdDt+dX1ZE9OdP8DNMuXE iyyg== X-Gm-Message-State: AOJu0YzPDCycdCl9+DL+8RQvr9NBuaXnfDs7mr461sNZtJ7IqxVjgFRk 1GXbzY5i0uC6F4+Sz20OI03GsdtUxK5hl3L9IwTiC1Ld9R56QxQezfBPuKtOrlYraBah9sOY9Df 1Bvd2m2FRX4kS2MMAp/7mOBEIb7oZTBxTwVtKHjDUjfY8tYV3Enuiz0z4XFTADrgXZ4bU0iNlGu g= X-Received: by 2002:ac8:5943:0:b0:419:5b6c:be62 with SMTP id 3-20020ac85943000000b004195b6cbe62mr15945382qtz.4.1698776248691; Tue, 31 Oct 2023 11:17:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGLEOlJyRzJ9kFUU8BvJ7IET57RWXK8zN05wVX+KmqjMUtSDRGyH9+7PF6Si1S87SVQ9X9e2Q== X-Received: by 2002:ac8:5943:0:b0:419:5b6c:be62 with SMTP id 3-20020ac85943000000b004195b6cbe62mr15945365qtz.4.1698776248271; Tue, 31 Oct 2023 11:17:28 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id ex10-20020a05622a518a00b004198f248e8dsm704706qtb.76.2023.10.31.11.17.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 11:17:27 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: constantness of local var in constexpr fn [PR111703, PR112269] Date: Tue, 31 Oct 2023 14:17:26 -0400 Message-ID: <20231031181726.3944801-1-ppalka@redhat.com> X-Mailer: git-send-email 2.42.0.526.g3130c155df MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.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_H3, 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? Does it look OK for release branches as well for sake of PR111703? -- >8 -- potential_constant_expression was incorrectly treating most local variables from a constexpr function as (potentially) constant because it wasn't considering the 'now' parameter. This patch fixes this by relaxing some var_in_maybe_constexpr_fn checks accordingly, which turns out to partially fix two recently reported regressions: PR111703 is a regression caused by r11-550-gf65a3299a521a4 for restricting constexpr evaluation during warning-dependent folding. The mechanism is intended to restrict only constant evaluation of the instantiated non-dependent expression, but it also ends up restricting constant evaluation (as part of satisfaction) during instantiation of the expression, in particular when resolving the ck_rvalue conversion of the 'x' argument into a copy constructor call. This seems like a bug in the mechanism[1], though I don't know if we want to refine the mechanism or get rid of it completely since the original testcases which motivated the mechanism are fixed more simply by r13-1225-gb00b95198e6720. In any case, this patch partially fixes this by making us correctly treat 'x' and therefore 'f(x)' in the below testcase as non-constant, which prevents the problematic warning-dependent folding from occurring at all. If this bug crops up again then I figure we could decide what to do with the mechanism then. PR112269 is caused by r14-4796-g3e3d73ed5e85e7 for merging tsubst_copy into tsubst_copy_and_build. tsubst_copy used to exit early when 'args' was empty, behavior which that commit deliberately didn't preserve. This early exit masked the fact that COMPLEX_EXPR wasn't handled by tsubst at all, and is a tree code that apparently we could see during warning-dependent folding on some targets. A complete fix is to add handling for this tree code in tsubst_expr, but this patch should fix the reported testsuite failures since the situations where COMPLEX_EXPR crops up in turn out to not be constant expressions in the first place after this patch. [1]: The mechanism incorrectly assumes that instantiation of the non-dependent expression shouldn't induce any template instantiation since ahead of time checking of the expression should've already induced whatever template instantiation was needed, but in this case although overload resolution was performed ahead of time, a ck_rvalue conversion gets resolved to a copy constructor call only at instantiation time. PR c++/111703 gcc/cp/ChangeLog: * constexpr.cc (potential_constant_expression_1) : Only consider var_in_maybe_constexpr_fn if 'now' is false. : Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-fn8.C: New test. --- gcc/cp/constexpr.cc | 4 ++-- gcc/testsuite/g++.dg/cpp2a/concepts-fn8.C | 24 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-fn8.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index c05760e6789..8a6b210144a 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -9623,7 +9623,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, return RECUR (DECL_VALUE_EXPR (t), rval); } if (want_rval - && !var_in_maybe_constexpr_fn (t) + && (now || !var_in_maybe_constexpr_fn (t)) && !type_dependent_expression_p (t) && !decl_maybe_constant_var_p (t) && (strict @@ -9737,7 +9737,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, STRIP_NOPS (x); if (is_this_parameter (x) && !is_capture_proxy (x)) { - if (!var_in_maybe_constexpr_fn (x)) + if (now || !var_in_maybe_constexpr_fn (x)) { if (flags & tf_error) constexpr_error (loc, fundef_p, "use of % in a " diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-fn8.C b/gcc/testsuite/g++.dg/cpp2a/concepts-fn8.C new file mode 100644 index 00000000000..3f63a5b28d7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-fn8.C @@ -0,0 +1,24 @@ +// PR c++/111703 +// { dg-do compile { target c++20 } } + +template +constexpr bool always_true() { return true; } + +struct P { + P() = default; + + template + requires (always_true()) // { dg-bogus "used before its definition" } + constexpr P(const T&) { } + + int n, m; +}; + +void (*f)(P); + +template +constexpr bool g() { + P x; + f(x); // { dg-bogus "from here" } + return true; +}