[PATCH] D42545: [Sema] Classify conversions from enum to float as narrowing

Mikhail Maltsev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 25 08:55:43 PST 2018


miyuki created this revision.
miyuki added reviewers: faisalv, rsmith.

According to [dcl.init.list]p7:

  A narrowing conversion is an implicit conversion
  - ...
  - from an integer type or unscoped enumeration type to a
    floating-point type, except where 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, or
  - ...

Currently clang does not handle the 'unscoped enumeration' case. This
patch fixes the corresponding check.


https://reviews.llvm.org/D42545

Files:
  lib/Sema/SemaOverload.cpp
  test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp


Index: test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
===================================================================
--- test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
+++ test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
@@ -24,6 +24,10 @@
     { 2, f(2), f(2.0) };  // OK: the double-to-int conversion is not at the top level
 }
 
+enum UnscopedEnum {
+  EnumVal = 300
+};
+
 // Test each rule individually.
 
 template<typename T>
@@ -115,15 +119,21 @@
 void int_to_float() {
   // Not a constant expression.
   char c = 1;
+  UnscopedEnum e = EnumVal;
 
   // Variables.  Yes, even though all char's will fit into any floating type.
   Agg<float> f1 = {c};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
   Agg<double> f2 = {c};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
   Agg<long double> f3 = {c};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
 
+  Agg<float> f4 = {e};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
+  Agg<double> f5 = {e};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
+  Agg<long double> f6 = {e};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
+
   // Constants.
-  Agg<float> f4 = {12345678};  // OK (exactly fits in a float)
-  Agg<float> f5 = {123456789};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
+  Agg<float> f7 = {12345678};  // OK (exactly fits in a float)
+  Agg<float> f8 = {EnumVal};  // OK
+  Agg<float> f9 = {123456789};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
 
   Agg<float> ce1 = { Convert<int>(123456789) }; // expected-error {{constant expression evaluates to 123456789 which cannot be narrowed to type 'float'}} expected-note {{silence}}
   Agg<double> ce2 = { ConvertVar<long long>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'long long' to 'double'}} expected-note {{silence}}
@@ -138,7 +148,9 @@
   // Not a constant expression.
   short s = 1;
   unsigned short us = 1;
+  UnscopedEnum e = EnumVal;
   Agg<char> c1 = {s};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
+  Agg<char> c2 = {e};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
   Agg<unsigned short> s1 = {s};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
   Agg<short> s2 = {us};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
 
@@ -148,16 +160,19 @@
   // long).
   long l1 = 1;
   Agg<int> i1 = {l1};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
+  Agg<int> i2 = {e};  // OK
   long long ll = 1;
   Agg<long> l2 = {ll};  // OK
 
   // Constants.
-  Agg<char> c2 = {127};  // OK
-  Agg<char> c3 = {300};  // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}}
-
-  Agg<int> i2 = {0x7FFFFFFFU};  // OK
-  Agg<int> i3 = {0x80000000U};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
-  Agg<unsigned int> i4 = {-0x80000000L};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
+  Agg<char> c3 = {127};  // OK
+  Agg<char> c4 = {300};  // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}}
+  Agg<char> c5 = {EnumVal};  // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}}
+
+  Agg<int> i3 = {0x7FFFFFFFU};  // OK
+  Agg<int> i4 = {EnumVal};  // OK
+  Agg<int> i5 = {0x80000000U};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
+  Agg<unsigned int> i6 = {-0x80000000L};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
 
   // Bool is also an integer type, but conversions to it are a different AST
   // node.
Index: lib/Sema/SemaOverload.cpp
===================================================================
--- lib/Sema/SemaOverload.cpp
+++ lib/Sema/SemaOverload.cpp
@@ -327,7 +327,8 @@
   FloatingIntegralConversion:
     if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) {
       return NK_Type_Narrowing;
-    } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) {
+    } else if (FromType->isIntegralOrUnscopedEnumerationType() &&
+               ToType->isRealFloatingType()) {
       llvm::APSInt IntConstantValue;
       const Expr *Initializer = IgnoreNarrowingConversion(Converted);
       assert(Initializer && "Unknown conversion expression");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42545.131465.patch
Type: text/x-patch
Size: 4513 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180125/b2758a16/attachment.bin>


More information about the cfe-commits mailing list