Skip to content

Commit d2945f8

Browse files
committed
Add deprecated warnings for base node casts in base node protocols
1 parent 0c0bf90 commit d2945f8

File tree

4 files changed

+253
-2
lines changed

4 files changed

+253
-2
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,39 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
7272
return self.as(S.self)!
7373
}
7474
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+
75108
/// 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)``.
76109
///
77110
/// - Returns: `false` since the node can not be cast to the node type from different base node protocol hierarchy than ``\#(node.kind.protocolType)``.

Release Notes/510.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@
2929
- 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.
3030
- Issue: https://github.com/apple/swift-syntax/issues/2092
3131
- Pull Request: https://github.com/apple/swift-syntax/pull/2108
32-
32+
3333
- Same-Type Casts
3434
- 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.
3535
- Issue: https://github.com/apple/swift-syntax/issues/2092
3636
- Pull Request: https://github.com/apple/swift-syntax/pull/2108
37-
37+
38+
- Base Node Casts
39+
- 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.
40+
- Issue: https://github.com/apple/swift-syntax/issues/2092
41+
- Pull Request: https://github.com/apple/swift-syntax/pull/2108
42+
3843
## API-Incompatible Changes
3944

4045

Sources/SwiftSyntax/Syntax.swift

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

216+
/// Checks if the current syntax node can be upcast to ``Syntax`` node.
217+
///
218+
/// - Returns: `true` since the node can always be upcast to ``Syntax`` node.
219+
///
220+
/// - Note: This method overloads the general `is` method and is marked deprecated to produce a warning
221+
/// informing the user that the upcast will always succeed.
222+
@available(*, deprecated, message: "This cast will always succeed")
223+
func `is`(_ syntaxType: Syntax.Type) -> Bool {
224+
return true
225+
}
226+
216227
/// Checks if the current syntax node can be cast to its own type.
217228
///
218229
/// - Returns: `true` since the node is already of its own type.
@@ -231,6 +242,17 @@ public extension SyntaxProtocol {
231242
return S.init(self)
232243
}
233244

245+
/// Attempts to upcast the current syntax node to ``Syntax`` node..
246+
///
247+
/// - Returns: The ``Syntax`` node created from the current syntax node, as the node can always be upcast to ``Syntax`` node.
248+
///
249+
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
250+
/// informing the user the upcast should be performed using the base node's initializer.
251+
@available(*, deprecated, message: "Use `Syntax.init` for upcasting.")
252+
func `as`(_ syntaxType: Syntax.Type) -> Syntax? {
253+
return Syntax(self)
254+
}
255+
234256
/// Casts the current syntax node to its own type.
235257
///
236258
/// - Returns: The current syntax node.
@@ -250,6 +272,17 @@ public extension SyntaxProtocol {
250272
return self.as(S.self)!
251273
}
252274

275+
/// Force-cast the current syntax node to ``Syntax`` node..
276+
///
277+
/// - Returns: The ``Syntax`` node created from the current syntax node, as the node can always be upcast to ``Syntax`` node.
278+
///
279+
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
280+
/// informing the user the upcast should be performed using the base node's initializer.
281+
@available(*, deprecated, message: "Use `Syntax.init` for upcasting.")
282+
func cast(_ syntaxType: Syntax.Type) -> Syntax {
283+
return Syntax(self)
284+
}
285+
253286
/// Force-casts the current syntax node to its own type.
254287
///
255288
/// - Returns: The current syntax node.

Sources/SwiftSyntax/generated/SyntaxBaseNodes.swift

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,42 @@ extension DeclSyntaxProtocol {
6363
}
6464

6565

