Properly recurse when removing "unsafe" from inlinable code

I forgot that I have to manually recurse in the syntactic rewriter. Do so.
Fixes rdar://147877042
This commit is contained in:
Doug Gregor
2025-03-25 16:24:23 -07:00
parent a95785c5b2
commit 1b2fad1e78
2 changed files with 7 additions and 3 deletions

View File

@@ -550,11 +550,13 @@ public func extractInlinableText(
/// a syntax tree. /// a syntax tree.
fileprivate class RemoveUnsafeExprSyntaxRewriter: SyntaxRewriter { fileprivate class RemoveUnsafeExprSyntaxRewriter: SyntaxRewriter {
override func visit(_ node: UnsafeExprSyntax) -> ExprSyntax { override func visit(_ node: UnsafeExprSyntax) -> ExprSyntax {
return node.expression.with(\.leadingTrivia, node.leadingTrivia) let rewritten = super.visit(node).cast(UnsafeExprSyntax.self)
return rewritten.expression.with(\.leadingTrivia, node.leadingTrivia)
} }
override func visit(_ node: ForStmtSyntax) -> StmtSyntax { override func visit(_ node: ForStmtSyntax) -> StmtSyntax {
return StmtSyntax(node.with(\.unsafeKeyword, nil)) let rewritten = super.visit(node).cast(ForStmtSyntax.self)
return StmtSyntax(rewritten.with(\.unsafeKeyword, nil))
} }
} }

View File

@@ -23,7 +23,9 @@ public struct SequenceWithUnsafeIterator: Sequence {
// CHECK-NOT: unsafe // CHECK-NOT: unsafe
print( unsafe getIntUnsafely()) print( unsafe getIntUnsafely())
for unsafe _ in SequenceWithUnsafeIterator() { } for unsafe _ in SequenceWithUnsafeIterator() {
_ = unsafe getIntUnsafely()
}
} }
// CHECK: public protocol P // CHECK: public protocol P