llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: SoulTch (SoulTch)

<details>
<summary>Changes</summary>

Hi, this is my first contribution to the LLVM project, so please let me know if 
there is anything I should follow when contributing.

Simply fixes https://github.com/llvm/llvm-project/issues/194298
- If a function has a ":" symbol in its declaration, the following "{" symbol 
is interpreted as an initializer, not as the function body.
- Hence, the function is interpreted as a function declaration without a body.
- Meanwhile, "co_await" marks the function as a coroutine, so it tries to find 
the function body and crashes while accessing it.
- This diff marks the function as invalid if there is an initializer but no 
actual function body.

I utilized AI while trying to understand why this change makes sense as much as 
possible.

Assisted-by: Claude Opus 5 (Claude Code)

---
Full diff: https://github.com/llvm/llvm-project/pull/218168.diff


4 Files Affected:

- (modified) clang/lib/Parse/ParseCXXInlineMethods.cpp (+1) 
- (modified) clang/lib/Parse/ParseTemplate.cpp (+3-1) 
- (modified) clang/lib/Parse/Parser.cpp (+2) 
- (modified) clang/test/SemaCXX/coroutines.cpp (+8) 


``````````diff
diff --git a/clang/lib/Parse/ParseCXXInlineMethods.cpp 
b/clang/lib/Parse/ParseCXXInlineMethods.cpp
index 3f101feb26a6d..51591011ca548 100644
--- a/clang/lib/Parse/ParseCXXInlineMethods.cpp
+++ b/clang/lib/Parse/ParseCXXInlineMethods.cpp
@@ -633,6 +633,7 @@ void Parser::ParseLexedMethodDef(LexedMethod &LM) {
     // Error recovery.
     if (!Tok.is(tok::l_brace)) {
       FnScope.Exit();
+      LM.D->getAsFunction()->setInvalidDecl();
       Actions.ActOnFinishFunctionBody(LM.D, nullptr);
       return;
     }
diff --git a/clang/lib/Parse/ParseTemplate.cpp 
b/clang/lib/Parse/ParseTemplate.cpp
index 735a9bd1f9f1c..e69d953215e89 100644
--- a/clang/lib/Parse/ParseTemplate.cpp
+++ b/clang/lib/Parse/ParseTemplate.cpp
@@ -1517,8 +1517,10 @@ void 
Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) {
              "current template being instantiated!");
       ParseFunctionStatementBody(LPT.D, FnScope);
       Actions.UnmarkAsLateParsedTemplate(FunD);
-    } else
+    } else {
+      FunD->setInvalidDecl();
       Actions.ActOnFinishFunctionBody(LPT.D, nullptr);
+    }
   }
 }
 
diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index bad81ea92cd2d..5c39ba2ad71a1 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -1407,6 +1407,8 @@ Decl *Parser::ParseFunctionDefinition(ParsingDeclarator 
&D,
     // Recover from error.
     if (!Tok.is(tok::l_brace)) {
       BodyScope.Exit();
+      if (Res)
+        Res->getAsFunction()->setInvalidDecl();
       Actions.ActOnFinishFunctionBody(Res, nullptr);
       return Res;
     }
diff --git a/clang/test/SemaCXX/coroutines.cpp 
b/clang/test/SemaCXX/coroutines.cpp
index 4cef2f2b7ea0f..19b2ce1678e1f 100644
--- a/clang/test/SemaCXX/coroutines.cpp
+++ b/clang/test/SemaCXX/coroutines.cpp
@@ -1566,3 +1566,11 @@ void g() {
 }
 
 }
+
+namespace GH194298 {
+// https://github.com/llvm/llvm-project/issues/194298
+coro<promise_void> f1() : bar { co_await suspend_always{} }; // expected-error 
{{only constructors take base initializers}}
+coro<promise> f2() : bar { co_yield 0 }; // expected-error {{only constructors 
take base initializers}}
+coro<promise_void> f3() : bar { co_return }; // expected-error {{expected 
expression}} \
+                                             // expected-error {{only 
constructors take base initializers}}
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/218168
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to