Skip to content

Migrator/QoI: Function input type: Don't fix Void to (Void), but fix … #9583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -3183,6 +3183,8 @@ ERROR(objc_convention_invalid,none,
" with '@convention(%1)'", (Type, StringRef))
ERROR(function_type_no_parens,none,
"single argument function types require parentheses", ())
WARNING(paren_void_probably_void,none,
"when calling this function in Swift 4 or later, you must pass a '()' tuple; did you mean for the input type to be '()'?", ())
NOTE(not_objc_empty_protocol_composition,none,
"'Any' is not considered '@objc'; use 'AnyObject' instead", ())
NOTE(not_objc_protocol,none,
Expand Down
9 changes: 8 additions & 1 deletion include/swift/Migrator/FixitFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ struct FixitFilter {
return false;
}

// Trying to add '_ in' to a closure signature can be counterproductive when
// fixing function signatures like (Void) -> () to () -> ().
if (Info.ID == diag::closure_argument_list_missing.ID) {
return false;
}

if (Kind == DiagnosticKind::Error)
return true;

Expand Down Expand Up @@ -120,7 +126,8 @@ struct FixitFilter {
Info.ID == diag::override_swift3_objc_inference.ID ||
Info.ID == diag::objc_inference_swift3_objc_derived.ID ||
Info.ID == diag::missing_several_cases.ID ||
Info.ID == diag::missing_particular_case.ID)
Info.ID == diag::missing_particular_case.ID ||
Info.ID == diag::paren_void_probably_void.ID)
return true;

