This is an archive of the discontinued LLVM Phabricator instance.

Try contextually converting condition of constexpr if to Boolean value
ClosedPublic

Authored by ismailp on Sep 1 2016, 2:26 PM.

Details

Summary

C++1z 6.4.1/p2:
If the if statement is of the form if constexpr, the value of the
condition shall be a contextually converted constant expression of type
bool [...]
C++1z 5.20/p4:
[...] A contextually converted constant expression of type bool is an
expression, contextually converted to bool (Clause4), where the
converted expression is a constant expression and the conversion
sequence contains only the conversions above. [...]

Contextually converting result of an expression e to a Boolean value
requires bool t(e) to be well-formed.

An explicit conversion function is only considered as a user-defined
conversion for direct-initialization, which is essentially what
contextually converted to bool requires.

Also, fixes PR28470.

Diff Detail

Event Timeline

ismailp updated this revision to Diff 70070.Sep 1 2016, 2:26 PM
ismailp retitled this revision from to Try contextually converting condition of constexpr if to Boolean value.
ismailp updated this object.
ismailp added a reviewer: rsmith.
ismailp added a subscriber: cfe-commits.
rsmith accepted this revision.Sep 6 2016, 7:04 PM
rsmith edited edge metadata.

Thanks, LGTM.

If you're looking for more issues in this area -- in passing I noticed that we fail to apply the "contextually converted constant expression of type bool" rules properly in the other cases where they apply, either (the operand of static_assert and noexcept).

This revision is now accepted and ready to land.Sep 6 2016, 7:04 PM
rsmith added inline comments.Sep 6 2016, 7:06 PM
test/CodeGenCXX/cxx1z-constexpr-if.cpp
27–30

Please also add a test that we reject expressions that are contextually convertible to a bool constant expression but are not contextually converted constant expressions of type bool. Example:

if constexpr (4.3) // ill-formed, boolean conversion not permitted

... and sadly ...

constexpr void *p = nullptr;
if constexpr (p) // ill-formed, boolean conversion not permitted
ismailp updated this revision to Diff 70561.Sep 7 2016, 10:29 AM
ismailp edited edge metadata.
  • Added more tests

Thank you for reviewing.

Sure, I'll try to look into those conversions as well.

test/CodeGenCXX/cxx1z-constexpr-if.cpp
2

Is there a more elegant way to do this? The previous check didn't seem to work with diagnostics.

rsmith added inline comments.Sep 7 2016, 10:58 AM
test/CodeGenCXX/cxx1z-constexpr-if.cpp
2

Tests for semantic issues should go into test/SemaCXX (organized by category) or test/CXX (organized by section of the standard). In this case, test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp is probably the most appropriate place (somewhere in its namespace ccce, perhaps).

This revision was automatically updated to reflect the committed changes.
ismailp added inline comments.Sep 7 2016, 11:35 AM
test/CodeGenCXX/cxx1z-constexpr-if.cpp
2

Thanks! I've done that, and pushed.