From patchwork Fri Apr 5 07:40:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 88069 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7AE3B38460A3 for ; Fri, 5 Apr 2024 07:41:30 +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 4D4013858C24 for ; Fri, 5 Apr 2024 07:41:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4D4013858C24 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 4D4013858C24 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=1712302862; cv=none; b=fkcSVOWyAWf5vxa0gkGik9nOwuokVu708qBYm4TytmbLM5ZJRJ7nfURIPsrB+2tqhN0h+Ybjtqjh7MK3H2oRpE7uWevflT4P7oQn0NwaQtwVeFyccLA7w0vpkzqp+fNjcd6J5EV1OvxhVDiC2cira493Uww1Lw+Z1F+jIVOTOcA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712302862; c=relaxed/simple; bh=RPRN/Ng6hr0fbnJlO3c70tOkpW+kxyJc9ujnb1Cn1LI=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=ulWliSp3EVtbeJ2ALJs2vG7y0peOz35QKGFiO9D3Wzlo6Ln9txmxJ/c/oLUU4Hig9d+SH+qbECsqg3rFGSYV2lBxAbdGJXq6udQIF40w3ZOHSsr+XF93ShpMwrE1rMWA3vE3WUpVKX/phk74bu8PSEa8dhWV6ImUTi/2JStOXms= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712302859; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=RcOaU9Htd8BbY89zPPmpV6SZUD3iq+cHug7zWhVQ/M0=; b=iLfHMD9dlBArQpTNkJR4qybyTZC5oYaF+/2RpirTFH+eF9uq/rBWokRLM9K5ZQd6hh6h3h l7sx55ntL4Lc5N7lAA7/A302yaWP1uwtZXziJxqC5L53s/uzVIMUrA03hxib5rbOY65zvt k2CbgY5XaT8UN56q+QHJtmcsLDsoNyI= 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-228-qnMNv8CQNMK3uAi9MTEoaQ-1; Fri, 05 Apr 2024 03:40:55 -0400 X-MC-Unique: qnMNv8CQNMK3uAi9MTEoaQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 17DA329AA3B9 for ; Fri, 5 Apr 2024 07:40:55 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.14]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 95839202451F; Fri, 5 Apr 2024 07:40:54 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 4357emEP2322604 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 5 Apr 2024 09:40:49 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4357emJq2322603; Fri, 5 Apr 2024 09:40:48 +0200 Date: Fri, 5 Apr 2024 09:40:48 +0200 From: Jakub Jelinek To: Jason Merrill , Marek Polacek Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Fix up maybe_warn_for_constant_evaluated calls [PR114580] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, 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: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Hi! When looking at maybe_warn_for_constant_evaluated for the trivial infinite loops patch, I've noticed that it can emit weird diagnostics for if constexpr in templates, first warn that std::is_constant_evaluted() always evaluates to false (because the function template is not constexpr) and then during instantiation warn that std::is_constant_evaluted() always evaluates to true (because it is used in if constexpr condition). Now, only the latter is actually true, even when the if constexpr is in a non-constexpr function, it will still always evaluate to true. So, the following patch fixes it to call maybe_warn_for_constant_evaluated always with IF_STMT_CONSTEXPR_P (if_stmt) as the second argument rather than true if it is if constexpr with non-dependent condition etc. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-04-05 Jakub Jelinek PR c++/114580 * semantics.cc (finish_if_stmt_cond): Call maybe_warn_for_constant_evaluated with IF_STMT_CONSTEXPR_P (if_stmt) as the second argument, rather than true/false depending on if it is if constexpr with non-dependent constant expression with bool type. * g++.dg/cpp2a/is-constant-evaluated15.C: New test. Jakub --- gcc/cp/semantics.cc.jj 2024-04-03 09:58:33.407772541 +0200 +++ gcc/cp/semantics.cc 2024-04-04 12:11:36.203886572 +0200 @@ -1126,6 +1126,9 @@ tree finish_if_stmt_cond (tree orig_cond, tree if_stmt) { tree cond = maybe_convert_cond (orig_cond); + maybe_warn_for_constant_evaluated (cond, + /*constexpr_if=*/ + IF_STMT_CONSTEXPR_P (if_stmt)); if (IF_STMT_CONSTEXPR_P (if_stmt) && !type_dependent_expression_p (cond) && require_constant_expression (cond) @@ -1134,16 +1137,11 @@ finish_if_stmt_cond (tree orig_cond, tre converted to bool. */ && TYPE_MAIN_VARIANT (TREE_TYPE (cond)) == boolean_type_node) { - maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/true); cond = instantiate_non_dependent_expr (cond); cond = cxx_constant_value (cond); } - else - { - maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/false); - if (processing_template_decl) - cond = orig_cond; - } + else if (processing_template_decl) + cond = orig_cond; finish_cond (&IF_COND (if_stmt), cond); add_stmt (if_stmt); THEN_CLAUSE (if_stmt) = push_stmt_list (); --- gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated15.C.jj 2024-04-04 12:23:36.706962932 +0200 +++ gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated15.C 2024-04-04 12:22:29.915882859 +0200 @@ -0,0 +1,28 @@ +// PR c++/114580 +// { dg-do compile { target c++17 } } +// { dg-options "-Wtautological-compare" } + +namespace std { + constexpr inline bool + is_constant_evaluated () noexcept + { +#if __cpp_if_consteval >= 202106L + if consteval { return true; } else { return false; } +#else + return __builtin_is_constant_evaluated (); +#endif + } +} + +template +void foo () +{ + if constexpr ((T) std::is_constant_evaluated ()) // { dg-warning "'std::is_constant_evaluated' always evaluates to true in 'if constexpr'" } + ; // { dg-bogus "'std::is_constant_evaluated' always evaluates to false in a non-'constexpr' function" } +} + +void +bar () +{ + foo (); +}