Skip to content

[C++20] Fix a crash with spaceship and vector types #139767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

AaronBallman
Copy link
Collaborator

Vector types cannot be directly compared, you get an error when you try to do so. This patch causes the explicitly defaulted spaceship operator to be implicitly deleted.

Fixes #137452

Vector types cannot be directly compared, you get an error when you try
to do so. This patch causes the explicitly defaulted spaceship operator
to be implicitly deleted.

Fixes llvm#137452
@AaronBallman AaronBallman added clang Clang issues not falling into any other category c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" crash-on-valid spaceship issues related to <=> labels May 13, 2025
@llvmbot
Copy link
Member

llvmbot commented May 13, 2025

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

Vector types cannot be directly compared, you get an error when you try to do so. This patch causes the explicitly defaulted spaceship operator to be implicitly deleted.

Fixes #137452


Full diff: https://github.com/llvm/llvm-project/pull/139767.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3)
  • (modified) clang/lib/Sema/SemaDeclCXX.cpp (+5)
  • (modified) clang/test/SemaCXX/cxx2a-three-way-comparison.cpp (+8)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index bc13d02e2d20b..3941eb51b00d7 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -118,6 +118,9 @@ C++23 Feature Support
 
 C++20 Feature Support
 ^^^^^^^^^^^^^^^^^^^^^
+- Fixed a crash with a defaulted spaceship (``<=>``) operator when the class
+  contains a member declaration of vector type. Vector types cannot yet be
+  compared directly, so this causes the operator to be deleted. (#GH137452)
 
 C++17 Feature Support
 ^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index cbccb567e2adf..2f2ff2d471c71 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -8651,6 +8651,11 @@ class DefaultedComparisonAnalyzer
         assert(Best->BuiltinParamTypes[2].isNull() &&
                "invalid builtin comparison");
 
+        // FIXME: If the type we deduced is a vector type, we mark the
+        // comparison as deleted because we don't yet support this.
+        if (isa<VectorType>(T))
+          return Result::deleted();
+
         if (NeedsDeducing) {
           std::optional<ComparisonCategoryType> Cat =
               getComparisonCategoryForBuiltinCmp(T);
diff --git a/clang/test/SemaCXX/cxx2a-three-way-comparison.cpp b/clang/test/SemaCXX/cxx2a-three-way-comparison.cpp
index b94225274fffb..36b603e4f7660 100644
--- a/clang/test/SemaCXX/cxx2a-three-way-comparison.cpp
+++ b/clang/test/SemaCXX/cxx2a-three-way-comparison.cpp
@@ -58,3 +58,11 @@ namespace PR44325 {
   // implicit rewrite rules, not for explicit use by programs.
   bool c = cmp_cat() < 0; // expected-warning {{zero as null pointer constant}}
 }
+
+namespace GH137452 {
+struct comparable_t {
+    __attribute__((vector_size(32))) double numbers;
+    auto operator<=>(const comparable_t& rhs) const = default; // expected-warning {{explicitly defaulted three-way comparison operator is implicitly deleted}} \
+                                                                  expected-note {{replace 'default' with 'delete'}}
+};
+} // namespace GH137452

// FIXME: If the type we deduced is a vector type, we mark the
// comparison as deleted because we don't yet support this.
if (isa<VectorType>(T))
return Result::deleted();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other versions of 'deleted' all seem to diagnose first, is there a reason to not do so here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed the "explain deleted" code paths entirely. I'll address that.

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to issue a diagnostic for this? I would imagine that users could get confused about this if they don’t know why the operator is marked as deleted.

@Sirraide
Copy link
Member

GCC does print a note explaining that this is not implemented apparently.

@AaronBallman
Copy link
Collaborator Author

Do we want to issue a diagnostic for this? I would imagine that users could get confused about this if they don’t know why the operator is marked as deleted.

Yeah, I missed that part of the patch. More changes coming.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category crash-on-valid spaceship issues related to <=>
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default operator<=> with an __attribute__((vector_size)) field crashes frontend
5 participants