Skip to content

[WIP] SE-0066: Standardize Function Type Syntax #2433

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

Closed
wants to merge 2 commits into from
Closed
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/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,8 @@ ERROR(expected_type_before_arrow,none,
"expected type before '->'", ())
ERROR(expected_type_after_arrow,none,
"expected type after '->'", ())
ERROR(non_tuple_function_param_type,none,
"type before '->' must be wrapped in parentheses", ())

// Enum Types
ERROR(expected_expr_enum_case_raw_value,PointsToFirstBadToken,
Expand Down
6 changes: 6 additions & 0 deletions lib/Parse/ParseType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
// Handle type-function if we have an arrow.
SourceLoc arrowLoc;
if (consumeIf(tok::arrow, arrowLoc)) {
if (!isa<TupleTypeRepr>(ty.get())) {
diagnose(ty.get()->getStartLoc(), diag::non_tuple_function_param_type)
.highlight(ty.get()->getSourceRange())
.fixItInsert(ty.get()->getStartLoc(), "(")
.fixItInsertAfter(ty.get()->getEndLoc(), ")");
}
ParserResult<TypeRepr> SecondHalf =
parseType(diag::expected_type_function_result);
if (SecondHalf.hasCodeCompletion())
Expand Down
2 changes: 1 addition & 1 deletion stdlib/internal/SwiftExperimental/SwiftExperimental.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ infix operator ∘ {
///
/// - Returns: a function that applies ``g`` to the result of applying ``f``
/// to the argument of the new function.
public func ∘<T, U, V>(g: U -> V, f: T -> U) -> (T -> V) {
public func ∘<T, U, V>(g: (U) -> V, f: (T) -> U) -> ((T) -> V) {
return { g(f($0)) }
}

Expand Down
10 changes: 5 additions & 5 deletions stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -2271,8 +2271,8 @@ public func expectEqualsUnordered<
}

public func expectEqualFunctionsForDomain<ArgumentType, Result : Equatable>(
_ arguments: [ArgumentType], _ function1: ArgumentType -> Result,
_ function2: ArgumentType -> Result
_ arguments: [ArgumentType], _ function1: (ArgumentType) -> Result,
_ function2: (ArgumentType) -> Result
) {
for a in arguments {
let expected = function1(a)
Expand All @@ -2285,8 +2285,8 @@ public func expectEqualMethodsForDomain<
SelfType, ArgumentType, Result : Equatable
>(
_ selfs: [SelfType], _ arguments: [ArgumentType],
_ function1: SelfType -> ArgumentType -> Result,
_ function2: SelfType -> ArgumentType -> Result
_ function1: (SelfType) -> (ArgumentType) -> Result,
_ function2: (SelfType) -> (ArgumentType) -> Result
) {
for s in selfs {
for a in arguments {
Expand All @@ -2313,7 +2313,7 @@ public func expectEqualUnicodeScalars(
}
}

func compose<A, B, C>(_ f: A -> B, _ g: B -> C) -> A -> C {
func compose<A, B, C>(_ f: (A) -> B, _ g: (B) -> C) -> (A) -> C {
return { a in
return g(f(a))
}
Expand Down