Skip to content

Conversation

@cor3ntin
Copy link
Contributor

@cor3ntin cor3ntin commented Dec 1, 2025

We were producing a diagnostic for zero-length arrays in Sfinae context, without invalidating the overload.

This causes the diagnostic to be emitted
if and when that undiagnosed overload is selected.

Fixes #170040

We were producing a diagnostic for zero-length arrays in
Sfinae context, without invalidating the overload.

This causes the diagnostic to be emitted
if and when that undiagnosed overload is selected.

Fixes llvm#170040
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Dec 1, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 1, 2025

@llvm/pr-subscribers-clang

Author: Corentin Jabot (cor3ntin)

Changes

We were producing a diagnostic for zero-length arrays in Sfinae context, without invalidating the overload.

This causes the diagnostic to be emitted
if and when that undiagnosed overload is selected.

Fixes #170040


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/Sema/SemaType.cpp (+2)
  • (modified) clang/test/SemaCXX/zero-length-arrays.cpp (+16-1)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8161ccdffca82..33ea81e4352c4 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -554,6 +554,7 @@ Bug Fixes to C++ Support
 - Fix for clang incorrectly rejecting the default construction of a union with
   nontrivial member when another member has an initializer. (#GH81774)
 - Fixed a template depth issue when parsing lambdas inside a type constraint. (#GH162092)
+- Fix the support of zero-length arrays in SFINAE context. (#GH170040)
 - Diagnose unresolved overload sets in non-dependent compound requirements. (#GH51246) (#GH97753)
 - Fix a crash when extracting unavailable member type from alias in template deduction. (#GH165560)
 - Fix incorrect diagnostics for lambdas with init-captures inside braced initializers. (#GH163498)
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index eb8b1352d1be1..91cc4eb4b1016 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -2259,6 +2259,8 @@ QualType Sema::BuildArrayType(QualType T, ArraySizeModifier ASM,
              isSFINAEContext() ? diag::err_typecheck_zero_array_size
                                : diag::ext_typecheck_zero_array_size)
             << 0 << ArraySize->getSourceRange();
+        if (isSFINAEContext())
+          return QualType();
       }
 
       // Is the array too large?
diff --git a/clang/test/SemaCXX/zero-length-arrays.cpp b/clang/test/SemaCXX/zero-length-arrays.cpp
index 0802ec7020463..6bfc7a5fd2e35 100644
--- a/clang/test/SemaCXX/zero-length-arrays.cpp
+++ b/clang/test/SemaCXX/zero-length-arrays.cpp
@@ -1,6 +1,7 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
 
 class Foo {
   ~Foo();
@@ -19,7 +20,7 @@ class Bar {
   Foo foos3[2][0];
 
 public:
-  Bar(): foo_count(0) { }    
+  Bar(): foo_count(0) { }
   ~Bar() { }
 };
 
@@ -33,3 +34,17 @@ void testBar() {
 #endif
   b = b2;
 }
+
+namespace GH170040 {
+#if __cplusplus >= 202002L
+template <int N> struct Foo {
+    operator int() const requires(N == 2);
+    template <int I = 0, char (*)[(I)] = nullptr> operator long() const;
+};
+
+void test () {
+    Foo<2> foo;
+    long bar = foo;
+}
+#endif
+}

@cor3ntin cor3ntin merged commit f01e8ac into llvm:main Dec 2, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang] Clang trunk regression breaks SFINAE with zero-sized array template parameter

3 participants