Skip to content

Commit 89418c6

Browse files
authored
Merge pull request #38112 from hborla/return-type-fixit-loc
[Diagnostics] Correct the insert location of missing return type fix-it.
2 parents 5f44fc4 + e139c45 commit 89418c6

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
@@ -5343,8 +5343,17 @@ bool ExtraneousReturnFailure::diagnoseAsError() {
53435343
if (FD->getResultTypeRepr() == nullptr &&
53445344
FD->getParameters()->getStartLoc().isValid() &&
53455345
!FD->getBaseIdentifier().empty()) {
5346-
auto fixItLoc = Lexer::getLocForEndOfToken(
5347-
getASTContext().SourceMgr, FD->getParameters()->getEndLoc());
5346+
// Insert the fix-it after the parameter list, and after any
5347+
// effects specifiers.
5348+
SourceLoc loc = FD->getParameters()->getEndLoc();
5349+
if (auto asyncLoc = FD->getAsyncLoc())
5350+
loc = asyncLoc;
5351+
5352+
if (auto throwsLoc = FD->getThrowsLoc())
5353+
if (throwsLoc.getOpaquePointerValue() > loc.getOpaquePointerValue())
5354+
loc = throwsLoc;
5355+
5356+
auto fixItLoc = Lexer::getLocForEndOfToken(getASTContext().SourceMgr, loc);
53485357
emitDiagnostic(diag::add_return_type_note)
53495358
.fixItInsert(fixItLoc, " -> <#Return Type#>");
53505359
}

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)