From patchwork Sun Jul 28 21:38:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1965755 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=gI2weYpE; 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 4WXFHl6YZbz1ybX for ; Mon, 29 Jul 2024 07:39:12 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2DE73385840F for ; Sun, 28 Jul 2024 21:39:10 +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.129.124]) by sourceware.org (Postfix) with ESMTP id EF9433858D28 for ; Sun, 28 Jul 2024 21:38:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EF9433858D28 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 EF9433858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722202732; cv=none; b=Xp3AJs3vLZQbHrJADGuOpVq2kSEzfwCJQJ6ICH2a9df8vKJCGAulaPxzv3GGHRMi8qMkOAEqWAnNpIGHLs5f19lDlFN5gP6SdblFyexQ10/5/fewy3CeL92wQafFlv//D8bPbeVCIhnrvni9ndsdXJsQ7xQVnAVed7eYW6iNHPM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722202732; c=relaxed/simple; bh=6R3OJzyOKX8uZsvwRUsHllXQntdi8+zb9hiWXM0HhLc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=b8Q4SbRHAXTohAsazQihMthRfaZ/ZnZzWz5XfbnZ+hpPV+GL+Y9E51ixw2Yp6n3HGyQeIgPXBJ4UTRck70h9Tr5Sc+o28m8XNE6VjpR8mrRz4LnpuM8PnSWqoeUceZdb2/SFyNlTC2fugAHVAkjf5Br21MH+P644Zngru0xCTS8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722202730; 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=wVYiHpMIRdCeMK94K2YBWiz+358hldgmo2SG+Rb6rY4=; b=gI2weYpEyTWxfxJBisSsouxGlI2nG31nZuT6XPYAdOM3p+RXanWPaZFQVkgffgWfEhzAsQ QmYX5+E9JreFQJkVU8pZJ5Nsvbb4EQGMREb+Cuz1XJ7Bf91Kh4o/jVbxA2fK19ExhwzBH7 48NczTWhoOSrnxpmRoCo110GjxpyRI0= Received: from mail-ua1-f70.google.com (mail-ua1-f70.google.com [209.85.222.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-493-w_ta87CdNVG8R1rN7VNUEg-1; Sun, 28 Jul 2024 17:38:47 -0400 X-MC-Unique: w_ta87CdNVG8R1rN7VNUEg-1 Received: by mail-ua1-f70.google.com with SMTP id a1e0cc1a2514c-81fae6acfdaso1279361241.0 for ; Sun, 28 Jul 2024 14:38:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722202727; x=1722807527; 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=wVYiHpMIRdCeMK94K2YBWiz+358hldgmo2SG+Rb6rY4=; b=HnAQ/+vImFetwaUO9rEhsR5+bXXbrsMRFoDyuCurAI9zlG5xdQl0/lD5831Zg07A/J GIcNDD6Dis4VidlXusPbJ11cP3cXFgpo5Mbtj0QmL55dTnu5wiri2CKnQOgRfrtIPsof cpl9ky3sBIUrZzKgm7x6JU66FdkKaVaX1627+JpGwQzeDEapY2dEpzAQjQdACyM6unS6 1vyRI8GUI1NwqNVwMpeRYAvvY0FVVgVTxx+owxWQrWkAFpEpdlpceBF32d/hitujBzdt z0/y63KSG5Fcw/X5AjCwTozRfbOJarCMPOKH2mrmDWHMYY16o3fuA5mFXZW+v3y5wu0U naLA== X-Gm-Message-State: AOJu0Yz0Aab+3TKTNijeFxrgmj2rLNUH9M0xeW36zn5jRmlZ827Ekq/A dR7RZtgVlMy00ajaGloav/Yoty6FsOwlwtD7dHhkLiO2XeBj6QheniOIXI7/ab8+EUJFYZvoA23 yq2cY/il83wb07f/e3iALXlpFA1/Zl+BXCxjcNcDJIgJyHUBEYmo3oLPo3B4t1ALAkyHkhWpIzW gKQu4r/H/73CtTBYhddFf444bg0+JY2h3Er5E= X-Received: by 2002:a05:6102:d91:b0:48f:df1d:696f with SMTP id ada2fe7eead31-493fa48e040mr7653566137.30.1722202727017; Sun, 28 Jul 2024 14:38:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGwgE0u9EXJC1rzVyqsJHVPH78v14GVxc1igOuiDnUVRIqTyuxalx3r+PRZhSOBfC+CCu3t9w== X-Received: by 2002:a05:6102:d91:b0:48f:df1d:696f with SMTP id ada2fe7eead31-493fa48e040mr7653548137.30.1722202726626; Sun, 28 Jul 2024 14:38:46 -0700 (PDT) Received: from jason-thinkpadp1gen4i.rmtusma.csb (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a1d73ef379sm458348085a.62.2024.07.28.14.38.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jul 2024 14:38:45 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Cc: Marek Polacek Subject: [pushed] c++: if consteval and consteval propagation [PR115583] Date: Sun, 28 Jul 2024 17:38:31 -0400 Message-ID: <20240728213843.161062-1-jason@redhat.com> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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 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 Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- During speculative constant folding of an if consteval, we take the false branch, but the true branch is an immediate function context, so we don't want to to cp_fold_immediate it. So we could check IF_STMT_CONSTEVAL_P here. But beyond that, we don't want to do this inside a call, only when first parsing a function. PR c++/115583 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_conditional_expression): Don't cp_fold_immediate for if consteval. gcc/testsuite/ChangeLog: * g++.dg/cpp23/consteval-if13.C: New test. --- gcc/cp/constexpr.cc | 7 +++++-- gcc/testsuite/g++.dg/cpp23/consteval-if13.C | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp23/consteval-if13.C base-commit: 027bde11d44d58e6ad473af3a69dc21b451bcbc0 diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index abd3b04ea7f..8277b3b79ba 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -3974,10 +3974,13 @@ cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t, if (TREE_CODE (t) == IF_STMT && !val) val = void_node; - /* P2564: a subexpression of a manifestly constant-evaluated expression - or conversion is an immediate function context. */ + /* P2564: If we aren't in immediate function context (including a manifestly + constant-evaluated expression), check any uses of immediate functions in + the arm we're discarding. But don't do this inside a call; we already + checked when parsing the function. */ if (ctx->manifestly_const_eval != mce_true && !in_immediate_context () + && !ctx->call && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2), ctx->manifestly_const_eval)) { diff --git a/gcc/testsuite/g++.dg/cpp23/consteval-if13.C b/gcc/testsuite/g++.dg/cpp23/consteval-if13.C new file mode 100644 index 00000000000..b98bbc33d13 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/consteval-if13.C @@ -0,0 +1,17 @@ +// PR c++/115583 +// { dg-do compile { target c++23 } } + +consteval int f(int i) { + return i; +} +const bool b = 0; +constexpr int g(int i) { + if consteval { + return f(i); + } else { + return i; + } +} +int main() { + return g(1); +}