This is an archive of the discontinued LLVM Phabricator instance.

[coroutines] Build fallthrough and set_exception statements.
ClosedPublic

Authored by EricWF on Oct 6 2016, 3:26 PM.

Details

Summary

This patch adds semantic checking and building of the fall-through co_return; statement as well as the p.set_exception(std::current_exception()) call for handling uncaught exceptions.

The fall-through statement is built and checked according to:

[dcl.fct.def.coroutine]/4
The unqualified-ids return_void and return_value are looked up in the scope of class P. If
both are found, the program is ill-formed. If the unqualified-id return_void is found, flowing
off the end of a coroutine is equivalent to a co_return with no operand. Otherwise, flowing off
the end of a coroutine results in undefined behavior.

Similarly the set_exception call is only built when that unqualified-id is found in the scope of class P.

Additionally this patch adds fall-through warnings for non-void returning coroutines. Since it's surprising undefined behavior I thought it would be important to add the warning right away.

Diff Detail

Event Timeline

EricWF updated this revision to Diff 73859.Oct 6 2016, 3:26 PM
EricWF retitled this revision from to [coroutines] Build fallthrough and set_exception statements..
EricWF updated this object.
EricWF added reviewers: rsmith, GorNishanov, majnemer.
EricWF added a subscriber: cfe-commits.
EricWF updated this revision to Diff 73862.Oct 6 2016, 3:35 PM
  • Remove useless line of code.
GorNishanov edited edge metadata.Oct 6 2016, 3:49 PM

Macro shipit:

test/SemaCXX/coreturn.cpp
36

No need to hve yield_value here, unless you want to use co_yield expression in the coroutine. In this file, we are using co_await only. (Same comment for promise_float and promise_int)

majnemer added inline comments.Oct 6 2016, 3:58 PM
lib/Sema/AnalysisBasedWarnings.cpp
537

const auto *

EricWF updated this revision to Diff 73866.Oct 6 2016, 4:18 PM
EricWF edited edge metadata.
EricWF marked 2 inline comments as done.

Address review comments.

test/SemaCXX/coreturn.cpp
36

Ack. I'll add them back if I need them.

LGTM, but, will need to wait for @rsmith to sign off.

@rsmith, I am wondering what were your thoughts on where to generate try { body } catch (...) { p.set_exception(std::exception()); }

Would it be in SemaCoroutine.cpp? Essentially, add something like this:

`
  bool makeBody() {
    if (!OnException)
      return true;

    StmtResult CatchBlock = S.ActOnCXXCatchBlock(Loc, nullptr, OnException);
    if (CatchBlock.isInvalid())
      return false;

    StmtResult TryBlock = S.ActOnCXXTryBlock(Loc, Body, {CatchBlock.get()});
    if (TryBlock.isInvalid())
      return false;

    Body = TryBlock.get();

    return true;
  }
`
rsmith accepted this revision.Oct 27 2016, 12:33 AM
rsmith edited edge metadata.

@rsmith, I am wondering what were your thoughts on where to generate try { body } catch (...) { p.set_exception(std::exception()); }

Would it be in SemaCoroutine.cpp?

Ideally, we'd avoid doing this desugaring at all, and just generate the correct code from CGCoroutine.cpp. Generally, we try to avoid desugaring in the AST whenever we can.

This revision is now accepted and ready to land.Oct 27 2016, 12:33 AM
EricWF closed this revision.Oct 27 2016, 12:39 AM