diff --git a/lib/Sema/CSDiagnostics.cpp b/lib/Sema/CSDiagnostics.cpp index 85d8441a6b624..11690855fd824 100644 --- a/lib/Sema/CSDiagnostics.cpp +++ b/lib/Sema/CSDiagnostics.cpp @@ -5337,8 +5337,17 @@ bool ExtraneousReturnFailure::diagnoseAsError() { if (FD->getResultTypeRepr() == nullptr && FD->getParameters()->getStartLoc().isValid() && !FD->getBaseIdentifier().empty()) { - auto fixItLoc = Lexer::getLocForEndOfToken( - getASTContext().SourceMgr, FD->getParameters()->getEndLoc()); + // Insert the fix-it after the parameter list, and after any + // effects specifiers. + SourceLoc loc = FD->getParameters()->getEndLoc(); + if (auto asyncLoc = FD->getAsyncLoc()) + loc = asyncLoc; + + if (auto throwsLoc = FD->getThrowsLoc()) + if (throwsLoc.getOpaquePointerValue() > loc.getOpaquePointerValue()) + loc = throwsLoc; + + auto fixItLoc = Lexer::getLocForEndOfToken(getASTContext().SourceMgr, loc); emitDiagnostic(diag::add_return_type_note) .fixItInsert(fixItLoc, " -> <#Return Type#>"); } diff --git a/test/Constraints/diagnostics.swift b/test/Constraints/diagnostics.swift index 77910cc013222..38a19cc4aaa2c 100644 --- a/test/Constraints/diagnostics.swift +++ b/test/Constraints/diagnostics.swift @@ -1213,6 +1213,43 @@ func voidFuncWithNestedVoidFunc() { } } +func voidFuncWithEffects1() throws { + return 1 + // expected-error@-1 {{unexpected non-void return value in void function}} + // expected-note@-2 {{did you mean to add a return type?}}{{35-35= -> <#Return Type#>}} +} + +func voidFuncWithEffects2() async throws { + return 1 + // expected-error@-1 {{unexpected non-void return value in void function}} + // expected-note@-2 {{did you mean to add a return type?}}{{41-41= -> <#Return Type#>}} +} + +// expected-error@+1 {{'async' must precede 'throws'}} +func voidFuncWithEffects3() throws async { + return 1 + // expected-error@-1 {{unexpected non-void return value in void function}} + // expected-note@-2 {{did you mean to add a return type?}}{{41-41= -> <#Return Type#>}} +} + +func voidFuncWithEffects4() async { + return 1 + // expected-error@-1 {{unexpected non-void return value in void function}} + // expected-note@-2 {{did you mean to add a return type?}}{{34-34= -> <#Return Type#>}} +} + +func voidFuncWithEffects5(_ closure: () throws -> Void) rethrows { + return 1 + // expected-error@-1 {{unexpected non-void return value in void function}} + // expected-note@-2 {{did you mean to add a return type?}}{{65-65= -> <#Return Type#>}} +} + +func voidGenericFuncWithEffects(arg: T) async where T: CustomStringConvertible { + return 1 + // expected-error@-1 {{unexpected non-void return value in void function}} + // expected-note@-2 {{did you mean to add a return type?}}{{49-49= -> <#Return Type#>}} +} + // Special cases: These should not offer a note + fix-it func voidFuncExplicitType() -> Void {