[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread Vlad Serebrennikov via cfe-commits

https://github.com/Endilll closed 
https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread Shafik Yaghmour via cfe-commits


@@ -1741,8 +1741,10 @@ void is_layout_compatible(int n)
   static_assert(!__is_layout_compatible(unsigned char, signed char));
   static_assert(__is_layout_compatible(int[], int[]));
   static_assert(__is_layout_compatible(int[2], int[2]));
-  static_assert(!__is_layout_compatible(int[n], int[2])); // FIXME: VLAs 
should be rejected
-  static_assert(!__is_layout_compatible(int[n], int[n])); // FIXME: VLAs 
should be rejected
+  static_assert(!__is_layout_compatible(int[n], int[2]));
+  // expected-error@-1 {{variable length arrays are not supported for 
'__is_layout_compatible'}}
+  static_assert(!__is_layout_compatible(int[n], int[n]));

shafik wrote:

I think focusing on VLAs here is fine, that was the original scope and I don't 
see an urgent need to expand it here.

https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread Vlad Serebrennikov via cfe-commits

https://github.com/Endilll updated 
https://github.com/llvm/llvm-project/pull/87737

>From ef24f642ca78d357018d6023fb3d9011f115299b Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov 
Date: Fri, 5 Apr 2024 06:22:35 +0300
Subject: [PATCH 1/2] [clang] Reject VLAs in `__is_layout_compatible()`

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.
---
 clang/lib/Sema/SemaExprCXX.cpp | 3 +++
 clang/test/SemaCXX/type-traits.cpp | 6 --
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 76bb78aa8b5458..db84f181012268 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -6026,6 +6026,9 @@ static bool EvaluateBinaryTypeTrait(Sema , TypeTrait 
BTT, QualType LhsT,
 return false;
   }
   case BTT_IsLayoutCompatible: {
+if (LhsT->isVariableArrayType() || RhsT->isVariableArrayType())
+  Self.Diag(KeyLoc, diag::err_vla_unsupported)
+  << 1 << tok::kw___is_layout_compatible;
 return Self.IsLayoutCompatible(LhsT, RhsT);
   }
 default: llvm_unreachable("not a BTT");
diff --git a/clang/test/SemaCXX/type-traits.cpp 
b/clang/test/SemaCXX/type-traits.cpp
index 14ec17989ec7c7..28653e82e5a16e 100644
--- a/clang/test/SemaCXX/type-traits.cpp
+++ b/clang/test/SemaCXX/type-traits.cpp
@@ -1741,8 +1741,10 @@ void is_layout_compatible(int n)
   static_assert(!__is_layout_compatible(unsigned char, signed char));
   static_assert(__is_layout_compatible(int[], int[]));
   static_assert(__is_layout_compatible(int[2], int[2]));
-  static_assert(!__is_layout_compatible(int[n], int[2])); // FIXME: VLAs 
should be rejected
-  static_assert(!__is_layout_compatible(int[n], int[n])); // FIXME: VLAs 
should be rejected
+  static_assert(!__is_layout_compatible(int[n], int[2]));
+  // expected-error@-1 {{variable length arrays are not supported for 
'__is_layout_compatible'}}
+  static_assert(!__is_layout_compatible(int[n], int[n]));
+  // expected-error@-1 {{variable length arrays are not supported for 
'__is_layout_compatible'}}
   static_assert(__is_layout_compatible(int&, int&));
   static_assert(!__is_layout_compatible(int&, char&));
   static_assert(__is_layout_compatible(void(int), void(int)));

>From a8c24fb363a6d2ee7c0dbda2f9e9d96b9451d732 Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov 
Date: Fri, 5 Apr 2024 18:51:58 +0300
Subject: [PATCH 2/2] Improve diagnostic wording

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 +-
 clang/test/SemaCXX/type-traits.cpp   | 8 
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index df57f5e6ce11ba..a1dda2d2461c31 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -165,7 +165,7 @@ def ext_vla_folded_to_constant : ExtWarn<
   "variable length array folded to constant array as an extension">,
   InGroup;
 def err_vla_unsupported : Error<
