Skip to content

Commit 298cccb

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

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
@@ -5335,8 +5335,17 @@ bool ExtraneousReturnFailure::diagnoseAsError() {
53355335
if (FD->getResultTypeRepr() == nullptr &&
53365336
FD->getParameters()->getStartLoc().isValid() &&
53375337
!FD->getBaseIdentifier().empty()) {
5338-
auto fixItLoc = Lexer::getLocForEndOfToken(
5339-
getASTContext().SourceMgr, FD->getParameters()->getEndLoc());
5338+
// Insert the fix-it after the parameter list, and after any
5339+
// effects specifiers.
5340+
SourceLoc loc = FD->getParameters()->getEndLoc();
5341+
if (auto asyncLoc = FD->getAsyncLoc())
5342+
loc = asyncLoc;
5343+
5344+
if (auto throwsLoc = FD->getThrowsLoc())
5345+
if (throwsLoc.getOpaquePointerValue() > loc.getOpaquePointerValue())
5346+
loc = throwsLoc;
5347+
5348+
auto fixItLoc = Lexer::getLocForEndOfToken(getASTContext().SourceMgr, loc);
53405349
emitDiagnostic(diag::add_return_type_note)
53415350
.fixItInsert(fixItLoc, " -> <#Return Type#>");
53425351
}

test/Constraints/diagnostics.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,43 @@ func voidFuncWithNestedVoidFunc() {
12051205
}
12061206
}
12071207

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

12101247
func voidFuncExplicitType() -> Void {

0 commit comments

Comments
 (0)