Skip to content

Commit 595b97b

Browse files
committed
Allow return types on initializers
SE-2305 allows returning `Self` and `Self?` from initializers so that the return type can be annotated with `_borrow(a)` etc. So we need to allow return clauses on initializers. rdar://123905900
1 parent d9bf34e commit 595b97b

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
@@ -1212,14 +1212,6 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
12121212
)
12131213
}
12141214

1215-
if let unexpectedOutput = node.signature.unexpectedAfterReturnClause {
1216-
addDiagnostic(
1217-
unexpectedOutput,
1218-
.initializerCannotHaveResultType,
1219-
handledNodes: [unexpectedOutput.id]
1220-
)
1221-
}
1222-
12231215
return .visitChildren
12241216
}
12251217

Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,6 @@ extension DiagnosticMessage where Self == StaticParserError {
182182
public static var initializerCannotHaveName: Self {
183183
.init("initializers cannot have a name")
184184
}
185-
public static var initializerCannotHaveResultType: Self {
186-
.init("initializers cannot have a result type")
187-
}
188185
public static var invalidFlagAfterPrecedenceGroupAssignment: Self {
189186
.init("expected 'true' or 'false' after 'assignment'")
190187
}

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)