-  "variable length arrays are not supported for %select{the current 
target|'%1'}0">;
+  "variable length arrays are not supported %select{for the current target|in 
'%1'}0">;
 def err_vla_in_coroutine_unsupported : Error<
   "variable length arrays in a coroutine are not supported">;
 def note_vla_unsupported : Note<
diff --git a/clang/test/SemaCXX/type-traits.cpp 
b/clang/test/SemaCXX/type-traits.cpp
index 28653e82e5a16e..e99ad11666e54c 100644
--- a/clang/test/SemaCXX/type-traits.cpp
+++ b/clang/test/SemaCXX/type-traits.cpp
@@ -740,7 +740,7 @@ void is_bounded_array(int n) {
   static_assert(!__is_bounded_array(cvoid *));
 
   int t32[n];
-  (void)__is_bounded_array(decltype(t32)); // expected-error{{variable length 
arrays are not supported for '__is_bounded_array'}}
+  (void)__is_bounded_array(decltype(t32)); // expected-error{{variable length 
arrays are not supported in '__is_bounded_array'}}
 }
 
 void is_unbounded_array(int n) {
@@ -772,7 +772,7 @@ void is_unbounded_array(int n) {
   static_assert(!__is_unbounded_array(cvoid *));
 
   int t32[n];
-  (void)__is_unbounded_array(decltype(t32)); // expected-error{{variable 
length arrays are not supported for '__is_unbounded_array'}}
+  (void)__is_unbounded_array(decltype(t32)); // expected-error{{variable 
length arrays are not supported in '__is_unbounded_array'}}
 }
 
 void is_referenceable() {
@@ -1742,9 +1742,9 @@ void is_layout_compatible(int n)
   static_assert(__is_layout_compatible(int[], int[]));
   static_assert(__is_layout_compatible(int[2], int[2]));
   

[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread via cfe-commits

https://github.com/cor3ntin approved this pull request.

Following discussions, LGTM but I'd like to make sure incomplete types don't 
fall through the cracks

https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread Vlad Serebrennikov via cfe-commits


@@ -1741,8 +1741,10 @@ void is_layout_compatible(int n)
   static_assert(!__is_layout_compatible(unsigned char, signed char));
   static_assert(__is_layout_compatible(int[], int[]));
   static_assert(__is_layout_compatible(int[2], int[2]));
-  static_assert(!__is_layout_compatible(int[n], int[2])); // FIXME: VLAs 
should be rejected
-  static_assert(!__is_layout_compatible(int[n], int[n])); // FIXME: VLAs 
should be rejected
+  static_assert(!__is_layout_compatible(int[n], int[2]));
+  // expected-error@-1 {{variable length arrays are not supported for 
'__is_layout_compatible'}}
+  static_assert(!__is_layout_compatible(int[n], int[n]));

Endilll wrote:

Yes, other items will be addressed in subsequent PRs.

https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread Aaron Ballman via cfe-commits


@@ -1741,8 +1741,10 @@ void is_layout_compatible(int n)
   static_assert(!__is_layout_compatible(unsigned char, signed char));
   static_assert(__is_layout_compatible(int[], int[]));
   static_assert(__is_layout_compatible(int[2], int[2]));
-  static_assert(!__is_layout_compatible(int[n], int[2])); // FIXME: VLAs 
should be rejected
-  static_assert(!__is_layout_compatible(int[n], int[n])); // FIXME: VLAs 
should be rejected
+  static_assert(!__is_layout_compatible(int[n], int[2]));
+  // expected-error@-1 {{variable length arrays are not supported for 
'__is_layout_compatible'}}
+  static_assert(!__is_layout_compatible(int[n], int[n]));

AaronBallman wrote:

I'm fine with splitting the work across multiple patches so long as we get to 
the desired end state (or have issues filed to track what that end state should 
be).

