Skip to content

Revert "Improve recovery of unknown pound literals" #949

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
wants to merge 1 commit into from
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
12 changes: 0 additions & 12 deletions Sources/SwiftParser/Diagnostics/ParseDiagnosticsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -447,18 +447,6 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
return .visitChildren
}

public override func visit(_ node: IdentifierExprSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
}
if node.identifier.presence == .missing,
let unexpected = node.unexpectedBeforeIdentifier,
unexpected.first?.as(TokenSyntax.self)?.tokenKind == .pound {
addDiagnostic(unexpected, UnknownDirectiveError(unexpected: unexpected), handledNodes: [unexpected.id, node.identifier.id])
}
return .visitChildren
}

public override func visit(_ node: PrecedenceGroupAssignmentSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,6 @@ public struct UnexpectedNodesError: ParserError {
}
}

public struct UnknownDirectiveError: ParserError {
public let unexpected: UnexpectedNodesSyntax

public var message: String {
return "use of unknown directive \(nodesDescription([unexpected], format: false))"
}
}

// MARK: - Fix-Its (please sort alphabetically)

public enum StaticParserFixIt: String, FixItMessage {
Expand Down
28 changes: 0 additions & 28 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1112,8 +1112,6 @@ extension Parser {
wildcard: wild,
arena: self.arena
))
case (.pound, let handle)?:
return RawExprSyntax(self.parseUnknownPoundLiteral(poundHandle: handle))
case (.poundSelectorKeyword, _)?:
return RawExprSyntax(self.parseObjectiveCSelectorLiteral())
case (.poundKeyPathKeyword, _)?:
Expand Down Expand Up @@ -1211,32 +1209,6 @@ extension Parser {
}
}

extension Parser {
/// Parse an unknown pound literal, that starts with a `#` at `poundHandle`
/// into a `RawIdentifierExprSyntax` that is missing the identifier and
/// instead contains all the tokens of the pound literal as unexpected tokens.
mutating func parseUnknownPoundLiteral(poundHandle: TokenConsumptionHandle) -> RawIdentifierExprSyntax {
var unexpected: [RawSyntax] = []
unexpected.append(RawSyntax(self.eat(poundHandle)))
if let name = self.consume(if: .identifier) {
unexpected.append(RawSyntax(name))
}
// If there is a parenthesis, consume all tokens up to the closing parenthesis.
if let leftParen = self.consume(if: .leftParen) {
unexpected.append(RawSyntax(leftParen))
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
unexpected += unexpectedBeforeRightParen?.elements ?? []
unexpected.append(RawSyntax(rightParen))
}
return RawIdentifierExprSyntax(
RawUnexpectedNodesSyntax(elements: unexpected, arena: self.arena),
identifier: missingToken(.identifier, text: nil),
declNameArguments: nil,
arena: self.arena
)
}
}

