From: Eric Fiselier Date: Fri, 30 Sep 2016 22:38:31 +0000 (+0000) Subject: [coroutines] Diagnose when 'main' is declared as a coroutine. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fa5a2d7a669a54f6ed90d6026a1ad25ed6cd6649;p=clang [coroutines] Diagnose when 'main' is declared as a coroutine. Summary: The title says it all. Reviewers: rsmith, GorNishanov Subscribers: mehdi_amini, cfe-commits Differential Revision: https://reviews.llvm.org/D25078 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@282973 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 00b9ca805a..58e8e00d85 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -8562,6 +8562,8 @@ def err_coroutine_ctor_dtor : Error< "'%1' cannot be used in a %select{constructor|destructor}0">; def err_coroutine_constexpr : Error< "'%0' cannot be used in a constexpr function">; +def err_coroutine_main : Error< + "'main' cannot be a coroutine">; def err_coroutine_varargs : Error< "'%0' cannot be used in a varargs function">; def ext_coroutine_without_co_await_co_yield : ExtWarn< diff --git a/lib/Sema/SemaCoroutine.cpp b/lib/Sema/SemaCoroutine.cpp index 5143036088..461ca8eddc 100644 --- a/lib/Sema/SemaCoroutine.cpp +++ b/lib/Sema/SemaCoroutine.cpp @@ -127,6 +127,11 @@ checkCoroutineContext(Sema &S, SourceLocation Loc, StringRef Keyword) { S.Diag(Loc, diag::err_coroutine_constexpr) << Keyword; } else if (FD->isVariadic()) { S.Diag(Loc, diag::err_coroutine_varargs) << Keyword; + } else if (FD->isMain()) { + S.Diag(FD->getLocStart(), diag::err_coroutine_main); + S.Diag(Loc, diag::note_declared_coroutine_here) + << (Keyword == "co_await" ? 0 : + Keyword == "co_yield" ? 1 : 2); } else { auto *ScopeInfo = S.getCurFunction(); assert(ScopeInfo && "missing function scope for function"); diff --git a/test/SemaCXX/coroutines.cpp b/test/SemaCXX/coroutines.cpp index d375e9eddb..92fdc79474 100644 --- a/test/SemaCXX/coroutines.cpp +++ b/test/SemaCXX/coroutines.cpp @@ -283,3 +283,11 @@ struct bad_promise_5 { coro bad_final_suspend() { // expected-error {{no member named 'await_ready' in 'not_awaitable'}} co_await a; } + + +template<> struct std::coroutine_traits +{ using promise_type = promise; }; + +int main(int, const char**) { // expected-error {{'main' cannot be a coroutine}} + co_await a; // expected-note {{function is a coroutine due to use of 'co_await' here}} +}