Bug 91428 - Please warn on if constexpr (std::is_constant_evaluated())
Summary: Please warn on if constexpr (std::is_constant_evaluated())
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 9.1.0
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning
  Show dependency treegraph
 
Reported: 2019-08-12 19:44 UTC by Barry Revzin
Modified: 2019-10-12 14:25 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-08-12 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Barry Revzin 2019-08-12 19:44:42 UTC
A very common user error with std::is_constant_evaluated is to write this:

#include <type_traits>

constexpr int foo(int i) {
    if constexpr (std::is_constant_evaluated()) {
        return 42;
    } else {
        return i;
    }
}

That is, check if we're in the middle of constant evaluation with "if constexpr" instead of plain "if". This is the intuitive check to do, but unfortunately, since the condition here is manifestly constant evaluated, the result of check is unconditionally true. gcc implements this correctly - the above function is exactly equivalent to "return 42;" - but that's unlikely to be what the user intended the program to do. They likely intended to write:

constexpr int foo(int i) {
    if (std::is_constant_evaluated()) {
        return 42;
    } else {
        return i;
    }
}

Can you please provide a warning for this erroneous usage?
Comment 1 Marek Polacek 2019-08-12 19:56:52 UTC
(In reply to Barry Revzin from comment #0)
> Can you please provide a warning for this erroneous usage?

Yes, I think this ought to me feasible to add to finish_if_stmt_cond.
Comment 2 Marek Polacek 2019-08-12 19:57:07 UTC
*be
Comment 3 Marek Polacek 2019-08-28 02:04:19 UTC
Author: mpolacek
Date: Wed Aug 28 02:03:48 2019
New Revision: 274981

URL: https://gcc.gnu.org/viewcvs?rev=274981&root=gcc&view=rev
Log:
	PR c++/91428 - warn about std::is_constant_evaluated in if constexpr.
	* cp-tree.h (decl_in_std_namespace_p): Declare.
	* semantics.c (is_std_constant_evaluated_p): New.
	(finish_if_stmt_cond): Warn about "std::is_constant_evaluated ()" in
	an if-constexpr.
	* typeck.c (decl_in_std_namespace_p): No longer static.

	* g++.dg/cpp2a/is-constant-evaluated9.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated9.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/semantics.c
    trunk/gcc/cp/typeck.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Marek Polacek 2019-08-28 02:08:02 UTC
Done in GCC 10.1.
Comment 5 Marc Glisse 2019-10-12 09:18:31 UTC
Would it make sense to add a fixit hint that removes "constexpr"? I think that might make the warning a bit clearer for some users.
On the other hand, if is_constant_evaluated gets removed by P1938, there is no point putting more work into it.
Comment 6 Marek Polacek 2019-10-12 14:25:02 UTC
Well, but perhaps the right fix is removing "std::is_constant_evaluated ()" instead?