Skip to content

Commit 813f68c

Browse files
authored
[clang] Reject VLAs in __is_layout_compatible() (#87737)
This is a follow-up to #81506. Since `__is_layout_compatible()` is a C++ intrinsic (https://github.com/llvm/llvm-project/blob/ff1e72d68d1224271801ff5192a8c14fbd3be83b/clang/include/clang/Basic/TokenKinds.def#L523), I don't think we should define how it interacts with VLA extension unless we have a compelling reason to. Since #81506 was merged after 18 cut-off, we don't have to follow any kind of deprecation process.
1 parent 76435f2 commit 813f68c

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def ext_vla_folded_to_constant : ExtWarn<
165165
"variable length array folded to constant array as an extension">,
166166
InGroup<GNUFoldingConstant>;
167167
def err_vla_unsupported : Error<
168-
"variable length arrays are not supported for %select{the current target|'%1'}0">;
168+
"variable length arrays are not supported %select{for the current target|in '%1'}0">;
169169
def err_vla_in_coroutine_unsupported : Error<
170170
"variable length arrays in a coroutine are not supported">;
171171
def note_vla_unsupported : Note<

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6026,6 +6026,9 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT,
60266026
return false;
60276027
}
60286028
case BTT_IsLayoutCompatible: {
6029+
if (LhsT->isVariableArrayType() || RhsT->isVariableArrayType())
6030+
Self.Diag(KeyLoc, diag::err_vla_unsupported)
6031+
<< 1 << tok::kw___is_layout_compatible;
60296032
return Self.IsLayoutCompatible(LhsT, RhsT);
60306033
}
60316034
default: llvm_unreachable("not a BTT");

clang/test/SemaCXX/type-traits.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ void is_bounded_array(int n) {
740740
static_assert(!__is_bounded_array(cvoid *));
741741

742742
int t32[n];
743-
(void)__is_bounded_array(decltype(t32)); // expected-error{{variable length arrays are not supported for '__is_bounded_array'}}
743+
(void)__is_bounded_array(decltype(t32)); // expected-error{{variable length arrays are not supported in '__is_bounded_array'}}
744744
}
745745

746746
void is_unbounded_array(int n) {
@@ -772,7 +772,7 @@ void is_unbounded_array(int n) {
772772
static_assert(!__is_unbounded_array(cvoid *));
773773

774774
int t32[n];
775-
(void)__is_unbounded_array(decltype(t32)); // expected-error{{variable length arrays are not supported for '__is_unbounded_array'}}
775+
(void)__is_unbounded_array(decltype(t32)); // expected-error{{variable length arrays are not supported in '__is_unbounded_array'}}
776776
}
777777

778778
void is_referenceable() {
@@ -1741,8 +1741,10 @@ void is_layout_compatible(int n)
17411741
static_assert(!__is_layout_compatible(unsigned char, signed char));
17421742
static_assert(__is_layout_compatible(int[], int[]));
17431743
static_assert(__is_layout_compatible(int[2], int[2]));
1744-
static_assert(!__is_layout_compatible(int[n], int[2])); // FIXME: VLAs should be rejected
1745-
static_assert(!__is_layout_compatible(int[n], int[n])); // FIXME: VLAs should be rejected
1744+
static_assert(!__is_layout_compatible(int[n], int[2]));
1745+
// expected-error@-1 {{variable length arrays are not supported in '__is_layout_compatible'}}
1746+
static_assert(!__is_layout_compatible(int[n], int[n]));
1747+
// expected-error@-1 {{variable length arrays are not supported in '__is_layout_compatible'}}
17461748
static_assert(__is_layout_compatible(int&, int&));
17471749
static_assert(!__is_layout_compatible(int&, char&));
17481750
static_assert(__is_layout_compatible(void(int), void(int)));

0 commit comments

Comments
 (0)