[clang] [Clang][Sema] Reject _Complex scalars in vector-scalar operations (PR #248101)
Akash Manna via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 2 07:48:33 PST 2026
https://github.com/akash-manna-sky created https://github.com/llvm/llvm-project/pull/248101
Fixes #111301
`tryGCCVectorConvertAndSplat` accepts any arithmetic scalar, and a `_Complex` is one. None of its conversion branches handles complex types, so the scalar was splatted unconverted and CodeGen asserted on a complex value used as a vector element. GCC rejects these operations.
Reject complex scalars there, so the existing "cannot convert between vector and non-scalar values" error is reported. The check #187954 added in `CheckImplicitConversion` for the same symptom is removed: it is skipped in system macros, misses SVE vectors, and broke complex to `ext_vector_type` conversions in C.
AI assisted!
>From fa9aadfae04fabc9a3f3679dd258d1f71be98f95 Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Fri, 6 Nov 2026 22:12:04 +0530
Subject: [PATCH] [clang][Sema] Reject _Complex scalars in vector-scalar
operations
Fixes #111301
`tryGCCVectorConvertAndSplat` accepts any arithmetic scalar, and a `_Complex` is one. None of its conversion branches handles complex types, so the scalar was splatted unconverted and CodeGen asserted on a complex value used as a vector element. GCC rejects these operations.
Reject complex scalars there, so the existing "cannot convert between vector and non-scalar values" error is reported. The check #187954 added in `CheckImplicitConversion` for the same symptom is removed: it is skipped in system macros, misses SVE vectors, and broke complex to `ext_vector_type` conversions in C.
AI assisted!
---
clang/docs/ReleaseNotes.md | 1 +
.../clang/Basic/DiagnosticSemaKinds.td | 2 --
clang/lib/Sema/SemaChecking.cpp | 5 ---
clang/lib/Sema/SemaExpr.cpp | 5 +--
.../test/Sema/aarch64-sve-vector-arith-ops.c | 7 +++++
clang/test/Sema/ext_vector_ops.c | 7 +++++
.../Sema/implicit-cast-complex-to-vector.c | 17 +++++++---
clang/test/Sema/vector-gcc-compat.c | 31 +++++++++++++++++++
clang/test/SemaCXX/vector.cpp | 28 +++++++++++++++++
9 files changed, 90 insertions(+), 13 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6d1dbbbb6eb77b..cea6b6e5456ecc 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -801,6 +801,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Fixed a crash when an OpenMP `copyprivate` clause names a non-static data member that is not private in the
enclosing context, which is now diagnosed, a data member inside a member function template, or a structured
binding. (#GH217893)
+- Fixed a crash when a `_Complex` scalar was an operand of a binary operator together with an integer vector. (#GH111301)
- Fixed a crash when template argument substitution fails in the type of an OpenMP `declare reduction` or
`declare mapper` directive. (#GH223366)
- Fixed an assertion failure when a variable implicitly mapped by an OpenMP `target` directive has a class type
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 56cc9721781063..3259eba43552bf 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4593,8 +4593,6 @@ def warn_impcast_float_result_precision : Warning<
def warn_impcast_double_promotion : Warning<
"implicit conversion increases floating-point precision: %0 to %1">,
InGroup<DoublePromotion>, DefaultIgnore;
-def err_impcast_incompatible_type : Error<
- "implicit conversion from %0 to incompatible type %1">;
def warn_impcast_integer_sign : Warning<
"implicit conversion changes signedness: %0 to %1">,
InGroup<SignConversion>, DefaultIgnore;
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 406f03a17f7ebe..36377140ab2925 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -13672,11 +13672,6 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC,
if (SourceMgr.isInSystemMacro(CC) || Target->isBooleanType())
return;
- if (!getLangOpts().CPlusPlus && Target->isVectorType()) {
- return DiagnoseImpCast(*this, E, T, CC,
- diag::err_impcast_incompatible_type);
- }
-
return DiagnoseImpCast(*this, E, T, CC,
getLangOpts().CPlusPlus
? diag::err_impcast_complex_scalar
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index a14c3d50d12851..3f19b7e77d0cb9 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -10894,8 +10894,9 @@ static bool tryGCCVectorConvertAndSplat(Sema &S, ExprResult *Scalar,
}
// Reject cases where the vector element type or the scalar element type are
- // not integral or floating point types.
- if (!VectorEltTy->isArithmeticType() || !ScalarTy->isArithmeticType())
+ // not integral or real floating point types.
+ if (!VectorEltTy->isArithmeticType() || !ScalarTy->isArithmeticType() ||
+ ScalarTy->isAnyComplexType())
return true;
// The conversion to apply to the scalar before splatting it,
diff --git a/clang/test/Sema/aarch64-sve-vector-arith-ops.c b/clang/test/Sema/aarch64-sve-vector-arith-ops.c
index 4ae8102349301a..738d7474705a28 100644
--- a/clang/test/Sema/aarch64-sve-vector-arith-ops.c
+++ b/clang/test/Sema/aarch64-sve-vector-arith-ops.c
@@ -709,3 +709,10 @@ svint8_t svi8(svint8_t a) {
svint8_t svi8_128(svint8_t a) {
return a + 128; // expected-warning{{implicit conversion from 'int' to 'svint8_t' (aka '__SVInt8_t') changes value from 128 to -128}}
}
+
+void complex_scalar(svint32_t i32, svfloat32_t f32, _Complex double c) {
+ (void)(i32 + c); // expected-error{{cannot convert between vector and non-scalar values}}
+ (void)(c - i32); // expected-error{{cannot convert between vector and non-scalar values}}
+ (void)(i32 == c); // expected-error{{cannot convert between vector and non-scalar values}}
+ (void)(f32 * c); // expected-error{{cannot convert between vector and non-scalar values}}
+}
diff --git a/clang/test/Sema/ext_vector_ops.c b/clang/test/Sema/ext_vector_ops.c
index 5dc3047e145f17..6bc45c0e0d7943 100644
--- a/clang/test/Sema/ext_vector_ops.c
+++ b/clang/test/Sema/ext_vector_ops.c
@@ -233,3 +233,10 @@ void test_incomplete_enum(enum Enum2 *ea, v2u v2ua) {
(void)(v2ua + *ea); // expected-error{{cannot convert between vector and non-scalar values}}
(void)(*ea + v2ua); // expected-error{{cannot convert between vector and non-scalar values}}
}
+
+void test_complex_scalar(v2u v2ua, v2f v2fa, _Complex float c) {
+ (void)(v2ua + c); // expected-error{{cannot convert between vector and non-scalar values}}
+ (void)(c - v2fa); // expected-error{{cannot convert between vector and non-scalar values}}
+ (void)(v2ua == c); // expected-error{{cannot convert between vector and non-scalar values}}
+ v2fa += c; // expected-error{{cannot convert between vector and non-scalar values}}
+}
diff --git a/clang/test/Sema/implicit-cast-complex-to-vector.c b/clang/test/Sema/implicit-cast-complex-to-vector.c
index 279bc60d69b685..cce52e1c2561d1 100644
--- a/clang/test/Sema/implicit-cast-complex-to-vector.c
+++ b/clang/test/Sema/implicit-cast-complex-to-vector.c
@@ -1,14 +1,23 @@
-// RUN: %clang_cc1 -fsyntax-only -verify=c %s
-// RUN: %clang_cc1 -fsyntax-only -verify=c %s -fexperimental-new-constant-interpreter
+// RUN: %clang_cc1 -fsyntax-only -Wconversion -verify=c %s
+// RUN: %clang_cc1 -fsyntax-only -Wconversion -verify=c %s -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify=cxx %s
typedef char __attribute__((__vector_size__(64))) V;
+typedef float __attribute__((ext_vector_type(4))) EV;
void implicit_cast_complex_to_vector() {
_Complex double x;
V y;
- // c-error at +2 {{implicit conversion from '_Complex double' to incompatible type 'V' (vector of 64 'char' values)}}
- // cxx-error at +1 {{implicit conversion from '_Complex double' to 'V' (vector of 64 'char' values) is not permitted in C++}}
+ // c-error at +2 {{cannot convert between vector and non-scalar values ('_Complex double' and 'V' (vector of 64 'char' values))}}
+ // cxx-error at +1 {{cannot convert between vector and non-scalar values ('_Complex double' and 'V' (vector of 64 'char' values))}}
V z = x + y;
}
+void implicit_cast_complex_to_ext_vector(_Complex double x) {
+ // c-warning at +2 {{implicit conversion discards imaginary component: '_Complex double' to 'EV' (vector of 4 'float' values)}}
+ // cxx-error at +1 {{implicit conversion from '_Complex double' to 'EV' (vector of 4 'float' values) is not permitted in C++}}
+ EV v = x;
+ // c-warning at +2 {{implicit conversion discards imaginary component: '_Complex double' to 'EV' (vector of 4 'float' values)}}
+ // cxx-error at +1 {{implicit conversion from '_Complex double' to 'EV' (vector of 4 'float' values) is not permitted in C++}}
+ v = x;
+}
diff --git a/clang/test/Sema/vector-gcc-compat.c b/clang/test/Sema/vector-gcc-compat.c
index 6bb2c5168457ca..a51c30af6fb317 100644
--- a/clang/test/Sema/vector-gcc-compat.c
+++ b/clang/test/Sema/vector-gcc-compat.c
@@ -16,6 +16,7 @@ typedef float v4f32 __attribute__((vector_size(16)));
typedef double v2f64 __attribute__((vector_size(16)));
typedef double v4f64 __attribute__((vector_size(32)));
typedef int v4i32 __attribute((vector_size(16)));
+typedef unsigned __int128 v4u128 __attribute__((vector_size(64)));
// Verify that we can use the [[]] spelling of the attribute.
// We intentionally use the same type alias name to check that both versions
@@ -55,6 +56,7 @@ void uintTestTypeUnsigned(unsigned char a, unsigned short b, unsigned int c,
unsigned long long d);
void uintTestConstant(v2u64 v2u64_a, v2u32 v2u32_a, v2u16 v2u16_a, v2u8 v2u8_a);
void intTestConstant(v2i64 v2i64_a, v2i32 v2i32_a, v2i16 v2i16_a, v2i8 v2i8_a);
+void complexTest(_Complex double cd, _Complex float cf);
void arithmeticTest(void) {
v2i64 v2i64_a = (v2i64){0, 1};
@@ -351,3 +353,32 @@ void intTestConstant(v2i64 v2i64_a, v2i32 v2i32_a, v2i16 v2i16_a, v2i8 v2i8_a) {
v2i16_a = v2i16_a + (-32769); // expected-warning {{implicit conversion from 'int' to 'v2i16' (vector of 2 'short' values) changes value from -32769 to 32767}}
v2i8_a = v2i8_a + (-129); // expected-error {{cannot convert between scalar type 'int' and vector type 'v2i8' (vector of 2 'char' values) as implicit conversion would cause truncation}}
}
+
+// GH111301
+void complexTest(_Complex double cd, _Complex float cf) {
+ v4u128 v4u128_a = {1, 2, 3, 4};
+ v2i64 v2i64_a = {1, 2};
+ v2f64 v2f64_a = {0.4, 0.4};
+ v4u128 v4u128_r;
+ v2i64 v2i64_r;
+ v2f64 v2f64_r;
+
+ v4u128_r = v4u128_a - -cd; // expected-error {{cannot convert between vector and non-scalar values ('v4u128' (vector of 4 'unsigned __int128' values) and '_Complex double')}}
+ v4u128_r = v4u128_a + cd; // expected-error {{cannot convert between vector and non-scalar values ('v4u128' (vector of 4 'unsigned __int128' values) and '_Complex double')}}
+ v2i64_r = v2i64_a - cd; // expected-error {{cannot convert between vector and non-scalar values ('v2i64' (vector of 2 'long long' values) and '_Complex double')}}
+ v2i64_r = cd - v2i64_a; // expected-error {{cannot convert between vector and non-scalar values ('_Complex double' and 'v2i64' (vector of 2 'long long' values))}}
+ v2i64_r = v2i64_a * cf; // expected-error {{cannot convert between vector and non-scalar values ('v2i64' (vector of 2 'long long' values) and '_Complex float')}}
+ v2i64_r = v2i64_a / cd; // expected-error {{cannot convert between vector and non-scalar values ('v2i64' (vector of 2 'long long' values) and '_Complex double')}}
+ v2i64_r = v2i64_a == cd; // expected-error {{cannot convert between vector and non-scalar values ('v2i64' (vector of 2 'long long' values) and '_Complex double')}}
+ v2i64_r = v2i64_a < cf; // expected-error {{cannot convert between vector and non-scalar values ('v2i64' (vector of 2 'long long' values) and '_Complex float')}}
+ v2i64_a += cd; // expected-error {{cannot convert between vector and non-scalar values ('v2i64' (vector of 2 'long long' values) and '_Complex double')}}
+ v2i64_r = 1 ? v2i64_a : cd; // expected-error {{cannot convert between vector and non-scalar values ('v2i64' (vector of 2 'long long' values) and '_Complex double')}}
+ v2i64_r = v2i64_a - 1.0i; // expected-warning {{imaginary constants are a C2y extension}} expected-error {{cannot convert between vector and non-scalar values ('v2i64' (vector of 2 'long long' values) and '_Complex double')}}
+ v2i64_r = v2i64_a - ~cd; // expected-warning {{ISO C does not support '~' for complex conjugation of '_Complex double'}} expected-error {{cannot convert between vector and non-scalar values ('v2i64' (vector of 2 'long long' values) and '_Complex double')}}
+ v2i64_r = v2i64_a - cd++; // expected-warning {{'++' on an object of complex type is a C2y extension}} expected-error {{cannot convert between vector and non-scalar values ('v2i64' (vector of 2 'long long' values) and '_Complex double')}}
+ v2f64_r = v2f64_a + cd; // expected-error {{cannot convert between vector and non-scalar values ('v2f64' (vector of 2 'double' values) and '_Complex double')}}
+
+ v2f64_r = v2f64_a + __real__ cd;
+ v2f64_r = v2f64_a - __imag__ cd;
+ v2i64_r = v2i64_a + (long long)__real__ cf;
+}
diff --git a/clang/test/SemaCXX/vector.cpp b/clang/test/SemaCXX/vector.cpp
index fd6127a33f1ae6..b902d590115db2 100644
--- a/clang/test/SemaCXX/vector.cpp
+++ b/clang/test/SemaCXX/vector.cpp
@@ -559,6 +559,34 @@ void use() {
}
} // namespace PR48540
+namespace GH111301 {
+typedef int v4i __attribute__((vector_size(16)));
+typedef float v4f __attribute__((vector_size(16)));
+
+template <typename T> v4i sub(v4i v, T t) {
+ // expected-error at +1 {{cannot convert between vector and non-scalar values ('v4i' (vector of 4 'int' values) and '_Complex double')}}
+ return v - t;
+}
+template <typename T> v4i mul(v4i v, T t) {
+ // expected-error at +1 {{cannot convert between vector and non-scalar values ('_Complex double' and 'v4i' (vector of 4 'int' values))}}
+ return t * v;
+}
+
+void use(v4i v, v4f f, _Complex double c, bool b) {
+ (void)sub(v, 1);
+ // expected-note at +1 {{in instantiation of function template specialization 'GH111301::sub<_Complex double>' requested here}}
+ (void)sub(v, c);
+ // expected-note at +1 {{in instantiation of function template specialization 'GH111301::mul<_Complex double>' requested here}}
+ (void)mul(v, c);
+ (void)(v + c); // expected-error {{cannot convert between vector and non-scalar values ('v4i' (vector of 4 'int' values) and '_Complex double')}}
+ (void)(v == c); // expected-error {{cannot convert between vector and non-scalar values}}
+ (void)(v && c); // expected-error {{cannot convert between vector and non-scalar values}}
+ (void)(b ? v : c); // expected-error {{cannot convert between vector and non-scalar values}}
+ (void)(f + c); // expected-error {{cannot convert between vector and non-scalar values ('v4f' (vector of 4 'float' values) and '_Complex double')}}
+ v += c; // expected-error {{cannot convert between vector and non-scalar values}}
+}
+} // namespace GH111301
+
#if __cplusplus >= 201703L // C++17 or later
namespace PR180563 {
typedef int __attribute__((vector_size(8))) v_int;
More information about the cfe-commits
mailing list