[clang] [coroutines][coro_lifetimebound] Detect lifetime issues with lambda captures (PR #77066)

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 8 18:25:38 PST 2024


================
@@ -7575,15 +7577,27 @@ static void visitLifetimeBoundArguments(IndirectLocalPath &Path, Expr *Call,
     Path.pop_back();
   };
 
-  if (ObjectArg && implicitObjectParamIsLifetimeBound(Callee))
-    VisitLifetimeBoundArg(Callee, ObjectArg);
-
   bool CheckCoroCall = false;
   if (const auto *RD = Callee->getReturnType()->getAsRecordDecl()) {
     CheckCoroCall = RD->hasAttr<CoroLifetimeBoundAttr>() &&
                     RD->hasAttr<CoroReturnTypeAttr>() &&
                     !Callee->hasAttr<CoroDisableLifetimeBoundAttr>();
   }
+
+  if (ObjectArg) {
+    bool CheckCoroObjArg = CheckCoroCall;
+    // Ignore `__promise.get_return_object()` as it is not lifetimebound.
+    if (CheckCoroObjArg && Callee->getDeclName().isIdentifier() &&
+        Callee->getName() == "get_return_object")
+      CheckCoroObjArg = false;
+    // Coroutine lambda objects with empty capture list are not lifetimebound.
+    if (auto *LE = dyn_cast<LambdaExpr>(ObjectArg->IgnoreImplicit());
+        LE && LE->captures().empty())
+      CheckCoroObjArg = false;
----------------
ChuanqiXu9 wrote:

> It may not result in runtime errors (even in sanitizers), because this is never accessed, but I wonder if it is actually legal or UB in the standard?

I feel it is literally undefined. Since the spec doesn't say a lot about resumption/suspension about coroutines. Also the description of http://eel.is/c++draft/dcl.fct.def.coroutine#note-3 is vague too. It says it is `likely` to be an undefined behavior.

> Which makes me skew in the direction of warning for these cases two

I didn't get the logic here. Why it is not good to warn the undefined things?

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


More information about the cfe-commits mailing list