Skip to content

Commit 0c0bf90

Browse files
committed
Add deprecated warnings for same-type casts in SyntaxProtocol
- Implement overloads for `is`, `as`, and `cast` methods in SyntaxProtocol to handle same-type conversions. - Mark these overloaded methods as deprecated with a warning message that the cast will always succeed. - Update the codebase to eliminate a redundant casts.
1 parent c2bdd2e commit 0c0bf90

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Release Notes/510.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
- Issue: https://github.com/apple/swift-syntax/issues/2092
3131
- Pull Request: https://github.com/apple/swift-syntax/pull/2108
3232

33+
- Same-Type Casts
34+
- 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.
35+
- Issue: https://github.com/apple/swift-syntax/issues/2092
36+
- Pull Request: https://github.com/apple/swift-syntax/pull/2108
37+
3338
## API-Incompatible Changes
3439

3540

Sources/SwiftSyntax/Syntax.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,20 +213,53 @@ public extension SyntaxProtocol {
213213
return self.as(syntaxType) != nil
214214
}
215215

216+
/// Checks if the current syntax node can be cast to its own type.
217+
///
218+
/// - Returns: `true` since the node is already of its own type.
219+
///
220+
/// - Note: This method overloads the general `is` method and is marked as deprecated to produce a warning,
221+
/// informing the user that the cast will always succeed.
222+
@available(*, deprecated, message: "This cast will always succeed")
223+
func `is`(_ syntaxType: Self.Type) -> Bool {
224+
return true
225+
}
226+
216227
/// Attempts to cast the current syntax node to a given specialized syntax type.
217228
///
218229
/// - Returns: An instance of the specialized type, or `nil` if the cast fails.
219230
func `as`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S? {
220231
return S.init(self)
221232
}
222233

234+
/// Casts the current syntax node to its own type.
235+
///
236+
/// - Returns: The current syntax node.
237+
///
238+
/// - Note: This method overloads the general `as` method and is marked as deprecated to produce a warning,
239+
/// informing the user that the cast will always succeed.
240+
@available(*, deprecated, message: "This cast will always succeed")
241+
func `as`(_ syntaxType: Self.Type) -> Self? {
242+
return self
243+
}
244+
223245
/// Force-casts the current syntax node to a given specialized syntax type.
224246
///
225247
/// - Returns: An instance of the specialized type.
226248
/// - Warning: This function will crash if the cast is not possible. Use `as` to safely attempt a cast.
227249
func cast<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S {
228250
return self.as(S.self)!
229251
}
252+
253+
/// Force-casts the current syntax node to its own type.
254+
///
255+
/// - Returns: The current syntax node.
256+
///
257+
/// - Note: This method overloads the general `cast` method and is marked as deprecated to produce a warning,
258+
/// informing the user that the cast will always succeed.
259+
@available(*, deprecated, message: "This cast will always succeed")
260+
func cast(_ syntaxType: Self.Type) -> Self {
261+
return self
262+
}
230263
}
231264

232265
// MARK: Modifying

0 commit comments

Comments
 (0)