From patchwork Sat Feb 10 22:42:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1897403 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=MVN3Of3U; 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 4TXQjb2p0fz23fc for ; Sun, 11 Feb 2024 09:43:13 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5EA043858D38 for ; Sat, 10 Feb 2024 22:43:11 +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 A4DDE3858D20 for ; Sat, 10 Feb 2024 22:42:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A4DDE3858D20 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 A4DDE3858D20 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=1707604975; cv=none; b=sYf/iGrLYjEaav73YkASS7PMkEW3BXvolRfr7cJb2aL7H+2xHucaIYZSsy9dn10hdwtBaecUmG2Jc4upjEv+50uru97i1B77+ICPO8tIgwnJQll2d2KgEGauh0U0ItUL583hIBoVpcxJC1kanrG9FB3EozPoRvzNk7hWKh8WoUk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707604975; c=relaxed/simple; bh=CuyjAjsojbNLieSZ2BUEnMQ+4g2TdC/ZV6IRotox83s=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=h8WGJ6zj+LMtP+XG9CehQ61f+0M5OpOKIen3STchPM8P00TgVxtWM06CpA1FkPKYj4pP59v3o9ekrenbc/n6O/pyqIyZwEnYMhWIobuO25ATtQJUeHmeO/gz0YWRWlQ2BYb0+atNZHQ/LSq8YE7fS0eX6TOP2rawcGUSxpPhB2U= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707604964; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7J7UmBLxOLuw/+6nkgEPKCeG6kgTp3J70S4kwJxhf20=; b=MVN3Of3UqyCr7IbZTtqXilrq9e2rdGwmhR2bvxrsxFSuxvn9AEMkRJpAfHBKzw2JfE3Q68 b4R92/4DE856yWMmZPH+MN0KbIyMz0uh8gMqxmU/xI+E0oCArbwY4sB51ByPLYrK86ZXDD Z+KRl6ZsCTk4gSoqZByNjBQtIl9Wuuk= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-508-rSd7Vm92OXueauFdd8sc4Q-1; Sat, 10 Feb 2024 17:42:33 -0500 X-MC-Unique: rSd7Vm92OXueauFdd8sc4Q-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4B5041C05EB7 for ; Sat, 10 Feb 2024 22:42:33 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.8.227]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2A9AFC090A9; Sat, 10 Feb 2024 22:42:33 +0000 (UTC) From: Marek Polacek To: Jason Merrill , GCC Patches Subject: [PATCH] c++: ICE with reinterpret_cast and switch [PR113545] Date: Sat, 10 Feb 2024 17:42:23 -0500 Message-ID: <20240210224223.2156462-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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_H5, 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/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- Jason, this is the patch you proposed for PR113545. It looks very safe so I'm posting it here so that it's not forgotten. PR c++/113545 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_switch_expr): If the condition doesn't reduce to an INTEGER_CST, consider it non-constant. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-reinterpret3.C: Remove dg-ice. --- gcc/cp/constexpr.cc | 10 ++++++++++ gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C | 4 +--- 2 files changed, 11 insertions(+), 3 deletions(-) base-commit: 93e1559bea434a681208e5e7a21513d7da2844d6 prerequisite-patch-id: 9dda211bd29817f18d2d46c3c5a7bbe3708cf9f2 diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 2ebb1470dd5..fa346fe01c9 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -7106,6 +7106,16 @@ cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue, non_constant_p, overflow_p); VERIFY_CONSTANT (cond); + if (TREE_CODE (cond) != INTEGER_CST) + { + /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable + switch condition even if it's constant enough for other things + (c++/113545). */ + gcc_checking_assert (ctx->quiet); + *non_constant_p = true; + return t; + } + *jump_target = cond; tree body diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C index 51feb2e558e..adc0b418df9 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C @@ -1,7 +1,5 @@ // PR c++/113545 -// { dg-do compile { target c++14 } } -// Please change the above "dg-do compile" to "dg-do run" when the ICE is resolved. -// { dg-ice "PR112545 - constexpr function with switch called for reinterpret_cast" } +// { dg-do run { target c++14 } } char foo;