Commit 7ef45f45 authored by Richard Smith's avatar Richard Smith
Browse files

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

This is being implemented somewhat speculatively, to match GCC's
behavior.
parent db875f66
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -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
  //
+12 −2
Original line number Diff line number Diff line
@@ -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}}
+0 −4
Original line number Diff line number Diff line
@@ -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}}
}