[clang] [llvm] [Clang][CodeGen][Coroutines] Make coroutine startup exception-safe (CWG2935) (PR #177628)

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 25 18:38:25 PST 2026


================
@@ -768,6 +772,23 @@ struct GetReturnObjectManager {
         CGF.EmitAnyExprToMem(S.getReturnValue(), CGF.ReturnValue,
                              S.getReturnValue()->getType().getQualifiers(),
                              /*IsInit*/ true);
+        // If the return value has a non-trivial destructor, register a
+        // conditional cleanup that will destroy it if an exception is thrown
+        // before initial-await-resume. The cleanup is activated now and will
+        // be deactivated once initial_suspend completes normally.
+        if (QualType::DestructionKind DtorKind =
+                S.getReturnValue()->getType().isDestructedType()) {
----------------
ChuanqiXu9 wrote:

We can check for if the await initial_suspend may throw. If not, we don't need to insert the code, then we can avoid affecting optimizer.

And also it is better to insert a warning if the initial_suspend may  throw. We did the similar thing for final_suspend.

https://github.com/llvm/llvm-project/pull/177628


More information about the cfe-commits mailing list