[cfe-commits] [PATCH] Diagnose bool conversions as C++0x narrowing conversions

Jeffrey Yasskin jyasskin at google.com
Fri Aug 12 13:47:36 PDT 2011


I missed this in my initial implementation.


diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp
index c406ad9..e410f2f 100644
--- a/lib/Sema/SemaInit.cpp
+++ b/lib/Sema/SemaInit.cpp
@@ -2321,6 +2321,7 @@ bool
InitializationSequence::endsWithNarrowing(ASTContext &Ctx,
   //   the source is a constant expression and the actual value after
   //   conversion will fit into the target type and will produce the original
   //   value when converted back to the original type.
+  case ICK_Boolean_Conversion:  // Bools are integers too.
   case ICK_Integral_Conversion: {
     assert(FromType->isIntegralOrUnscopedEnumerationType());
     assert(ToType->isIntegralOrUnscopedEnumerationType());
diff --git a/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
b/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
index f82e6ca..c7f61fb 100644
--- a/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
+++ b/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
@@ -138,6 +138,12 @@ void shrink_int() {
   Agg<int> i2 = {0x7FFFFFFFU};  // OK
   Agg<int> i3 = {0x80000000U};  // expected-error {{ cannot be
narrowed }} expected-note {{override}}
   Agg<unsigned int> i4 = {-0x80000000L};  // expected-error {{ cannot
be narrowed }} expected-note {{override}}
+
+  // Bool is also an integer type, but conversions to it are a different AST
+  // node.
+  Agg<bool> b1 = {0};  // OK
+  Agg<bool> b2 = {1};  // OK
+  Agg<bool> b3 = {-1};  // expected-error {{ cannot be narrowed }}
expected-note {{override}}
 }

 // Be sure that type- and value-dependent expressions in templates
get the error



More information about the cfe-commits mailing list