Skip to content

Commit 00b0ce7

Browse files
authored
Merge pull request #1588 from kavon/better-parsing-suppression
redo 'without' operator parsing so it's within parseType to match C++ parser
2 parents cef8815 + d141df5 commit 00b0ce7

File tree

20 files changed

+391
-93
lines changed

20 files changed

+391
-93
lines changed

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,14 +1028,6 @@ public let DECL_NODES: [Node] = [
10281028
"WithTrailingComma"
10291029
],
10301030
children: [
1031-
/// Indicates whether the 'without' operator was applied to the type to
1032-
/// indicate the suppression of implicit conformance to this type.
1033-
/// This child stores the token representing the 'without' operator.
1034-
Child(
1035-
name: "WithoutTilde",
1036-
kind: .token(choices: [.token(tokenKind: "PrefixOperatorToken")]),
1037-
isOptional: true
1038-
),
10391031
Child(
10401032
name: "TypeName",
10411033
kind: .node(kind: "Type")

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/Nominals.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ extension Parser {
292292
var keepGoing: RawTokenSyntax? = nil
293293
var loopProgress = LoopProgressCondition()
294294
repeat {
295-
var withoutToken: RawTokenSyntax? = nil
296295
let type: RawTypeSyntax
297296
if let classKeyword = self.consume(if: .keyword(.class)) {
298297
type = RawTypeSyntax(
@@ -302,14 +301,12 @@ extension Parser {
302301
)
303302
)
304303
} else {
305-
withoutToken = self.consumeIfContextualPunctuator("~", remapping: .prefixOperator)
306304
type = self.parseType()
307305
}
308306

309307
keepGoing = self.consume(if: .comma)
310308
elements.append(
311309
RawInheritedTypeSyntax(
312-
withoutTilde: withoutToken,
313310
typeName: type,
314311
trailingComma: keepGoing,
315312
arena: self.arena

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: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,12 +1712,8 @@ internal func childName(_ keyPath: AnyKeyPath) -> String? {
17121712
return "rightOperand"
17131713
case \InfixOperatorExprSyntax.unexpectedAfterRightOperand:
17141714
return "unexpectedAfterRightOperand"
1715-
case \InheritedTypeSyntax.unexpectedBeforeWithoutTilde:
1716-
return "unexpectedBeforeWithoutTilde"
1717-
case \InheritedTypeSyntax.withoutTilde:
1718-
return "withoutTilde"
1719-
case \InheritedTypeSyntax.unexpectedBetweenWithoutTildeAndTypeName:
1720-
return "unexpectedBetweenWithoutTildeAndTypeName"
1715+
case \InheritedTypeSyntax.unexpectedBeforeTypeName:
1716+
return "unexpectedBeforeTypeName"
17211717
case \InheritedTypeSyntax.typeName:
17221718
return "typeName"
17231719
case \InheritedTypeSyntax.unexpectedBetweenTypeNameAndTrailingComma:
@@ -2872,6 +2868,16 @@ internal func childName(_ keyPath: AnyKeyPath) -> String? {
28722868
return "superKeyword"
28732869
case \SuperRefExprSyntax.unexpectedAfterSuperKeyword:
28742870
return "unexpectedAfterSuperKeyword"
2871+
case \SuppressedTypeSyntax.unexpectedBeforeWithoutTilde:
2872+
return "unexpectedBeforeWithoutTilde"
2873+
case \SuppressedTypeSyntax.withoutTilde:
2874+
return "withoutTilde"
2875+
case \SuppressedTypeSyntax.unexpectedBetweenWithoutTildeAndPatternType:
2876+
return "unexpectedBetweenWithoutTildeAndPatternType"
2877+
case \SuppressedTypeSyntax.patternType:
2878+
return "patternType"
2879+
case \SuppressedTypeSyntax.unexpectedAfterPatternType:
2880+
return "unexpectedAfterPatternType"
28752881
case \SwitchCaseLabelSyntax.unexpectedBeforeCaseKeyword:
28762882
return "unexpectedBeforeCaseKeyword"
28772883
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:

0 commit comments

Comments
 (0)