You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxBaseNodesFile.swift
+25-27Lines changed: 25 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -79,9 +79,6 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
79
79
/// Checks if the current syntax node can be upcast to its base node type (``\#(node.kind.syntaxType)``).
80
80
///
81
81
/// - 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.
85
82
@available(*, deprecated, message: "This cast will always succeed")
86
83
public func `is`(_ syntaxType: \#(node.kind.syntaxType).Type) -> Bool {
87
84
return true
@@ -90,9 +87,6 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
90
87
/// Attempts to upcast the current syntax node to its base node type (``\#(node.kind.syntaxType)``).
91
88
///
92
89
/// - 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.
96
90
@available(*, deprecated, message: "Use `\#(node.kind.syntaxType).init` for upcasting")
97
91
public func `as`(_ syntaxType: \#(node.kind.syntaxType).Type) -> \#(node.kind.syntaxType)? {
98
92
return \#(node.kind.syntaxType)(self)
@@ -101,46 +95,50 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
101
95
/// Force-upcast the current syntax node to its base node type (``\#(node.kind.syntaxType)``).
102
96
///
103
97
/// - 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.
107
98
@available(*, deprecated, message: "Use `\#(node.kind.syntaxType).init` for upcasting")
108
99
public func cast(_ syntaxType: \#(node.kind.syntaxType).Type) -> \#(node.kind.syntaxType){
109
100
return \#(node.kind.syntaxType)(self)
110
101
}
111
102
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)``.
113
105
///
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.
115
107
///
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")
119
112
public func `is`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
120
-
return false
113
+
return self.as(syntaxType) != nil
121
114
}
122
115
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)``.
124
118
///
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.
126
120
///
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")
130
125
public func `as`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S? {
131
-
return nil
126
+
return S.init(self)
132
127
}
133
128
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)``.
135
131
///
136
132
/// - Returns: This method will always trigger a runtime crash and never return.
137
133
///
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")
142
140
public func cast<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S {
143
-
fatalError("\(Self.self) cannot be cast to \(S.self)")
0 commit comments