[PATCH] D42545: [Sema] Classify conversions from enum to float as narrowing
This revision was automatically updated to reflect the committed changes. Closed by commit rC325668: [Sema] Classify conversions from enum to float as narrowing (authored by miyuki, committed by ). Repository: rC Clang https://reviews.llvm.org/D42545 Files: lib/Sema/SemaOverload.cpp test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp 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"); 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 @@ -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 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}} 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"); 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 @@ -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 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
[PATCH] D42545: [Sema] Classify conversions from enum to float as narrowing
miyuki added a comment. In https://reviews.llvm.org/D42545#1010335, @rogfer01 wrote: > Do you plan to submit (in another change) the unscoped enum → integer type > testcases? If this is not currently tested anywhere I think it may be a good > thing to have them too. Yes, I'll submit them in a follow-up patch. https://reviews.llvm.org/D42545 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D42545: [Sema] Classify conversions from enum to float as narrowing
rogfer01 accepted this revision. rogfer01 added a comment. This revision is now accepted and ready to land. Looks good to me now. Wait a couple of days before submitting it just in case the other reviewers have more comments. Do you plan to submit (in another change) the unscoped enum → integer type testcases? If this is not currently tested anywhere I think it may be a good thing to have them too. https://reviews.llvm.org/D42545 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D42545: [Sema] Classify conversions from enum to float as narrowing
miyuki added a comment. ping https://reviews.llvm.org/D42545 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D42545: [Sema] Classify conversions from enum to float as narrowing
miyuki updated this revision to Diff 132974. miyuki added a comment. Removed the changes that are unrelated to the 'enum->float' case from the test. 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 @@ -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 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}} 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"); 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 @@ -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 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}} 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 (F
[PATCH] D42545: [Sema] Classify conversions from enum to float as narrowing
rogfer01 added a comment. I understand you're fixing the narrowing check from "unscoped enum/integer type" → float. But you have also extended some tests which are "unscoped enum" → integer type (lines 153, 170 and 173). So I presume they were already handled correctly before your patch. Can these be submitted separatedly? https://reviews.llvm.org/D42545 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D42545: [Sema] Classify conversions from enum to float as narrowing
miyuki added a comment. ping https://reviews.llvm.org/D42545 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D42545: [Sema] Classify conversions from enum to float as narrowing
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 @@ -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 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}} @@ -138,7 +148,9 @@ // Not a constant expression. short s = 1; unsigned short us = 1; + UnscopedEnum e = EnumVal; Agg c1 = {s}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} + Agg c2 = {e}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} Agg s1 = {s}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} Agg s2 = {us}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} @@ -148,16 +160,19 @@ // long). long l1 = 1; Agg i1 = {l1}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} + Agg i2 = {e}; // OK long long ll = 1; Agg l2 = {ll}; // OK // Constants. - Agg c2 = {127}; // OK - Agg c3 = {300}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}} - - Agg i2 = {0x7FFFU}; // OK - Agg i3 = {0x8000U}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} - Agg i4 = {-0x8000L}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} + Agg c3 = {127}; // OK + Agg c4 = {300}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}} + Agg c5 = {EnumVal}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}} + + Agg i3 = {0x7FFFU}; // OK + Agg i4 = {EnumVal}; // OK + Agg i5 = {0x8000U}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} + Agg i6 = {-0x8000L}; // 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"); Index: test/CXX/dcl.decl/dcl