From fd3a8c59e357b297e4305ef7c9c21b55500a93b7 Mon Sep 17 00:00:00 2001 From: Kim de Vos Date: Thu, 6 Apr 2023 23:49:12 +0200 Subject: [PATCH] Fix wrong diagnostic for generics --- Sources/SwiftParser/Declarations.swift | 4 +-- .../translated/RecoveryTests.swift | 32 ++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Sources/SwiftParser/Declarations.swift b/Sources/SwiftParser/Declarations.swift index 181595f7e2d..298bbe8c75a 100644 --- a/Sources/SwiftParser/Declarations.swift +++ b/Sources/SwiftParser/Declarations.swift @@ -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 @@ -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 } diff --git a/Tests/SwiftParserTest/translated/RecoveryTests.swift b/Tests/SwiftParserTest/translated/RecoveryTests.swift index 92be5a53967..728fe218155 100644 --- a/Tests/SwiftParserTest/translated/RecoveryTests.swift +++ b/Tests/SwiftParserTest/translated/RecoveryTests.swift @@ -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️⃣ { } """, @@ -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#>>() {} + """ + ) + } }