Skip to content

Commit f2545dc

Browse files
authored
[CSDiagnostics] Insert '@escaping' fix-it when type has '@autoclosure' during non-escaping function type diagnostic (#33191)
1 parent 0c684f0 commit f2545dc

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -915,12 +915,21 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const {
915915
// Give a note and fix-it
916916
auto note = emitDiagnosticAt(PD, diag::noescape_parameter, PD->getName());
917917

918+
SourceLoc reprLoc;
919+
SourceLoc autoclosureEndLoc;
920+
if (auto *repr = PD->getTypeRepr()) {
921+
reprLoc = repr->getStartLoc();
922+
if (auto *attrRepr = dyn_cast<AttributedTypeRepr>(repr)) {
923+
autoclosureEndLoc = Lexer::getLocForEndOfToken(
924+
getASTContext().SourceMgr,
925+
attrRepr->getAttrs().getLoc(TAK_autoclosure));
926+
}
927+
}
918928
if (!PD->isAutoClosure()) {
919-
SourceLoc reprLoc;
920-
if (auto *repr = PD->getTypeRepr())
921-
reprLoc = repr->getStartLoc();
922929
note.fixItInsert(reprLoc, "@escaping ");
923-
} // TODO: add in a fixit for autoclosure
930+
} else {
931+
note.fixItInsertAfter(autoclosureEndLoc, " @escaping");
932+
}
924933

925934
return true;
926935
}

test/attr/attr_escaping.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ struct StoresClosure {
4444
return ["ultimate answer": fn] // expected-error{{using non-escaping parameter 'fn' in a context expecting an @escaping closure}}
4545
}
4646

47+
func autoclosureDictPack(_ fn: @autoclosure () -> Int) -> [String: () -> Int] {
48+
// expected-note@-1{{parameter 'fn' is implicitly non-escaping}} {{46-46= @escaping}}
49+
return ["ultimate answer": fn] // expected-error{{using non-escaping parameter 'fn' in a context expecting an @escaping closure}}
50+
}
51+
4752
func arrayPack(_ fn: @escaping () -> Int, _ fn2 : () -> Int) -> [() -> Int] {
4853
// expected-note@-1{{parameter 'fn2' is implicitly non-escaping}} {{53-53=@escaping }}
4954

0 commit comments

Comments
 (0)