[clang] 9fbf998 - Reject operations between vectors and enum types.

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Mon May 4 13:11:47 PDT 2020


Author: Erich Keane
Date: 2020-05-04T13:11:24-07:00
New Revision: 9fbf9989a2bf0edfbe1b482de470dcccd1108e24

URL: https://github.com/llvm/llvm-project/commit/9fbf9989a2bf0edfbe1b482de470dcccd1108e24
DIFF: https://github.com/llvm/llvm-project/commit/9fbf9989a2bf0edfbe1b482de470dcccd1108e24.diff

LOG: Reject operations between vectors and enum types.

There are some lookup oddities with these as reported in PR45780, and
GCC doesn't support these behaviors at all.  To be more consistent with
GCC and prevent the crashes caused by our lookup issues, nip the problem
in the bud and prohibit enums here.

Added: 
    

Modified: 
    clang/lib/Sema/SemaExpr.cpp
    clang/test/SemaCXX/vector-conditional.cpp
    clang/test/SemaCXX/vector.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 7e8446c6f1ab..871f3703e6d8 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -1483,7 +1483,7 @@ QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS,
     return LHSType;
 
   // ExtInt types aren't subject to conversions between them or normal integers,
-  // so this fails. 
+  // so this fails.
   if(LHSType->isExtIntType() || RHSType->isExtIntType())
     return QualType();
 
@@ -9621,7 +9621,8 @@ static bool tryGCCVectorConvertAndSplat(Sema &S, ExprResult *Scalar,
       ScalarCast = CK_IntegralToFloating;
     } else
       return true;
-  }
+  } else if (ScalarTy->isEnumeralType())
+    return true;
 
   // Adjust scalar if desired.
   if (Scalar) {

diff  --git a/clang/test/SemaCXX/vector-conditional.cpp b/clang/test/SemaCXX/vector-conditional.cpp
index 5676d7a3880d..1b360e4fa832 100644
--- a/clang/test/SemaCXX/vector-conditional.cpp
+++ b/clang/test/SemaCXX/vector-conditional.cpp
@@ -97,7 +97,7 @@ void Operands() {
 
   // When there is a vector and a scalar, conversions must be legal.
   (void)(four_ints ? four_floats : 3); // should work, ints can convert to floats.
-  (void)(four_ints ? four_uints : e);  // should work, non-scoped enum can convert to uint.
+  (void)(four_ints ? four_uints : e);  // expected-error {{cannot convert between scalar type 'E' and vector type 'FourUInts'}}
   (void)(four_ints ? four_uints : se); // expected-error {{cannot convert between vector and non-scalar values ('FourUInts' (vector of 4 'unsigned int' values) and 'SE'}}
   // GCC permits this, but our conversion rules reject this for truncation.
   (void)(two_ints ? two_ints : us);        // expected-error {{cannot convert between scalar type 'unsigned int' and vector type 'TwoInts'}}

diff  --git a/clang/test/SemaCXX/vector.cpp b/clang/test/SemaCXX/vector.cpp
index 0c143babbe3b..724ccece0c42 100644
--- a/clang/test/SemaCXX/vector.cpp
+++ b/clang/test/SemaCXX/vector.cpp
@@ -484,3 +484,15 @@ template <class T> void f() {
   second_type st;
 }
 }
+
+namespace PR45780 {
+enum E { Value = 15 };
+void use(char16 c) {
+  E e;
+  c &Value;   // expected-error{{cannot convert between scalar type 'PR45780::E' and vector type 'char16'}}
+  c == Value; // expected-error{{cannot convert between scalar type 'PR45780::E' and vector type 'char16'}}
+  e | c;      // expected-error{{cannot convert between scalar type 'PR45780::E' and vector type 'char16'}}
+  e != c;     // expected-error{{cannot convert between scalar type 'PR45780::E' and vector type 'char16'}}
+}
+
+} // namespace PR45780


        


More information about the cfe-commits mailing list