Repository: apple/swift-syntax (swift)

Issue:
`SyntaxProtocol.expand(macros:)` drops `IfConfigDeclSyntax` nodes from result
### Description

Given the following code:

```swift
let sourceCode = """
#if true
@inlinable
#endif
func f() {}
"""

let originalSyntax = Parser.parse(source: sourceCode)
print(originalSyntax)
let context = BasicMacroExpansionContext(lexicalContext: [], expansionDiscriminator: "", sourceFiles: [:])
let syntax = originalSyntax.expand(macros: [:]) { syntax in
  BasicMacroExpansionContext(sharingWith: context, lexicalContext: syntax.allMacroLexicalContexts())
}
print(syntax)
```

I would expect to see (ignoring potential reformatting):

```swift
#if true
@inlinable
#endif
func f() {}
```

But `syntax` drops everything in the `IfConfigDeclSyntax` node (`#if` onward) and just produces:

```swift
func f() {}
```

`originalSyntax` is correct, so the issue is during macro expansion, not the initial parse.

### Steps to Reproduce

See above.

Pull request description:
Fixes #2923
rdar://141840779

Patch:
diff --git a/Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift b/Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift
index e4e923f794b..b77cf33ec02 100644
--- a/Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift
+++ b/Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift
@@ -530,7 +530,11 @@ public class AttributeRemover: SyntaxRewriter {
 
   public override func visit(_ node: AttributeListSyntax) -> AttributeListSyntax {
     var filteredAttributes: [AttributeListSyntax.Element] = []
-    for case .attribute(let attribute) in node {
+    for attribute in node {
+      guard case .attribute(let attribute) = attribute else {
+        filteredAttributes.append(attribute)
+        continue
+      }
       if self.predicate(attribute) {
         var leadingTrivia = attribute.leadingTrivia
 


Test patch:
diff --git a/Tests/SwiftSyntaxMacroExpansionTest/AttributeRemoverTests.swift b/Tests/SwiftSyntaxMacroExpansionTest/AttributeRemoverTests.swift
index 1858bbd92bd..fe8a41e6fbe 100644
--- a/Tests/SwiftSyntaxMacroExpansionTest/AttributeRemoverTests.swift
+++ b/Tests/SwiftSyntaxMacroExpansionTest/AttributeRemoverTests.swift
@@ -470,4 +470,21 @@ final class AttributeRemoverTests: XCTestCase {
         """
     )
   }
+
+  func testKeepPoundIfInAttributes() {
+    assertSyntaxRemovingTestAttributes(
+      """
+      #if true
+      @inlinable
+      #endif
+      func f() {}
+      """,
+      reduction: """
+        #if true
+        @inlinable
+        #endif
+        func f() {}
+        """
+    )
+  }
 }