Skip to content

Commit f3c525f

Browse files
authored
Merge pull request #987 from DougGregor/pound-literal-not-as-decl
Don't treat a `#` as starting a declaration.
2 parents 97a5893 + 499119d commit f3c525f

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,16 @@ extension Parser {
174174
return RawDeclSyntax(directive)
175175
case (.poundWarningKeyword, _)?, (.poundErrorKeyword, _)?:
176176
return self.parsePoundDiagnosticDeclaration()
177-
case (.pound, _)?:
178-
return RawDeclSyntax(self.parseMacroExpansionDeclaration())
179177
case nil:
180178
break
181179
}
182180

181+
if (self.at(.pound)) {
182+
// FIXME: If we can have attributes for macro expansions, handle this
183+
// via DeclarationStart.
184+
return RawDeclSyntax(self.parseMacroExpansionDeclaration())
185+
}
186+
183187
let attrs = DeclAttributes(
184188
attributes: self.parseAttributeList(),
185189
modifiers: self.parseModifierList())

Sources/SwiftParser/RawTokenKindSubset.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,12 @@ enum PoundDeclarationStart: RawTokenKindSubset {
574574
case poundIfKeyword
575575
case poundWarningKeyword
576576
case poundErrorKeyword
577-
case pound
578577

579578
init?(lexeme: Lexer.Lexeme) {
580579
switch lexeme.tokenKind {
581580
case .poundIfKeyword: self = .poundIfKeyword
582581
case .poundWarningKeyword: self = .poundWarningKeyword
583582
case .poundErrorKeyword: self = .poundErrorKeyword
584-
case .pound: self = .pound
585583
default: return nil
586584
}
587585
}
@@ -591,7 +589,6 @@ enum PoundDeclarationStart: RawTokenKindSubset {
591589
case .poundIfKeyword: return .poundIfKeyword
592590
case .poundWarningKeyword: return .poundWarningKeyword
593591
case .poundErrorKeyword: return .poundErrorKeyword
594-
case .pound: return .pound
595592
}
596593
}
597594
}

Tests/SwiftParserTest/Expressions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ final class ExpressionTests: XCTestCase {
238238
__COLUMN__
239239
__FUNCTION__
240240
__DSO_HANDLE__
241+
242+
func f() {
243+
return #function
244+
}
241245
"""
242246
)
243247
}

Tests/SwiftParserTest/translated/RawStringErrorsTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ final class RawStringErrorsTests: XCTestCase {
4141
// TODO: Old parser expected error on line 1: consecutive statements on a line must be separated by ';'
4242
// TODO: Old parser expected error on line 1: expected expression
4343
DiagnosticSpec(locationMarker: "1️⃣", message: "consecutive statements on a line must be separated by ';'"),
44-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in pound literal declaration"),
45-
DiagnosticSpec(locationMarker: "3️⃣", message: "expected identifier in pound literal declaration"),
46-
DiagnosticSpec(locationMarker: "4️⃣", message: "expected identifier in pound literal declaration"),
44+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in pound literal expression"),
45+
DiagnosticSpec(locationMarker: "3️⃣", message: "expected identifier in pound literal expression"),
46+
DiagnosticSpec(locationMarker: "4️⃣", message: "expected identifier in pound literal expression"),
4747
]
4848
)
4949
}
@@ -70,9 +70,9 @@ final class RawStringErrorsTests: XCTestCase {
7070
// TODO: Old parser expected error on line 1: consecutive statements on a line must be separated by ';'
7171
// TODO: Old parser expected error on line 1: expected expression
7272
DiagnosticSpec(locationMarker: "1️⃣", message: "consecutive statements on a line must be separated by ';'"),
73-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in pound literal declaration"),
74-
DiagnosticSpec(locationMarker: "3️⃣", message: "expected identifier in pound literal declaration"),
75-
DiagnosticSpec(locationMarker: "4️⃣", message: "expected identifier in pound literal declaration"),
73+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in pound literal expression"),
74+
DiagnosticSpec(locationMarker: "3️⃣", message: "expected identifier in pound literal expression"),
75+
DiagnosticSpec(locationMarker: "4️⃣", message: "expected identifier in pound literal expression"),
7676
]
7777
)
7878
}

Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,20 @@ final class MacroSystemTests: XCTestCase {
4343
struct X {
4444
var computed: String {
4545
get {
46-
(#function)
46+
#function
4747
}
4848
}
4949
5050
init(from: String) {
51-
(#function)
51+
#function
5252
}
5353
5454
subscript(a: Int) -> String {
55-
(#function)
55+
#function
5656
}
5757
5858
subscript(a a: Int) -> String {
59-
(#function)
59+
#function
6060
}
6161
}
6262
@@ -81,20 +81,20 @@ final class MacroSystemTests: XCTestCase {
8181
struct X {
8282
var computed: String {
8383
get {
84-
("computed")
84+
"computed"
8585
}
8686
}
8787
8888
init(from: String) {
89-
("init(from:)")
89+
"init(from:)"
9090
}
9191
9292
subscript(a: Int) -> String {
93-
("subscript(_:)")
93+
"subscript(_:)"
9494
}
9595
9696
subscript(a a: Int) -> String {
97-
("subscript(a:)")
97+
"subscript(a:)"
9898
}
9999
}
100100

0 commit comments

Comments
 (0)