Skip to content

Commit 77efdcc

Browse files
authored
Merge pull request swiftlang#2541 from ahoppen/ahoppen/return-type-on-initializer
Allow return types on initializers
2 parents 0a3851b + 3e368ca commit 77efdcc

File tree

5 files changed

+14
-30
lines changed

5 files changed

+14
-30
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ extension Parser {
968968
}
969969

970970
// Parse the signature.
971-
let signature = self.parseFunctionSignature(allowOutput: false)
971+
let signature = self.parseFunctionSignature()
972972

973973
let whereClause: RawGenericWhereClauseSyntax?
974974
if self.at(.keyword(.where)) {
@@ -1131,7 +1131,7 @@ extension Parser {
11311131
)
11321132
}
11331133

1134-
mutating func parseFunctionSignature(allowOutput: Bool = true) -> RawFunctionSignatureSyntax {
1134+
mutating func parseFunctionSignature() -> RawFunctionSignatureSyntax {
11351135
let parameterClause = self.parseParameterClause(RawFunctionParameterClauseSyntax.self) { parser in
11361136
parser.parseFunctionParameter()
11371137
}
@@ -1148,19 +1148,10 @@ extension Parser {
11481148
returnClause = nil
11491149
}
11501150

1151-
var unexpectedAfterReturnClause: RawUnexpectedNodesSyntax?
1152-
if !allowOutput,
1153-
let unexpectedOutput = returnClause
1154-
{
1155-
returnClause = nil
1156-
unexpectedAfterReturnClause = RawUnexpectedNodesSyntax([unexpectedOutput], arena: self.arena)
1157-
}
1158-
11591151
return RawFunctionSignatureSyntax(
11601152
parameterClause: parameterClause,
11611153
effectSpecifiers: effectSpecifiers,
11621154
returnClause: returnClause,
1163-
unexpectedAfterReturnClause,
11641155
arena: self.arena
11651156
)
11661157
}

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,14 +1168,6 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
11681168
)
11691169
}
11701170

1171-
if let unexpectedOutput = node.signature.unexpectedAfterReturnClause {
1172-
addDiagnostic(
1173-
unexpectedOutput,
1174-
.initializerCannotHaveResultType,
1175-
handledNodes: [unexpectedOutput.id]
1176-
)
1177-
}
1178-
11791171
return .visitChildren
11801172
}
11811173

Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,6 @@ extension DiagnosticMessage where Self == StaticParserError {
176176
public static var initializerCannotHaveName: Self {
177177
.init("initializers cannot have a name")
178178
}
179-
public static var initializerCannotHaveResultType: Self {
180-
.init("initializers cannot have a result type")
181-
}
182179
public static var invalidFlagAfterPrecedenceGroupAssignment: Self {
183180
.init("expected 'true' or 'false' after 'assignment'")
184181
}

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,4 +3156,14 @@ final class DeclarationTests: ParserTestCase {
31563156
XCTAssertEqual(decl.description, input, line: line)
31573157
}
31583158
}
3159+
3160+
func testInitializerWithReturnType() {
3161+
assertParse(
3162+
"init(_ ptr: UnsafeRawBufferPointer, _ a: borrowing Array<Int>) -> _borrow(a) Self",
3163+
experimentalFeatures: .nonescapableTypes
3164+
)
3165+
3166+
// Not actually valid, needs to be diagnosed during type checking
3167+
assertParse("public init() -> Int")
3168+
}
31593169
}

Tests/SwiftParserTest/translated/InitDeinitTests.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ final class InitDeinitTests: ParserTestCase {
104104
struct FooStructConstructorD {
105105
init() 1️⃣-> FooStructConstructorD { }
106106
}
107-
""",
108-
diagnostics: [
109-
DiagnosticSpec(message: "initializers cannot have a result type")
110-
]
107+
"""
111108
)
112109
}
113110

@@ -425,10 +422,7 @@ final class InitDeinitTests: ParserTestCase {
425422
assertParse(
426423
"""
427424
init(_ foo: T) 1️⃣-> Int where T: Comparable {}
428-
""",
429-
diagnostics: [
430-
DiagnosticSpec(message: "initializers cannot have a result type")
431-
]
425+
"""
432426
)
433427
}
434428

0 commit comments

Comments
 (0)