Skip to content

Commit e66b92f

Browse files
authored
Merge pull request #2561 from ahoppen/ahoppen/6.0/return-type-on-initializer
[6.0] Allow return types on initializers
2 parents b12939f + 595b97b commit e66b92f

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
@@ -984,7 +984,7 @@ extension Parser {
984984
}
985985

986986
// Parse the signature.
987-
let signature = self.parseFunctionSignature(allowOutput: false)
987+
let signature = self.parseFunctionSignature()
988988

989989
let whereClause: RawGenericWhereClauseSyntax?
990990
if self.at(.keyword(.where)) {
@@ -1148,7 +1148,7 @@ extension Parser {
11481148
)
11491149
}
11501150

1151-
mutating func parseFunctionSignature(allowOutput: Bool = true) -> RawFunctionSignatureSyntax {
1151+
mutating func parseFunctionSignature() -> RawFunctionSignatureSyntax {
11521152
let parameterClause = self.parseParameterClause(RawFunctionParameterClauseSyntax.self) { parser in
11531153
parser.parseFunctionParameter()
11541154
}
@@ -1168,19 +1168,10 @@ extension Parser {
11681168
returnClause = nil
11691169
}
11701170

1171-
var unexpectedAfterReturnClause: RawUnexpectedNodesSyntax?
1172-
if !allowOutput,
1173-
let unexpectedOutput = returnClause
1174-
{
1175-
returnClause = nil
1176-
unexpectedAfterReturnClause = RawUnexpectedNodesSyntax([unexpectedOutput], arena: self.arena)
1177-
}
1178-
11791171
return RawFunctionSignatureSyntax(
11801172
parameterClause: parameterClause,
11811173
effectSpecifiers: effectSpecifiers,
11821174
returnClause: returnClause,
1183-
unexpectedAfterReturnClause,
11841175
arena: self.arena
11851176
)
11861177
}

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,14 +1257,6 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
12571257
)
12581258
}
12591259

1260-
if let unexpectedOutput = node.signature.unexpectedAfterReturnClause {
1261-
addDiagnostic(
1262-
unexpectedOutput,
1263-
.initializerCannotHaveResultType,
1264-
handledNodes: [unexpectedOutput.id]
1265-
)
1266-
}
1267-
12681260
return .visitChildren
12691261
}
12701262

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
@@ -3283,4 +3283,14 @@ final class DeclarationTests: ParserTestCase {
32833283
XCTAssertEqual(decl.description, input, line: line)
32843284
}
32853285
}
3286+
3287+
func testInitializerWithReturnType() {
3288+
assertParse(
3289+
"init(_ ptr: UnsafeRawBufferPointer, _ a: borrowing Array<Int>) -> _borrow(a) Self",
3290+
experimentalFeatures: .nonescapableTypes
3291+
)
3292+
3293+
// Not actually valid, needs to be diagnosed during type checking
3294+
assertParse("public init() -> Int")
3295+
}
32863296
}

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)