Skip to content

Commit 3e3bd6a

Browse files
authored
Merge pull request #1559 from kavon/without-operator-improvements
improve implementation of without operator
2 parents 560df35 + f06d3d0 commit 3e3bd6a

File tree

6 files changed

+62
-36
lines changed

6 files changed

+62
-36
lines changed

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ public let DECL_NODES: [Node] = [
10331033
/// indicate the suppression of implicit conformance to this type.
10341034
/// This child stores the token representing the 'without' operator.
10351035
Child(
1036-
name: "HasWithout",
1036+
name: "WithoutTilde",
10371037
kind: .token(choices: [.token(tokenKind: "PrefixOperatorToken")]),
10381038
isOptional: true
10391039
),

Sources/SwiftParser/Nominals.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,14 @@ extension Parser {
289289
)
290290
)
291291
} else {
292-
if self.currentToken.starts(with: "~") {
293-
withoutToken = self.consumePrefix("~", as: .prefixOperator)
294-
}
292+
withoutToken = self.consumeIfContextualPunctuator("~", remapping: .prefixOperator)
295293
type = self.parseType()
296294
}
297295

298296
keepGoing = self.consume(if: .comma)
299297
elements.append(
300298
RawInheritedTypeSyntax(
301-
hasWithout: withoutToken,
299+
withoutTilde: withoutToken,
302300
typeName: type,
303301
trailingComma: keepGoing,
304302
arena: self.arena

Sources/SwiftSyntax/generated/ChildNameForKeyPath.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,12 +1712,12 @@ internal func childName(_ keyPath: AnyKeyPath) -> String? {
17121712
return "rightOperand"
17131713
case \InfixOperatorExprSyntax.unexpectedAfterRightOperand:
17141714
return "unexpectedAfterRightOperand"
1715-
case \InheritedTypeSyntax.unexpectedBeforeHasWithout:
1716-
return "unexpectedBeforeHasWithout"
1717-
case \InheritedTypeSyntax.hasWithout:
1718-
return "hasWithout"
1719-
case \InheritedTypeSyntax.unexpectedBetweenHasWithoutAndTypeName:
1720-
return "unexpectedBetweenHasWithoutAndTypeName"
1715+
case \InheritedTypeSyntax.unexpectedBeforeWithoutTilde:
1716+
return "unexpectedBeforeWithoutTilde"
1717+
case \InheritedTypeSyntax.withoutTilde:
1718+
return "withoutTilde"
1719+
case \InheritedTypeSyntax.unexpectedBetweenWithoutTildeAndTypeName:
1720+
return "unexpectedBetweenWithoutTildeAndTypeName"
17211721
case \InheritedTypeSyntax.typeName:
17221722
return "typeName"
17231723
case \InheritedTypeSyntax.unexpectedBetweenTypeNameAndTrailingComma:

Sources/SwiftSyntax/generated/raw/RawSyntaxNodes.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11708,9 +11708,9 @@ public struct RawInheritedTypeSyntax: RawSyntaxNodeProtocol {
1170811708
}
1170911709

1171011710
public init(
11711-
_ unexpectedBeforeHasWithout: RawUnexpectedNodesSyntax? = nil,
11712-
hasWithout: RawTokenSyntax?,
11713-
_ unexpectedBetweenHasWithoutAndTypeName: RawUnexpectedNodesSyntax? = nil,
11711+
_ unexpectedBeforeWithoutTilde: RawUnexpectedNodesSyntax? = nil,
11712+
withoutTilde: RawTokenSyntax?,
11713+
_ unexpectedBetweenWithoutTildeAndTypeName: RawUnexpectedNodesSyntax? = nil,
1171411714
typeName: RawTypeSyntax,
1171511715
_ unexpectedBetweenTypeNameAndTrailingComma: RawUnexpectedNodesSyntax? = nil,
1171611716
trailingComma: RawTokenSyntax?,
@@ -11720,9 +11720,9 @@ public struct RawInheritedTypeSyntax: RawSyntaxNodeProtocol {
1172011720
let raw = RawSyntax.makeLayout(
1172111721
kind: .inheritedType, uninitializedCount: 7, arena: arena) { layout in
1172211722
layout.initialize(repeating: nil)
11723-
layout[0] = unexpectedBeforeHasWithout?.raw
11724-
layout[1] = hasWithout?.raw
11725-
layout[2] = unexpectedBetweenHasWithoutAndTypeName?.raw
11723+
layout[0] = unexpectedBeforeWithoutTilde?.raw
11724+
layout[1] = withoutTilde?.raw
11725+
layout[2] = unexpectedBetweenWithoutTildeAndTypeName?.raw
1172611726
layout[3] = typeName.raw
1172711727
layout[4] = unexpectedBetweenTypeNameAndTrailingComma?.raw
1172811728
layout[5] = trailingComma?.raw
@@ -11731,15 +11731,15 @@ public struct RawInheritedTypeSyntax: RawSyntaxNodeProtocol {
1173111731
self.init(unchecked: raw)
1173211732
}
1173311733

11734-
public var unexpectedBeforeHasWithout: RawUnexpectedNodesSyntax? {
11734+
public var unexpectedBeforeWithoutTilde: RawUnexpectedNodesSyntax? {
1173511735
layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:))
1173611736
}
1173711737

11738-
public var hasWithout: RawTokenSyntax? {
11738+
public var withoutTilde: RawTokenSyntax? {
1173911739
layoutView.children[1].map(RawTokenSyntax.init(raw:))
1174011740
}
1174111741

11742-
public var unexpectedBetweenHasWithoutAndTypeName: RawUnexpectedNodesSyntax? {
11742+
public var unexpectedBetweenWithoutTildeAndTypeName: RawUnexpectedNodesSyntax? {
1174311743
layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:))
1174411744
}
1174511745

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodes.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10504,9 +10504,9 @@ public struct InheritedTypeSyntax: SyntaxProtocol, SyntaxHashable {
1050410504

1050510505
public init<T: TypeSyntaxProtocol>(
1050610506
leadingTrivia: Trivia? = nil,
10507-
_ unexpectedBeforeHasWithout: UnexpectedNodesSyntax? = nil,
10508-
hasWithout: TokenSyntax? = nil,
10509-
_ unexpectedBetweenHasWithoutAndTypeName: UnexpectedNodesSyntax? = nil,
10507+
_ unexpectedBeforeWithoutTilde: UnexpectedNodesSyntax? = nil,
10508+
withoutTilde: TokenSyntax? = nil,
10509+
_ unexpectedBetweenWithoutTildeAndTypeName: UnexpectedNodesSyntax? = nil,
1051010510
typeName: T,
1051110511
_ unexpectedBetweenTypeNameAndTrailingComma: UnexpectedNodesSyntax? = nil,
1051210512
trailingComma: TokenSyntax? = nil,
@@ -10517,18 +10517,18 @@ public struct InheritedTypeSyntax: SyntaxProtocol, SyntaxHashable {
1051710517
// Extend the lifetime of all parameters so their arenas don't get destroyed
1051810518
// before they can be added as children of the new arena.
1051910519
let data: SyntaxData = withExtendedLifetime((SyntaxArena(), (
10520-
unexpectedBeforeHasWithout,
10521-
hasWithout,
10522-
unexpectedBetweenHasWithoutAndTypeName,
10520+
unexpectedBeforeWithoutTilde,
10521+
withoutTilde,
10522+
unexpectedBetweenWithoutTildeAndTypeName,
1052310523
typeName,
1052410524
unexpectedBetweenTypeNameAndTrailingComma,
1052510525
trailingComma,
1052610526
unexpectedAfterTrailingComma
1052710527
))) {(arena, _) in
1052810528
let layout: [RawSyntax?] = [
10529-
unexpectedBeforeHasWithout?.raw,
10530-
hasWithout?.raw,
10531-
unexpectedBetweenHasWithoutAndTypeName?.raw,
10529+
unexpectedBeforeWithoutTilde?.raw,
10530+
withoutTilde?.raw,
10531+
unexpectedBetweenWithoutTildeAndTypeName?.raw,
1053210532
typeName.raw,
1053310533
unexpectedBetweenTypeNameAndTrailingComma?.raw,
1053410534
trailingComma?.raw,
@@ -10546,7 +10546,7 @@ public struct InheritedTypeSyntax: SyntaxProtocol, SyntaxHashable {
1054610546
self.init(data)
1054710547
}
1054810548

10549-
public var unexpectedBeforeHasWithout: UnexpectedNodesSyntax? {
10549+
public var unexpectedBeforeWithoutTilde: UnexpectedNodesSyntax? {
1055010550
get {
1055110551
return data.child(at: 0, parent: Syntax(self)).map(UnexpectedNodesSyntax.init)
1055210552
}
@@ -10555,7 +10555,7 @@ public struct InheritedTypeSyntax: SyntaxProtocol, SyntaxHashable {
1055510555
}
1055610556
}
1055710557

10558-
public var hasWithout: TokenSyntax? {
10558+
public var withoutTilde: TokenSyntax? {
1055910559
get {
1056010560
return data.child(at: 1, parent: Syntax(self)).map(TokenSyntax.init)
1056110561
}
@@ -10564,7 +10564,7 @@ public struct InheritedTypeSyntax: SyntaxProtocol, SyntaxHashable {
1056410564
}
1056510565
}
1056610566

10567-
public var unexpectedBetweenHasWithoutAndTypeName: UnexpectedNodesSyntax? {
10567+
public var unexpectedBetweenWithoutTildeAndTypeName: UnexpectedNodesSyntax? {
1056810568
get {
1056910569
return data.child(at: 2, parent: Syntax(self)).map(UnexpectedNodesSyntax.init)
1057010570
}
@@ -10611,9 +10611,9 @@ public struct InheritedTypeSyntax: SyntaxProtocol, SyntaxHashable {
1061110611

1061210612
public static var structure: SyntaxNodeStructure {
1061310613
return .layout([
10614-
\Self.unexpectedBeforeHasWithout,
10615-
\Self.hasWithout,
10616-
\Self.unexpectedBetweenHasWithoutAndTypeName,
10614+
\Self.unexpectedBeforeWithoutTilde,
10615+
\Self.withoutTilde,
10616+
\Self.unexpectedBetweenWithoutTildeAndTypeName,
1061710617
\Self.typeName,
1061810618
\Self.unexpectedBetweenTypeNameAndTrailingComma,
1061910619
\Self.trailingComma,

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1577,9 +1577,37 @@ final class DeclarationTests: XCTestCase {
15771577
assertParse(
15781578
"""
15791579
struct Hello: ~Copyable {}
1580+
""",
1581+
substructure: Syntax(
1582+
InheritedTypeSyntax(
1583+
withoutTilde: .prefixOperator("~"),
1584+
typeName: TypeSyntax(stringLiteral: "Copyable")
1585+
)
1586+
)
1587+
)
15801588

1581-
enum Whatever: Int, ~ Hashable, Equatable {}
1589+
assertParse(
15821590
"""
1591+
enum Whatever: Int, ~ Hashable, Equatable {}
1592+
""",
1593+
substructure:
1594+
Syntax(
1595+
TypeInheritanceClauseSyntax(
1596+
colon: .colonToken(),
1597+
inheritedTypeCollection: InheritedTypeListSyntax([
1598+
InheritedTypeSyntax(
1599+
typeName: TypeSyntax(stringLiteral: "Int"),
1600+
trailingComma: .commaToken()
1601+
),
1602+
InheritedTypeSyntax(
1603+
withoutTilde: .prefixOperator("~"),
1604+
typeName: TypeSyntax(stringLiteral: "Hashable"),
1605+
trailingComma: .commaToken()
1606+
),
1607+
InheritedTypeSyntax(typeName: TypeSyntax(stringLiteral: "Equatable")),
1608+
])
1609+
)
1610+
)
15831611
)
15841612
}
15851613
}

0 commit comments

Comments
 (0)