Skip to content

Fix a crash in diagnostic generation if a primary associated type is not ended with a > #1110

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 2 commits into from
Dec 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public let DECL_NODES: [Node] = [
]),

Node(name: "InheritedType",
nameForDiagnostics: "type",
nameForDiagnostics: "inherited type",
kind: "Syntax",
traits: [
"WithTrailingComma"
Expand Down
10 changes: 9 additions & 1 deletion Sources/SwiftParser/Nominals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,18 @@ extension Parser {
arena: self.arena))
} while keepGoing != nil && loopProgress.evaluate(currentToken)
}
let rangle = self.consumeAnyToken(remapping: .rightAngle)
let unexpectedBeforeRangle: RawUnexpectedNodesSyntax?
let rangle: RawTokenSyntax
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the precedence of : compared to >? Could we use expect here instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think we currently have a way to expect the > as a token prefix. The problem here is that sometimes the > can be part of a bigger token.

if self.currentToken.starts(with: ">") {
unexpectedBeforeRangle = nil
rangle = self.consumePrefix(">", as: .rightAngle)
} else {
(unexpectedBeforeRangle, rangle) = self.expect(.rightAngle)
}
return RawPrimaryAssociatedTypeClauseSyntax(
leftAngleBracket: langle,
primaryAssociatedTypeList: RawPrimaryAssociatedTypeListSyntax(elements: associatedTypes, arena: self.arena),
unexpectedBeforeRangle,
rightAngleBracket: rangle,
arena: self.arena)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/gyb_generated/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ extension SyntaxKind {
case .declModifier:
return "modifier"
case .inheritedType:
return "type"
return "inherited type"
case .inheritedTypeList:
return nil
case .typeInheritanceClause:
Expand Down
2 changes: 2 additions & 0 deletions Sources/swift-parser-cli/swift-parser-cli.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class VerifyRoundTrip: ParsableCommand {
) throws {
let tree = Parser.parse(source: source)

_ = ParseDiagnosticsGenerator.diagnostics(for: tree)

let resultTree: Syntax
if foldSequences {
resultTree = foldAllSequences(tree).0
Expand Down
13 changes: 13 additions & 0 deletions Tests/SwiftParserTest/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,19 @@ final class DeclarationTests: XCTestCase {
]
)
}

func testPrimaryAssociatedTypeNotTerminatedWithAngleBracket() {
AssertParse(
"protocol1️⃣<2️⃣:3️⃣",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in protocol"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected name in primary associated type clause"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '>' to end primary associated type clause"),
DiagnosticSpec(locationMarker: "3️⃣", message: "expected type in inherited type"),
DiagnosticSpec(locationMarker: "3️⃣", message: "expected member block in protocol"),
]
)
}
}

extension Parser.DeclAttributes {
Expand Down
2 changes: 1 addition & 1 deletion gyb_syntax_support/DeclNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
Child('Detail', kind='DeclModifierDetail', is_optional=True),
]),

Node('InheritedType', name_for_diagnostics='type', kind='Syntax',
Node('InheritedType', name_for_diagnostics='inherited type', kind='Syntax',
traits=['WithTrailingComma'],
children=[
Child('TypeName', kind='Type'),
Expand Down