<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - __builtin_sub_overflow reports invalid overflow in constexpr context"
   href="https://bugs.llvm.org/show_bug.cgi?id=42000">42000</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>__builtin_sub_overflow reports invalid overflow in constexpr context
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>FreeBSD
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Frontend
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>m@mikaelsimonsson.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This code fails to compile:

#include <cstdint>

constexpr bool overflows(uint8_t a, int b)
{
    uint8_t diff = 0;
    return __builtin_sub_overflow(a, b, &diff);
}

int main()
{
    static_assert(!overflows(255, 100));
    return 0;
}

11:5: error: static_assert failed due to requirement '!overflows(255, 100)'
    static_assert(!overflows(255, 100));
    ^             ~~~~~~~~~~~~~~~~~~~~
1 error generated.


But this code compiles and runs without triggering the assert:

#include <cassert>
#include <cstdint>

inline bool overflows(uint8_t a, int b)
{
    uint8_t diff = 0;
    return __builtin_sub_overflow(a, b, &diff);
}

int main()
{
    assert(!overflows(255, 100));
    return 0;
}


Tested with godbolt.org: clang version 9.0.0 (trunk 361583)</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>