[llvm-branch-commits] [clang] 7d6969b - [clang][ObjC] Fix incorrect return type inference for discarded blocks (#154109)

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Aug 20 23:07:58 PDT 2025


Author: Oliver Hunt
Date: 2025-08-21T08:07:51+02:00
New Revision: 7d6969bc6f86265ac938112b0d4d5f7dfea0510d

URL: https://github.com/llvm/llvm-project/commit/7d6969bc6f86265ac938112b0d4d5f7dfea0510d
DIFF: https://github.com/llvm/llvm-project/commit/7d6969bc6f86265ac938112b0d4d5f7dfea0510d.diff

LOG: [clang][ObjC] Fix incorrect return type inference for discarded blocks (#154109)

When parsing a block expression we were not entering a new eval context
and as a result when parsing the block body we continue to treat any
return statements as discarded so infer a `void` result.

This fixes the problem by introducing an evaluation context around the
parsing of the body.

(cherry picked from commit ec4e6aaac4612af26322b2b10b8f518ecf053c74)

Added: 
    clang/test/SemaObjCXX/discarded-block-type-inference.mm

Modified: 
    clang/lib/Parse/ParseExpr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp
index bc238a9517a37..3515343202de1 100644
--- a/clang/lib/Parse/ParseExpr.cpp
+++ b/clang/lib/Parse/ParseExpr.cpp
@@ -3342,7 +3342,8 @@ ExprResult Parser::ParseBlockLiteralExpression() {
     Actions.ActOnBlockError(CaretLoc, getCurScope());
     return ExprError();
   }
-
+  EnterExpressionEvaluationContextForFunction PotentiallyEvaluated(
+       Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
   StmtResult Stmt(ParseCompoundStatementBody());
   BlockScope.Exit();
   if (!Stmt.isInvalid())

diff  --git a/clang/test/SemaObjCXX/discarded-block-type-inference.mm b/clang/test/SemaObjCXX/discarded-block-type-inference.mm
new file mode 100644
index 0000000000000..8e2587724a7f6
--- /dev/null
+++ b/clang/test/SemaObjCXX/discarded-block-type-inference.mm
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -fobjc-arc -fblocks %s
+
+void  block_receiver(int (^)() );
+
+int f1() {
+  if constexpr (0)
+    (block_receiver)(^{ return 2; });
+  return 1;
+}
+
+int f2() {
+  if constexpr (0)
+    return (^{ return 2; })();
+  return 1;
+}


        


More information about the llvm-branch-commits mailing list