66+
/// Checks if the current syntax node can be upcast to its base node type (``DeclSyntax``).
67+
///
68+
/// - Returns: `true` since the node can always be upcast to its base node.
69+
///
70+
/// - Note: This method overloads the general `is` method and is marked deprecated to produce a warning
71+
/// informing the user that the upcast will always succeed.
72+
@available(*, deprecated, message: "This cast will always succeed")
73+
public func `is`(_ syntaxType: DeclSyntax.Type) -> Bool {
74+
return true
75+
}
76+
77+
78+
/// Attempts to upcast the current syntax node to its base node type (``DeclSyntax``).
79+
///
80+
/// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type.
81+
///
82+
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
83+
/// informing the user the upcast should be performed using the target base node's initializer.
84+
@available(*, deprecated, message: "Use `DeclSyntax.init` for upcasting")
85+
public func `as`(_ syntaxType: DeclSyntax.Type) -> DeclSyntax? {
86+
return DeclSyntax(self)
87+
}
88+
89+
90+
/// Force-upcast the current syntax node to its base node type (``DeclSyntax``).
91+
///
92+
/// - 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+
@available(*, deprecated, message: "Use `DeclSyntax.init` for upcasting")
97+
public func cast(_ syntaxType: DeclSyntax.Type) -> DeclSyntax {
98+
return DeclSyntax(self)
99+
}
100+
101+
66102
/// Checks if the current syntax node can be cast to a given node type from the different base node protocol hierarchy than ``DeclSyntaxProtocol``.
67103
///
68104
/// - Returns: `false` since the node can not be cast to the node type from different base node protocol hierarchy than ``DeclSyntaxProtocol``.
@@ -315,6 +351,42 @@ extension ExprSyntaxProtocol {
315351
}
316352

317353

354+
/// Checks if the current syntax node can be upcast to its base node type (``ExprSyntax``).
355+
///
356+
/// - Returns: `true` since the node can always be upcast to its base node.
357+
///
358+
/// - Note: This method overloads the general `is` method and is marked deprecated to produce a warning
359+
/// informing the user that the upcast will always succeed.
360+
@available(*, deprecated, message: "This cast will always succeed")
361+
public func `is`(_ syntaxType: ExprSyntax.Type) -> Bool {
362+
return true
363+
}
364+
365+
366+
/// Attempts to upcast the current syntax node to its base node type (``ExprSyntax``).
367+
///
368+
/// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type.
369+
///
370+
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
371+
/// informing the user the upcast should be performed using the target base node's initializer.
372+
@available(*, deprecated, message: "Use `ExprSyntax.init` for upcasting")
373+
public func `as`(_ syntaxType: ExprSyntax.Type) -> ExprSyntax? {
374+
return ExprSyntax(self)
375+
}
376+
377+
378+
/// Force-upcast the current syntax node to its base node type (``ExprSyntax``).
379+
///
380+
/// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type.
381+
///
382+
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
383+
/// informing the user the upcast should be performed using the target base node's initializer.
384+
@available(*, deprecated, message: "Use `ExprSyntax.init` for upcasting")
385+
public func cast(_ syntaxType: ExprSyntax.Type) -> ExprSyntax {
386+
return ExprSyntax(self)
387+
}
388+
389+
318390
/// Checks if the current syntax node can be cast to a given node type from the different base node protocol hierarchy than ``ExprSyntaxProtocol``.
319391
///
320392
/// - Returns: `false` since the node can not be cast to the node type from different base node protocol hierarchy than ``ExprSyntaxProtocol``.
@@ -595,6 +667,42 @@ extension PatternSyntaxProtocol {
595667
}
596668

597669

670+
/// Checks if the current syntax node can be upcast to its base node type (``PatternSyntax``).
671+
///
672+
/// - Returns: `true` since the node can always be upcast to its base node.
673+
///
674+
/// - Note: This method overloads the general `is` method and is marked deprecated to produce a warning
675+
/// informing the user that the upcast will always succeed.
676+
@available(*, deprecated, message: "This cast will always succeed")
677+
public func `is`(_ syntaxType: PatternSyntax.Type) -> Bool {
678+
return true
679+
}
680+
681+
682+
/// Attempts to upcast the current syntax node to its base node type (``PatternSyntax``).
683+
///
684+
/// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type.
685+
///
686+
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
687+
/// informing the user the upcast should be performed using the target base node's initializer.
688+
@available(*, deprecated, message: "Use `PatternSyntax.init` for upcasting")
689+
public func `as`(_ syntaxType: PatternSyntax.Type) -> PatternSyntax? {
690+
return PatternSyntax(self)
691+
}
692+
693+
694+
/// Force-upcast the current syntax node to its base node type (``PatternSyntax``).
695+
///
696+
/// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type.
697+
///
698+
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
699+
/// informing the user the upcast should be performed using the target base node's initializer.
700+
@available(*, deprecated, message: "Use `PatternSyntax.init` for upcasting")
701+
public func cast(_ syntaxType: PatternSyntax.Type) -> PatternSyntax {
702+
return PatternSyntax(self)
703+
}
704+
705+
598706
/// Checks if the current syntax node can be cast to a given node type from the different base node protocol hierarchy than ``PatternSyntaxProtocol``.
599707
///
600708
/// - Returns: `false` since the node can not be cast to the node type from different base node protocol hierarchy than ``PatternSyntaxProtocol``.
@@ -830,6 +938,42 @@ extension StmtSyntaxProtocol {
830938
}
831939