https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread Vlad Serebrennikov via cfe-commits


@@ -1741,8 +1741,10 @@ void is_layout_compatible(int n)
   static_assert(!__is_layout_compatible(unsigned char, signed char));
   static_assert(__is_layout_compatible(int[], int[]));
   static_assert(__is_layout_compatible(int[2], int[2]));
-  static_assert(!__is_layout_compatible(int[n], int[2])); // FIXME: VLAs 
should be rejected
-  static_assert(!__is_layout_compatible(int[n], int[n])); // FIXME: VLAs 
should be rejected
+  static_assert(!__is_layout_compatible(int[n], int[2]));
+  // expected-error@-1 {{variable length arrays are not supported for 
'__is_layout_compatible'}}
+  static_assert(!__is_layout_compatible(int[n], int[n]));

Endilll wrote:

I recognize that discussion has spiraled out of VLAs into incomplete types and 
flexible array members, but can I keep the original scope of this PR, focusing 
on just VLAs?

https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread Aaron Ballman via cfe-commits


@@ -1741,8 +1741,10 @@ void is_layout_compatible(int n)
   static_assert(!__is_layout_compatible(unsigned char, signed char));
   static_assert(__is_layout_compatible(int[], int[]));
   static_assert(__is_layout_compatible(int[2], int[2]));
-  static_assert(!__is_layout_compatible(int[n], int[2])); // FIXME: VLAs 
should be rejected
-  static_assert(!__is_layout_compatible(int[n], int[n])); // FIXME: VLAs 
should be rejected
+  static_assert(!__is_layout_compatible(int[n], int[2]));
+  // expected-error@-1 {{variable length arrays are not supported for 
'__is_layout_compatible'}}
+  static_assert(!__is_layout_compatible(int[n], int[n]));

AaronBallman wrote:

I think where we ended up is:

1) Reject incomplete types (still needs to be done)
2) Reject VLAs (done, looks good)
3) FAMs are accepted (no changes needed)

So I think you should implement #1 and add tests for it, then add tests for #3, 
and I think that finishes this.

https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

