Skip to content

Commit 94daf84

Browse files
committed
Casts to children of SyntaxProtocol are not always invalid
Update these overrides so they don't always fail.
1 parent 8d5fab7 commit 94daf84

File tree

2 files changed

+160
-162
lines changed

2 files changed

+160
-162
lines changed

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxBaseNodesFile.swift

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
7979
/// Checks if the current syntax node can be upcast to its base node type (``\#(node.kind.syntaxType)``).
8080
///
8181
/// - Returns: `true` since the node can always be upcast to its base node.
82-
///
83-
/// - Note: This method overloads the general `is` method and is marked deprecated to produce a warning
84-
/// informing the user that the upcast will always succeed.
8582
@available(*, deprecated, message: "This cast will always succeed")
8683
public func `is`(_ syntaxType: \#(node.kind.syntaxType).Type) -> Bool {
8784
return true
@@ -90,9 +87,6 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
9087
/// Attempts to upcast the current syntax node to its base node type (``\#(node.kind.syntaxType)``).
9188
///
9289
/// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type.
93-
///
94-
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
95-
/// informing the user the upcast should be performed using the target base node's initializer.
9690
@available(*, deprecated, message: "Use `\#(node.kind.syntaxType).init` for upcasting")
9791
public func `as`(_ syntaxType: \#(node.kind.syntaxType).Type) -> \#(node.kind.syntaxType)? {
9892
return \#(node.kind.syntaxType)(self)
@@ -101,46 +95,50 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
10195
/// Force-upcast the current syntax node to its base node type (``\#(node.kind.syntaxType)``).
10296
///
10397
/// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type.
104-
///
105-
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
106-
/// informing the user the upcast should be performed using the target base node's initializer.
10798
@available(*, deprecated, message: "Use `\#(node.kind.syntaxType).init` for upcasting")
10899
public func cast(_ syntaxType: \#(node.kind.syntaxType).Type) -> \#(node.kind.syntaxType) {
109100
return \#(node.kind.syntaxType)(self)
110101
}
111102
112-
/// Checks if the current syntax node can be cast to a given node type from the different base node protocol hierarchy than ``\#(node.kind.protocolType)``.
103+
/// Checks if the current syntax node can be cast to a given node type from a base node protocol hierarchy other
104+
/// than ``\#(node.kind.protocolType)``.
113105
///
114-
/// - Returns: `false` since the node can not be cast to the node type from different base node protocol hierarchy than ``\#(node.kind.protocolType)``.
106+
/// - Returns: `true` if the node can be cast, `false` otherwise.
115107
///
116-
/// - Note: This method overloads the general `is` method and is marked as deprecated to produce a warning,
117-
/// informing the user that the cast will always fail.
118-
@available(*, deprecated, message: "This cast will always fail")
108+
/// - Note: This generally does not make sense to call - if in a generic ``SyntaxProtocol`` context, use
109+
/// ``\#(node.kind.protocolType)`` instead. Otherwise this must be a node outside of ``\#(node.kind.protocolType)``,
110+
/// in which case this call will always fail.
111+
@available(*, deprecated, message: "Type should be part of the '\#(node.kind.protocolType)' hierarchy")
119112
public func `is`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
120-
return false
113+
return self.as(syntaxType) != nil
121114
}
122115
123-
/// Attempts to cast the current syntax node to a given node type from the different base node protocol hierarchy than ``\#(node.kind.protocolType)``.
116+
/// Attempts to cast the current syntax node to a given node type from the a base node protocol hierarchy other than
117+
/// ``\#(node.kind.protocolType)``.
124118
///
125-
/// - Returns: `nil` since the node can not be cast to the node type from different base node protocol hierarchy than ``\#(node.kind.protocolType)``.
119+
/// - Returns: An instance of the specialized type, or `nil` if the cast fails.
126120
///
127-
/// - Note: This method overloads the general `as` method and is marked as deprecated to produce a warning,
128-
/// informing the user that the cast will always fail.
129-
@available(*, deprecated, message: "This cast will always fail")
121+
/// - Note: This generally does not make sense to call - if in a generic ``SyntaxProtocol`` context, use
122+
/// ``\#(node.kind.protocolType)`` instead. Otherwise this must be a node outside of ``\#(node.kind.protocolType)``,
123+
/// in which case this cast will always return `nil`.
124+
@available(*, deprecated, message: "Type should be part of the '\#(node.kind.protocolType)' hierarchy")
130125
public func `as`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S? {
131-
return nil
126+
return S.init(self)
132127
}
133128
134-
/// Force-casts the current syntax node to a given node type from the different base node protocol hierarchy than ``\#(node.kind.protocolType)``.
129+
/// Force-casts the current syntax node to a given node type from a base node protocol hierarchy other than
130+
/// ``\#(node.kind.protocolType)``.
135131
///
136132
/// - Returns: This method will always trigger a runtime crash and never return.
137133
///
138-
/// - Note: This method overloads the general `cast` method and is marked as deprecated to produce a warning,
139-
/// informing the user that the cast will always fail.
140-
/// - Warning: Invoking this method will lead to a fatal error.
141-
@available(*, deprecated, message: "This cast will always fail")
134+
/// - Warning: This function will crash if the cast is not possible. Use `as` to safely attempt a cast.
135+
///
136+
/// - Note: This generally does not make sense to call - if in a generic ``SyntaxProtocol`` context, use
137+
/// ``\#(node.kind.protocolType)`` instead. Otherwise this must be a node outside of ``\#(node.kind.protocolType)``,
138+
/// in which case this cast will always crash.
139+
@available(*, deprecated, message: "Type should be part of the '\#(node.kind.protocolType)' hierarchy")
142140
public func cast<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S {
143-
fatalError("\(Self.self) cannot be cast to \(S.self)")
141+
return self.as(S.self)!
144142
}
145143
}
146144
"""#

0 commit comments

Comments
 (0)