[PATCH] D106216: Disallow narrowing conversions to bool in explicit specififers.

Corentin Jabot via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 17 03:55:28 PDT 2021


cor3ntin created this revision.
cor3ntin requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Completes the support for P1401R5


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D106216

Files:
  clang/lib/Sema/SemaOverload.cpp
  clang/test/SemaCXX/cxx2a-explicit-bool.cpp
  clang/www/cxx_status.html


Index: clang/www/cxx_status.html
===================================================================
--- clang/www/cxx_status.html
+++ clang/www/cxx_status.html
@@ -1296,7 +1296,7 @@
     <tr>
       <td>Narrowing contextual conversions to bool</td>
       <td><a href="https://wg21.link/P1401R5">P1401R5</a></td>
-      <td class="partial" align="center">Clang 13</td>
+      <td class="full" align="center">Clang 13</td>
     </tr>
     <tr>
       <td>Trimming whitespaces before line splicing</td>
Index: clang/test/SemaCXX/cxx2a-explicit-bool.cpp
===================================================================
--- clang/test/SemaCXX/cxx2a-explicit-bool.cpp
+++ clang/test/SemaCXX/cxx2a-explicit-bool.cpp
@@ -727,3 +727,18 @@
 Str b = "not so short";// expected-error {{no viable conversion}}
 
 }
+
+namespace P1401 {
+
+const int *ptr;
+
+struct S {
+  explicit(sizeof(char[2])) S(char); // expected-error {{explicit specifier argument evaluates to 2, which cannot be narrowed to type 'bool'}}
+  explicit(ptr) S(long);             // expected-error {{conversion from 'const int *' to 'bool' is not allowed in a converted constant expression}}
+  explicit(nullptr) S(int);          // expected-error {{value of type 'nullptr_t' is not implicitly convertible to 'bool'}}
+  explicit(42L) S(int, int);         // expected-error {{explicit specifier argument evaluates to 42, which cannot be narrowed to type 'bool'}}
+  explicit(sizeof(char)) S();
+  explicit(0) S(char, char);
+  explicit(1L) S(char, char, char);
+};
+} // namespace P1401
Index: clang/lib/Sema/SemaOverload.cpp
===================================================================
--- clang/lib/Sema/SemaOverload.cpp
+++ clang/lib/Sema/SemaOverload.cpp
@@ -5635,13 +5635,11 @@
   //  expression is a constant expression and the implicit conversion
   //  sequence contains only [... list of conversions ...].
   ImplicitConversionSequence ICS =
-      CCE == Sema::CCEK_ExplicitBool
-          ? TryContextuallyConvertToBool(S, From)
-          : TryCopyInitialization(S, From, T,
-                                  /*SuppressUserConversions=*/false,
-                                  /*InOverloadResolution=*/false,
-                                  /*AllowObjCWritebackConversion=*/false,
-                                  /*AllowExplicit=*/false);
+      TryCopyInitialization(S, From, T,
+                            /*SuppressUserConversions=*/false,
+                            /*InOverloadResolution=*/false,
+                            /*AllowObjCWritebackConversion=*/false,
+                            /*AllowExplicit=*/false);
   StandardConversionSequence *SCS = nullptr;
   switch (ICS.getKind()) {
   case ImplicitConversionSequence::StandardConversion:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106216.359554.patch
Type: text/x-patch
Size: 2750 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210717/1d1aa9a6/attachment-0001.bin>


More information about the cfe-commits mailing list