https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70552

            Bug ID: 70552
           Summary: __builtin_constant_p fails to reflect the constness of
                    constexpr calls
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

While testing the __builtin_constant_p() intrinsic to better understand the
motivation for and problems with bug 14505, I discovered that the built-in
doesn't behave as documented for calls to constexpr functions as arguments
except in constexpr contexts, or unless optimization is enabled.  For example,
the following program compiles successfully (i.e., both static assertions pass)
but then aborts at runtime.

Since not every call to a given constexpr function is necessarily a core
constant expression (i.e., a compile-time constant), for the instrinsic to be
useful with C++ constexpr expressions it needs to return the same result for
the same argument regardless of the context in which it's invoked.

This may be related to the problem as pointed out in comment #6 on bug 65656,
though the complaint there seems to be about the built-in yielding false
negatives due to folding non-constexpr expressions too early, while the problem
here seems to be that the built-in isn't attempting to fold the constexpr
function call at all without optimization.

$ cat t.c && /build/gcc-trunk/gcc/xgcc -B /build/gcc-trunk/gcc -Wall -Wextra
-Wpedantic -xc++ t.c  && ./a.out 
constexpr int foo () { return 1; }

#define A(e) static_assert ((e), #e)

A (1 == foo ());
A (1 ==  __builtin_constant_p (foo ()));

int main ()
{
    if (!__builtin_constant_p (foo ()))
        __builtin_abort ();
}
Aborted

Reply via email to