Skip to content

[5.9🍒] without operator using ~ for implicit conformance suppression #1560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,23 @@ public let TYPE_NODES: [Node] = [
]
),

// suppressed-type -> '~' type
Node(
name: "SuppressedType",
nameForDiagnostics: "suppressed type conformance",
kind: "Type",
children: [
Child(
name: "WithoutTilde",
kind: .token(choices: [.token(tokenKind: "PrefixOperatorToken")])
),
Child(
name: "PatternType",
kind: .node(kind: "Type")
),
]
),

// pack-expansion-type -> type '...'
Node(
name: "PackExpansionType",
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ extension Parser {
let unexpectedBeforeInherited: RawUnexpectedNodesSyntax?
let inherited: RawTypeSyntax?
if colon != nil {
if self.at(.identifier, .keyword(.protocol), .keyword(.Any)) {
if self.at(.identifier, .keyword(.protocol), .keyword(.Any)) || self.atContextualPunctuator("~") {
unexpectedBeforeInherited = nil
inherited = self.parseType()
} else if let classKeyword = self.consume(if: .keyword(.class)) {
Expand Down
12 changes: 12 additions & 0 deletions Sources/SwiftParser/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ extension Parser {
)
}

// Parse without operator preceding a type '~ T'.
if let withoutTilde = self.consumeIfContextualPunctuator("~", remapping: .prefixOperator) {
let type = self.parseTypeScalar(misplacedSpecifiers: misplacedSpecifiers)
return RawTypeSyntax(
RawSuppressedTypeSyntax(
withoutTilde: withoutTilde,
patternType: type,
arena: self.arena
)
)
}

return self.parseTypeScalar(misplacedSpecifiers: misplacedSpecifiers)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ extension SyntaxKind {
return "subscript"
case .subscriptExpr:
return "subscript"
case .suppressedType:
return "suppressed type conformance"
case .switchCase:
return "switch case"
case .switchExpr:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
- <doc:SwiftSyntax/PackExpansionTypeSyntax>
- <doc:SwiftSyntax/PackReferenceTypeSyntax>
- <doc:SwiftSyntax/SimpleTypeIdentifierSyntax>
- <doc:SwiftSyntax/SuppressedTypeSyntax>
- <doc:SwiftSyntax/TupleTypeSyntax>

### Collections
Expand Down
10 changes: 10 additions & 0 deletions Sources/SwiftSyntax/generated/ChildNameForKeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2869,6 +2869,16 @@ public func childName(_ keyPath: AnyKeyPath) -> String? {
return "superKeyword"
case \SuperRefExprSyntax.unexpectedAfterSuperKeyword:
return "unexpectedAfterSuperKeyword"
case \SuppressedTypeSyntax.unexpectedBeforeWithoutTilde:
return "unexpectedBeforeWithoutTilde"
case \SuppressedTypeSyntax.withoutTilde:
return "withoutTilde"
case \SuppressedTypeSyntax.unexpectedBetweenWithoutTildeAndPatternType:
return "unexpectedBetweenWithoutTildeAndPatternType"
case \SuppressedTypeSyntax.patternType:
return "patternType"
case \SuppressedTypeSyntax.unexpectedAfterPatternType:
return "unexpectedAfterPatternType"
case \SwitchCaseLabelSyntax.unexpectedBeforeCaseKeyword:
return "unexpectedBeforeCaseKeyword"
case \SwitchCaseLabelSyntax.caseKeyword:
Expand Down
8 changes: 8 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxAnyVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1869,6 +1869,14 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
visitAnyPost(node._syntaxNode)
}

override open func visit(_ node: SuppressedTypeSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}

override open func visitPost(_ node: SuppressedTypeSyntax) {
visitAnyPost(node._syntaxNode)
}

override open func visit(_ node: SwitchCaseLabelSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/SwiftSyntax/generated/SyntaxBaseNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {

public init?<S: SyntaxProtocol>(_ node: S) {
switch node.raw.kind {
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .tupleType:
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .suppressedType, .tupleType:
self._syntaxNode = node._syntaxNode
default:
return nil
Expand All @@ -622,7 +622,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
/// is undefined.
internal init(_ data: SyntaxData) {
switch data.raw.kind {
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .tupleType:
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .suppressedType, .tupleType:
break
default:
preconditionFailure("Unable to create TypeSyntax from \(data.raw.kind)")
Expand Down Expand Up @@ -674,6 +674,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
.node(PackExpansionTypeSyntax.self),
.node(PackReferenceTypeSyntax.self),
.node(SimpleTypeIdentifierSyntax.self),
.node(SuppressedTypeSyntax.self),
.node(TupleTypeSyntax.self)
])
}
Expand Down Expand Up @@ -910,6 +911,7 @@ extension Syntax {
.node(SubscriptDeclSyntax.self),
.node(SubscriptExprSyntax.self),
.node(SuperRefExprSyntax.self),
.node(SuppressedTypeSyntax.self),
.node(SwitchCaseLabelSyntax.self),
.node(SwitchCaseListSyntax.self),
.node(SwitchCaseSyntax.self),
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public enum SyntaxEnum {
case subscriptDecl(SubscriptDeclSyntax)
case subscriptExpr(SubscriptExprSyntax)
case superRefExpr(SuperRefExprSyntax)
case suppressedType(SuppressedTypeSyntax)
case switchCaseLabel(SwitchCaseLabelSyntax)
case switchCaseList(SwitchCaseListSyntax)
case switchCase(SwitchCaseSyntax)
Expand Down Expand Up @@ -746,6 +747,8 @@ public extension Syntax {
return .subscriptExpr(SubscriptExprSyntax(self)!)
case .superRefExpr:
return .superRefExpr(SuperRefExprSyntax(self)!)
case .suppressedType:
return .suppressedType(SuppressedTypeSyntax(self)!)
case .switchCaseLabel:
return .switchCaseLabel(SwitchCaseLabelSyntax(self)!)
case .switchCaseList:
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public enum SyntaxKind {
case subscriptDecl
case subscriptExpr
case superRefExpr
case suppressedType
case switchCaseLabel
case switchCaseList
case switchCase
Expand Down Expand Up @@ -861,6 +862,8 @@ public enum SyntaxKind {
return SubscriptExprSyntax.self
case .superRefExpr:
return SuperRefExprSyntax.self
case .suppressedType:
return SuppressedTypeSyntax.self
case .switchCaseLabel:
return SwitchCaseLabelSyntax.self
case .switchCaseList:
Expand Down
25 changes: 25 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,13 @@ open class SyntaxRewriter {
return ExprSyntax(visitChildren(node))
}

/// Visit a `SuppressedTypeSyntax`.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
open func visit(_ node: SuppressedTypeSyntax) -> TypeSyntax {
return TypeSyntax(visitChildren(node))
}

/// Visit a `SwitchCaseLabelSyntax`.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
Expand Down Expand Up @@ -5153,6 +5160,20 @@ open class SyntaxRewriter {
return Syntax(visit(node))
}

/// Implementation detail of visit(_:). Do not call directly.
private func visitImplSuppressedTypeSyntax(_ data: SyntaxData) -> Syntax {
let node = SuppressedTypeSyntax(data)
// Accessing _syntaxNode directly is faster than calling Syntax(node)
visitPre(node._syntaxNode)
defer {
visitPost(node._syntaxNode)
}
if let newNode = visitAny(node._syntaxNode) {
return newNode
}
return Syntax(visit(node))
}

/// Implementation detail of visit(_:). Do not call directly.
private func visitImplSwitchCaseLabelSyntax(_ data: SyntaxData) -> Syntax {
let node = SwitchCaseLabelSyntax(data)
Expand Down Expand Up @@ -6223,6 +6244,8 @@ open class SyntaxRewriter {
return visitImplSubscriptExprSyntax
case .superRefExpr:
return visitImplSuperRefExprSyntax
case .suppressedType:
return visitImplSuppressedTypeSyntax
case .switchCaseLabel:
return visitImplSwitchCaseLabelSyntax
case .switchCaseList:
Expand Down Expand Up @@ -6769,6 +6792,8 @@ open class SyntaxRewriter {
return visitImplSubscriptExprSyntax(data)
case .superRefExpr:
return visitImplSuperRefExprSyntax(data)
case .suppressedType:
return visitImplSuppressedTypeSyntax(data)
case .switchCaseLabel:
return visitImplSwitchCaseLabelSyntax(data)
case .switchCaseList:
Expand Down
14 changes: 14 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,11 @@ public protocol SyntaxTransformVisitor {
/// - Returns: the sum of whatever the child visitors return.
func visit(_ node: SuperRefExprSyntax) -> ResultType

/// Visiting `SuppressedTypeSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: the sum of whatever the child visitors return.
func visit(_ node: SuppressedTypeSyntax) -> ResultType

/// Visiting `SwitchCaseLabelSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: the sum of whatever the child visitors return.
Expand Down Expand Up @@ -2954,6 +2959,13 @@ extension SyntaxTransformVisitor {
visitAny(Syntax(node))
}

/// Visiting `SuppressedTypeSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: nil by default.
public func visit(_ node: SuppressedTypeSyntax) -> ResultType {
visitAny(Syntax(node))
}

/// Visiting `SwitchCaseLabelSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: nil by default.
Expand Down Expand Up @@ -3699,6 +3711,8 @@ extension SyntaxTransformVisitor {
return visit(derived)
case .superRefExpr(let derived):
return visit(derived)
case .suppressedType(let derived):
return visit(derived)
case .switchCaseLabel(let derived):
return visit(derived)
case .switchCaseList(let derived):
Expand Down
25 changes: 25 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2758,6 +2758,18 @@ open class SyntaxVisitor {
open func visitPost(_ node: SuperRefExprSyntax) {
}

/// Visiting `SuppressedTypeSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: how should we continue visiting.
open func visit(_ node: SuppressedTypeSyntax) -> SyntaxVisitorContinueKind {
return .visitChildren
}

/// The function called after visiting `SuppressedTypeSyntax` and its descendents.
/// - node: the node we just finished visiting.
open func visitPost(_ node: SuppressedTypeSyntax) {
}

/// Visiting `SwitchCaseLabelSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: how should we continue visiting.
Expand Down Expand Up @@ -5759,6 +5771,17 @@ open class SyntaxVisitor {
visitPost(node)
}

/// Implementation detail of doVisit(_:_:). Do not call directly.
private func visitImplSuppressedTypeSyntax(_ data: SyntaxData) {
let node = SuppressedTypeSyntax(data)
let needsChildren = (visit(node) == .visitChildren)
// Avoid calling into visitChildren if possible.
if needsChildren && !node.raw.layoutView!.children.isEmpty {
visitChildren(node)
}
visitPost(node)
}

/// Implementation detail of doVisit(_:_:). Do not call directly.
private func visitImplSwitchCaseLabelSyntax(_ data: SyntaxData) {
let node = SwitchCaseLabelSyntax(data)
Expand Down Expand Up @@ -6671,6 +6694,8 @@ open class SyntaxVisitor {
visitImplSubscriptExprSyntax(data)
case .superRefExpr:
visitImplSuperRefExprSyntax(data)
case .suppressedType:
visitImplSuppressedTypeSyntax(data)
case .switchCaseLabel:
visitImplSwitchCaseLabelSyntax(data)
case .switchCaseList:
Expand Down
72 changes: 71 additions & 1 deletion Sources/SwiftSyntax/generated/raw/RawSyntaxNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19028,6 +19028,76 @@ public struct RawSuperRefExprSyntax: RawExprSyntaxNodeProtocol {
}
}

@_spi(RawSyntax)
public struct RawSuppressedTypeSyntax: RawTypeSyntaxNodeProtocol {
@_spi(RawSyntax)
public var layoutView: RawSyntaxLayoutView {
return raw.layoutView!
}

public static func isKindOf(_ raw: RawSyntax) -> Bool {
return raw.kind == .suppressedType
}

public var raw: RawSyntax

init(raw: RawSyntax) {
precondition(Self.isKindOf(raw))
self.raw = raw
}

private init(unchecked raw: RawSyntax) {
self.raw = raw
}

public init?<Node: RawSyntaxNodeProtocol>(_ other: Node) {
guard Self.isKindOf(other.raw) else {
return nil
}
self.init(unchecked: other.raw)
}

public init(
_ unexpectedBeforeWithoutTilde: RawUnexpectedNodesSyntax? = nil,
withoutTilde: RawTokenSyntax,
_ unexpectedBetweenWithoutTildeAndPatternType: RawUnexpectedNodesSyntax? = nil,
patternType: RawTypeSyntax,
_ unexpectedAfterPatternType: RawUnexpectedNodesSyntax? = nil,
arena: __shared SyntaxArena
) {
let raw = RawSyntax.makeLayout(
kind: .suppressedType, uninitializedCount: 5, arena: arena) { layout in
layout.initialize(repeating: nil)
layout[0] = unexpectedBeforeWithoutTilde?.raw
layout[1] = withoutTilde.raw
layout[2] = unexpectedBetweenWithoutTildeAndPatternType?.raw
layout[3] = patternType.raw
layout[4] = unexpectedAfterPatternType?.raw
}
self.init(unchecked: raw)
}

public var unexpectedBeforeWithoutTilde: RawUnexpectedNodesSyntax? {
layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:))
}

public var withoutTilde: RawTokenSyntax {
layoutView.children[1].map(RawTokenSyntax.init(raw:))!
}

public var unexpectedBetweenWithoutTildeAndPatternType: RawUnexpectedNodesSyntax? {
layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:))
}

public var patternType: RawTypeSyntax {
layoutView.children[3].map(RawTypeSyntax.init(raw:))!
}

public var unexpectedAfterPatternType: RawUnexpectedNodesSyntax? {
layoutView.children[4].map(RawUnexpectedNodesSyntax.init(raw:))
}
}

@_spi(RawSyntax)
public struct RawSwitchCaseLabelSyntax: RawSyntaxNodeProtocol {
@_spi(RawSyntax)
Expand Down Expand Up @@ -20905,7 +20975,7 @@ public struct RawTypeSyntax: RawTypeSyntaxNodeProtocol {

public static func isKindOf(_ raw: RawSyntax) -> Bool {
switch raw.kind {
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .tupleType:
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .suppressedType, .tupleType:
return true
default:
return false
Expand Down
Loading