Index: clang/include/clang/AST/StmtCXX.h =================================================================== --- clang/include/clang/AST/StmtCXX.h +++ clang/include/clang/AST/StmtCXX.h @@ -461,19 +461,20 @@ enum SubStmt { Operand, PromiseCall, Count }; Stmt *SubStmts[SubStmt::Count]; + bool IsVoid : 1; // A void return with non-null operand. bool IsImplicit : 1; friend class ASTStmtReader; public: CoreturnStmt(SourceLocation CoreturnLoc, Stmt *Operand, Stmt *PromiseCall, - bool IsImplicit = false) - : Stmt(CoreturnStmtClass), CoreturnLoc(CoreturnLoc), + bool IsVoid, bool IsImplicit = false) + : Stmt(CoreturnStmtClass), CoreturnLoc(CoreturnLoc), IsVoid(IsVoid), IsImplicit(IsImplicit) { SubStmts[SubStmt::Operand] = Operand; SubStmts[SubStmt::PromiseCall] = PromiseCall; } - CoreturnStmt(EmptyShell) : CoreturnStmt({}, {}, {}) {} + CoreturnStmt(EmptyShell) : CoreturnStmt({}, {}, {}, {}) {} SourceLocation getKeywordLoc() const { return CoreturnLoc; } @@ -482,8 +483,7 @@ Expr *getOperand() const { return static_cast(SubStmts[Operand]); } /// Retrieve the promise call that results from this 'co_return' - /// statement. Will be nullptr if either the coroutine has not yet been - /// finalized or the coroutine has no eventual return type. + /// statement. Will encapsulate the operand, unless IsVoid is true. Expr *getPromiseCall() const { return static_cast(SubStmts[PromiseCall]); } @@ -491,6 +491,9 @@ bool isImplicit() const { return IsImplicit; } void setIsImplicit(bool value = true) { IsImplicit = value; } + bool getIsVoid() const { return IsVoid; } + void setIsVoid(bool value = true) { IsVoid = value; } + SourceLocation getBeginLoc() const LLVM_READONLY { return CoreturnLoc; } SourceLocation getEndLoc() const LLVM_READONLY { return getOperand() ? getOperand()->getEndLoc() : getBeginLoc(); Index: clang/lib/CodeGen/CGCoroutine.cpp =================================================================== --- clang/lib/CodeGen/CGCoroutine.cpp +++ clang/lib/CodeGen/CGCoroutine.cpp @@ -274,12 +274,10 @@ void CodeGenFunction::EmitCoreturnStmt(CoreturnStmt const &S) { ++CurCoro.Data->CoreturnCount; - const Expr *RV = S.getOperand(); - if (RV && RV->getType()->isVoidType() && !isa(RV)) { - // Make sure to evaluate the non initlist expression of a co_return - // with a void expression for side effects. + if (S.getIsVoid()) { + // Evaluate the void expression for side effects. RunCleanupsScope cleanupScope(*this); - EmitIgnoredExpr(RV); + EmitIgnoredExpr(S.getOperand()); } EmitStmt(S.getPromiseCall()); EmitBranchThroughCleanup(CurCoro.Data->FinalJD); Index: clang/lib/Sema/SemaCoroutine.cpp =================================================================== --- clang/lib/Sema/SemaCoroutine.cpp +++ clang/lib/Sema/SemaCoroutine.cpp @@ -983,21 +983,21 @@ } VarDecl *Promise = FSI->CoroutinePromise; + bool IsVoid = false; ExprResult PC; if (E && (isa(E) || !E->getType()->isVoidType())) { getNamedReturnInfo(E, SimplerImplicitMoveMode::ForceOn); PC = buildPromiseCall(*this, Promise, Loc, "return_value", E); } else { E = MakeFullDiscardedValueExpr(E).get(); + IsVoid = bool(E); PC = buildPromiseCall(*this, Promise, Loc, "return_void", None); } if (PC.isInvalid()) return StmtError(); Expr *PCE = ActOnFinishFullExpr(PC.get(), /*DiscardedValue*/ false).get(); - - Stmt *Res = new (Context) CoreturnStmt(Loc, E, PCE, IsImplicit); - return Res; + return new (Context) CoreturnStmt(Loc, E, PCE, IsVoid, IsImplicit); } /// Look up the std::nothrow object.