Skip to content

Commit 8283e34

Browse files
author
v01dxyz
committed
[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.
1 parent 8035d38 commit 8283e34

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

clang/include/clang/AST/ExprCXX.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,8 +2847,8 @@ class TypeTraitExpr final
28472847
///
28482848
/// Example:
28492849
/// \code
2850-
/// __array_rank(int[10][20]) == 2
2851-
/// __array_extent(int, 1) == 20
2850+
/// __array_rank(int[10][20]) == 2
2851+
/// __array_extent(int[10][20], 1) == 20
28522852
/// \endcode
28532853
class ArrayTypeTraitExpr : public Expr {
28542854
/// The trait. An ArrayTypeTrait enum in MSVC compat unsigned.

clang/lib/Sema/TreeTransform.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14947,9 +14947,6 @@ TreeTransform<Derived>::TransformArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1494714947
SubExpr = getDerived().TransformExpr(E->getDimensionExpression());
1494814948
if (SubExpr.isInvalid())
1494914949
return ExprError();
14950-
14951-
if (!getDerived().AlwaysRebuild() && SubExpr.get() == E->getDimensionExpression())
14952-
return E;
1495314950
}
1495414951

1495514952
return getDerived().RebuildArrayTypeTrait(E->getTrait(), E->getBeginLoc(), T,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %clang_cc1 -fsyntax-only %s
2+
3+
// When __array_rank is used with a template type parameter, this
4+
// test ensures clang considers the final expression as having an
5+
// integral type.
6+
//
7+
// Although array_extent was handled well, it is added here.
8+
template <typename T, int N>
9+
constexpr int array_rank(T (&lhs)[N]) {
10+
return __array_rank(T[N]);
11+
}
12+
13+
template <int I, typename T, int N>
14+
constexpr int array_extent(T (&lhs)[N]) {
15+
return __array_extent(T[N], I);
16+
}
17+
18+
int main() {
19+
constexpr int vec[] = {0, 1, 2, 1};
20+
constexpr int mat[4][4] = {
21+
{1, 0, 0, 0},
22+
{0, 1, 0, 0},
23+
{0, 0, 1, 0},
24+
{0, 0, 0, 1}
25+
};
26+
27+
(void) (array_rank(vec) == 1);
28+
(void) (array_rank(vec) == 2);
29+
30+
static_assert(array_rank(vec) == 1);
31+
static_assert(array_rank(mat) == 2);
32+
33+
static_assert(array_extent<0>(vec) == 4);
34+
static_assert(array_extent<0>(mat) == 4);
35+
static_assert(array_extent<1>(mat) == 4);
36+
static_assert(array_extent<1>(vec) == 0);
37+
}

0 commit comments

Comments
 (0)