> > However, at the language level, I cannot find any wording either way.
> 
> In my reading, http://eel.is/c++draft/basic.types.general#11 makes any type 
> layout-compatible with itself, and even ignores cv-qualification:
> 
> > Two types cv1 T1 and cv2 T2 are [layout-compatible 
> > types](http://eel.is/c++draft/basic.types.general#def:type,layout-compatible)
> >  if T1 and T2 are the same type, [layout-compatible 
> > enumerations](http://eel.is/c++draft/dcl.enum#def:layout-compatible,enumeration),
> >  or [layout-compatible standard-layout class 
> > types](http://eel.is/c++draft/class.mem#def:layout-compatible,class)[.](http://eel.is/c++draft/basic.types.general#11.sentence-1)
> 
> I find the gap between core language term and type trait rather unfortunate.

There are a lot of gaps between core language terms and type traits. For 
another example, copy constructible in the library is quite different from it 
in the language, volatile types are almost entirely ignored by the library, etc.

At the end of the day, these builtins are intended to be used as the underlying 
implementation for the type traits, so when in doubt, following the library 
rules is usually a safe bet.

https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread Vlad Serebrennikov via cfe-commits

Endilll wrote:

> However, at the language level, I cannot find any wording either way.

In my reading, http://eel.is/c++draft/basic.types.general#11 makes any type 
layout-compatible with itself, and even ignores cv-qualification:

> Two types cv1 T1 and cv2 T2 are [layout-compatible 
> types](http://eel.is/c++draft/basic.types.general#def:type,layout-compatible) 
> if T1 and T2 are the same type, [layout-compatible 
> enumerations](http://eel.is/c++draft/dcl.enum#def:layout-compatible,enumeration),
>  or [layout-compatible standard-layout class 
> types](http://eel.is/c++draft/class.mem#def:layout-compatible,class)[.](http://eel.is/c++draft/basic.types.general#11.sentence-1)

I find the gap between core language term and type trait rather unfortunate.

https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread Nikolas Klauser via cfe-commits

philnik777 wrote:

I think clang should reject incomplete types when the standard says so. It 
doesn't seem particularly useful to accept some special cases but reject 
incomplete types in general. All the traits should probably be audited once. It 
looks like Clang has other problematic cases: https://godbolt.org/z/hajWfq7a6

I don't really care whether Clang should reject VLAs when using the builtin, 
since the trait will be used through some template and that's rejected that 
anyways. FWIW it'd be more consistent, since in my mind `__some_type_trait(T, 
U)` behaves the same as `std::some_type_trait_v`.

Flexible arrays are accepted because they are arrays of unknown bounds in the 
type system, which is part of the standard. The extension is only that they 
aren't rejected at the end of a struct and have some special meaning there. 
They should definitely not be rejected.


https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread via cfe-commits

cor3ntin wrote:

The library does require complete types, interesting 
https://eel.is/c++draft/meta 
However, at the language level, I cannot find any wording either way.

https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

> We should not reject (ie, make the programm ill-form) _any_ type. Just return 
> `false` in all of these cases

GCC rejects incomplete types: https://godbolt.org/z/xWbes5Wsc
Both Clang and GCC reject instantiating templates with VLAs but accept it in 
the builtin: https://godbolt.org/z/b95GEGcGx
Both Clang and GCC accept flexible arrays: https://godbolt.org/z/fWbfExMTb

I think the behavior from GCC is defensible, so I think we should reject 
incomplete types, but accept flexible arrays. VLAs I'm ambivalent about because 
of the template instantiation behavior. CC @ldionne @philnik777 @mordante for 
libc++ perspective.

https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread via cfe-commits

cor3ntin wrote:

We should not reject (ie, make the programm ill-form) _any_ type. Just return 
`false` in all of these cases

https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread Vlad Serebrennikov via cfe-commits

Endilll wrote:

> I think the current behavior is reasonable-ish. Rejecting specific types is a 
> bit weird... I think VLA should model incomplete types (but we currently 
> don't reject that either, which is a bug)
> 
> IE, I would expect __is_layout_compatible to return false in the presence of 
> VLAs, incomplete types, and FAM

My reading of your comment is that in the first paragraph you're asking both 
VLAs and incomplete types to be rejected, then in the second paragraph you're 
asking to extend the intrinsic to accept them and yield false. Can you pick one?

> (but we currently don't reject that either, which is a bug)

I'll address that in a follow-up PR, even though rejecting 
`__is_layout_compatible(IncompleteStruct, IncompleteStruct)` doesn't make sense 
to me.

https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-05 Thread via cfe-commits

cor3ntin wrote:

I think the current behavior is reasonable-ish. Rejecting specific types is a 
bit weird... I think VLA should model incomplete types (but we currently don't 
reject that either, which is a bug)

IE, I would expect  __is_layout_compatible to return false in the presence of 
VLAs, incomplete types, and FAM

https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-04 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Vlad Serebrennikov (Endilll)


Changes

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.

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


2 Files Affected:

- (modified) clang/lib/Sema/SemaExprCXX.cpp (+3) 
- (modified) clang/test/SemaCXX/type-traits.cpp (+4-2) 


``diff
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 76bb78aa8b5458..db84f181012268 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -6026,6 +6026,9 @@ static bool EvaluateBinaryTypeTrait(Sema , TypeTrait 
BTT, QualType LhsT,
 return false;
   }
   case BTT_IsLayoutCompatible: {
+if (LhsT->isVariableArrayType() || RhsT->isVariableArrayType())
+  Self.Diag(KeyLoc, diag::err_vla_unsupported)
+  << 1 << tok::kw___is_layout_compatible;
 return Self.IsLayoutCompatible(LhsT, RhsT);
   }
 default: llvm_unreachable("not a BTT");
diff --git a/clang/test/SemaCXX/type-traits.cpp 
b/clang/test/SemaCXX/type-traits.cpp
index 14ec17989ec7c7..28653e82e5a16e 100644
--- a/clang/test/SemaCXX/type-traits.cpp
+++ b/clang/test/SemaCXX/type-traits.cpp
@@ -1741,8 +1741,10 @@ void is_layout_compatible(int n)
   static_assert(!__is_layout_compatible(unsigned char, signed char));
   static_assert(__is_layout_compatible(int[], int[]));
   static_assert(__is_layout_compatible(int[2], int[2]));
-  static_assert(!__is_layout_compatible(int[n], int[2])); // FIXME: VLAs 
should be rejected
-  static_assert(!__is_layout_compatible(int[n], int[n])); // FIXME: VLAs 
should be rejected
+  static_assert(!__is_layout_compatible(int[n], int[2]));
+  // expected-error@-1 {{variable length arrays are not supported for 
'__is_layout_compatible'}}
+  static_assert(!__is_layout_compatible(int[n], int[n]));
+  // expected-error@-1 {{variable length arrays are not supported for 
'__is_layout_compatible'}}
   static_assert(__is_layout_compatible(int&, int&));
   static_assert(!__is_layout_compatible(int&, char&));
   static_assert(__is_layout_compatible(void(int), void(int)));

``




https://github.com/llvm/llvm-project/pull/87737
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Reject VLAs in `__is_layout_compatible()` (PR #87737)

2024-04-04 Thread Vlad Serebrennikov via cfe-commits

https://github.com/Endilll created 
https://github.com/llvm/llvm-project/pull/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.

>From ef24f642ca78d357018d6023fb3d9011f115299b Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov 
Date: Fri, 5 Apr 2024 06:22:35 +0300
Subject: [PATCH] [clang] Reject VLAs in `__is_layout_compatible()`

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.
---
 clang/lib/Sema/SemaExprCXX.cpp | 3 +++
 clang/test/SemaCXX/type-traits.cpp | 6 --
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 76bb78aa8b5458..db84f181012268 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -6026,6 +6026,9 @@ static bool EvaluateBinaryTypeTrait(Sema , TypeTrait 
BTT, QualType LhsT,
 return false;
   }
   case BTT_IsLayoutCompatible: {
+if (LhsT->isVariableArrayType() || RhsT->isVariableArrayType())
+  Self.Diag(KeyLoc, diag::err_vla_unsupported)
+  << 1 << tok::kw___is_layout_compatible;
 return Self.IsLayoutCompatible(LhsT, RhsT);
   }
 default: llvm_unreachable("not a BTT");
diff --git a/clang/test/SemaCXX/type-traits.cpp 
b/clang/test/SemaCXX/type-traits.cpp
index 14ec17989ec7c7..28653e82e5a16e 100644
--- a/clang/test/SemaCXX/type-traits.cpp
+++ b/clang/test/SemaCXX/type-traits.cpp
@@ -1741,8 +1741,10 @@ void is_layout_compatible(int n)
   static_assert(!__is_layout_compatible(unsigned char, signed char));
   static_assert(__is_layout_compatible(int[], int[]));
   static_assert(__is_layout_compatible(int[2], int[2]));
-  static_assert(!__is_layout_compatible(int[n], int[2])); // FIXME: VLAs 
should be rejected
-  static_assert(!__is_layout_compatible(int[n], int[n])); // FIXME: VLAs 
should be rejected
+  static_assert(!__is_layout_compatible(int[n], int[2]));
+  // expected-error@-1 {{variable length arrays are not supported for 
'__is_layout_compatible'}}
+  static_assert(!__is_layout_compatible(int[n], int[n]));
+  // expected-error@-1 {{variable length arrays are not supported for 
'__is_layout_compatible'}}
   static_assert(__is_layout_compatible(int&, int&));
   static_assert(!__is_layout_compatible(int&, char&));
   static_assert(__is_layout_compatible(void(int), void(int)));

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits