Skip to content

Commit e139c45

Browse files
committed
[Diagnostics] In ExtraneousReturnFailure, insert the return type fix-it
after any effects specifiers like async, throws, rethrows, etc.
1 parent 61805a5 commit e139c45

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5337,8 +5337,17 @@ bool ExtraneousReturnFailure::diagnoseAsError() {
53375337
if (FD->getResultTypeRepr() == nullptr &&
53385338
FD->getParameters()->getStartLoc().isValid() &&
53395339
!FD->getBaseIdentifier().empty()) {
5340-
auto fixItLoc = Lexer::getLocForEndOfToken(
5341-
getASTContext().SourceMgr, FD->getParameters()->getEndLoc());
5340+
// Insert the fix-it after the parameter list, and after any
5341+
// effects specifiers.
5342+
SourceLoc loc = FD->getParameters()->getEndLoc();
5343+
if (auto asyncLoc = FD->getAsyncLoc())
5344+
loc = asyncLoc;
5345+
5346+
if (auto throwsLoc = FD->getThrowsLoc())
5347+
if (throwsLoc.getOpaquePointerValue() > loc.getOpaquePointerValue())
5348+
loc = throwsLoc;
5349+
5350+
auto fixItLoc = Lexer::getLocForEndOfToken(getASTContext().SourceMgr, loc);
53425351
emitDiagnostic(diag::add_return_type_note)
53435352
.fixItInsert(fixItLoc, " -> <#Return Type#>");
53445353
}

test/Constraints/diagnostics.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,43 @@ func voidFuncWithNestedVoidFunc() {
12131213
}
12141214
}
12151215

1216+
func voidFuncWithEffects1() throws {
1217+
return 1
1218+
// expected-error@-1 {{unexpected non-void return value in void function}}
1219+
// expected-note@-2 {{did you mean to add a return type?}}{{35-35= -> <#Return Type#>}}
1220+
}
1221+
1222+
func voidFuncWithEffects2() async throws {
1223+
return 1
1224+
// expected-error@-1 {{unexpected non-void return value in void function}}
1225+
// expected-note@-2 {{did you mean to add a return type?}}{{41-41= -> <#Return Type#>}}
1226+
}
1227+
1228+
// expected-error@+1 {{'async' must precede 'throws'}}
1229+
func voidFuncWithEffects3() throws async {
1230+
return 1
1231+
// expected-error@-1 {{unexpected non-void return value in void function}}
1232+
// expected-note@-2 {{did you mean to add a return type?}}{{41-41= -> <#Return Type#>}}
1233+
}
1234+
1235+
func voidFuncWithEffects4() async {
1236+
return 1
1237+
// expected-error@-1 {{unexpected non-void return value in void function}}
1238+
// expected-note@-2 {{did you mean to add a return type?}}{{34-34= -> <#Return Type#>}}
1239+
}
1240+
1241+
func voidFuncWithEffects5(_ closure: () throws -> Void) rethrows {
1242+
return 1
1243+
// expected-error@-1 {{unexpected non-void return value in void function}}
1244+
// expected-note@-2 {{did you mean to add a return type?}}{{65-65= -> <#Return Type#>}}
1245+
}
1246+
1247+
func voidGenericFuncWithEffects<T>(arg: T) async where T: CustomStringConvertible {
1248+
return 1
1249+
// expected-error@-1 {{unexpected non-void return value in void function}}
1250+
// expected-note@-2 {{did you mean to add a return type?}}{{49-49= -> <#Return Type#>}}
1251+
}
1252+
12161253
// Special cases: These should not offer a note + fix-it
12171254

12181255
func voidFuncExplicitType() -> Void {

0 commit comments

Comments
 (0)