[clang] 7ef45f4 - P1957R2: conversion from a pointer to bool is considered narrowing.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 11 06:53:02 PST 2020


Author: Richard Smith
Date: 2020-02-11T06:52:44-08:00
New Revision: 7ef45f45f6721af4942f0e0bc38329e236b468cd

URL: https://github.com/llvm/llvm-project/commit/7ef45f45f6721af4942f0e0bc38329e236b468cd
DIFF: https://github.com/llvm/llvm-project/commit/7ef45f45f6721af4942f0e0bc38329e236b468cd.diff

LOG: P1957R2: conversion from a pointer to bool is considered narrowing.

This is being implemented somewhat speculatively, to match GCC's
behavior.

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 003d9bb3a97d..1e838fd75130 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -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
   //

diff  --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
index 4436cb0aac60..eac9ac0e8279 100644
--- a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
+++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
@@ -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}}

diff  --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-cxx11-nowarn.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-cxx11-nowarn.cpp
index d4d8198d4fc8..d701ec964605 100644
--- a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-cxx11-nowarn.cpp
+++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-cxx11-nowarn.cpp
@@ -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}}
 }


        


More information about the cfe-commits mailing list