Skip to content

Improve recovery of unknown pound literals #943

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
Oct 14, 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
12 changes: 12 additions & 0 deletions Sources/SwiftParser/Diagnostics/ParseDiagnosticsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,18 @@ 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,6 +183,14 @@ 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: 28 additions & 0 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,8 @@ 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 @@ -1209,6 +1211,32 @@ 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: 3 additions & 0 deletions Sources/SwiftParser/RawTokenKindSubset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ 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 @@ -765,6 +766,7 @@ 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 @@ -807,6 +809,7 @@ 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: "extraneous code '#$UnknownDirective()' at top level"),
DiagnosticSpec(message: "use of unknown directive '#$UnknownDirective()'"),
]
)
}
Expand Down
96 changes: 50 additions & 46 deletions Tests/SwiftParserTest/translated/ObjectLiteralsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,50 @@
import XCTest

final class ObjectLiteralsTests: XCTestCase {
func testObjectLiterals1() {
func testObjectLiterals1a() {
AssertParse(
"""
let _ = [1️⃣#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)#]
let _ = [2️⃣#Image(imageLiteral: localResourceNameAsString)#]
let _ = [3️⃣#FileReference(fileReferenceLiteral: localResourceNameAsString)#]
let _ = [1️⃣#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)2️⃣#]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think we need to have parser support for these ancient object literals.

""",
diagnostics: [
// 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"),
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 '#'"),
]
)
}

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: [
// 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"),
DiagnosticSpec(message: "use of unknown directive '#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)'"),
]
)
}
Expand All @@ -40,9 +57,7 @@ final class ObjectLiteralsTests: XCTestCase {
let _ = 1️⃣#Image(imageLiteral: localResourceNameAsString)
""",
diagnostics: [
// 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"),
DiagnosticSpec(message: "use of unknown directive '#Image(imageLiteral: localResourceNameAsString)'"),
]
)
}
Expand All @@ -54,9 +69,7 @@ final class ObjectLiteralsTests: XCTestCase {
let _ = 1️⃣#FileReference(fileReferenceLiteral: localResourceNameAsString)
""",
diagnostics: [
// 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"),
DiagnosticSpec(message: "use of unknown directive '#FileReference(fileReferenceLiteral: localResourceNameAsString)'"),
]
)
}
Expand All @@ -68,9 +81,7 @@ final class ObjectLiteralsTests: XCTestCase {
let _ = 1️⃣#notAPound
""",
diagnostics: [
// 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"),
DiagnosticSpec(message: "use of unknown directive '#notAPound'"),
]
)
}
Expand All @@ -81,72 +92,67 @@ final class ObjectLiteralsTests: XCTestCase {
let _ = 1️⃣#notAPound(1, 2)
""",
diagnostics: [
// 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"),
DiagnosticSpec(message: "use of unknown directive '#notAPound(1, 2)'"),
]
)
}

func testObjectLiterals3c() {
AssertParse(
"""
let _ = 1️⃣#Color // {{none}}
let _ = 1️⃣#Color
""",
diagnostics: [
// 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"),
DiagnosticSpec(message: "use of unknown directive '#Color'"),
]
)
}

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

func testObjectLiterals5() {
AssertParse(
"""
let _ = [1️⃣#Color(_: 1, green: 1, 2)
let _ = [1️⃣#Color(_: 1, green: 1, 2)2️⃣
""",
diagnostics: [
// 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"),
DiagnosticSpec(locationMarker: "1️⃣", message: "use of unknown directive '#Color(_: 1, green: 1, 2)'"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ']' to end array"),
]
)
}

func testObjectLiterals6() {
AssertParse(
"""
let _ = [1️⃣#Color(red: 1, green: 1, blue: 1)#
let _ = [1️⃣#Color(red: 1, green: 1, blue: 1)2️⃣#3️⃣
""",
diagnostics: [
// 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"),
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"),
]
)
}

func testObjectLiterals7() {
AssertParse(
"""
let _ = [1️⃣#Color(withRed: 1, green: 1, whatever: 2)#]
let _ = [1️⃣#Color(withRed: 1, green: 1, whatever: 2)2️⃣#]
""",
diagnostics: [
// 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"),
DiagnosticSpec(message: "use of unknown directive '#Color(withRed: 1, green: 1, whatever: 2)'"),
DiagnosticSpec(locationMarker: "2️⃣", message: "use of unknown directive '#'")
]
)
}
Expand All @@ -157,9 +163,7 @@ final class ObjectLiteralsTests: XCTestCase {
let _ = 1️⃣#Color(_: 1, green: 1)
""",
diagnostics: [
// 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"),
DiagnosticSpec(message: "use of unknown directive '#Color(_: 1, green: 1)'"),
]
)
}
Expand Down
14 changes: 10 additions & 4 deletions Tests/SwiftParserTest/translated/RawStringErrorsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ final class RawStringErrorsTests: XCTestCase {
func testRawStringErrors3() {
AssertParse(
#####"""
let _ = ###"""invalid"###1️⃣###
let _ = ###"""invalid"###1️⃣#2️⃣#3️⃣#
"""#####,
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(message: "extraneous code '###' at top level"),
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 '#'"),
]
)
}
Expand All @@ -60,13 +63,16 @@ final class RawStringErrorsTests: XCTestCase {
func testRawStringErrors5() {
AssertParse(
#####"""
let _ = ###"invalid"###1️⃣###
let _ = ###"invalid"###1️⃣#2️⃣#3️⃣#
"""#####,
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(message: "extraneous code '###' at top level"),
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 '#'"),
]
)
}
Expand Down