[clang] [Sema] Fix __array_rank queried type at template instantiation (PR #124491)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 27 02:12:47 PST 2025
https://github.com/cor3ntin updated https://github.com/llvm/llvm-project/pull/124491
>From 9063b4f5ada77fa593b8e23e4706a2cd126e5858 Mon Sep 17 00:00:00 2001
From: v01dxyz <v01dxyz at v01d.xyz>
Date: Sun, 26 Jan 2025 23:07:13 +0100
Subject: [PATCH] [Sema] Fix __array_rank queried type at template
instantiation
The type being queried was left as a template type parameter, making
the whole expression as dependent and thus not eligible to
static_assert.
---
clang/docs/ReleaseNotes.rst | 2 +
clang/include/clang/AST/ExprCXX.h | 4 +-
clang/lib/Sema/TreeTransform.h | 3 -
.../array-type-trait-with-template.cpp | 129 ++++++++++++++++++
4 files changed, 133 insertions(+), 5 deletions(-)
create mode 100644 clang/test/SemaCXX/array-type-trait-with-template.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e9fffddd507c66..646483b3b2d711 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -991,6 +991,8 @@ Bug Fixes to C++ Support
- Fixed assertions or false compiler diagnostics in the case of C++ modules for
lambda functions or inline friend functions defined inside templates (#GH122493).
- Clang now rejects declaring an alias template with the same name as its template parameter. (#GH123423)
+- Fix type of expression when calling a template which returns an ``__array_rank`` querying a type depending on a
+ template parameter. Now, such expression can be used with ``static_assert`` and ``constexpr``. (#GH123498)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h
index aa10945addf78f..2a130bc6da79a0 100644
--- a/clang/include/clang/AST/ExprCXX.h
+++ b/clang/include/clang/AST/ExprCXX.h
@@ -2847,8 +2847,8 @@ class TypeTraitExpr final
///
/// Example:
/// \code
-/// __array_rank(int[10][20]) == 2
-/// __array_extent(int, 1) == 20
+/// __array_rank(int[10][20]) == 2
+/// __array_extent(int[10][20], 1) == 20
/// \endcode
class ArrayTypeTraitExpr : public Expr {
/// The trait. An ArrayTypeTrait enum in MSVC compat unsigned.
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 12680843a434a0..f04adf7fdf8ad2 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -14947,9 +14947,6 @@ TreeTransform<Derived>::TransformArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
SubExpr = getDerived().TransformExpr(E->getDimensionExpression());
if (SubExpr.isInvalid())
return ExprError();
-
- if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getDimensionExpression())
- return E;
}
return getDerived().RebuildArrayTypeTrait(E->getTrait(), E->getBeginLoc(), T,
diff --git a/clang/test/SemaCXX/array-type-trait-with-template.cpp b/clang/test/SemaCXX/array-type-trait-with-template.cpp
new file mode 100644
index 00000000000000..942714ec5d55a5
--- /dev/null
+++ b/clang/test/SemaCXX/array-type-trait-with-template.cpp
@@ -0,0 +1,129 @@
+// RUN: %clang_cc1 -fsyntax-only %s
+// RUN: %clang_cc1 -fsyntax-only -std=c++20 -DWITH_AUTO_FUNCTION_PARAMETER=1 %s
+
+// When __array_rank is used with a template type parameter, this test
+// ensures clang considers the final expression could be used with
+// static_assert/constexpr.
+//
+// Although array_extent was handled well, we add it as a precaution.
+
+template <typename T>
+using remove_reference_t = __remove_reference_t(T);
+
+template <typename T, int N>
+constexpr int array_rank(T (&lhs)[N]) {
+ return __array_rank(T[N]);
+}
+
+template <int I, typename T, int N>
+ constexpr int array_extent(T (&lhs)[N]) {
+ return __array_extent(T[N], I);
+}
+
+template <typename T>
+struct Rank {
+ using ArrayT = remove_reference_t<T>;
+
+ template <int N>
+ static constexpr int call(ArrayT (&lhs)[N]) {
+ return __array_rank(ArrayT[N]);
+ }
+};
+
+template <typename T>
+struct Extent {
+ using ArrayT = remove_reference_t<T>;
+
+ template <int I, int N>
+ static constexpr int call(ArrayT (&lhs)[N]) {
+ return __array_extent(ArrayT[N], I);
+ }
+};
+
+#ifdef WITH_AUTO_FUNCTION_PARAMETER
+template <int N>
+constexpr int array_rank_auto(auto (&lhs)[N]) {
+ return __array_rank(remove_reference_t<decltype(lhs[0])>[N]);
+}
+
+template <int I, int N>
+constexpr int array_extent_auto(auto (&lhs)[N]) {
+ return __array_extent(remove_reference_t<decltype(lhs[0])>[N], I);
+}
+#endif
+
+template <int N>
+constexpr int array_rank_int(const int (&lhs)[N]) {
+ return __array_rank(const int[N]);
+}
+
+template <int I, int N>
+constexpr int array_extent_int(const int (&lhs)[N]) {
+ return __array_extent(const int[N], I);
+}
+
+template <int M, int N>
+constexpr int array_rank_int(const int (&lhs)[M][N]) {
+ return __array_rank(const int[M][N]);
+}
+
+template <int I, int M, int N>
+constexpr int array_extent_int(const int (&lhs)[M][N]) {
+ return __array_extent(const int[M][N], I);
+}
+
+int main() {
+ constexpr int vec[] = {0, 1, 2, 1};
+ constexpr int mat[4][4] = {
+ {1, 0, 0, 0},
+ {0, 1, 0, 0},
+ {0, 0, 1, 0},
+ {0, 0, 0, 1}
+ };
+
+#define ATT_TESTS_WITH_ASSERT(ATT_ASSERT) \
+ { ATT_ASSERT(RANK(vec) == 1); } \
+ { ATT_ASSERT(RANK(mat) == 2); } \
+ { ATT_ASSERT(EXTENT(vec, 0) == 4); } \
+ { ATT_ASSERT(EXTENT(vec, 1) == 0); } \
+ { ATT_ASSERT(EXTENT(mat, 1) == 4); }
+
+#define ATT_TESTS() \
+ ATT_TESTS_WITH_ASSERT( constexpr bool cst = ) \
+ ATT_TESTS_WITH_ASSERT( (void) ) \
+ ATT_TESTS_WITH_ASSERT( static_assert )
+
+ {
+#define RANK(lhs) array_rank(lhs)
+#define EXTENT(lhs, i) array_extent<i>(lhs)
+ ATT_TESTS();
+#undef RANK
+#undef EXTENT
+ }
+
+ {
+#define RANK(lhs) Rank<decltype(lhs[0])>::call(lhs)
+#define EXTENT(lhs, i) Extent<decltype(lhs[0])>::call<i>(lhs)
+ ATT_TESTS();
+#undef RANK
+#undef EXTENT
+ }
+
+#ifdef WITH_AUTO_FUNCTION_PARAMETER
+ {
+#define RANK(lhs) array_rank_auto(lhs)
+#define EXTENT(lhs, i) array_extent_auto<i>(lhs)
+ ATT_TESTS();
+#undef RANK
+#undef EXTENT
+ }
+#endif
+
+ {
+#define RANK(lhs) array_rank_int(lhs)
+#define EXTENT(lhs, i) array_extent_int<i>(lhs)
+ ATT_TESTS();
+#undef RANK
+#undef EXTENT
+ }
+}
More information about the cfe-commits
mailing list