Merge pull request #79502 from gottesmm/pr-0157e2d0f6c260a7cd233938fd987b1ada6a30da

[rbi] Fix a thinko where while finding closure uses, I was not checking if functions had a body when looking for arguments.
This commit is contained in:
Michael Gottesman
2025-02-19 21:13:21 -08:00
committed by GitHub
2 changed files with 14 additions and 3 deletions

View File

@@ -238,7 +238,7 @@ findClosureUse(Operand *initialOperand) {
return {};
auto *f = as.getCalleeFunction();
if (!f)
if (!f || f->empty())
return {};
unsigned argumentIndex = as.getCalleeArgIndex(*initialOperand);
@@ -280,7 +280,7 @@ findClosureUse(Operand *initialOperand) {
// See if we have a callee function. In such a case, find our operand in the
// callee and visit its uses.
if (auto as = dyn_cast<PartialApplyInst>(op->getUser())) {
if (auto *f = as->getCalleeFunction()) {
if (auto *f = as->getCalleeFunction(); f && !f->empty()) {
auto *fArg = f->getArgument(ApplySite(as).getCalleeArgIndex(*op));
for (auto *use : fArg->getUses()) {
if (visitedOperand.insert(use).second)
@@ -294,7 +294,7 @@ findClosureUse(Operand *initialOperand) {
// immediately invoked. In such a case, we can emit a better diagnostic in
// the called closure.
if (auto fas = FullApplySite::isa(op->getUser())) {
if (auto *f = fas.getCalleeFunction()) {
if (auto *f = fas.getCalleeFunction(); f && !f->empty()) {
auto *fArg = cast<SILFunctionArgument>(
f->getArgument(fas.getCalleeArgIndex(*op)));
if (fArg->isClosureCapture()) {

View File

@@ -1933,3 +1933,14 @@ func testIndirectAndDirectSendingResultsWithGlobalActor() async {
_ = ns2
}
}
// We used to not check if bodies were not empty when emitting the error for
// using result in the throwing task group. Make sure we do not crash.
func testFunctionIsNotEmpty(input: SendableKlass) async throws {
var result: [SendableKlass] = []
try await withThrowingTaskGroup(of: Void.self) { taskGroup in // expected-warning {{no calls to throwing functions occur within 'try' expression}}
taskGroup.addTask { // expected-tns-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
result.append(input) // expected-tns-note {{closure captures reference to mutable var 'result' which is accessible to code in the current task}}
}
}
}