extension Parser {
/// Parse a string literal expression.
///
Expand Down
3 changes: 0 additions & 3 deletions Sources/SwiftParser/RawTokenKindSubset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,6 @@ enum PrimaryExpressionStart: RawTokenKindSubset {
case leftSquareBracket
case nilKeyword
case period
case pound // For recovery of unknown directives
case poundColorLiteralKeyword
case poundColumnKeyword
case poundDsohandleKeyword
Expand Down Expand Up @@ -777,7 +776,6 @@ enum PrimaryExpressionStart: RawTokenKindSubset {
case .leftSquareBracket: self = .leftSquareBracket
case .nilKeyword: self = .nilKeyword
case .period: self = .period
case .pound: self = .pound
case .poundColorLiteralKeyword: self = .poundColorLiteralKeyword
case .poundColumnKeyword: self = .poundColumnKeyword
case .poundDsohandleKeyword: self = .poundDsohandleKeyword
Expand Down Expand Up @@ -820,7 +818,6 @@ enum PrimaryExpressionStart: RawTokenKindSubset {
case .leftSquareBracket: return .leftSquareBracket
case .nilKeyword: return .nilKeyword
case .period: return .period
case .pound: return .pound
case .poundColorLiteralKeyword: return .poundColorLiteralKeyword
case .poundColumnKeyword: return .poundColumnKeyword
case .poundDsohandleKeyword: return .poundDsohandleKeyword
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ final class DollarIdentifierTests: XCTestCase {
// TODO: Old parser expected error on line 32: cannot declare entity named '$Protocol'; the '$' prefix is reserved
// TODO: Old parser expected error on line 33: cannot declare entity named '$Precedence'; the '$' prefix is reserved
// TODO: Old parser expected error on line 37: use of unknown directive '#$UnknownDirective'
DiagnosticSpec(message: "use of unknown directive '#$UnknownDirective()'"),
DiagnosticSpec(message: "extraneous code '#$UnknownDirective()' at top level"),
]
)
}
Expand Down
96 changes: 46 additions & 50 deletions Tests/SwiftParserTest/translated/ObjectLiteralsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,33 @@
import XCTest

final class ObjectLiteralsTests: XCTestCase {
func testObjectLiterals1a() {
func testObjectLiterals1() {
AssertParse(
"""
let _ = [1️⃣#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)2️⃣#]
let _ = [1️⃣#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)#]
let _ = [2️⃣#Image(imageLiteral: localResourceNameAsString)#]
let _ = [3️⃣#FileReference(fileReferenceLiteral: localResourceNameAsString)#]
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "use of unknown directive '#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)'"),
DiagnosticSpec(locationMarker: "2️⃣", message: "use of unknown directive '#'"),
// TODO: Old parser expected error on line 1: '[#Color(...)#]' has been renamed to '#colorLiteral(...), Fix-It replacements: 9 - 10 = '', 11 - 16 = 'colorLiteral', 17 - 32 = 'red', 78 - 80 = ''
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)#' in array"),
// TODO: Old parser expected error on line 2: '[#Image(...)#]' has been renamed to '#imageLiteral(...)', Fix-It replacements: 9 - 10 = '', 11 - 16 = 'imageLiteral', 17 - 29 = 'resourceName', 57 - 59 = ''
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code '#Image(imageLiteral: localResourceNameAsString)#' in array"),
// TODO: Old parser expected error on line 3: '[#FileReference(...)#]' has been renamed to '#fileLiteral(...)', Fix-It replacements: 9 - 10 = '', 11 - 24 = 'fileLiteral', 25 - 45 = 'resourceName', 73 - 75 = ''
DiagnosticSpec(locationMarker: "3️⃣", message: "unexpected code '#FileReference(fileReferenceLiteral: localResourceNameAsString)#' in array"),
]
)
}

func testObjectLiterals1b() {
AssertParse(
"""
let _ = [1️⃣#Image(imageLiteral: localResourceNameAsString)2️⃣#]
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "use of unknown directive '#Image(imageLiteral: localResourceNameAsString)'"),
DiagnosticSpec(locationMarker: "2️⃣", message: "use of unknown directive '#'"),
]
)
}

func testObjectLiterals1c() {
AssertParse(
"""
let _ = [1️⃣#FileReference(fileReferenceLiteral: localResourceNameAsString)2️⃣#]
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "use of unknown directive '#FileReference(fileReferenceLiteral: localResourceNameAsString)'"),
DiagnosticSpec(locationMarker: "2️⃣", message: "use of unknown directive '#'"),
]
)
}


func testObjectLiterals2a() {
AssertParse(
"""
let _ = 1️⃣#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)
""",
diagnostics: [
DiagnosticSpec(message: "use of unknown directive '#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)'"),
// TODO: Old parser expected error on line 1: '#Color(...)' has been renamed to '#colorLiteral(...), Fix-It replacements: 10 - 15 = 'colorLiteral', 16 - 31 = 'red'
DiagnosticSpec(message: "expected expression in variable"),
DiagnosticSpec(message: "extraneous code '#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)' at top level"),
]
)
}
Expand All @@ -57,7 +40,9 @@ final class ObjectLiteralsTests: XCTestCase {
let _ = 1️⃣#Image(imageLiteral: localResourceNameAsString)
""",
diagnostics: [
DiagnosticSpec(message: "use of unknown directive '#Image(imageLiteral: localResourceNameAsString)'"),
// TODO: Old parser expected error on line 1: '#Image(...)' has been renamed to '#imageLiteral(...)', Fix-It replacements: 10 - 15 = 'imageLiteral', 16 - 28 = 'resourceName'
DiagnosticSpec(message: "expected expression in variable"),
DiagnosticSpec(message: "extraneous code '#Image(imageLiteral: localResourceNameAsString)' at top level"),
]
)
}
Expand All @@ -69,7 +54,9 @@ final class ObjectLiteralsTests: XCTestCase {
let _ = 1️⃣#FileReference(fileReferenceLiteral: localResourceNameAsString)
""",
diagnostics: [
DiagnosticSpec(message: "use of unknown directive '#FileReference(fileReferenceLiteral: localResourceNameAsString)'"),
// TODO: Old parser expected error on line 1: '#FileReference(...)' has been renamed to '#fileLiteral(...)', Fix-It replacements: 10 - 23 = 'fileLiteral', 24 - 44 = 'resourceName'
DiagnosticSpec(message: "expected expression in variable"),
DiagnosticSpec(message: "extraneous code '#FileReference(fileReferenceLiteral: localResourceNameAsString)' at top level"),
]
)
}
Expand All @@ -81,7 +68,9 @@ final class ObjectLiteralsTests: XCTestCase {
let _ = 1️⃣#notAPound
""",
diagnostics: [
DiagnosticSpec(message: "use of unknown directive '#notAPound'"),
// TODO: Old parser expected error on line 1: use of unknown directive '#notAPound'
DiagnosticSpec(message: "expected expression in variable"),
DiagnosticSpec(message: "extraneous code '#notAPound' at top level"),
]
)
}
Expand All @@ -92,67 +81,72 @@ final class ObjectLiteralsTests: XCTestCase {
let _ = 1️⃣#notAPound(1, 2)
""",
diagnostics: [
DiagnosticSpec(message: "use of unknown directive '#notAPound(1, 2)'"),
// TODO: Old parser expected error on line 1: use of unknown directive '#notAPound'
DiagnosticSpec(message: "expected expression in variable"),
DiagnosticSpec(message: "extraneous code '#notAPound(1, 2)' at top level"),
]
)
}

func testObjectLiterals3c() {
AssertParse(
"""
let _ = 1️⃣#Color
let _ = 1️⃣#Color // {{none}}
""",
diagnostics: [
DiagnosticSpec(message: "use of unknown directive '#Color'"),
// TODO: Old parser expected error on line 1: expected argument list in object literal
DiagnosticSpec(message: "expected expression in variable"),
DiagnosticSpec(message: "extraneous code '#Color' at top level"),
]
)
}

func testObjectLiterals4() {
AssertParse(
"""
let _ = [1️⃣#2️⃣#]
let _ = [1️⃣##] // {{none}}
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "use of unknown directive '#'"),
DiagnosticSpec(locationMarker: "2️⃣", message: "use of unknown directive '#'"),
// TODO: Old parser expected error on line 1: expected expression in container literal
DiagnosticSpec(message: "unexpected code '##' in array"),
]
)
}

func testObjectLiterals5() {
AssertParse(
"""
let _ = [1️⃣#Color(_: 1, green: 1, 2)2️⃣
let _ = [1️⃣#Color(_: 1, green: 1, 2)
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "use of unknown directive '#Color(_: 1, green: 1, 2)'"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ']' to end array"),
// TODO: Old parser expected error on line 1: '[#Color(...)#]' has been renamed to '#colorLiteral(...)', Fix-It replacements: 9 - 10 = '', 11 - 16 = 'colorLiteral', 17 - 18 = 'red'
DiagnosticSpec(message: "expected ']' to end array"),
DiagnosticSpec(message: "extraneous code '#Color(_: 1, green: 1, 2)' at top level"),
]
)
}

func testObjectLiterals6() {
AssertParse(
"""
let _ = [1️⃣#Color(red: 1, green: 1, blue: 1)2️⃣#3️⃣
let _ = [1️⃣#Color(red: 1, green: 1, blue: 1)#
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "use of unknown directive '#Color(red: 1, green: 1, blue: 1)'"),
DiagnosticSpec(locationMarker: "2️⃣", message: "use of unknown directive '#'"),
DiagnosticSpec(locationMarker: "3️⃣", message: "expected ']' to end array"),
// TODO: Old parser expected error on line 1: '[#Color(...)#]' has been renamed to '#colorLiteral(...)', Fix-It replacements: 9 - 10 = '', 11 - 16 = 'colorLiteral', 17 - 20 = 'red', 43 - 44 = ''
DiagnosticSpec(message: "expected ']' to end array"),
DiagnosticSpec(message: "extraneous code '#Color(red: 1, green: 1, blue: 1)#' at top level"),
]
)
}

func testObjectLiterals7() {
AssertParse(
"""
let _ = [1️⃣#Color(withRed: 1, green: 1, whatever: 2)2️⃣#]
let _ = [1️⃣#Color(withRed: 1, green: 1, whatever: 2)#]
""",
diagnostics: [
DiagnosticSpec(message: "use of unknown directive '#Color(withRed: 1, green: 1, whatever: 2)'"),
DiagnosticSpec(locationMarker: "2️⃣", message: "use of unknown directive '#'")
// TODO: Old parser expected error on line 1: '[#Color(...)#]' has been renamed to '#colorLiteral(...)', Fix-It replacements: 9 - 10 = '', 11 - 16 = 'colorLiteral', 17 - 24 = 'red', 51 - 53 = ''
DiagnosticSpec(message: "unexpected code '#Color(withRed: 1, green: 1, whatever: 2)#' in array"),
]
)
}
Expand All @@ -163,7 +157,9 @@ final class ObjectLiteralsTests: XCTestCase {
let _ = 1️⃣#Color(_: 1, green: 1)
""",
diagnostics: [
DiagnosticSpec(message: "use of unknown directive '#Color(_: 1, green: 1)'"),
// TODO: Old parser expected error on line 1: '#Color(...)' has been renamed to '#colorLiteral(...)', Fix-It replacements: 10 - 15 = 'colorLiteral', 16 - 17 = 'red'
DiagnosticSpec(message: "expected expression in variable"),
DiagnosticSpec(message: "extraneous code '#Color(_: 1, green: 1)' at top level"),
]
)
}
Expand Down
14 changes: 4 additions & 10 deletions Tests/SwiftParserTest/translated/RawStringErrorsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,13 @@ final class RawStringErrorsTests: XCTestCase {
func testRawStringErrors3() {
AssertParse(
#####"""
let _ = ###"""invalid"###1️⃣#2️⃣#3️⃣#
let _ = ###"""invalid"###1️⃣###
"""#####,
diagnostics: [
// TODO: Old parser expected error on line 1: too many '#' characters in closing delimiter, Fix-It replacements: 26 - 29 = ''
// TODO: Old parser expected error on line 1: consecutive statements on a line must be separated by ';'
// TODO: Old parser expected error on line 1: expected expression
DiagnosticSpec(locationMarker: "1️⃣", message: "consecutive statements on a line must be separated by ';'"),
DiagnosticSpec(locationMarker: "1️⃣", message: "use of unknown directive '#'"),
DiagnosticSpec(locationMarker: "2️⃣", message: "use of unknown directive '#'"),
DiagnosticSpec(locationMarker: "3️⃣", message: "use of unknown directive '#'"),
DiagnosticSpec(message: "extraneous code '###' at top level"),
]
)
}
Expand All @@ -63,16 +60,13 @@ final class RawStringErrorsTests: XCTestCase {
func testRawStringErrors5() {
AssertParse(
#####"""
let _ = ###"invalid"###1️⃣#2️⃣#3️⃣#
let _ = ###"invalid"###1️⃣###
"""#####,
diagnostics: [
// TODO: Old parser expected error on line 1: too many '#' characters in closing delimiter, Fix-It replacements: 24 - 27 = ''
// TODO: Old parser expected error on line 1: consecutive statements on a line must be separated by ';'
// TODO: Old parser expected error on line 1: expected expression
DiagnosticSpec(locationMarker: "1️⃣", message: "consecutive statements on a line must be separated by ';'"),
DiagnosticSpec(locationMarker: "1️⃣", message: "use of unknown directive '#'"),
DiagnosticSpec(locationMarker: "2️⃣", message: "use of unknown directive '#'"),
DiagnosticSpec(locationMarker: "3️⃣", message: "use of unknown directive '#'"),
DiagnosticSpec(message: "extraneous code '###' at top level"),
]
)
}
Expand Down