Skip to content

Commit 81eeb74

Browse files
Applied nit comments.
1 parent f0f6a1a commit 81eeb74

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

clang/lib/Sema/SemaCoroutine.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -687,13 +687,14 @@ bool Sema::checkFinalSuspendNoThrow(const Stmt *FinalSuspend) {
687687
// [stmt.return.coroutine]p1:
688688
// A coroutine shall not enclose a return statement ([stmt.return]).
689689
static void checkReturnStmtInCoroutine(Sema &S, FunctionScopeInfo *FSI) {
690-
if (FSI && FSI->FirstReturnLoc.isValid()) {
691-
assert(FSI->FirstCoroutineStmtLoc.isValid() &&
692-
"first coroutine location not set");
693-
S.Diag(FSI->FirstReturnLoc, diag::err_return_in_coroutine);
694-
S.Diag(FSI->FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
695-
<< FSI->getFirstCoroutineStmtKeyword();
696-
}
690+
assert (!FSI && "FunctionScopeInfo is null");
691+
assert(FSI->FirstCoroutineStmtLoc.isValid() &&
692+
"first coroutine location not set");
693+
if (FSI->FirstReturnLoc.isInvalid())
694+
return;
695+
S.Diag(FSI->FirstReturnLoc, diag::err_return_in_coroutine);
696+
S.Diag(FSI->FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
697+
<< FSI->getFirstCoroutineStmtKeyword();
697698
}
698699

699700
bool Sema::ActOnCoroutineBodyStart(Scope *SC, SourceLocation KWLoc,
@@ -706,9 +707,8 @@ bool Sema::ActOnCoroutineBodyStart(Scope *SC, SourceLocation KWLoc,
706707
auto *ScopeInfo = getCurFunction();
707708
assert(ScopeInfo->CoroutinePromise);
708709

709-
if (ScopeInfo->FirstCoroutineStmtLoc == KWLoc) {
710+
if (ScopeInfo->FirstCoroutineStmtLoc == KWLoc)
710711
checkReturnStmtInCoroutine(*this, ScopeInfo);
711-
}
712712

713713
// If we have existing coroutine statements then we have already built
714714
// the initial and final suspend points.

clang/test/SemaCXX/coroutines.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify=expected,cxx20_23,cxx23 %s -fcxx-exceptions -fexceptions -Wunused-result
55
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=expected,cxx14_20,cxx20_23 %s -fcxx-exceptions -fexceptions -Wunused-result
6+
7+
// Run without -verify to check the order of errors we show.
68
// RUN: not %clang_cc1 -std=c++20 -fsyntax-only %s -fcxx-exceptions -fexceptions -Wunused-result 2>&1 | FileCheck %s
79

810
void no_coroutine_traits_bad_arg_await() {
@@ -339,18 +341,20 @@ Handle mixed_return_value() {
339341
co_await a; // expected-note {{function is a coroutine due to use of 'co_await' here}}
340342
return 0; // expected-error {{return statement not allowed in coroutine}}
341343
// expected-error@-1 {{no viable conversion from returned value of type}}
344+
// Check that we first show that return is not allowed in coroutine.
345+
// The error about bad conversion is most likely spurious so we prefer to have it afterwards.
342346
// CHECK-NOT: error: no viable conversion from returned value of type
343347
// CHECK: error: return statement not allowed in coroutine
344348
// CHECK: error: no viable conversion from returned value of type
345349
}
346350

347351
Handle mixed_return_value_return_first(bool b) {
348-
if (b) {
349-
return 0; // expected-error {{no viable conversion from returned value of type}}
350-
// expected-error@-1 {{return statement not allowed in coroutine}}
351-
}
352-
co_await a; // expected-note {{function is a coroutine due to use of 'co_await' here}}
353-
co_return 0; // expected-error {{no member named 'return_value' in 'promise_handle'}}
352+
if (b) {
353+
return 0; // expected-error {{no viable conversion from returned value of type}}
354+
// expected-error@-1 {{return statement not allowed in coroutine}}
355+
}
356+
co_await a; // expected-note {{function is a coroutine due to use of 'co_await' here}}
357+
co_return 0; // expected-error {{no member named 'return_value' in 'promise_handle'}}
354358
}
355359

356360
Handle mixed_multiple_returns(bool b) {
@@ -359,6 +363,7 @@ Handle mixed_multiple_returns(bool b) {
359363
// expected-error@-1 {{return statement not allowed in coroutine}}
360364
}
361365
co_await a; // expected-note {{function is a coroutine due to use of 'co_await' here}}
366+
// The error 'return statement not allowed in coroutine' should appear only once.
362367
return 0; // expected-error {{no viable conversion from returned value of type}}
363368
}
364369

0 commit comments

Comments
 (0)