Skip to content

Commit 09e8ef9

Browse files
authored
Diagnose use of VLAs in a coroutine (#70341)
Fixes #65858
1 parent a7d6039 commit 09e8ef9

File tree

6 files changed

+60
-6
lines changed

6 files changed

+60
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,10 @@ Bug Fixes to C++ Support
636636
(`#46200 <https://github.com/llvm/llvm-project/issues/46200>`_)
637637
(`#57812 <https://github.com/llvm/llvm-project/issues/57812>`_)
638638

639+
- Diagnose use of a variable-length array in a coroutine. The design of
640+
coroutines is such that it is not possible to support VLA use. Fixes:
641+
(`#65858 <https://github.com/llvm/llvm-project/issues/65858>`_)
642+
639643
- Fix bug where we were overriding zero-initialization of class members when
640644
default initializing a base class in a constant expression context. Fixes:
641645
(`#69890 <https://github.com/llvm/llvm-project/issues/69890>`_)

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ def ext_vla_folded_to_constant : ExtWarn<
166166
InGroup<GNUFoldingConstant>;
167167
def err_vla_unsupported : Error<
168168
"variable length arrays are not supported for %select{the current target|'%1'}0">;
169+
def err_vla_in_coroutine_unsupported : Error<
170+
"variable length arrays in a coroutine are not supported">;
169171
def note_vla_unsupported : Note<
170172
"variable length arrays are not supported for the current target">;
171173

clang/include/clang/Sema/ScopeInfo.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ class FunctionScopeInfo {
189189
/// First SEH '__try' statement in the current function.
190190
SourceLocation FirstSEHTryLoc;
191191

192+
/// First use of a VLA within the current function.
193+
SourceLocation FirstVLALoc;
194+
192195
private:
193196
/// Used to determine if errors occurred in this function or block.
194197
DiagnosticErrorTrap ErrorTrap;
@@ -473,6 +476,11 @@ class FunctionScopeInfo {
473476
FirstSEHTryLoc = TryLoc;
474477
}
475478

479+
void setHasVLA(SourceLocation VLALoc) {
480+
if (FirstVLALoc.isInvalid())
481+
FirstVLALoc = VLALoc;
482+
}
483+
476484
bool NeedsScopeChecking() const {
477485
return !HasDroppedStmt && (HasIndirectGoto || HasMustTail ||
478486
(HasBranchProtectedScope && HasBranchIntoScope));

clang/lib/Sema/SemaCoroutine.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,11 @@ void Sema::CheckCompletedCoroutineBody(FunctionDecl *FD, Stmt *&Body) {
11981198
if (FD->hasAttr<AlwaysInlineAttr>())
11991199
Diag(FD->getLocation(), diag::warn_always_inline_coroutine);
12001200

1201+
// The design of coroutines means we cannot allow use of VLAs within one, so
1202+
// diagnose if we've seen a VLA in the body of this function.
1203+
if (Fn->FirstVLALoc.isValid())
1204+
Diag(Fn->FirstVLALoc, diag::err_vla_in_coroutine_unsupported);
1205+
12011206
// [stmt.return.coroutine]p1:
12021207
// A coroutine shall not enclose a return statement ([stmt.return]).
12031208
if (Fn->FirstReturnLoc.isValid()) {

clang/lib/Sema/SemaType.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2706,12 +2706,18 @@ QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
27062706
}
27072707
}
27082708

2709-
if (T->isVariableArrayType() && !Context.getTargetInfo().isVLASupported()) {
2710-
// CUDA device code and some other targets don't support VLAs.
2711-
bool IsCUDADevice = (getLangOpts().CUDA && getLangOpts().CUDAIsDevice);
2712-
targetDiag(Loc,
2713-
IsCUDADevice ? diag::err_cuda_vla : diag::err_vla_unsupported)
2714-
<< (IsCUDADevice ? CurrentCUDATarget() : 0);
2709+
if (T->isVariableArrayType()) {
2710+
if (!Context.getTargetInfo().isVLASupported()) {
2711+
// CUDA device code and some other targets don't support VLAs.
2712+
bool IsCUDADevice = (getLangOpts().CUDA && getLangOpts().CUDAIsDevice);
2713+
targetDiag(Loc,
2714+
IsCUDADevice ? diag::err_cuda_vla : diag::err_vla_unsupported)
2715+
<< (IsCUDADevice ? CurrentCUDATarget() : 0);
2716+
} else if (sema::FunctionScopeInfo *FSI = getCurFunction()) {
2717+
// VLAs are supported on this target, but we may need to do delayed
2718+
// checking that the VLA is not being used within a coroutine.
2719+
FSI->setHasVLA(Loc);
2720+
}
27152721
}
27162722

27172723
// If this is not C99, diagnose array size modifiers on non-VLAs.

clang/test/SemaCXX/coroutine-vla.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %clang_cc1 %s -std=c++20 -fsyntax-only -Wno-vla-cxx-extension -verify
2+
#include "Inputs/std-coroutine.h"
3+
4+
struct promise;
5+
6+
struct coroutine : std::coroutine_handle<promise> {
7+
using promise_type = ::promise;
8+
};
9+
10+
struct promise
11+
{
12+
coroutine get_return_object();
13+
std::suspend_always initial_suspend() noexcept;
14+
std::suspend_always final_suspend() noexcept;
15+
void return_void();
16+
void unhandled_exception();
17+
};
18+
19+
coroutine foo(int n) {
20+
int array[n]; // expected-error {{variable length arrays in a coroutine are not supported}}
21+
co_return;
22+
}
23+
24+
void lambda() {
25+
[](int n) -> coroutine {
26+
int array[n]; // expected-error {{variable length arrays in a coroutine are not supported}}
27+
co_return;
28+
}(10);
29+
}

0 commit comments

Comments
 (0)