Skip to content

Commit f2b149e

Browse files
authored
Merge pull request #1560 from kavon/5.9-without-operator
[5.9🍒] without operator using `~` for implicit conformance suppression
2 parents 27cd619 + b51216b commit f2b149e

File tree

18 files changed

+417
-24
lines changed

18 files changed

+417
-24
lines changed

CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,23 @@ public let TYPE_NODES: [Node] = [
349349
]
350350
),
351351

352+
// suppressed-type -> '~' type
353+
Node(
354+
name: "SuppressedType",
355+
nameForDiagnostics: "suppressed type conformance",
356+
kind: "Type",
357+
children: [
358+
Child(
359+
name: "WithoutTilde",
360+
kind: .token(choices: [.token(tokenKind: "PrefixOperatorToken")])
361+
),
362+
Child(
363+
name: "PatternType",
364+
kind: .node(kind: "Type")
365+
),
366+
]
367+
),
368+
352369
// pack-expansion-type -> type '...'
353370
Node(
354371
name: "PackExpansionType",

Sources/SwiftParser/Declarations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ extension Parser {
497497
let unexpectedBeforeInherited: RawUnexpectedNodesSyntax?
498498
let inherited: RawTypeSyntax?
499499
if colon != nil {
500-
if self.at(.identifier, .keyword(.protocol), .keyword(.Any)) {
500+
if self.at(.identifier, .keyword(.protocol), .keyword(.Any)) || self.atContextualPunctuator("~") {
501501
unexpectedBeforeInherited = nil
502502
inherited = self.parseType()
503503
} else if let classKeyword = self.consume(if: .keyword(.class)) {

Sources/SwiftParser/Types.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ extension Parser {
3737
)
3838
}
3939

40+
// Parse without operator preceding a type '~ T'.
41+
if let withoutTilde = self.consumeIfContextualPunctuator("~", remapping: .prefixOperator) {
42+
let type = self.parseTypeScalar(misplacedSpecifiers: misplacedSpecifiers)
43+
return RawTypeSyntax(
44+
RawSuppressedTypeSyntax(
45+
withoutTilde: withoutTilde,
46+
patternType: type,
47+
arena: self.arena
48+
)
49+
)
50+
}
51+
4052
return self.parseTypeScalar(misplacedSpecifiers: misplacedSpecifiers)
4153
}
4254

Sources/SwiftParserDiagnostics/generated/SyntaxKindNameForDiagnostics.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ extension SyntaxKind {
341341
return "subscript"
342342
case .subscriptExpr:
343343
return "subscript"
344+
case .suppressedType:
345+
return "suppressed type conformance"
344346
case .switchCase:
345347
return "switch case"
346348
case .switchExpr:

Sources/SwiftSyntax/Documentation.docc/generated/SwiftSyntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
186186
- <doc:SwiftSyntax/PackExpansionTypeSyntax>
187187
- <doc:SwiftSyntax/PackReferenceTypeSyntax>
188188
- <doc:SwiftSyntax/SimpleTypeIdentifierSyntax>
189+
- <doc:SwiftSyntax/SuppressedTypeSyntax>
189190
- <doc:SwiftSyntax/TupleTypeSyntax>
190191

191192
### Collections

Sources/SwiftSyntax/generated/ChildNameForKeyPath.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,6 +2869,16 @@ public func childName(_ keyPath: AnyKeyPath) -> String? {
28692869
return "superKeyword"
28702870
case \SuperRefExprSyntax.unexpectedAfterSuperKeyword:
28712871
return "unexpectedAfterSuperKeyword"
2872+
case \SuppressedTypeSyntax.unexpectedBeforeWithoutTilde:
2873+
return "unexpectedBeforeWithoutTilde"
2874+
case \SuppressedTypeSyntax.withoutTilde:
2875+
return "withoutTilde"
2876+
case \SuppressedTypeSyntax.unexpectedBetweenWithoutTildeAndPatternType:
2877+
return "unexpectedBetweenWithoutTildeAndPatternType"
2878+
case \SuppressedTypeSyntax.patternType:
2879+
return "patternType"
2880+
case \SuppressedTypeSyntax.unexpectedAfterPatternType:
2881+
return "unexpectedAfterPatternType"
28722882
case \SwitchCaseLabelSyntax.unexpectedBeforeCaseKeyword:
28732883
return "unexpectedBeforeCaseKeyword"
28742884
case \SwitchCaseLabelSyntax.caseKeyword:

Sources/SwiftSyntax/generated/SyntaxAnyVisitor.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,14 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
18691869
visitAnyPost(node._syntaxNode)
18701870
}
18711871

1872+
override open func visit(_ node: SuppressedTypeSyntax) -> SyntaxVisitorContinueKind {
1873+
return visitAny(node._syntaxNode)
1874+
}
1875+
1876+
override open func visitPost(_ node: SuppressedTypeSyntax) {
1877+
visitAnyPost(node._syntaxNode)
1878+
}
1879+
18721880
override open func visit(_ node: SwitchCaseLabelSyntax) -> SyntaxVisitorContinueKind {
18731881
return visitAny(node._syntaxNode)
18741882
}

Sources/SwiftSyntax/generated/SyntaxBaseNodes.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
610610

611611
public init?<S: SyntaxProtocol>(_ node: S) {
612612
switch node.raw.kind {
613-
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .tupleType:
613+
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .suppressedType, .tupleType:
614614
self._syntaxNode = node._syntaxNode
615615
default:
616616
return nil
@@ -622,7 +622,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
622622
/// is undefined.
623623
internal init(_ data: SyntaxData) {
624624
switch data.raw.kind {
625-
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .tupleType:
625+
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .suppressedType, .tupleType:
626626
break
627627
default:
628628
preconditionFailure("Unable to create TypeSyntax from \(data.raw.kind)")
@@ -674,6 +674,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
674674
.node(PackExpansionTypeSyntax.self),
675675
.node(PackReferenceTypeSyntax.self),
676676
.node(SimpleTypeIdentifierSyntax.self),
677+
.node(SuppressedTypeSyntax.self),
677678
.node(TupleTypeSyntax.self)
678679
])
679680
}
@@ -910,6 +911,7 @@ extension Syntax {
910911
.node(SubscriptDeclSyntax.self),
911912
.node(SubscriptExprSyntax.self),
912913
.node(SuperRefExprSyntax.self),
914+
.node(SuppressedTypeSyntax.self),
913915
.node(SwitchCaseLabelSyntax.self),
914916
.node(SwitchCaseListSyntax.self),
915917
.node(SwitchCaseSyntax.self),

Sources/SwiftSyntax/generated/SyntaxEnum.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public enum SyntaxEnum {
243243
case subscriptDecl(SubscriptDeclSyntax)
244244
case subscriptExpr(SubscriptExprSyntax)
245245
case superRefExpr(SuperRefExprSyntax)
246+
case suppressedType(SuppressedTypeSyntax)
246247
case switchCaseLabel(SwitchCaseLabelSyntax)
247248
case switchCaseList(SwitchCaseListSyntax)
248249
case switchCase(SwitchCaseSyntax)
@@ -746,6 +747,8 @@ public extension Syntax {
746747
return .subscriptExpr(SubscriptExprSyntax(self)!)
747748
case .superRefExpr:
748749
return .superRefExpr(SuperRefExprSyntax(self)!)
750+
case .suppressedType:
751+
return .suppressedType(SuppressedTypeSyntax(self)!)
749752
case .switchCaseLabel:
750753
return .switchCaseLabel(SwitchCaseLabelSyntax(self)!)
751754
case .switchCaseList:

Sources/SwiftSyntax/generated/SyntaxKind.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public enum SyntaxKind {
243243
case subscriptDecl
244244
case subscriptExpr
245245
case superRefExpr
246+
case suppressedType
246247
case switchCaseLabel
247248
case switchCaseList
248249
case switchCase
@@ -861,6 +862,8 @@ public enum SyntaxKind {
861862
return SubscriptExprSyntax.self
862863
case .superRefExpr:
863864
return SuperRefExprSyntax.self
865+
case .suppressedType:
866+
return SuppressedTypeSyntax.self
864867
case .switchCaseLabel:
865868
return SwitchCaseLabelSyntax.self
866869
case .switchCaseList:

Sources/SwiftSyntax/generated/SyntaxRewriter.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,13 @@ open class SyntaxRewriter {
16141614
return ExprSyntax(visitChildren(node))
16151615
}
16161616

1617+
/// Visit a `SuppressedTypeSyntax`.
1618+
/// - Parameter node: the node that is being visited
1619+
/// - Returns: the rewritten node
1620+
open func visit(_ node: SuppressedTypeSyntax) -> TypeSyntax {
1621+
return TypeSyntax(visitChildren(node))
1622+
}
1623+
16171624
/// Visit a `SwitchCaseLabelSyntax`.
16181625
/// - Parameter node: the node that is being visited
16191626
/// - Returns: the rewritten node
@@ -5153,6 +5160,20 @@ open class SyntaxRewriter {
51535160
return Syntax(visit(node))
51545161
}
51555162

5163+
/// Implementation detail of visit(_:). Do not call directly.
5164+
private func visitImplSuppressedTypeSyntax(_ data: SyntaxData) -> Syntax {
5165+
let node = SuppressedTypeSyntax(data)
5166+
// Accessing _syntaxNode directly is faster than calling Syntax(node)
5167+
visitPre(node._syntaxNode)
5168+
defer {
5169+
visitPost(node._syntaxNode)
5170+
}
5171+
if let newNode = visitAny(node._syntaxNode) {
5172+
return newNode
5173+
}
5174+
return Syntax(visit(node))
5175+
}
5176+
51565177
/// Implementation detail of visit(_:). Do not call directly.
51575178
private func visitImplSwitchCaseLabelSyntax(_ data: SyntaxData) -> Syntax {
51585179
let node = SwitchCaseLabelSyntax(data)
@@ -6223,6 +6244,8 @@ open class SyntaxRewriter {
62236244
return visitImplSubscriptExprSyntax
62246245
case .superRefExpr:
62256246
return visitImplSuperRefExprSyntax
6247+
case .suppressedType:
6248+
return visitImplSuppressedTypeSyntax
62266249
case .switchCaseLabel:
62276250
return visitImplSwitchCaseLabelSyntax
62286251
case .switchCaseList:
@@ -6769,6 +6792,8 @@ open class SyntaxRewriter {
67696792
return visitImplSubscriptExprSyntax(data)
67706793
case .superRefExpr:
67716794
return visitImplSuperRefExprSyntax(data)
6795+
case .suppressedType:
6796+
return visitImplSuppressedTypeSyntax(data)
67726797
case .switchCaseLabel:
67736798
return visitImplSwitchCaseLabelSyntax(data)
67746799
case .switchCaseList:

Sources/SwiftSyntax/generated/SyntaxTransform.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,11 @@ public protocol SyntaxTransformVisitor {
11541154
/// - Returns: the sum of whatever the child visitors return.
11551155
func visit(_ node: SuperRefExprSyntax) -> ResultType
11561156

1157+
/// Visiting `SuppressedTypeSyntax` specifically.
1158+
/// - Parameter node: the node we are visiting.
1159+
/// - Returns: the sum of whatever the child visitors return.
1160+
func visit(_ node: SuppressedTypeSyntax) -> ResultType
1161+
11571162
/// Visiting `SwitchCaseLabelSyntax` specifically.
11581163
/// - Parameter node: the node we are visiting.
11591164
/// - Returns: the sum of whatever the child visitors return.
@@ -2954,6 +2959,13 @@ extension SyntaxTransformVisitor {
29542959
visitAny(Syntax(node))
29552960
}
29562961

2962+
/// Visiting `SuppressedTypeSyntax` specifically.
2963+
/// - Parameter node: the node we are visiting.
2964+
/// - Returns: nil by default.
2965+
public func visit(_ node: SuppressedTypeSyntax) -> ResultType {
2966+
visitAny(Syntax(node))
2967+
}
2968+
29572969
/// Visiting `SwitchCaseLabelSyntax` specifically.
29582970
/// - Parameter node: the node we are visiting.
29592971
/// - Returns: nil by default.
@@ -3699,6 +3711,8 @@ extension SyntaxTransformVisitor {
36993711
return visit(derived)
37003712
case .superRefExpr(let derived):
37013713
return visit(derived)
3714+
case .suppressedType(let derived):
3715+
return visit(derived)
37023716
case .switchCaseLabel(let derived):
37033717
return visit(derived)
37043718
case .switchCaseList(let derived):

Sources/SwiftSyntax/generated/SyntaxVisitor.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,6 +2758,18 @@ open class SyntaxVisitor {
27582758
open func visitPost(_ node: SuperRefExprSyntax) {
27592759
}
27602760

2761+
/// Visiting `SuppressedTypeSyntax` specifically.
2762+
/// - Parameter node: the node we are visiting.
2763+
/// - Returns: how should we continue visiting.
2764+
open func visit(_ node: SuppressedTypeSyntax) -> SyntaxVisitorContinueKind {
2765+
return .visitChildren
2766+
}
2767+
2768+
/// The function called after visiting `SuppressedTypeSyntax` and its descendents.
2769+
/// - node: the node we just finished visiting.
2770+
open func visitPost(_ node: SuppressedTypeSyntax) {
2771+
}
2772+
27612773
/// Visiting `SwitchCaseLabelSyntax` specifically.
27622774
/// - Parameter node: the node we are visiting.
27632775
/// - Returns: how should we continue visiting.
@@ -5759,6 +5771,17 @@ open class SyntaxVisitor {
57595771
visitPost(node)
57605772
}
57615773

5774+
/// Implementation detail of doVisit(_:_:). Do not call directly.
5775+
private func visitImplSuppressedTypeSyntax(_ data: SyntaxData) {
5776+
let node = SuppressedTypeSyntax(data)
5777+
let needsChildren = (visit(node) == .visitChildren)
5778+
// Avoid calling into visitChildren if possible.
5779+
if needsChildren && !node.raw.layoutView!.children.isEmpty {
5780+
visitChildren(node)
5781+
}
5782+
visitPost(node)
5783+
}
5784+
57625785
/// Implementation detail of doVisit(_:_:). Do not call directly.
57635786
private func visitImplSwitchCaseLabelSyntax(_ data: SyntaxData) {
57645787
let node = SwitchCaseLabelSyntax(data)
@@ -6671,6 +6694,8 @@ open class SyntaxVisitor {
66716694
visitImplSubscriptExprSyntax(data)
66726695
case .superRefExpr:
66736696
visitImplSuperRefExprSyntax(data)
6697+
case .suppressedType:
6698+
visitImplSuppressedTypeSyntax(data)
66746699
case .switchCaseLabel:
66756700
visitImplSwitchCaseLabelSyntax(data)
66766701
case .switchCaseList:

Sources/SwiftSyntax/generated/raw/RawSyntaxNodes.swift

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19028,6 +19028,76 @@ public struct RawSuperRefExprSyntax: RawExprSyntaxNodeProtocol {
1902819028
}
1902919029
}
1903019030

19031+
@_spi(RawSyntax)
19032+
public struct RawSuppressedTypeSyntax: RawTypeSyntaxNodeProtocol {
19033+
@_spi(RawSyntax)
19034+
public var layoutView: RawSyntaxLayoutView {
19035+
return raw.layoutView!
19036+
}
19037+
19038+
public static func isKindOf(_ raw: RawSyntax) -> Bool {
19039+
return raw.kind == .suppressedType
19040+
}
19041+
19042+
public var raw: RawSyntax
19043+
19044+
init(raw: RawSyntax) {
19045+
precondition(Self.isKindOf(raw))
19046+
self.raw = raw
19047+
}
19048+
19049+
private init(unchecked raw: RawSyntax) {
19050+
self.raw = raw
19051+
}
19052+
19053+
public init?<Node: RawSyntaxNodeProtocol>(_ other: Node) {
19054+
guard Self.isKindOf(other.raw) else {
19055+
return nil
19056+
}
19057+
self.init(unchecked: other.raw)
19058+
}
19059+
19060+
public init(
19061+
_ unexpectedBeforeWithoutTilde: RawUnexpectedNodesSyntax? = nil,
19062+
withoutTilde: RawTokenSyntax,
19063+
_ unexpectedBetweenWithoutTildeAndPatternType: RawUnexpectedNodesSyntax? = nil,
19064+
patternType: RawTypeSyntax,
19065+
_ unexpectedAfterPatternType: RawUnexpectedNodesSyntax? = nil,
19066+
arena: __shared SyntaxArena
19067+
) {
19068+
let raw = RawSyntax.makeLayout(
19069+
kind: .suppressedType, uninitializedCount: 5, arena: arena) { layout in
19070+
layout.initialize(repeating: nil)
19071+
layout[0] = unexpectedBeforeWithoutTilde?.raw
19072+
layout[1] = withoutTilde.raw
19073+
layout[2] = unexpectedBetweenWithoutTildeAndPatternType?.raw
19074+
layout[3] = patternType.raw
19075+
layout[4] = unexpectedAfterPatternType?.raw
19076+
}
19077+
self.init(unchecked: raw)
19078+
}
19079+
19080+
public var unexpectedBeforeWithoutTilde: RawUnexpectedNodesSyntax? {
19081+
layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:))
19082+
}
19083+
19084+
public var withoutTilde: RawTokenSyntax {
19085+
layoutView.children[1].map(RawTokenSyntax.init(raw:))!
19086+
}
19087+
19088+
public var unexpectedBetweenWithoutTildeAndPatternType: RawUnexpectedNodesSyntax? {
19089+
layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:))
19090+
}
19091+
19092+
public var patternType: RawTypeSyntax {
19093+
layoutView.children[3].map(RawTypeSyntax.init(raw:))!
19094+
}
19095+
19096+
public var unexpectedAfterPatternType: RawUnexpectedNodesSyntax? {
19097+
layoutView.children[4].map(RawUnexpectedNodesSyntax.init(raw:))
19098+
}
19099+
}
19100+
1903119101
@_spi(RawSyntax)
1903219102
public struct RawSwitchCaseLabelSyntax: RawSyntaxNodeProtocol {
1903319103
@_spi(RawSyntax)
@@ -20905,7 +20975,7 @@ public struct RawTypeSyntax: RawTypeSyntaxNodeProtocol {
2090520975

2090620976
public static func isKindOf(_ raw: RawSyntax) -> Bool {
2090720977
switch raw.kind {
20908-
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .tupleType:
20978+
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .suppressedType, .tupleType:
2090920979
return true
2091020980
default:
2091120981
return false

0 commit comments

Comments
 (0)