P1957R2: conversion from a pointer to bool is considered narrowing.
authorRichard Smith <richard@metafoo.co.uk>
Mon, 10 Feb 2020 15:22:51 +0000 (07:22 -0800)
committerRichard Smith <richard@metafoo.co.uk>
Tue, 11 Feb 2020 14:52:44 +0000 (06:52 -0800)
This is being implemented somewhat speculatively, to match GCC's
behavior.

clang/lib/Sema/SemaOverload.cpp
clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-cxx11-nowarn.cpp

index 003d9bb..1e838fd 100644 (file)
@@ -328,9 +328,8 @@ NarrowingKind StandardConversionSequence::getNarrowingKind(
       goto FloatingIntegralConversion;
     if (FromType->isIntegralOrUnscopedEnumerationType())
       goto IntegralConversion;
-    // Boolean conversions can be from pointers and pointers to members
-    // [conv.bool], and those aren't considered narrowing conversions.
-    return NK_Not_Narrowing;
+    // -- from a pointer type or pointer-to-member type to bool, or
+    return NK_Type_Narrowing;
 
   // -- from a floating-point type to an integer type, or
   //
index 4436cb0..eac9ac0 100644 (file)
@@ -180,9 +180,9 @@ void shrink_int() {
   Agg<bool> b2 = {1};  // OK
   Agg<bool> b3 = {-1};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
 
-  // Conversions from pointers to booleans aren't narrowing conversions.
+  // Conversions from pointers to booleans are narrowing conversions.
   Agg<bool>* ptr = &b1;
-  Agg<bool> b = {ptr};  // OK
+  Agg<bool> b = {ptr};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
 
   Agg<short> ce1 = { Convert<int>(100000) }; // expected-error {{constant expression evaluates to 100000 which cannot be narrowed to type 'short'}} expected-note {{silence}} expected-warning {{changes value from 100000 to -31072}}
   Agg<char> ce2 = { ConvertVar<short>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'short' to 'char'}} expected-note {{silence}}
@@ -240,3 +240,13 @@ void test_narrowed(Value<sizeof(int)> vi, Value<sizeof(double)> vd) {
   int &ir = check_narrowed<double>(vd);
   float &fr = check_narrowed<int>(vi);
 }
+
+// * from a pointer type or a pointer-to-member type to bool.
+void P1957R2(void *a, int *b, Agg<int> *c, int Agg<int>::*d) {
+  Agg<bool> ta = {a}; // expected-error {{cannot be narrowed}} expected-note {{}}
+  Agg<bool> tb = {b}; // expected-error {{cannot be narrowed}} expected-note {{}}
+  Agg<bool> tc = {c}; // expected-error {{cannot be narrowed}} expected-note {{}}
+  Agg<bool> td = {d}; // expected-error {{cannot be narrowed}} expected-note {{}}
+}
+template<bool> struct BoolParam {};
+BoolParam<&P1957R2> bp; // expected-error {{not allowed in a converted constant expression}}
index d4d8198..d701ec9 100644 (file)
@@ -163,10 +163,6 @@ void shrink_int() {
   Agg<bool> b2 = {1};  // OK
   Agg<bool> b3 = {-1};  // expected-warning {{ cannot be narrowed }} expected-note {{silence}}
 
-  // Conversions from pointers to booleans aren't narrowing conversions.
-  Agg<bool>* ptr = &b1;
-  Agg<bool> b = {ptr};  // OK
-
   Agg<short> ce1 = { Convert<int>(100000) }; // expected-warning {{constant expression evaluates to 100000 which cannot be narrowed to type 'short'}} expected-note {{silence}} expected-warning {{changes value from 100000 to -31072}}
   Agg<char> ce2 = { ConvertVar<short>() }; // expected-warning {{non-constant-expression cannot be narrowed from type 'short' to 'char'}} expected-note {{silence}}
 }