Skip to content

Conversation

@ojhunt
Copy link
Contributor

@ojhunt ojhunt commented Aug 18, 2025

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.

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.
@ojhunt ojhunt requested a review from cor3ntin August 18, 2025 12:40
@ojhunt ojhunt self-assigned this Aug 18, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 18, 2025

@llvm/pr-subscribers-clang

Author: Oliver Hunt (ojhunt)

Changes

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.


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

2 Files Affected:

  • (modified) clang/lib/Parse/ParseExpr.cpp (+2-1)
  • (modified) clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp (+10)
diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp
index bc238a9517a37..d9e86405d338d 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/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp b/clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp
index 05830de9891fe..4c3c23164908e 100644
--- a/clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp
+++ b/clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -std=c++1z -verify %s
+// RUN: %clang_cc1 -std=c++1z -fblocks -verify %s -DBLOCK_TEST
 // RUN: %clang_cc1 -std=c++1z -verify %s -DUNDEFINED
 
 #ifdef UNDEFINED
@@ -254,6 +255,15 @@ namespace GH153884 {
     // expected-note@-1 {{in instantiation of function template specialization 'GH153884::f2()}}
     return false;
   }
+
+#if BLOCK_TEST
+void  block_receiver(int (^)() );
+int f3() {
+  if constexpr (0)
+    (block_receiver)(^{ return 2; });
+  return 1;
+}
+#endif
 }
 
 #endif

@github-actions
Copy link

github-actions bot commented Aug 18, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp -- clang/lib/Parse/ParseExpr.cpp
View the diff from clang-format here.
diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp
index 351534320..ca20093c7 100644
--- a/clang/lib/Parse/ParseExpr.cpp
+++ b/clang/lib/Parse/ParseExpr.cpp
@@ -3343,7 +3343,7 @@ ExprResult Parser::ParseBlockLiteralExpression() {
     return ExprError();
   }
   EnterExpressionEvaluationContextForFunction PotentiallyEvaluated(
-       Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
+      Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
   StmtResult Stmt(ParseCompoundStatementBody());
   BlockScope.Exit();
   if (!Stmt.isInvalid())

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once the bots are happy

@ojhunt ojhunt merged commit ec4e6aa into main Aug 18, 2025
8 of 9 checks passed
@ojhunt ojhunt deleted the users/ojhunt/fix-discarded-block-return-type-inference branch August 18, 2025 21:38
tru pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 21, 2025
llvm#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 ec4e6aa)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category rejects-valid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants