From ab78fd585df911307fad6d5b89b0b22cb14e9001 Mon Sep 17 00:00:00 2001 From: Mikhail Maltsev Date: Wed, 21 Feb 2018 10:08:18 +0000 Subject: [PATCH] [Sema] Classify conversions from enum to float as narrowing Summary: 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. Reviewers: faisalv, rsmith, rogfer01 Reviewed By: rogfer01 Subscribers: rogfer01, cfe-commits Differential Revision: https://reviews.llvm.org/D42545 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@325668 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaOverload.cpp | 3 ++- test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index 1b07ec60ce..2c03f6977a 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -327,7 +327,8 @@ StandardConversionSequence::getNarrowingKind(ASTContext &Ctx, 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"); 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 48c5b23207..2142c098f8 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 @@ -24,6 +24,10 @@ void std_example() { { 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 @@ -115,15 +119,21 @@ void shrink_float() { 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 f1 = {c}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} Agg f2 = {c}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} Agg f3 = {c}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} + Agg f4 = {e}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} + Agg f5 = {e}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} + Agg f6 = {e}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} + // Constants. - Agg f4 = {12345678}; // OK (exactly fits in a float) - Agg f5 = {123456789}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} + Agg f7 = {12345678}; // OK (exactly fits in a float) + Agg f8 = {EnumVal}; // OK + Agg f9 = {123456789}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} Agg ce1 = { Convert(123456789) }; // expected-error {{constant expression evaluates to 123456789 which cannot be narrowed to type 'float'}} expected-note {{silence}} Agg ce2 = { ConvertVar() }; // expected-error {{non-constant-expression cannot be narrowed from type 'long long' to 'double'}} expected-note {{silence}} -- 2.40.0