Skip to content

Commit 21e1f4c

Browse files
committed
Differentiate labeled and wildcard package requirement
1 parent 9d338a6 commit 21e1f4c

File tree

18 files changed

+531
-159
lines changed

18 files changed

+531
-159
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/AttributeNodes.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,31 @@ public let ATTRIBUTE_NODES: [Node] = [
263263
Child(name: "Comma",
264264
kind: .token(choices: [.token(tokenKind: "CommaToken")]),
265265
description: "The comma between the package location and requirement"),
266-
Child(name: "RequirementLabel",
266+
Child(name: "Requirement",
267+
kind: .nodeChoices(choices: [
268+
Child(name: "Labeled",
269+
kind: .node(kind: "LabeledPackageRequirement")),
270+
Child(name: "Wildcard",
271+
kind: .node(kind: "Expr"))
272+
]),
273+
description: "Version requirement of remote package")
274+
]),
275+
276+
Node(name: "LabeledPackageRequirement",
277+
nameForDiagnostics: "labeled package requirement",
278+
description: "Labeled requirement of a remote package",
279+
kind: "Syntax",
280+
children: [
281+
Child(name: "Label",
267282
kind: .token(choices: [.keyword(text: "branch"), .keyword(text: "exact"), .keyword(text: "from"), .keyword(text: "revision")]),
268283
description: "The requirement label",
269284
isOptional: true),
270-
Child(name: "RequirementColon",
285+
Child(name: "Colon",
271286
kind: .token(choices: [.token(tokenKind: "ColonToken")]),
272287
isOptional: true),
273288
Child(name: "Requirement",
274-
kind: .node(kind: "Expr"),
275-
description: "Version requirement of remote package")
289+
kind: .node(kind: "StringLiteralExpr"),
290+
description: "Requirement description of remote package")
276291
]),
277292

