Skip to content

Resolve TODOs in EnumTests #967

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
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
5 changes: 3 additions & 2 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,8 @@ extension Parser {
var keepGoing: RawTokenSyntax? = nil
var loopProgress = LoopProgressCondition()
repeat {
let (unexpectedBeforeName, name) = self.expectIdentifier(keywordRecovery: true)
let unexpectedPeriod = self.consume(ifAny: [.period, .prefixPeriod])
let (unexpectedBeforeName, name) = self.expectIdentifier(allowIdentifierLikeKeywords: false, keywordRecovery: true)

let associatedValue: RawParameterClauseSyntax?
if self.at(.leftParen, where: { !$0.isAtStartOfLine }) {
Expand All @@ -895,7 +896,7 @@ extension Parser {
// Continue through the comma-separated list.
keepGoing = self.consume(if: .comma)
elements.append(RawEnumCaseElementSyntax(
unexpectedBeforeName,
RawUnexpectedNodesSyntax([unexpectedPeriod.map(RawSyntax.init)] + (unexpectedBeforeName?.elements ?? []), arena: self.arena),
identifier: name,
associatedValue: associatedValue,
rawValue: rawValue,
Expand Down
12 changes: 9 additions & 3 deletions Sources/SwiftParser/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,15 @@ extension Parser {
/// This should be set if keywords aren't strong recovery marker at this
/// position, e.g. because the parser expects a punctuator next.
@_spi(RawSyntax)
public mutating func expectIdentifier(keywordRecovery: Bool = false) -> (RawUnexpectedNodesSyntax?, Token) {
if let (_, handle) = self.canRecoverTo(anyIn: IdentifierTokens.self) {
return self.eat(handle)
public mutating func expectIdentifier(allowIdentifierLikeKeywords: Bool = true, keywordRecovery: Bool = false) -> (RawUnexpectedNodesSyntax?, Token) {
if allowIdentifierLikeKeywords {
if let (_, handle) = self.canRecoverTo(anyIn: IdentifierTokens.self) {
return self.eat(handle)
}
} else {
if let identifier = self.consume(if: .identifier) {
return (nil, identifier)
}
}
if let unknown = self.consume(if: .unknown) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ final class EnumElementPatternSwift4Tests: XCTestCase {
}
}
}
""",
diagnostics: [
// TODO: Old parser expected error on line 5: cannot specialize a non-generic definition
// TODO: Old parser expected note on line 5: while parsing this '<' as a type parameter bracket
// TODO: Old parser expected error on line 7: cannot specialize a non-generic definition
// TODO: Old parser expected note on line 7: while parsing this '<' as a type parameter bracket
]
"""
)
}

Expand All @@ -61,13 +55,7 @@ final class EnumElementPatternSwift4Tests: XCTestCase {
case .D(let payload) = e
else { return }
}
""",
diagnostics: [
// TODO: Old parser expected error on line 3: cannot specialize a non-generic definition
// TODO: Old parser expected note on line 3: while parsing this '<' as a type parameter bracket
// TODO: Old parser expected error on line 5: cannot specialize a non-generic definition
// TODO: Old parser expected note on line 5: while parsing this '<' as a type parameter bracket
]
"""
)
}

Expand Down
88 changes: 29 additions & 59 deletions Tests/SwiftParserTest/translated/EnumTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,16 @@ final class EnumTests: XCTestCase {
}
""",
diagnostics: [
// TODO: Old parser expected error on line 2: 'case' label can only appear inside a 'switch' statement
DiagnosticSpec(locationMarker: "1️⃣", message: "expected name in enum case"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ':' and type in function parameter"),
DiagnosticSpec(locationMarker: "3️⃣", message: "expected type in function parameter"),
DiagnosticSpec(locationMarker: "3️⃣", message: "unexpected code '0' in parameter clause"),
DiagnosticSpec(locationMarker: "4️⃣", message: "unexpected code ':' in enum"),
]
], fixedSource: """
enum SwitchEnvy {
case <#identifier#>(_, var x: <#type#>, <#type#>0):
}
"""
)
}

Expand Down Expand Up @@ -330,10 +333,7 @@ final class EnumTests: XCTestCase {
case Mopsy
var ivar : Int
}
""",
diagnostics: [
// TODO: Old parser expected error on line 4: enums must not contain stored properties
]
"""
)
}

Expand All @@ -348,7 +348,6 @@ final class EnumTests: XCTestCase {
}
""",
diagnostics: [
// TODO: Old parser expected error on line 3: expected identifier after comma in enum 'case' declaration
DiagnosticSpec(message: "expected name in enum case"),
]
)
Expand All @@ -362,8 +361,6 @@ final class EnumTests: XCTestCase {
}
""",
diagnostics: [
// TODO: Old parser expected error on line 2: 'case' label can only appear inside a 'switch' statement
// TODO: Old parser expected error on line 2: expected pattern
DiagnosticSpec(message: "expected name in enum case"),
DiagnosticSpec(message: "unexpected code ':' in enum"),
]
Expand All @@ -378,7 +375,6 @@ final class EnumTests: XCTestCase {
}
""",
diagnostics: [
// TODO: Old parser expected error on line 2: 'case' label can only appear inside a 'switch' statement
DiagnosticSpec(message: "unexpected code ':' in enum"),
]
)
Expand All @@ -392,7 +388,6 @@ final class EnumTests: XCTestCase {
}
""",
diagnostics: [
// TODO: Old parser expected error on line 2: 'case' label can only appear inside a 'switch' statement
DiagnosticSpec(message: "unexpected code ':' in enum"),
]
)
Expand All @@ -402,16 +397,12 @@ final class EnumTests: XCTestCase {
AssertParse(
"""
enum Recovery4 {
case Self 1️⃣Self
case 1️⃣Self 2️⃣Self
}
""",
diagnostics: [
// TODO: Old parser expected note on line 1: in declaration of 'Recovery4'
// TODO: Old parser expected error on line 2: keyword 'Self' cannot be used as an identifier here
// TODO: Old parser expected note on line 2: if this name is unavoidable, use backticks to escape it, Fix-It replacements: 8 - 12 = '`Self`'
// TODO: Old parser expected error on line 2: consecutive declarations on a line must be separated by ';', Fix-It replacements: 12 - 12 = ';'
// TODO: Old parser expected error on line 2: expected declaration
DiagnosticSpec(message: "unexpected code 'Self' in enum"),
DiagnosticSpec(locationMarker: "1️⃣", message: "keyword 'Self' cannot be used as an identifier here"),
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code 'Self' in enum"),
]
)
}
Expand All @@ -421,17 +412,13 @@ final class EnumTests: XCTestCase {
"""
enum Recovery5 {
case 1️⃣.UE3
case 2️⃣.UE4, .UE5
case 2️⃣.UE4, 3️⃣.UE5
}
""",
diagnostics: [
// TODO: Old parser expected error on line 2: extraneous code '.' in enum 'case' declaration, Fix-It replacements: 8 - 9 = ''
DiagnosticSpec(locationMarker: "1️⃣", message: "expected name in enum case"),
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '.UE3' before enum case"),
// TODO: Old parser expected error on line 3: extraneous code '.' in enum 'case' declaration, Fix-It replacements: 8 - 9 = ''
// TODO: Old parser expected error on line 3: extraneous code '.' in enum 'case' declaration, Fix-It replacements: 14 - 15 = ''
DiagnosticSpec(locationMarker: "2️⃣", message: "expected name in enum case"),
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code '.UE4, .UE5' in enum"),
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '.' in enum case"),
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code '.' in enum case"),
DiagnosticSpec(locationMarker: "3️⃣", message: "unexpected code '.' in enum case"),
]
)
}
Expand All @@ -448,7 +435,6 @@ final class EnumTests: XCTestCase {
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "'_' cannot be used as an identifier here"),
DiagnosticSpec(locationMarker: "2️⃣", message: "'_' cannot be used as an identifier here"),
// TODO: Old parser expected error on line 20: expected identifier after comma in enum 'case' declaration
DiagnosticSpec(locationMarker: "3️⃣", message: "expected name in enum case"),
]
)
Expand Down Expand Up @@ -478,10 +464,7 @@ final class EnumTests: XCTestCase {
enum MultiRawType : Int64, Int32 {
case Couch, Davis
}
""",
diagnostics: [
// TODO: Old parser expected error on line 1: multiple enum raw types 'Int64' and 'Int32'
]
"""
)
}

Expand Down Expand Up @@ -834,10 +817,7 @@ final class EnumTests: XCTestCase {
enum NonliteralRawValue : Int {
case Yeon = 100 + 20 + 3
}
""",
diagnostics: [
// TODO: Old parser expected error on line 2: raw value for enum case must be a literal
]
"""
)
}

Expand Down Expand Up @@ -914,10 +894,7 @@ final class EnumTests: XCTestCase {
case Foo = 1
case Foo = 1 + 1
}
""",
diagnostics: [
// TODO: Old parser expected error on line 3: raw value for enum case must be a literal
]
"""
)
}

Expand Down Expand Up @@ -1194,10 +1171,7 @@ final class EnumTests: XCTestCase {
self = SE0036.B(SE0036_Auxiliary())
}
}
""",
diagnostics: [
// TODO: Old parser expected error on line 25: '_' can only appear in a pattern or on the left side of an assignment
]
"""
)
}

Expand All @@ -1218,10 +1192,17 @@ final class EnumTests: XCTestCase {
}
}
}
""",
diagnostics: [
// TODO: Old parser expected error on line 5: '_' can only appear in a pattern or on the left side of an assignment
]
"""
)
}

func testEnum81b() {
AssertParse(
"""
switch self {
case A(_): break
}
"""
)
}

Expand All @@ -1242,12 +1223,7 @@ final class EnumTests: XCTestCase {
enum SE0155 {
case emptyArgs()
}
""",
diagnostics: [
// TODO: Old parser expected warning on line 2: enum element with associated values must have at least one associated value
// TODO: Old parser expected note on line 2: did you mean to remove the empty associated value list?, Fix-It replacements: 17 - 19 = ''
// TODO: Old parser expected note on line 2: did you mean to explicitly add a 'Void' associated value?, Fix-It replacements: 18 - 18 = 'Void'
]
"""
)
}

Expand Down Expand Up @@ -1312,8 +1288,6 @@ final class EnumTests: XCTestCase {
}
""",
diagnostics: [
// TODO: Old parser expected error on line 2: keyword 'func' cannot be used as an identifier here
// TODO: Old parser expected note on line 2: if this name is unavoidable, use backticks to escape it, Fix-It replacements: 17 - 21 = '`func`'
DiagnosticSpec(message: "keyword 'func' cannot be used as an identifier here"),
]
)
Expand All @@ -1331,8 +1305,6 @@ final class EnumTests: XCTestCase {
""",
diagnostics: [
DiagnosticSpec(message: "expected name in enum case"),
// TODO: Old parser expected error on line 5: keyword 'case' cannot be used as an identifier here
// TODO: Old parser expected note on line 5: if this name is unavoidable, use backticks to escape it, Fix-It replacements: 3 - 7 = '`case`'
]
)
}
Expand Down Expand Up @@ -1374,9 +1346,7 @@ final class EnumTests: XCTestCase {
}
"""#,
diagnostics: [
// TODO: Old parser expected error on line 2: expected identifier after comma in enum 'case' declaration
DiagnosticSpec(locationMarker: "1️⃣", message: "expected name in enum case"),
// TODO: Old parser expected error on line 3: expected identifier after comma in enum 'case' declaration
DiagnosticSpec(locationMarker: "2️⃣", message: "expected name in enum case"),
]
)
Expand Down