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),

0 commit comments

Comments
 (0)