@@ -25,7 +25,8 @@ import SwiftSyntax
25
25
/// to a particular build configuration build configuration.
26
26
///
27
27
/// Given an example such as
28
- /// ```
28
+ ///
29
+ /// ```swift
29
30
/// #if os(Linux)
30
31
/// func f() { }
31
32
/// #elseif os(iOS)
@@ -37,14 +38,14 @@ import SwiftSyntax
37
38
/// those nodes that are in active clauses. When rewriting the above given
38
39
/// a build configuration for Linux, the resulting tree will be
39
40
///
40
- /// ```
41
+ /// ```swift
41
42
/// func f() { }
42
43
/// ```
43
44
///
44
45
/// When rewriting the above given a build configuration for iOS, the resulting
45
46
/// tree will be
46
47
///
47
- /// ```
48
+ /// ```swift
48
49
/// func g() { }
49
50
/// ```
50
51
///
@@ -84,9 +85,14 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration>: SyntaxRewriter {
84
85
continue
85
86
}
86
87
87
- let innerElements = Syntax ( elements) . cast ( List . self)
88
- let newInnerElements = dropInactive ( innerElements, elementAsIfConfig: elementAsIfConfig)
89
- newElements. append ( contentsOf: newInnerElements)
88
+ // In a well-formed syntax tree, the element list is always the
89
+ // same type as List. However, handle a manually-constructed,
90
+ // ill-formed syntax tree gracefully by dropping the inner elements
91
+ // as well.
92
+ if let innerElements = Syntax ( elements) . as ( List . self) {
93
+ let newInnerElements = dropInactive ( innerElements, elementAsIfConfig: elementAsIfConfig)
94
+ newElements. append ( contentsOf: newInnerElements)
95
+ }
90
96
91
97
continue
92
98
}
@@ -231,8 +237,9 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration>: SyntaxRewriter {
231
237
else {
232
238
// If there is no active clause, return the base.
233
239
234
- // Prefer the base we have and, if not, use the outer base.
235
- // TODO: Can we have both? If so, then what?
240
+ // Prefer the base we have and, if not, use the outer base. We can
241
+ // only have both in an ill-formed syntax tree that was manually
242
+ // created.
236
243
if let base = postfixIfConfig. base ?? outerBase {
237
244
return base
238
245
}
0 commit comments