This is an archive of the discontinued LLVM Phabricator instance.

[coroutines] Sema: Allow co_return all by itself.
ClosedPublic

Authored by GorNishanov on Oct 27 2016, 10:45 AM.

Diff Detail

Repository
rL LLVM

Event Timeline

GorNishanov retitled this revision from to [coroutines] Sema: Allow co_return all by itself..
GorNishanov updated this object.
GorNishanov added a reviewer: rsmith.
GorNishanov added subscribers: EricWF, llvm-commits.

Tiny. Super tiny patch. Barely anything to review. Allows co_return all by itself in a coroutine. As per P0057R2+

I don't fully get the rational for this. Which part of P0057R2 talks about this behavior?

EricWF edited edge metadata.Dec 5 2016, 12:26 PM

@GorNishanov Ping. Can you provide some clarity on this?

@GorNishanov Ping. Can you provide some clarity on this?

Starting from P0057R2, wording stated: 8.4.4/1: A function is a coroutine if it contains a coroutine-return-statement (6.6.3.1), an await-expression (5.3.8), a yield-expression (5.21), or a range-based for (6.5.4) with co_await.

Before the Big Kona Keyword Rename of 2015, you had to have 'await' or 'yield' for a function to a be a coroutine. Now, since, co_return is only usable in coroutines, have a co_return also triggers the coroutine processing.

task<int> f() {
  Do something (no await here) 
  #if 0 // not yet implemented
     something with await
  #endif
  co_return 42;
}

This is not the reason to have co_return, but, once we have it, I think using it as one of the indicators that a function is a coroutine is a reasonable approach.

Cheers,
Gor

EricWF added a comment.Dec 7 2016, 5:58 PM

@GorNishanov Ping. Can you provide some clarity on this?

Starting from P0057R2, wording stated: 8.4.4/1: A function is a coroutine if it contains a coroutine-return-statement (6.6.3.1), an await-expression (5.3.8), a yield-expression (5.21), or a range-based for (6.5.4) with co_await.

Before the Big Kona Keyword Rename of 2015, you had to have 'await' or 'yield' for a function to a be a coroutine. Now, since, co_return is only usable in coroutines, have a co_return also triggers the coroutine processing.

task<int> f() {
  Do something (no await here) 
  #if 0 // not yet implemented
     something with await
  #endif
  co_return 42;
}

This is not the reason to have co_return, but, once we have it, I think using it as one of the indicators that a function is a coroutine is a reasonable approach.

Clang already treats functions with only co_return as coroutines, the diagnostic is a warning not an error. Although IMHO Clang should emit a warning in this case. Obviously it's a false positive in your above example; but in general writing coroutines w/o co_return or co_await is a misuse of coroutines.

Clang already treats functions with only co_return as coroutines, the diagnostic is a warning not an error. Although IMHO Clang should emit a warning in this case. Obviously it's a false positive in your above example; but in general writing coroutines w/o co_return or co_await is a misuse of coroutines.

I am not sure about that. Consider these examples:

Empty Generator

generator<int> g() { co_return; } // this is a generator representing an empty sequence.

Without co_return being legal, you would need to do something like;

generator<int> g() { 
   if (0 != 0) co_yield 1;
 }

Or work in progress async function:

future<int> h() {
  // TODO: add more stuff
  co_return 42;
}

would have to be:

future<int> h() {
  // TODO: add more stuff
  co_await std::experimental::suspend_never{}; // to squash the warning
  co_return 42;
}

I think coroutine by itself with just co_return is a reasonable approach. Besides, that is what the TS says :)

EricWF accepted this revision.Dec 17 2016, 1:40 AM
EricWF edited edge metadata.

Well I'm convinced. And I feel comfortable reviewing this. LGTM.

This revision is now accepted and ready to land.Dec 17 2016, 1:40 AM
GorNishanov updated this revision to Diff 83717.Jan 9 2017, 3:12 PM
GorNishanov edited edge metadata.

merged with latest. Preparing to land.

This revision was automatically updated to reflect the committed changes.