https://github.com/ojhunt created https://github.com/llvm/llvm-project/pull/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. >From b5b6ddcbc06aa5795fc7b9fad757170f6e961d02 Mon Sep 17 00:00:00 2001 From: Oliver Hunt <oli...@apple.com> Date: Mon, 18 Aug 2025 05:30:36 -0700 Subject: [PATCH] [clang][ObjC] Fix incorrect return type inference for discarded blocks 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. --- clang/lib/Parse/ParseExpr.cpp | 3 ++- clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) 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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits