Skip to content

Commit ec4e6aa

Browse files
authored
[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.
1 parent 5612dc5 commit ec4e6aa

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

clang/lib/Parse/ParseExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3342,7 +3342,8 @@ ExprResult Parser::ParseBlockLiteralExpression() {
33423342
Actions.ActOnBlockError(CaretLoc, getCurScope());
33433343
return ExprError();
33443344
}
3345-
3345+
EnterExpressionEvaluationContextForFunction PotentiallyEvaluated(
3346+
Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
33463347
StmtResult Stmt(ParseCompoundStatementBody());
33473348
BlockScope.Exit();
33483349
if (!Stmt.isInvalid())
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_cc1 -std=c++23 -fsyntax-only -fobjc-arc -fblocks %s
2+
3+
void block_receiver(int (^)() );
4+
5+
int f1() {
6+
if constexpr (0)
7+
(block_receiver)(^{ return 2; });
8+
return 1;
9+
}
10+
11+
int f2() {
12+
if constexpr (0)
13+
return (^{ return 2; })();
14+
return 1;
15+
}

0 commit comments

Comments
 (0)