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
+33Lines changed: 33 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -72,6 +72,39 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
72
72
return self.as(S.self)!
73
73
}
74
74
75
+
/// Checks if the current syntax node can be upcast to its base node type (``\#(node.kind.syntaxType)``).
76
+
///
77
+
/// - Returns: `true` since the node can always be upcast to its base node.
78
+
///
79
+
/// - Note: This method overloads the general `is` method and is marked deprecated to produce a warning
80
+
/// informing the user that the upcast will always succeed.
81
+
@available(*, deprecated, message: "This cast will always succeed")
82
+
public func `is`(_ syntaxType: \#(node.kind.syntaxType).Type) -> Bool {
83
+
return true
84
+
}
85
+
86
+
/// Attempts to upcast the current syntax node to its base node type (``\#(node.kind.syntaxType)``).
87
+
///
88
+
/// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type.
89
+
///
90
+
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
91
+
/// informing the user the upcast should be performed using the target base node's initializer.
92
+
@available(*, deprecated, message: "Use `\#(node.kind.syntaxType).init` for upcasting")
93
+
public func `as`(_ syntaxType: \#(node.kind.syntaxType).Type) -> \#(node.kind.syntaxType)? {
94
+
return \#(node.kind.syntaxType)(self)
95
+
}
96
+
97
+
/// Force-upcast the current syntax node to its base node type (``\#(node.kind.syntaxType)``).
98
+
///
99
+
/// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type.
100
+
///
101
+
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
102
+
/// informing the user the upcast should be performed using the target base node's initializer.
103
+
@available(*, deprecated, message: "Use `\#(node.kind.syntaxType).init` for upcasting")
104
+
public func cast(_ syntaxType: \#(node.kind.syntaxType).Type) -> \#(node.kind.syntaxType){
105
+
return \#(node.kind.syntaxType)(self)
106
+
}
107
+
75
108
/// 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)``.
76
109
///
77
110
/// - Returns: `false` since the node can not be cast to the node type from different base node protocol hierarchy than ``\#(node.kind.protocolType)``.
Copy file name to clipboardExpand all lines: Release Notes/510.md
+7-2Lines changed: 7 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -29,12 +29,17 @@
29
29
- Description: Syntax nodes that do not act as base nodes for other syntax types have the casting methods marked as deprecated. This prevents unsafe type-casting by issuing deprecation warnings for methods that will always result in failed casts.
- Description: `is`, `as`, and `cast` overloads on `SyntaxProtocol` with same-type conversions are marked as deprecated. The deprecated methods emit a warning indicating the cast will always succeed.
- Description: `is`, `as`, and `cast` methods on base node protocols with base-type conversions are marked as deprecated. The deprecated methods emit a warning that informs the developer that the cast will always succeed and should be done using the base node's initializer.
0 commit comments