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
+27-28Lines changed: 27 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,7 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
71
71
/// Force-casts the current syntax node to a given specialized syntax type.
72
72
///
73
73
/// - Returns: An instance of the specialized type.
74
+
///
74
75
/// - Warning: This function will crash if the cast is not possible. Use `as` to safely attempt a cast.
75
76
public func cast<S: \#(node.kind.protocolType)>(_ syntaxType: S.Type) -> S {
76
77
return self.as(S.self)!
@@ -79,9 +80,6 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
79
80
/// Checks if the current syntax node can be upcast to its base node type (``\#(node.kind.syntaxType)``).
80
81
///
81
82
/// - 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
83
@available(*, deprecated, message: "This cast will always succeed")
86
84
public func `is`(_ syntaxType: \#(node.kind.syntaxType).Type) -> Bool {
87
85
return true
@@ -90,9 +88,6 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
90
88
/// Attempts to upcast the current syntax node to its base node type (``\#(node.kind.syntaxType)``).
91
89
///
92
90
/// - 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
91
@available(*, deprecated, message: "Use `\#(node.kind.syntaxType).init` for upcasting")
97
92
public func `as`(_ syntaxType: \#(node.kind.syntaxType).Type) -> \#(node.kind.syntaxType)? {
98
93
return \#(node.kind.syntaxType)(self)
@@ -101,46 +96,50 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
101
96
/// Force-upcast the current syntax node to its base node type (``\#(node.kind.syntaxType)``).
102
97
///
103
98
/// - 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
99
@available(*, deprecated, message: "Use `\#(node.kind.syntaxType).init` for upcasting")
108
100
public func cast(_ syntaxType: \#(node.kind.syntaxType).Type) -> \#(node.kind.syntaxType){
109
101
return \#(node.kind.syntaxType)(self)
110
102
}
111
103
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)``.
104
+
/// Checks if the current syntax node can be cast to a given node type from a base node protocol hierarchy other
105
+
/// than ``\#(node.kind.protocolType)``.
113
106
///
114
-
/// - Returns: `false` since the node can not be cast to the node type from different base node protocol hierarchy than ``\#(node.kind.protocolType)``.
107
+
/// - Returns: `true` if the node can be cast, `false` otherwise.
115
108
///
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")
109
+
/// - Note: In most cases, this is comparing a ``\#(node.kind.protocolType)`` to a node that is not a
110
+
/// ``\#(node.kind.protocolType)``, which will always fail. If the `syntaxType` argument is a generic type,
111
+
/// constrain it to ``\#(node.kind.protocolType)`` instead of ``SyntaxProtocol``.
112
+
@available(*, deprecated, message: "Type argument should be part of the '\#(node.kind.protocolType)' hierarchy")
119
113
public func `is`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
120
-
return false
114
+
return self.as(syntaxType) != nil
121
115
}
122
116
123
-
/// Attempts to cast the current syntax node to a given node type from the different base node protocol hierarchy than ``\#(node.kind.protocolType)``.
117
+
/// Attempts to cast the current syntax node to a given node type from the a base node protocol hierarchy other than
118
+
/// ``\#(node.kind.protocolType)``.
124
119
///
125
-
/// - Returns: `nil` since the node can not be cast to the node type from different base node protocol hierarchy than ``\#(node.kind.protocolType)``.
120
+
/// - Returns: An instance of the specialized type, or `nil` if the cast fails.
126
121
///
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")
122
+
/// - Note: In most cases, this is casting a ``\#(node.kind.protocolType)`` to a node that is not a
123
+
/// ``\#(node.kind.protocolType)``, which will always fail. If the `syntaxType` argument is a generic type,
124
+
/// constrain it to ``\#(node.kind.protocolType)`` instead of ``SyntaxProtocol``.
125
+
@available(*, deprecated, message: "Type argument should be part of the '\#(node.kind.protocolType)' hierarchy")
130
126
public func `as`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S? {
131
-
return nil
127
+
return S.init(self)
132
128
}
133
129
134
-
/// Force-casts the current syntax node to a given node type from the different base node protocol hierarchy than ``\#(node.kind.protocolType)``.
130
+
/// Force-casts the current syntax node to a given node type from a base node protocol hierarchy other than
131
+
/// ``\#(node.kind.protocolType)``.
132
+
///
133
+
/// - Returns: An instance of the specialized type.
135
134
///
136
-
/// - Returns: This method will always trigger a runtime crash and never return.
135
+
/// - Warning: This function will crash if the cast is not possible. Use `as` to safely attempt a cast.
137
136
///
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")
137
+
/// - Note: In most cases, this is casting a ``\#(node.kind.protocolType)`` to a node that is not a
138
+
/// ``\#(node.kind.protocolType)``, which will always fail. If the `syntaxType` argument is a generic type,
139
+
/// constrain it to ``\#(node.kind.protocolType)`` instead of ``SyntaxProtocol``.
140
+
@available(*, deprecated, message: "Type argument should be part of the '\#(node.kind.protocolType)' hierarchy")
142
141
public func cast<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S {
143
-
fatalError("\(Self.self) cannot be cast to \(S.self)")
0 commit comments