Skip to content

Add RecoveryTest 28 #2303

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

Closed
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
9 changes: 9 additions & 0 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,14 @@ extension Parser {
let unexpectedPeriod = self.consume(if: .period)
let (unexpectedBeforeName, name) = self.expectIdentifier(keywordRecovery: true)

let unexpectedGenericParameters: RawUnexpectedNodesSyntax?
if self.at(prefix: "<") {
let genericParameters = self.parseGenericParameters()
unexpectedGenericParameters = RawUnexpectedNodesSyntax([genericParameters], arena: self.arena)
} else {
unexpectedGenericParameters = nil
}

let parameterClause: RawEnumCaseParameterClauseSyntax?
if self.at(TokenSpec(.leftParen)) {
parameterClause = self.parseParameterClause(RawEnumCaseParameterClauseSyntax.self) { parser in
Expand Down Expand Up @@ -836,6 +844,7 @@ extension Parser {
RawEnumCaseElementSyntax(
RawUnexpectedNodesSyntax(combining: unexpectedPeriod, unexpectedBeforeName, arena: self.arena),
name: name,
unexpectedGenericParameters,
parameterClause: parameterClause,
rawValue: rawValue,
trailingComma: keepGoing,
Expand Down
18 changes: 18 additions & 0 deletions Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,24 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
return .visitChildren
}

public override func visit(_ node: EnumCaseElementSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
}

if let unexpectedBetweenNameAndParameterClause = node.unexpectedBetweenNameAndParameterClause,
let genericParameter = unexpectedBetweenNameAndParameterClause.compactMap({ $0.as(GenericParameterClauseSyntax.self) }).only
{
addDiagnostic(
genericParameter,
.genericParamCantBeUsedInEnumCaseDecl,
handledNodes: [unexpectedBetweenNameAndParameterClause.id]
)
}

return .visitChildren
}

public override func visit(_ node: IfConfigClauseSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ extension DiagnosticMessage where Self == StaticParserError {
public static var forbiddenInterpolatedString: Self {
return .init("argument cannot be an interpolated string literal")
}
public static var genericParamCantBeUsedInEnumCaseDecl: Self {
return .init("generic signature cannot be declared in enum 'case'")
}
public static var initializerInPattern: Self {
.init("unexpected initializer in pattern; did you mean to use '='?")
}
Expand Down
113 changes: 113 additions & 0 deletions Tests/SwiftParserTest/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2946,4 +2946,117 @@ final class DeclarationTests: ParserTestCase {
]
)
}

// https://github.com/apple/swift-syntax/issues/2273
func testEnumCaseWithGenericParameter() {
assertParse(
"""
enum Foo {
case foo1️⃣<T>(T)
}
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "generic signature cannot be declared in enum 'case'"
)
]
)

assertParse(
"""
enum Foo {
case bar1️⃣<T>(param: T)
}
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "generic signature cannot be declared in enum 'case'"
)
]
)

assertParse(
"""
enum Foo {
case baz1️⃣<T>
}
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "generic signature cannot be declared in enum 'case'"
)
]
)

assertParse(
"""
enum Foo {
case one, two1️⃣<T>
}
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "generic signature cannot be declared in enum 'case'"
)
]
)

assertParse(
"""
enum Foo {
case three1️⃣<T>, four
}
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "generic signature cannot be declared in enum 'case'"
)
]
)

assertParse(
"""
enum Foo {
case five1️⃣<T>(param: T), six
}
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "generic signature cannot be declared in enum 'case'"
)
]
)

assertParse(
"""
enum Foo {
case seven1️⃣<T>, eight2️⃣<U>
}
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "generic signature cannot be declared in enum 'case'"
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "generic signature cannot be declared in enum 'case'"
),
]
)

assertParse(
"""
enum Foo<T> {
case five(param: T), six
}
"""
)
}
}
13 changes: 9 additions & 4 deletions Tests/SwiftParserTest/translated/RecoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,15 @@ final class RecoveryTests: ParserTestCase {
} while { true }()
""",
diagnostics: [
// TODO: Old parser expected error on line 2: missing condition in 'while' statement
// TODO: Old parser expected error on line 2: consecutive statements on a line must be separated by ';', Fix-It replacements: 10 - 10 = ';'
// TODO: Old parser expected warning on line 2: result of call to closure returning 'Bool' is unused
]
DiagnosticSpec(message: "missing condition in 'while' statement"),
DiagnosticSpec(message: "consecutive statements on a line must be separated by newline or ';'",
fixIts: ["insert newline", "insert ';'"]),
DiagnosticSpec(message: "result of call to closure returning 'Bool' is unused", severity: .warning)
],
fixedSource: """
repeat {
} while <#expression#>
"""
)
}

Expand Down