return false;
Expand Down
15 changes: 13 additions & 2 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5143,9 +5143,20 @@ static bool diagnoseSingleCandidateFailures(CalleeCandidateInfo &CCI,
tuple->hasTrailingClosure())
TC.diagnose(loc, diag::extra_trailing_closure_in_call)
.highlight(arg->getSourceRange());
else if (Parameters.empty())
TC.diagnose(loc, diag::extra_argument_to_nullary_call)
else if (Parameters.empty()) {
auto Paren = dyn_cast<ParenExpr>(ArgExpr);
Expr *SubExpr = nullptr;
if (Paren) {
SubExpr = Paren->getSubExpr();
}
if (SubExpr && SubExpr->getType() && SubExpr->getType()->isVoid()) {
TC.diagnose(loc, diag::extra_argument_to_nullary_call)
.fixItRemove(SubExpr->getSourceRange());
} else {
TC.diagnose(loc, diag::extra_argument_to_nullary_call)
.highlight(ArgExpr->getSourceRange());
}
}
else if (name.empty())
TC.diagnose(loc, diag::extra_argument_positional)
.highlight(arg->getSourceRange());
Expand Down
38 changes: 34 additions & 4 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2324,13 +2324,43 @@ Type TypeResolver::resolveASTFunctionType(FunctionTypeRepr *repr,
if (!isa<TupleTypeRepr>(repr->getArgsTypeRepr()) &&
!repr->isWarnedAbout()) {
auto args = repr->getArgsTypeRepr();
TC.diagnose(args->getStartLoc(), diag::function_type_no_parens)
.highlight(args->getSourceRange())
.fixItInsert(args->getStartLoc(), "(")
.fixItInsertAfter(args->getEndLoc(), ")");

bool isVoid = false;
if (const auto Void = dyn_cast<SimpleIdentTypeRepr>(args)) {
if (Void->getIdentifier().str() == "Void") {
isVoid = true;
}
}
if (isVoid) {
TC.diagnose(args->getStartLoc(), diag::function_type_no_parens)
.fixItReplace(args->getStartLoc(), "()");
} else {
TC.diagnose(args->getStartLoc(), diag::function_type_no_parens)
.highlight(args->getSourceRange())
.fixItInsert(args->getStartLoc(), "(")
.fixItInsertAfter(args->getEndLoc(), ")");
}

// Don't emit this warning three times when in generics.
repr->setWarned();
} else if (isa<TupleTypeRepr>(repr->getArgsTypeRepr()) &&
!repr->isWarnedAbout()) {
// If someone wrote (Void) -> () in Swift 3, they probably meant
// () -> (), but (Void) -> () is (()) -> () so emit a warning
// asking if they meant () -> ().
auto args = repr->getArgsTypeRepr();
if (const auto Tuple = dyn_cast<TupleTypeRepr>(args)) {
if (Tuple->getNumElements() == 1) {
if (const auto Void =
dyn_cast<SimpleIdentTypeRepr>(Tuple->getElement(0))) {
if (Void->getIdentifier().str() == "Void") {
TC.diagnose(args->getStartLoc(), diag::paren_void_probably_void)
.fixItReplace(args->getSourceRange(), "()");
repr->setWarned();
}
}
}
}
}

// SIL uses polymorphic function types to resolve overloaded member functions.
Expand Down
6 changes: 3 additions & 3 deletions test/Constraints/diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ for j in i.wibble(a, a) { // expected-error {{type 'A' does not conform to proto
}

// Generic as part of function/tuple types
func f6<T:P2>(_ g: (Void) -> T) -> (c: Int, i: T) {
func f6<T:P2>(_ g: (Void) -> T) -> (c: Int, i: T) { // expected-warning {{when calling this function in Swift 4 or later, you must pass a '()' tuple; did you mean for the input type to be '()'?}} {{20-26=()}}
return (c: 0, i: g())
}

func f7() -> (c: Int, v: A) {
let g: (Void) -> A = { return A() }
let g: (Void) -> A = { return A() } // expected-warning {{when calling this function in Swift 4 or later, you must pass a '()' tuple; did you mean for the input type to be '()'?}} {{10-16=()}}
return f6(g) // expected-error {{cannot convert return expression of type '(c: Int, i: A)' to return type '(c: Int, v: A)'}}
}

Expand Down Expand Up @@ -966,4 +966,4 @@ class SR_4692_b {

private func f(x: Int, y: Bool) {
}
}
}
17 changes: 17 additions & 0 deletions test/Migrator/fixit_void.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-swift-frontend -typecheck %s -swift-version 3
// RUN: %target-swift-frontend -typecheck -update-code -primary-file %s -emit-migrated-file-path %t.result -swift-version 3
// RUN: diff -u %s.expected %t.result
// RUN: %target-swift-frontend -typecheck %s.expected -swift-version 4

func takesNothing(_ f: () -> ()) {
f()
f(())
}

func takesVoidFunction(_ f: (Void) -> ()) {
f()
f(())
}

takesNothing { print("Hello") }
takesVoidFunction { print("Hello") }
17 changes: 17 additions & 0 deletions test/Migrator/fixit_void.swift.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-swift-frontend -typecheck %s -swift-version 3
// RUN: %target-swift-frontend -typecheck -update-code -primary-file %s -emit-migrated-file-path %t.result -swift-version 3
// RUN: diff -u %s.expected %t.result
// RUN: %target-swift-frontend -typecheck %s.expected -swift-version 4

func takesNothing(_ f: () -> ()) {
f()
f()
}

func takesVoidFunction(_ f: () -> ()) {
f()
f()
}

takesNothing { print("Hello") }
takesVoidFunction { print("Hello") }
2 changes: 2 additions & 0 deletions test/Parse/type_expr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,5 @@ func complexSequence() {
// expected-error @-3 {{expected member name or constructor call after type name}}
// expected-note @-4 {{use '.self' to reference the type object}} {{11-11=(}} {{36-36=).self}}
}

func takesVoid(f: Void -> ()) {} // expected-error {{single argument function types require parentheses}} {{19-23=()}}
2 changes: 1 addition & 1 deletion test/expr/closure/single_expr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func testMap(_ array: [Int]) {
// Nested single-expression closures -- <rdar://problem/20931915>
class NestedSingleExpr {
private var b: Bool = false
private func callClosure(_ callback: (Void) -> Void) {}
private func callClosure(_ callback: () -> Void) {}

func call() {
callClosure { [weak self] in
Expand Down