Skip to content

Add diagnostic if deinits have names and parameters #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1302,12 +1302,18 @@ extension Parser {
_ handle: RecoveryConsumptionHandle
) -> RawDeinitializerDeclSyntax {
let (unexpectedBeforeDeinitKeyword, deinitKeyword) = self.eat(handle)
var unexpectedNameAndSignature: [RawSyntax?] = []
unexpectedNameAndSignature.append(self.consume(if: .identifier, where: { !$0.isAtStartOfLine }).map(RawSyntax.init))
if self.at(.leftParen) && !self.currentToken.isAtStartOfLine {
unexpectedNameAndSignature.append(RawSyntax(parseFunctionSignature()))
}
let items = self.parseOptionalCodeBlock()
return RawDeinitializerDeclSyntax(
attributes: attrs.attributes,
modifiers: attrs.modifiers,
unexpectedBeforeDeinitKeyword,
deinitKeyword: deinitKeyword,
RawUnexpectedNodesSyntax(unexpectedNameAndSignature, arena: self.arena),
body: items,
arena: self.arena
)
Expand Down
6 changes: 6 additions & 0 deletions Sources/SwiftParser/Diagnostics/DiagnosticExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ extension FixIt.Changes {
return FixIt.Changes(changes: changes)
}

static func makeMissing<SyntaxType: SyntaxProtocol>(node: SyntaxType) -> Self {
return FixIt.Changes(changes: [
.replace(oldNode: Syntax(node), newNode: MissingMaker().visit(Syntax(node)))
])
}

/// Remove the nodes in `unexpected`.
static func remove(unexpected: UnexpectedNodesSyntax) -> Self {
var changes: [FixIt.Change] = [
Expand Down
23 changes: 23 additions & 0 deletions Sources/SwiftParser/Diagnostics/ParseDiagnosticsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
if shouldSkip(node) {
return .skipChildren
}
if node.allSatisfy({ handledNodes.contains($0.id) }) {
return .skipChildren
}
if let tryKeyword = node.onlyToken(where: { $0.tokenKind == .tryKeyword }),
let nextToken = tryKeyword.nextToken(viewMode: .sourceAccurate),
nextToken.tokenKind.isKeyword {
Expand Down Expand Up @@ -446,6 +449,26 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
return .visitChildren
}

public override func visit(_ node: DeinitializerDeclSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
}
if let unexpected = node.unexpectedBetweenDeinitKeywordAndBody,
let name = unexpected.filter({ $0.as(TokenSyntax.self)?.tokenKind.isIdentifier == true }).only?.as(TokenSyntax.self) {
addDiagnostic(name, .deinitCannotHaveName, fixIts: [
FixIt(message: RemoveNodesFixIt(name), changes: .makeMissing(token: name))
], handledNodes: [name.id])
}
if let unexpected = node.unexpectedBetweenDeinitKeywordAndBody,
let signature = unexpected.compactMap({ $0.as(FunctionSignatureSyntax.self) }).only {
addDiagnostic(signature, .deinitCannotHaveParameters, fixIts: [
FixIt(message: RemoveNodesFixIt(signature), changes: .makeMissing(node: signature))
], handledNodes: [signature.id])
}

return .visitChildren
}

public override func visit(_ node: ForInStmtSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public enum StaticParserError: String, DiagnosticMessage {
case cStyleForLoop = "C-style for statement has been removed in Swift 3"
case defaultCannotBeUsedWithWhere = "'default' cannot be used with a 'where' guard expression"
case defaultOutsideOfSwitch = "'default' label can only appear inside a 'switch' statement"
case deinitCannotHaveName = "deinitializers cannot have a name"
case deinitCannotHaveParameters = "deinitializers cannot have parameters"
case editorPlaceholderInSourceFile = "editor placeholder in source file"
case expectedExpressionAfterTry = "expected expression after 'try'"
case invalidFlagAfterPrecedenceGroupAssignment = "expected 'true' or 'false' after 'assignment'"
Expand Down
9 changes: 9 additions & 0 deletions Sources/SwiftParser/Diagnostics/PresenceUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,12 @@ class PresentMaker: SyntaxRewriter {
))
}
}

class MissingMaker: SyntaxRewriter {
override func visit(_ node: TokenSyntax) -> Syntax {
guard node.presence == .present else {
return Syntax(node)
}
return Syntax(TokenSyntax(node.tokenKind, presence: .missing))
}
}
15 changes: 4 additions & 11 deletions Tests/SwiftParserTest/translated/AsyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ final class AsyncTests: XCTestCase {
"""
class X {
init() async { }
deinit1️⃣ async 2️⃣{ }
deinit 1️⃣async { }
func f() async { }
subscript(x: Int) 4️⃣async -> Int {
subscript(x: Int) 2️⃣async -> Int {
get {
return 0
}
Expand All @@ -124,15 +124,8 @@ final class AsyncTests: XCTestCase {
}
""",
diagnostics: [
// TODO: Old parser expected error on line 3: deinitializers cannot have a name
DiagnosticSpec(locationMarker: "1️⃣", message: "consecutive declarations on a line must be separated by ';'"),
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code '{ }' in function"),
// TODO: Old parser expected error on line 5: expected '->' for subscript element type
// TODO: Old parser expected error on line 5: single argument function types require parentheses
// TODO: Old parser expected error on line 5: cannot find type 'async' in scope
// TODO: Old parser expected note on line 5: cannot use module 'async' as a type
DiagnosticSpec(locationMarker: "4️⃣", message: "unexpected code 'async' in subscript"),
// TODO: Old parser expected error on line 9: 'set' accessor cannot have specifier 'async'
DiagnosticSpec(locationMarker: "1️⃣", message: "deinitializers cannot have a name"),
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code 'async' in subscript"),
]
)
}
Expand Down
Loading