278293
Node(name: "ObjCSelectorPiece",

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SyntaxCollectionsFile.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: [.blockComment(gener
3838
for node in SYNTAX_NODES where node.isSyntaxCollection {
3939
let documentation =
4040
node.description.map { "/// " + $0 }
41-
?? """
42-
/// `\(node.name)` represents a collection of one or more
43-
/// `\(node.collectionElement)` nodes. \(node.name) behaves
44-
/// as a regular Swift collection, and has accessors that return new
45-
/// versions of the collection with different children.
46-
"""
41+
?? """
42+
/// `\(node.name)` represents a collection of one or more
43+
/// `\(node.collectionElement)` nodes. \(node.name) behaves
44+
/// as a regular Swift collection, and has accessors that return new
45+
/// versions of the collection with different children.
46+
"""
4747

4848
try! StructDeclSyntax(
4949
"""

Sources/SwiftParser/Attributes.swift

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -892,45 +892,39 @@ extension Parser {
892892
)
893893
} else {
894894
let (unexpectedBeforeRequirementComma, requirementComma) = self.expect(.comma)
895+
// Parsing package requirement
896+
let packageRequirement: RawRemotePackageDescriptionSyntax.Requirement
895897
if self.at(any: [.colon, .keyword(.from), .keyword(.exact), .keyword(.branch), .keyword(.revision)]) {
896898
let (unexpectedBeforeRequirementLabel, requirementLabel) = self.expectAny([.keyword(.from), .keyword(.exact), .keyword(.branch), .keyword(.revision)], default: .keyword(.from))
897899
let (unexpectedBeforeRequirementColon, requirementColon) = self.expect(.colon)
898-
let requirement = self.parseStringLiteral().as(RawExprSyntax.self)!
899-
packageDescription = .remote(
900-
RawRemotePackageDescriptionSyntax(
901-
unexpectedBeforeLocationLabel,
902-
locationLabel: locationLabel,
903-
unexpectedBeforeLocationColon,
904-
locationColon: locationColon,
905-
location: location,
906-
unexpectedBeforeRequirementComma,
907-
comma: requirementComma,
900+
let requirement = self.parseStringLiteral()
901+
packageRequirement = .labeled(
902+
RawLabeledPackageRequirementSyntax(
908903
unexpectedBeforeRequirementLabel,
909-
requirementLabel: requirementLabel,
904+
label: requirementLabel,
910905
unexpectedBeforeRequirementColon,
911-
requirementColon: requirementColon,
906+
colon: requirementColon,
912907
requirement: requirement,
913908
arena: self.arena
914909
)
915910
)
916911
} else {
917912
let requirement = self.parseExpression()
918-
packageDescription = .remote(
919-
RawRemotePackageDescriptionSyntax(
920-
unexpectedBeforeLocationLabel,
921-
locationLabel: locationLabel,
922-
unexpectedBeforeLocationColon,
923-
locationColon: locationColon,
924-
location: location,
925-
unexpectedBeforeRequirementComma,
926-
comma: requirementComma,
927-
requirementLabel: nil,
928-
requirementColon: nil,
929-
requirement: requirement,
930-
arena: self.arena
931-
)
932-
)
913+
packageRequirement = .wildcard(requirement)
933914
}
915+
packageDescription = .remote(
916+
RawRemotePackageDescriptionSyntax(
917+
unexpectedBeforeLocationLabel,
918+
locationLabel: locationLabel,
919+
unexpectedBeforeLocationColon,
920+
locationColon: locationColon,
921+
location: location,
922+
unexpectedBeforeRequirementComma,
923+
comma: requirementComma,
924+
requirement: packageRequirement,
925+
arena: self.arena
926+
)
927+
)
934928
}
935929
// Parsing package product
936930
let comma = self.consume(if: .comma)

Sources/SwiftSyntax/Documentation.docc/gyb_generated/SwiftSyntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
356356
- <doc:SwiftSyntax/PackageProductSyntax>
357357
- <doc:SwiftSyntax/LocalPackageDescriptionSyntax>
358358
- <doc:SwiftSyntax/RemotePackageDescriptionSyntax>
359+
- <doc:SwiftSyntax/LabeledPackageRequirementSyntax>
359360
- <doc:SwiftSyntax/ObjCSelectorPieceSyntax>
360361
- <doc:SwiftSyntax/ObjCSelectorSyntax>
361362
- <doc:SwiftSyntax/DifferentiableAttributeArgumentsSyntax>

Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxNodes.swift

Lines changed: 104 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11956,6 +11956,35 @@ public struct RawLocalPackageDescriptionSyntax: RawSyntaxNodeProtocol {
1195611956

1195711957
@_spi(RawSyntax)
1195811958
public struct RawRemotePackageDescriptionSyntax: RawSyntaxNodeProtocol {
11959+
@frozen // FIXME: Not actually stable, works around a miscompile
11960+
public enum Requirement: RawSyntaxNodeProtocol {
11961+
case `labeled`(RawLabeledPackageRequirementSyntax)
11962+
case `wildcard`(RawExprSyntax)
11963+
11964+
public static func isKindOf(_ raw: RawSyntax) -> Bool {
11965+
return RawLabeledPackageRequirementSyntax.isKindOf(raw) || RawExprSyntax.isKindOf(raw)
11966+
}
11967+
11968+
public var raw: RawSyntax {
11969+
switch self {
11970+
case .labeled(let node): return node.raw
11971+
case .wildcard(let node): return node.raw
11972+
}
11973+
}
11974+
11975+
public init?<T>(_ other: T) where T : RawSyntaxNodeProtocol {
11976+
if let node = RawLabeledPackageRequirementSyntax(other) {
11977+
self = .labeled(node)
11978+
return
11979+
}
11980+
if let node = RawExprSyntax(other) {
11981+
self = .wildcard(node)
11982+
return
11983+
}
11984+
return nil
11985+
}
11986+
}
11987+
1195911988

1196011989
@_spi(RawSyntax)
1196111990
public var layoutView: RawSyntaxLayoutView {
@@ -11986,17 +12015,13 @@ public struct RawRemotePackageDescriptionSyntax: RawSyntaxNodeProtocol {
1198612015
location: RawStringLiteralExprSyntax,
1198712016
_ unexpectedBetweenLocationAndComma: RawUnexpectedNodesSyntax? = nil,
1198812017
comma: RawTokenSyntax,
11989-
_ unexpectedBetweenCommaAndRequirementLabel: RawUnexpectedNodesSyntax? = nil,
11990-
requirementLabel: RawTokenSyntax?,
11991-
_ unexpectedBetweenRequirementLabelAndRequirementColon: RawUnexpectedNodesSyntax? = nil,
11992-
requirementColon: RawTokenSyntax?,
11993-
_ unexpectedBetweenRequirementColonAndRequirement: RawUnexpectedNodesSyntax? = nil,
11994-
requirement: RawExprSyntax,
12018+
_ unexpectedBetweenCommaAndRequirement: RawUnexpectedNodesSyntax? = nil,
12019+
requirement: Requirement,
1199512020
_ unexpectedAfterRequirement: RawUnexpectedNodesSyntax? = nil,
1199612021
arena: __shared SyntaxArena
1199712022
) {
1199812023
let raw = RawSyntax.makeLayout(
11999-
kind: .remotePackageDescription, uninitializedCount: 15, arena: arena) { layout in
12024+
kind: .remotePackageDescription, uninitializedCount: 11, arena: arena) { layout in
1200012025
layout.initialize(repeating: nil)
1200112026
layout[0] = unexpectedBeforeLocationLabel?.raw
1200212027
layout[1] = locationLabel.raw
@@ -12006,13 +12031,9 @@ public struct RawRemotePackageDescriptionSyntax: RawSyntaxNodeProtocol {
1200612031
layout[5] = location.raw
1200712032
layout[6] = unexpectedBetweenLocationAndComma?.raw
1200812033
layout[7] = comma.raw
12009-
layout[8] = unexpectedBetweenCommaAndRequirementLabel?.raw
12010-
layout[9] = requirementLabel?.raw
12011-
layout[10] = unexpectedBetweenRequirementLabelAndRequirementColon?.raw
12012-
layout[11] = requirementColon?.raw
12013-
layout[12] = unexpectedBetweenRequirementColonAndRequirement?.raw
12014-
layout[13] = requirement.raw
12015-
layout[14] = unexpectedAfterRequirement?.raw
12034+
layout[8] = unexpectedBetweenCommaAndRequirement?.raw
12035+
layout[9] = requirement.raw
12036+
layout[10] = unexpectedAfterRequirement?.raw
1201612037
}
1201712038
self.init(raw: raw)
1201812039
}
@@ -12041,26 +12062,84 @@ public struct RawRemotePackageDescriptionSyntax: RawSyntaxNodeProtocol {
1204112062
public var comma: RawTokenSyntax {
1204212063
layoutView.children[7].map(RawTokenSyntax.init(raw:))!
1204312064
}
12044-
public var unexpectedBetweenCommaAndRequirementLabel: RawUnexpectedNodesSyntax? {
12065+
public var unexpectedBetweenCommaAndRequirement: RawUnexpectedNodesSyntax? {
1204512066
layoutView.children[8].map(RawUnexpectedNodesSyntax.init(raw:))
1204612067
}
12047-
public var requirementLabel: RawTokenSyntax? {
12048-
layoutView.children[9].map(RawTokenSyntax.init(raw:))
12068+
public var requirement: RawSyntax {
12069+
layoutView.children[9]!
1204912070
}
12050-
public var unexpectedBetweenRequirementLabelAndRequirementColon: RawUnexpectedNodesSyntax? {
12071+
public var unexpectedAfterRequirement: RawUnexpectedNodesSyntax? {
1205112072
layoutView.children[10].map(RawUnexpectedNodesSyntax.init(raw:))
1205212073
}
12053-
public var requirementColon: RawTokenSyntax? {
12054-
layoutView.children[11].map(RawTokenSyntax.init(raw:))
12074+
}
12075+
12076+
@_spi(RawSyntax)
12077+
public struct RawLabeledPackageRequirementSyntax: RawSyntaxNodeProtocol {
12078+
12079+
@_spi(RawSyntax)
12080+
public var layoutView: RawSyntaxLayoutView {
12081+
return raw.layoutView!
1205512082
}
12056-
public var unexpectedBetweenRequirementColonAndRequirement: RawUnexpectedNodesSyntax? {
12057-
layoutView.children[12].map(RawUnexpectedNodesSyntax.init(raw:))
12083+
12084+
public static func isKindOf(_ raw: RawSyntax) -> Bool {
12085+
return raw.kind == .labeledPackageRequirement
12086+
}
12087+
12088+
public var raw: RawSyntax
12089+
init(raw: RawSyntax) {
12090+
assert(Self.isKindOf(raw))
12091+
self.raw = raw
12092+
}
12093+
12094+
public init?<Node: RawSyntaxNodeProtocol>(_ other: Node) {
12095+
guard Self.isKindOf(other.raw) else { return nil }
12096+
self.init(raw: other.raw)
12097+
}
12098+
12099+
public init(
12100+
_ unexpectedBeforeLabel: RawUnexpectedNodesSyntax? = nil,
12101+
label: RawTokenSyntax?,
12102+
_ unexpectedBetweenLabelAndColon: RawUnexpectedNodesSyntax? = nil,
12103+
colon: RawTokenSyntax?,
12104+
_ unexpectedBetweenColonAndRequirement: RawUnexpectedNodesSyntax? = nil,
12105+
requirement: RawStringLiteralExprSyntax,
12106+
_ unexpectedAfterRequirement: RawUnexpectedNodesSyntax? = nil,
12107+
arena: __shared SyntaxArena
12108+
) {
12109+
let raw = RawSyntax.makeLayout(
12110+
kind: .labeledPackageRequirement, uninitializedCount: 7, arena: arena) { layout in
12111+
layout.initialize(repeating: nil)
12112+
layout[0] = unexpectedBeforeLabel?.raw
12113+
layout[1] = label?.raw
12114+
layout[2] = unexpectedBetweenLabelAndColon?.raw
12115+
layout[3] = colon?.raw
12116+
layout[4] = unexpectedBetweenColonAndRequirement?.raw
12117+
layout[5] = requirement.raw
12118+
layout[6] = unexpectedAfterRequirement?.raw
12119+
}
12120+
self.init(raw: raw)
12121+
}
12122+
12123+
public var unexpectedBeforeLabel: RawUnexpectedNodesSyntax? {
12124+
layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:))
12125+
}
12126+
public var label: RawTokenSyntax? {
12127+
layoutView.children[1].map(RawTokenSyntax.init(raw:))
12128+
}
12129+
public var unexpectedBetweenLabelAndColon: RawUnexpectedNodesSyntax? {
12130+
layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:))
12131+
}
12132+
public var colon: RawTokenSyntax? {
12133+
layoutView.children[3].map(RawTokenSyntax.init(raw:))
1205812134
}
12059-
public var requirement: RawExprSyntax {
12060-
layoutView.children[13].map(RawExprSyntax.init(raw:))!
12135+
public var unexpectedBetweenColonAndRequirement: RawUnexpectedNodesSyntax? {
12136+
layoutView.children[4].map(RawUnexpectedNodesSyntax.init(raw:))
12137+
}
12138+
public var requirement: RawStringLiteralExprSyntax {
12139+
layoutView.children[5].map(RawStringLiteralExprSyntax.init(raw:))!
1206112140
}
1206212141
public var unexpectedAfterRequirement: RawUnexpectedNodesSyntax? {
12063-
layoutView.children[14].map(RawUnexpectedNodesSyntax.init(raw:))
12142+
layoutView.children[6].map(RawUnexpectedNodesSyntax.init(raw:))
1206412143
}
1206512144
}
1206612145

Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxValidation.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
17581758
assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self))
17591759
break
17601760
case .remotePackageDescription:
1761-
assert(layout.count == 15)
1761+
assert(layout.count == 11)
17621762
assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self))
17631763
assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax.self))
17641764
assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self))
@@ -1768,12 +1768,21 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
17681768
assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self))
17691769
assertNoError(kind, 7, verify(layout[7], as: RawTokenSyntax.self))
17701770
assertNoError(kind, 8, verify(layout[8], as: RawUnexpectedNodesSyntax?.self))
1771-
assertNoError(kind, 9, verify(layout[9], as: RawTokenSyntax?.self))
1771+
assertAnyHasNoError(kind, 9, [
1772+
verify(layout[9], as: RawSyntax.self),
1773+
verify(layout[9], as: RawSyntax.self),
1774+
])
17721775
assertNoError(kind, 10, verify(layout[10], as: RawUnexpectedNodesSyntax?.self))
1773-
assertNoError(kind, 11, verify(layout[11], as: RawTokenSyntax?.self))
1774-
assertNoError(kind, 12, verify(layout[12], as: RawUnexpectedNodesSyntax?.self))
1775-
assertNoError(kind, 13, verify(layout[13], as: RawExprSyntax.self))
1776-
assertNoError(kind, 14, verify(layout[14], as: RawUnexpectedNodesSyntax?.self))
1776+
break
1777+
case .labeledPackageRequirement:
1778+
assert(layout.count == 7)
1779+
assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self))
1780+
assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax?.self))
1781+
assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self))
1782+
assertNoError(kind, 3, verify(layout[3], as: RawTokenSyntax?.self))
1783+
assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self))
1784+
assertNoError(kind, 5, verify(layout[5], as: RawStringLiteralExprSyntax.self))
1785+
assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self))
17771786
break
17781787
case .objCSelectorPiece:
17791788
assert(layout.count == 5)

Sources/SwiftSyntax/generated/Misc.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ extension Syntax {
163163
.node(KeyPathOptionalComponentSyntax.self),
164164
.node(KeyPathPropertyComponentSyntax.self),
165165
.node(KeyPathSubscriptComponentSyntax.self),
166+
.node(LabeledPackageRequirementSyntax.self),
166167
.node(LabeledSpecializeEntrySyntax.self),
167168
.node(LabeledStmtSyntax.self),
168169
.node(LayoutRequirementSyntax.self),
@@ -579,6 +580,8 @@ extension SyntaxKind {
579580
return KeyPathPropertyComponentSyntax.self
580581
case .keyPathSubscriptComponent:
581582
return KeyPathSubscriptComponentSyntax.self
583+
case .labeledPackageRequirement:
584+
return LabeledPackageRequirementSyntax.self
582585
case .labeledSpecializeEntry:
583586
return LabeledSpecializeEntrySyntax.self
584587
case .labeledStmt:
@@ -1114,6 +1117,8 @@ extension SyntaxKind {
11141117
return "key path property component"
11151118
case .keyPathSubscriptComponent:
11161119
return "key path subscript component"
1120+
case .labeledPackageRequirement:
1121+
return "labeled package requirement"
11171122
case .labeledSpecializeEntry:
11181123
return "attribute argument"
11191124
case .labeledStmt:

Sources/SwiftSyntax/generated/SyntaxAnyVisitor.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,14 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
12091209
visitAnyPost(node._syntaxNode)
12101210
}
12111211

1212+
override open func visit(_ node: LabeledPackageRequirementSyntax) -> SyntaxVisitorContinueKind {
1213+
return visitAny(node._syntaxNode)
1214+
}
1215+
1216+
override open func visitPost(_ node: LabeledPackageRequirementSyntax) {
1217+
visitAnyPost(node._syntaxNode)
1218+
}
1219+
12121220
override open func visit(_ node: LabeledSpecializeEntrySyntax) -> SyntaxVisitorContinueKind {
12131221
return visitAny(node._syntaxNode)
12141222
}

Sources/SwiftSyntax/generated/SyntaxEnum.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ public enum SyntaxEnum {
307307

308308
case keyPathSubscriptComponent(KeyPathSubscriptComponentSyntax)
309309

310+
case labeledPackageRequirement(LabeledPackageRequirementSyntax)
311+
310312
case labeledSpecializeEntry(LabeledSpecializeEntrySyntax)
311313

312314
case labeledStmt(LabeledStmtSyntax)
@@ -842,6 +844,8 @@ public extension Syntax {
842844
return .keyPathPropertyComponent(KeyPathPropertyComponentSyntax(self)!)
843845
case .keyPathSubscriptComponent:
844846
return .keyPathSubscriptComponent(KeyPathSubscriptComponentSyntax(self)!)
847+
case .labeledPackageRequirement:
848+
return .labeledPackageRequirement(LabeledPackageRequirementSyntax(self)!)
845849
case .labeledSpecializeEntry:
846850
return .labeledSpecializeEntry(LabeledSpecializeEntrySyntax(self)!)
847851
case .labeledStmt:

0 commit comments

Comments
 (0)