-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang][ObjC] Fix incorrect return type inference for discarded blocks #154109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang][ObjC] Fix incorrect return type inference for discarded blocks #154109
Conversation
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.
|
@llvm/pr-subscribers-clang Author: Oliver Hunt (ojhunt) ChangesWhen 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 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:
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
|
You can test this locally with the following command:git-clang-format --diff HEAD~1 HEAD --extensions cpp -- clang/lib/Parse/ParseExpr.cppView 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())
|
cor3ntin
left a comment
There was a problem hiding this 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
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)
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
voidresult.This fixes the problem by introducing an evaluation context around the parsing of the body.