832940

941+
/// Checks if the current syntax node can be upcast to its base node type (``StmtSyntax``).
942+
///
943+
/// - Returns: `true` since the node can always be upcast to its base node.
944+
///
945+
/// - Note: This method overloads the general `is` method and is marked deprecated to produce a warning
946+
/// informing the user that the upcast will always succeed.
947+
@available(*, deprecated, message: "This cast will always succeed")
948+
public func `is`(_ syntaxType: StmtSyntax.Type) -> Bool {
949+
return true
950+
}
951+
952+
953+
/// Attempts to upcast the current syntax node to its base node type (``StmtSyntax``).
954+
///
955+
/// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type.
956+
///
957+
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
958+
/// informing the user the upcast should be performed using the target base node's initializer.
959+
@available(*, deprecated, message: "Use `StmtSyntax.init` for upcasting")
960+
public func `as`(_ syntaxType: StmtSyntax.Type) -> StmtSyntax? {
961+
return StmtSyntax(self)
962+
}
963+
964+
965+
/// Force-upcast the current syntax node to its base node type (``StmtSyntax``).
966+
///
967+
/// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type.
968+
///
969+
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
970+
/// informing the user the upcast should be performed using the target base node's initializer.
971+
@available(*, deprecated, message: "Use `StmtSyntax.init` for upcasting")
972+
public func cast(_ syntaxType: StmtSyntax.Type) -> StmtSyntax {
973+
return StmtSyntax(self)
974+
}
975+
976+
833977
/// Checks if the current syntax node can be cast to a given node type from the different base node protocol hierarchy than ``StmtSyntaxProtocol``.
834978
///
835979
/// - Returns: `false` since the node can not be cast to the node type from different base node protocol hierarchy than ``StmtSyntaxProtocol``.
@@ -1075,6 +1219,42 @@ extension TypeSyntaxProtocol {
10751219
}
10761220

10771221

1222+
/// Checks if the current syntax node can be upcast to its base node type (``TypeSyntax``).
1223+
///
1224+
/// - Returns: `true` since the node can always be upcast to its base node.
1225+
///
1226+
/// - Note: This method overloads the general `is` method and is marked deprecated to produce a warning
1227+
/// informing the user that the upcast will always succeed.
1228+
@available(*, deprecated, message: "This cast will always succeed")
1229+
public func `is`(_ syntaxType: TypeSyntax.Type) -> Bool {
1230+
return true
1231+
}
1232+
1233+
1234+
/// Attempts to upcast the current syntax node to its base node type (``TypeSyntax``).
1235+
///
1236+
/// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type.
1237+
///
1238+
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
1239+
/// informing the user the upcast should be performed using the target base node's initializer.
1240+
@available(*, deprecated, message: "Use `TypeSyntax.init` for upcasting")
1241+
public func `as`(_ syntaxType: TypeSyntax.Type) -> TypeSyntax? {
1242+
return TypeSyntax(self)
1243+
}
1244+
1245+
1246+
/// Force-upcast the current syntax node to its base node type (``TypeSyntax``).
1247+
///
1248+
/// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type.
1249+
///
1250+
/// - Note: This method overloads the general `as` method and is marked deprecated to produce a warning
1251+
/// informing the user the upcast should be performed using the target base node's initializer.
1252+
@available(*, deprecated, message: "Use `TypeSyntax.init` for upcasting")
1253+
public func cast(_ syntaxType: TypeSyntax.Type) -> TypeSyntax {
1254+
return TypeSyntax(self)
1255+
}
1256+
1257+
10781258
/// Checks if the current syntax node can be cast to a given node type from the different base node protocol hierarchy than ``TypeSyntaxProtocol``.
10791259
///
10801260
/// - Returns: `false` since the node can not be cast to the node type from different base node protocol hierarchy than ``TypeSyntaxProtocol``.

0 commit comments

Comments
 (0)