Skip to content

Commit e6352d5

Browse files
committed
Revert "Allow return types on initializers"
This reverts commit 595b97b.
1 parent 320a9b2 commit e6352d5

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

Diff for: Sources/SwiftParser/Declarations.swift

+11-2
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ extension Parser {
995995
}
996996

997997
// Parse the signature.
998-
let signature = self.parseFunctionSignature()
998+
let signature = self.parseFunctionSignature(allowOutput: false)
999999

10001000
let whereClause: RawGenericWhereClauseSyntax?
10011001
if self.at(.keyword(.where)) {
@@ -1159,7 +1159,7 @@ extension Parser {
11591159
)
11601160
}
11611161

1162-
mutating func parseFunctionSignature() -> RawFunctionSignatureSyntax {
1162+
mutating func parseFunctionSignature(allowOutput: Bool = true) -> RawFunctionSignatureSyntax {
11631163
let parameterClause = self.parseParameterClause(RawFunctionParameterClauseSyntax.self) { parser in
11641164
parser.parseFunctionParameter()
11651165
}
@@ -1179,10 +1179,19 @@ extension Parser {
11791179
returnClause = nil
11801180
}
11811181

1182+
var unexpectedAfterReturnClause: RawUnexpectedNodesSyntax?
1183+
if !allowOutput,
1184+
let unexpectedOutput = returnClause
1185+
{
1186+
returnClause = nil
1187+
unexpectedAfterReturnClause = RawUnexpectedNodesSyntax([unexpectedOutput], arena: self.arena)
1188+
}
1189+
11821190
return RawFunctionSignatureSyntax(
11831191
parameterClause: parameterClause,
11841192
effectSpecifiers: effectSpecifiers,
11851193
returnClause: returnClause,
1194+
unexpectedAfterReturnClause,
11861195
arena: self.arena
11871196
)
11881197
}

Diff for: Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

+8
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,14 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
12731273
)
12741274
}
12751275

1276+
if let unexpectedOutput = node.signature.unexpectedAfterReturnClause {
1277+
addDiagnostic(
1278+
unexpectedOutput,
1279+
.initializerCannotHaveResultType,
1280+
handledNodes: [unexpectedOutput.id]
1281+
)
1282+
}
1283+
12761284
return .visitChildren
12771285
}
12781286

Diff for: Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift

+3
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ 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+
}
179182
public static var invalidFlagAfterPrecedenceGroupAssignment: Self {
180183
.init("expected 'true' or 'false' after 'assignment'")
181184
}

Diff for: Tests/SwiftParserTest/translated/InitDeinitTests.swift

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

@@ -422,7 +425,10 @@ final class InitDeinitTests: ParserTestCase {
422425
assertParse(
423426
"""
424427
init(_ foo: T) 1️⃣-> Int where T: Comparable {}
425-
"""
428+
""",
429+
diagnostics: [
430+
DiagnosticSpec(message: "initializers cannot have a result type")
431+
]
426432
)
427433
}
428434

0 commit comments

Comments
 (0)