Skip to content

Fix wrong diagnostic for generics #1498

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
Apr 13, 2023
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
4 changes: 2 additions & 2 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ extension Parser {
}

precondition(self.currentToken.starts(with: "<"))
let langle = self.consumeAnyToken(remapping: .leftAngle)
let langle = self.consumePrefix("<", as: .leftAngle)
var elements = [RawGenericParameterSyntax]()
do {
var keepGoing: RawTokenSyntax? = nil
Expand All @@ -477,7 +477,7 @@ extension Parser {
var each = self.consume(if: .keyword(.each))

let (unexpectedBetweenEachAndName, name) = self.expectIdentifier(allowSelfOrCapitalSelfAsIdentifier: true)
if attributes == nil && each == nil && unexpectedBetweenEachAndName == nil && name.isMissing && elements.isEmpty {
if attributes == nil && each == nil && unexpectedBetweenEachAndName == nil && name.isMissing && elements.isEmpty && !self.currentToken.starts(with: ">") {
break
}

Expand Down
32 changes: 31 additions & 1 deletion Tests/SwiftParserTest/translated/RecoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ final class RecoveryTests: XCTestCase {
assertParse(
"""
// Note: Don't move braces to a different line here.
struct ErrorGenericParameterList4< 1️⃣
struct ErrorGenericParameterList4<1️⃣
{
}
""",
Expand Down Expand Up @@ -2280,4 +2280,34 @@ final class RecoveryTests: XCTestCase {
)
}

// https://github.com/apple/swift-syntax/issues/1483
func testRecovery183() {
// Can be parsed and produces no diagnostics.
assertParse(
"func f< 1️⃣>() {}",
diagnostics: [
DiagnosticSpec(
message: "expected generic parameter in generic parameter clause",
fixIts: ["insert generic parameter"]
)
],
fixedSource: """
func f<<#identifier#> >() {}
"""
)

// Can be parsed. Printing the node or asking for the diagnostics leads to a crash.
assertParse(
"func f<1️⃣>() {}",
diagnostics: [
DiagnosticSpec(
message: "expected generic parameter in generic parameter clause",
fixIts: ["insert generic parameter"]
)
],
fixedSource: """
func f<<#identifier#>>() {}
"""
)
}
}