From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 837133858D34 for ; Fri, 5 Apr 2024 18:47:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 837133858D34 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 837133858D34 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=1712342842; cv=none; b=kfqaGYg9jTKeadJTcAf90glZgxbNjbMPFR1hIp01mNJR/334SRidNKP7qhb4Ug/x+S4uol/WriwWvnIVpe+nt5M9G52v3tL4vtm6xJvlal0+oTyyxacgNKqXytLQB44zzv4+7uMWvrgOINMWoriT3Rk+eK4OU8E236uixUYlXco= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712342842; c=relaxed/simple; bh=XOlOrZwPTB7yqI/e/HFykDc0IhOUN/lymRWQNuzOAKM=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=rZ2U6+cpL/9ReESdDw/p4UJ33qzawTY54G1tv3l8apE4tx3kzxLtdbc9prlqUFrFX6vlbzaczVXkAoYq+Uk/jtMm2a067lhj+i0PmeOPsu5oAxPF/l8vWntcKTJd82gDvEqV0dA4r7yRAscpBkswYmJBu2Pz4oMu8fwYnfMO/2M= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712342840; 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: in-reply-to:in-reply-to:references:references; bh=4USR5e/gN25rY2yPb+miklDyp+k+VvSFtup7cxC7xLg=; b=OXPqRojLZXnYcZ5bg4iNC1Syq3Dd8bUnuP7Koy5mJg6CusZSV90gFpCM9zaaHvzoZaDVes Lma9fOpika+QnX6CPEBsL9oUUx8l95zYosmzblpoWDOnlW8xSp90Me4Mk8Dri64G8QCI8d JUOPb04wExqGBPLvuAGVXBbpDu2gXcU= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-652-wFBIfPrKN1-92gHC25oCaw-1; Fri, 05 Apr 2024 14:47:18 -0400 X-MC-Unique: wFBIfPrKN1-92gHC25oCaw-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-78d3352237cso350881385a.1 for ; Fri, 05 Apr 2024 11:47:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712342838; x=1712947638; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4USR5e/gN25rY2yPb+miklDyp+k+VvSFtup7cxC7xLg=; b=ZYRJPD3n8heMOObB24Tp5ZLxnIW0Q/P1A714MnXaJfjwbTO3RFCczGP/hUU1Qznx1C WWB5XuLP67IYDyv6NbdCXbeBhHtiqqqvOQH8q+t4Ou7JrfyXU4IetYyjpWPWKSaAtfo9 Ja+zFExuH6U5p7qr2nWs2RYYrjMNu+RabqVePD4EADr39cdazsVwXPAwK6tXeBJdyfBS iapFzcpLI8hs21fjMfW+3Eg7l1XjvKGyHnIWmfe2MiXF+02Q5PZQivReWLr5phyQxGfE RJ8sMF5mMkw7oiZxUCc/yCq7f0+OncnZrKVJM1PDzj65PQoODf4GyBpx/dVGid4wnA/Y +x1w== X-Forwarded-Encrypted: i=1; AJvYcCUYZHzPbhvi2vqdJnpVyCl4Ak5aDG7UW/TwyzUbM+B/WJzmCMqfjqHj1OYVizd1AB+pcCwr24Mcl6vCiHn7dil1dCJM2CLEkA== X-Gm-Message-State: AOJu0Yx5n17kKT7bZzl25U6ZpDImJH119QO3RfgU5ME7KHQBV7hhfg5r WV/ZNJLzAE4/V/LaToaz4mLehUJlSpE+5U9pMZBKfpR5lBNKTC2vbEtC5c1vsH41gCrrSZNElLS 0S0q1/QH0KONvXusy7gGBr/ktFfvrjOrP68h6AKA7alwQ03xOnZbbNUc= X-Received: by 2002:a05:620a:199b:b0:78d:4751:24f9 with SMTP id bm27-20020a05620a199b00b0078d475124f9mr5665411qkb.1.1712342838364; Fri, 05 Apr 2024 11:47:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEOHHo6naPsdlFnoAslzXADV9gvLUcgt08n0rj1sDdyHO5iegNBs5q0eWAZQ/FPV0+jU18zFw== X-Received: by 2002:a05:620a:199b:b0:78d:4751:24f9 with SMTP id bm27-20020a05620a199b00b0078d475124f9mr5665391qkb.1.1712342838067; Fri, 05 Apr 2024 11:47:18 -0700 (PDT) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id dp5-20020a05622a47c500b0043438a061d7sm1003557qtb.6.2024.04.05.11.47.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 11:47:17 -0700 (PDT) Date: Fri, 5 Apr 2024 14:47:15 -0400 From: Marek Polacek To: Jakub Jelinek Cc: Jason Merrill , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] c++: Fix up maybe_warn_for_constant_evaluated calls [PR114580] Message-ID: References: MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-6.3 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 List-Id: On Fri, Apr 05, 2024 at 09:40:48AM +0200, Jakub Jelinek wrote: > 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. > > --- 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)); I don't think we need the comment anymore since it's clear what the argument does, and then the whole call can fit on a single line. But either way, the patch looks good, thanks. > 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 (); > +} > > Jakub > Marek