Skip to content

Commit 86dd041

Browse files
committed
Add diagnostics for init declarations
1 parent 8212815 commit 86dd041

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,14 @@ extension Parser {
10611061
// Parse the signature.
10621062
let signature = self.parseFunctionSignature()
10631063

1064+
// mark the unexpected result type of initializer
1065+
// and replace the ‘output‘ of signature with nil
1066+
var unexpectedResultType: RawUnexpectedNodesSyntax?
1067+
if let output = signature.output {
1068+
signature.raw = signature.layoutView.replacingChild(at: 5, with: nil, arena: self.arena)
1069+
unexpectedResultType = RawUnexpectedNodesSyntax([output.raw], arena: self.arena)
1070+
}
1071+
10641072
let whereClause: RawGenericWhereClauseSyntax?
10651073
if self.at(.keyword(.where)) {
10661074
whereClause = self.parseGenericWhereClause()
@@ -1078,6 +1086,7 @@ extension Parser {
10781086
optionalMark: failable,
10791087
genericParameterClause: generics,
10801088
signature: signature,
1089+
unexpectedResultType,
10811090
genericWhereClause: whereClause,
10821091
body: items,
10831092
arena: self.arena

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,33 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
748748
return .visitChildren
749749
}
750750

751+
public override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind {
752+
if shouldSkip(node) {
753+
return .skipChildren
754+
}
755+
756+
if let unexpectedName = node.signature.input.unexpectedBeforeLeftParen {
757+
addDiagnostic(
758+
unexpectedName,
759+
.initializerCannotHaveName,
760+
fixIts: [
761+
FixIt(message: RemoveNodesFixIt(unexpectedName), changes: .makeMissing(unexpectedName))
762+
],
763+
handledNodes: [unexpectedName.id]
764+
)
765+
}
766+
767+
if let unexpectedOutput = node.unexpectedBetweenSignatureAndGenericWhereClause {
768+
addDiagnostic(
769+
unexpectedOutput,
770+
.initializerCannotHaveResultType,
771+
handledNodes: [unexpectedOutput.id]
772+
)
773+
}
774+
775+
return .visitChildren
776+
}
777+
751778
public override func visit(_ node: MemberDeclListItemSyntax) -> SyntaxVisitorContinueKind {
752779
if shouldSkip(node) {
753780
return .skipChildren

Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ extension DiagnosticMessage where Self == StaticParserError {
131131
public static var initializerInPattern: Self {
132132
.init("unexpected initializer in pattern; did you mean to use '='?")
133133
}
134+
public static var initializerCannotHaveName: Self {
135+
.init("initializers cannot have a name")
136+
}
137+
public static var initializerCannotHaveResultType: Self {
138+
.init("initializers cannot have a result type")
139+
}
134140
public static var invalidFlagAfterPrecedenceGroupAssignment: Self {
135141
.init("expected 'true' or 'false' after 'assignment'")
136142
}

0 commit comments

Comments
 (0)