@@ -25,7 +25,8 @@ import SwiftSyntax
2525/// to a particular build configuration build configuration.
2626///
2727/// Given an example such as
28- /// ```
28+ ///
29+ /// ```swift
2930/// #if os(Linux)
3031/// func f() { }
3132/// #elseif os(iOS)
@@ -37,14 +38,14 @@ import SwiftSyntax
3738/// those nodes that are in active clauses. When rewriting the above given
3839/// a build configuration for Linux, the resulting tree will be
3940///
40- /// ```
41+ /// ```swift
4142/// func f() { }
4243/// ```
4344///
4445/// When rewriting the above given a build configuration for iOS, the resulting
4546/// tree will be
4647///
47- /// ```
48+ /// ```swift
4849/// func g() { }
4950/// ```
5051///
@@ -84,9 +85,14 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration>: SyntaxRewriter {
8485 continue
8586 }
8687
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+ }
9096
9197 continue
9298 }
@@ -231,8 +237,9 @@ class ActiveSyntaxRewriter<Configuration: BuildConfiguration>: SyntaxRewriter {
231237 else {
232238 // If there is no active clause, return the base.
233239
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.
236243 if let base = postfixIfConfig. base ?? outerBase {
237244 return base
238245 }
0 commit comments