Skip to content

Allow keywords after # in freestanding macro expansions #1778

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
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
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2107,7 +2107,7 @@ extension Parser {
var unexpectedBeforeMacro: RawUnexpectedNodesSyntax?
var macro: RawTokenSyntax
if !self.currentToken.isAtStartOfLine {
(unexpectedBeforeMacro, macro) = self.expectIdentifier(keywordRecovery: true)
(unexpectedBeforeMacro, macro) = self.expectIdentifier(allowKeywordsAsIdentifier: true)
if macro.leadingTriviaByteLength != 0 {
unexpectedBeforeMacro = RawUnexpectedNodesSyntax(combining: unexpectedBeforeMacro, macro, arena: self.arena)
pound = self.missingToken(.identifier, text: macro.tokenText)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ extension Parser {
var unexpectedBeforeMacro: RawUnexpectedNodesSyntax?
var macro: RawTokenSyntax
if !self.currentToken.isAtStartOfLine {
(unexpectedBeforeMacro, macro) = self.expectIdentifier(keywordRecovery: true)
(unexpectedBeforeMacro, macro) = self.expectIdentifier(allowKeywordsAsIdentifier: true)
if macro.leadingTriviaByteLength != 0 {
// If there're whitespaces after '#' diagnose.
unexpectedBeforeMacro = RawUnexpectedNodesSyntax(combining: unexpectedBeforeMacro, macro, arena: self.arena)
Expand Down
32 changes: 20 additions & 12 deletions Sources/SwiftParser/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -431,24 +431,32 @@ extension Parser {
)
}

/// If `keywordRecovery` is set to `true` and the parser is currently
/// positioned at a keyword instead of an identifier, this method recovers by
/// eating the keyword in place of an identifier, recovering if the developer
/// incorrectly used a keyword as an identifier.
/// This should be set if keywords aren't strong recovery marker at this
/// position, e.g. because the parser expects a punctuator next.
///
/// If `allowSelfOrCapitalSelfAsIdentifier` is `true`, then `self` and `Self`
/// are also accepted and remapped to identifiers. This is exclusively used
/// to maintain compatibility with the C++ parser. No new uses of this should
/// be introduced.
/// - Parameters:
/// - keywordRecovery: If set to `true` and the parser is currently
/// positioned at a keyword instead of an identifier, this method recovers
/// by eating the keyword in place of an identifier, recovering if the
/// developer incorrectly used a keyword as an identifier. This should be
/// set if keywords aren't strong recovery marker at this position, e.g.
/// because the parser expects a punctuator next
/// - allowSelfOrCapitalSelfAsIdentifier: If set to `true`, then `self` and
/// `Self` are also accepted and remapped to identifiers. This is
/// exclusively used to maintain compatibility with the C++ parser. No new
/// uses of this should be introduced.
/// - allowKeywordsAsIdentifier: If set to `true` and the parser is
/// currently positioned at a keyword, consume that keyword and remap it
/// to and identifier.
/// - Returns: The consumed token and any unexpected tokens that were skipped.
mutating func expectIdentifier(
keywordRecovery: Bool = false,
allowSelfOrCapitalSelfAsIdentifier: Bool = false
allowSelfOrCapitalSelfAsIdentifier: Bool = false,
allowKeywordsAsIdentifier: Bool = false
) -> (RawUnexpectedNodesSyntax?, RawTokenSyntax) {
if let identifier = self.consume(if: .identifier) {
return (nil, identifier)
}
if allowKeywordsAsIdentifier, self.currentToken.isLexerClassifiedKeyword {
return (nil, self.consumeAnyToken(remapping: .identifier))
}
if allowSelfOrCapitalSelfAsIdentifier,
let selfOrCapitalSelf = self.consume(if: TokenSpec(.self, remapping: .identifier), TokenSpec(.Self, remapping: .identifier))
{
Expand Down
57 changes: 23 additions & 34 deletions Tests/SwiftParserTest/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,23 @@ final class DeclarationTests: XCTestCase {
)
}

func testMacroExpansionDeclarationWithKeywordName() {
assertParse(
"""
struct X {
#case
}
""",
substructure: Syntax(
MacroExpansionDeclSyntax(
poundToken: .poundToken(),
macro: .identifier("case"),
argumentList: TupleExprElementListSyntax([])
)
)
)
}

func testAttributedMacroExpansionDeclaration() {
assertParse(
"""
Expand Down Expand Up @@ -1806,60 +1823,32 @@ final class DeclarationTests: XCTestCase {
assertParse(
"""
struct S {
@attrib #1️⃣class
@attrib #class
}
""",
substructure: Syntax(
MacroExpansionDeclSyntax(
attributes: [.attribute(AttributeSyntax(attributeName: TypeSyntax("attrib")))],
poundToken: .poundToken(),
/*unexpectedBetweenPoundTokenAndMacro:*/ [
TokenSyntax.keyword(.class)
],
macro: .identifier("", presence: .missing),
macro: .identifier("class"),
argumentList: []
)
),
diagnostics: [
DiagnosticSpec(
message: "keyword 'class' cannot be used as an identifier here",
fixIts: ["if this name is unavoidable, use backticks to escape it"]
)
],
fixedSource: """
struct S {
@attrib #`class`
}
"""
)
)

assertParse(
"""
struct S {
#1️⃣struct
#struct
}
""",
substructure: Syntax(
MacroExpansionDeclSyntax(
poundToken: .poundToken(),
/*unexpectedBetweenPoundTokenAndMacro:*/ [
TokenSyntax.keyword(.struct)
],
macro: .identifier("", presence: .missing),
macro: .identifier("struct"),
argumentList: []
)
),
diagnostics: [
DiagnosticSpec(
message: "keyword 'struct' cannot be used as an identifier here",
fixIts: ["if this name is unavoidable, use backticks to escape it"]
)
],
fixedSource: """
struct S {
#`struct`
}
"""
)
)
}

Expand Down
13 changes: 13 additions & 0 deletions Tests/SwiftParserTest/ExpressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,19 @@ final class ExpressionTests: XCTestCase {
)
}

func testMacroExpansionExpressionWithKeywordName() {
assertParse(
"#case",
substructure: Syntax(
MacroExpansionExprSyntax(
poundToken: .poundToken(),
macro: .identifier("case"),
argumentList: TupleExprElementListSyntax([])
)
)
)
}

func testNewlineInInterpolationOfSingleLineString() {
assertParse(
#"""
Expand Down