Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-11170] SyntaxRewriter visitation exhausts the stack space that dispatch threads get #357

Closed
tonyarnold opened this issue Jul 21, 2019 · 5 comments
Assignees
Labels
bug swift-format SwiftSyntax

Comments

@tonyarnold
Copy link

@tonyarnold tonyarnold commented Jul 21, 2019

Previous ID SR-11170
Radar None
Original Reporter @tonyarnold
Type Bug
Status Resolved
Resolution Done
Environment

macOS 10.14.5, Xcode 11 beta 4

Additional Detail from JIRA
Votes 0
Component/s swift-format, SwiftSyntax
Labels Bug
Assignee @ahoppen
Priority Medium

md5: 1b490d131fc3f34c23c544d465e9bd6f

Issue Description:

I've been working on putting together an Xcode Source Editor Extension that will allow me to format code from within Xcode using the SwiftFormat library, however when I use essentially the same code to reformat the source as the command line `swift-format` utility, I get a crash in SwiftSyntax, as shown in the backtrace below:

* thread #​3, queue = 'com.apple.NSXPCConnection.user.16716', stop reason = EXC_BAD_ACCESS (code=2, address=0x70000b360ff8)
    frame #&#8203;0: 0x00000001087ac09b SwiftFormatter`initializeBufferWithCopyOfBuffer for IdentifierPatternSyntax at <compiler-generated>:0
    frame #&#8203;1: 0x0000000108311bf8 SwiftFormatter`outlined init with copy of SyntaxData at <compiler-generated>:0
  * frame #&#8203;2: 0x00000001084e47aa SwiftFormatter`makeSyntax(data=SwiftSyntax.SyntaxData @ 0x000070000b365370) at SyntaxKind.swift:456:15
    frame #&#8203;3: 0x00000001087d3698 SwiftFormatter`closure #&#8203;1 in SyntaxRewriter.visitChildren(n=(Swift.Optional<SwiftSyntax.RawSyntax>, SwiftSyntax.AbsoluteSyntaxInfo) @ 0x000070000b365418, node=SwiftSyntax.IdentifierPatternSyntax @ 0x00007fea3ed0dbb0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1182:20
    frame #&#8203;4: 0x00000001087d393d SwiftFormatter`thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;5: 0x00000001087d39b4 SwiftFormatter`partial apply for thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;6: 0x00007fff639bdc23 libswiftCore.dylib`(extension in Swift):Swift.Sequence.map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> + 1331
    frame #&#8203;7: 0x00000001087d2ee4 SwiftFormatter`SyntaxRewriter.visitChildren(nodeS=SwiftSyntax.IdentifierPatternSyntax @ 0x00007fea3ed0db60, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1177:45
    frame #&#8203;8: 0x00000001087c275f SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.IdentifierPatternSyntax @ 0x000070000b36fc10, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:862:12
    frame #&#8203;9: 0x00000001087d1af4 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.IdentifierPatternSyntax @ 0x00007fea3ed0dae0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1155:37
    frame #&#8203;10: 0x00000001087d3761 SwiftFormatter`closure #&#8203;1 in SyntaxRewriter.visitChildren(n=(Swift.Optional<SwiftSyntax.RawSyntax>, SwiftSyntax.AbsoluteSyntaxInfo) @ 0x000070000b370568, node=SwiftSyntax.PatternBindingSyntax @ 0x00007fea3ed0da40, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1182:14
    frame #&#8203;11: 0x00000001087d393d SwiftFormatter`thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;12: 0x00000001087d39b4 SwiftFormatter`partial apply for thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;13: 0x00007fff639bdc23 libswiftCore.dylib`(extension in Swift):Swift.Sequence.map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> + 1331
    frame #&#8203;14: 0x00000001087d2ee4 SwiftFormatter`SyntaxRewriter.visitChildren(nodeS=SwiftSyntax.PatternBindingSyntax @ 0x00007fea3ed0d9f0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1177:45
    frame #&#8203;15: 0x00000001087bb648 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.PatternBindingSyntax @ 0x000070000b3780e0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:486:12
    frame #&#8203;16: 0x00000001087cbb1a SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.PatternBindingSyntax @ 0x00007fea3ed0d7c0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1061:34
    frame #&#8203;17: 0x00000001087d3761 SwiftFormatter`closure #&#8203;1 in SyntaxRewriter.visitChildren(n=(Swift.Optional<SwiftSyntax.RawSyntax>, SwiftSyntax.AbsoluteSyntaxInfo) @ 0x000070000b37b658, node=SwiftSyntax.PatternBindingListSyntax @ 0x00007fea3ed0d720, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1182:14
    frame #&#8203;18: 0x00000001087d393d SwiftFormatter`thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;19: 0x00000001087d39b4 SwiftFormatter`partial apply for thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;20: 0x00007fff639bdc23 libswiftCore.dylib`(extension in Swift):Swift.Sequence.map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> + 1331
    frame #&#8203;21: 0x00000001087d2ee4 SwiftFormatter`SyntaxRewriter.visitChildren(nodeS=SwiftSyntax.PatternBindingListSyntax @ 0x00007fea3ed0d6d0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1177:45
    frame #&#8203;22: 0x00000001087bb748 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.PatternBindingListSyntax @ 0x000070000b383238, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:490:12
    frame #&#8203;23: 0x00000001087cbbe2 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.PatternBindingListSyntax @ 0x00007fea3ed0d8d0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1062:38
    frame #&#8203;24: 0x00000001087d3761 SwiftFormatter`closure #&#8203;1 in SyntaxRewriter.visitChildren(n=(Swift.Optional<SwiftSyntax.RawSyntax>, SwiftSyntax.AbsoluteSyntaxInfo) @ 0x000070000b386748, node=SwiftSyntax.VariableDeclSyntax @ 0x00007fea3ed0d650, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1182:14
    frame #&#8203;25: 0x00000001087d393d SwiftFormatter`thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;26: 0x00000001087d39b4 SwiftFormatter`partial apply for thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;27: 0x00007fff639bdc23 libswiftCore.dylib`(extension in Swift):Swift.Sequence.map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> + 1331
    frame #&#8203;28: 0x00000001087d2ee4 SwiftFormatter`SyntaxRewriter.visitChildren(nodeS=SwiftSyntax.VariableDeclSyntax @ 0x00007fea3ed0d600, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1177:45
    frame #&#8203;29: 0x00000001087bb85f SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.VariableDeclSyntax @ 0x000070000b38e3f0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:494:12
    frame #&#8203;30: 0x00000001087cbcaa SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.VariableDeclSyntax @ 0x00007fea3ed0d580, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1063:32
    frame #&#8203;31: 0x00000001087d3761 SwiftFormatter`closure #&#8203;1 in SyntaxRewriter.visitChildren(n=(Swift.Optional<SwiftSyntax.RawSyntax>, SwiftSyntax.AbsoluteSyntaxInfo) @ 0x000070000b391898, node=SwiftSyntax.MemberDeclListItemSyntax @ 0x00007fea3ed0d0f0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1182:14
    frame #&#8203;32: 0x00000001087d393d SwiftFormatter`thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;33: 0x00000001087d39b4 SwiftFormatter`partial apply for thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;34: 0x00007fff639bdc23 libswiftCore.dylib`(extension in Swift):Swift.Sequence.map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> + 1331
    frame #&#8203;35: 0x00000001087d2ee4 SwiftFormatter`SyntaxRewriter.visitChildren(nodeS=SwiftSyntax.MemberDeclListItemSyntax @ 0x00007fea3ed0d0a0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1177:45
    frame #&#8203;36: 0x00000001087ba248 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.MemberDeclListItemSyntax @ 0x000070000b398c38, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:418:12
    frame #&#8203;37: 0x00000001087caa48 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.MemberDeclListItemSyntax @ 0x00007fea3ed0d050, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1044:38
    frame #&#8203;38: 0x00000001087d3761 SwiftFormatter`closure #&#8203;1 in SyntaxRewriter.visitChildren(n=(Swift.Optional<SwiftSyntax.RawSyntax>, SwiftSyntax.AbsoluteSyntaxInfo) @ 0x000070000b39c988, node=SwiftSyntax.MemberDeclListSyntax @ 0x00007fea3ed0cfb0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1182:14
    frame #&#8203;39: 0x00000001087d393d SwiftFormatter`thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;40: 0x00000001087d39b4 SwiftFormatter`partial apply for thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;41: 0x00007fff639bdc23 libswiftCore.dylib`(extension in Swift):Swift.Sequence.map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> + 1331
    frame #&#8203;42: 0x00000001087d2ee4 SwiftFormatter`SyntaxRewriter.visitChildren(nodeS=SwiftSyntax.MemberDeclListSyntax @ 0x00007fea3ed0d450, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1177:45
    frame #&#8203;43: 0x00000001087ba148 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.MemberDeclListSyntax @ 0x000070000b3a3cc0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:414:12
    frame #&#8203;44: 0x00000001087ca980 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.MemberDeclListSyntax @ 0x00007fea3ed0d3d0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1043:34
    frame #&#8203;45: 0x00000001087d3761 SwiftFormatter`closure #&#8203;1 in SyntaxRewriter.visitChildren(n=(Swift.Optional<SwiftSyntax.RawSyntax>, SwiftSyntax.AbsoluteSyntaxInfo) @ 0x000070000b3a7a78, node=SwiftSyntax.MemberDeclBlockSyntax @ 0x00007fea3ed0c660, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1182:14
    frame #&#8203;46: 0x00000001087d393d SwiftFormatter`thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;47: 0x00000001087d39b4 SwiftFormatter`partial apply for thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;48: 0x00007fff639bdc23 libswiftCore.dylib`(extension in Swift):Swift.Sequence.map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> + 1331
    frame #&#8203;49: 0x00000001087d2ee4 SwiftFormatter`SyntaxRewriter.visitChildren(nodeS=SwiftSyntax.MemberDeclBlockSyntax @ 0x00007fea3ed0cea0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1177:45
    frame #&#8203;50: 0x00000001087ba048 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.MemberDeclBlockSyntax @ 0x000070000b3aed48, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:410:12
    frame #&#8203;51: 0x00000001087ca8b8 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.MemberDeclBlockSyntax @ 0x00007fea3ed0d220, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1042:35
    frame #&#8203;52: 0x00000001087d3761 SwiftFormatter`closure #&#8203;1 in SyntaxRewriter.visitChildren(n=(Swift.Optional<SwiftSyntax.RawSyntax>, SwiftSyntax.AbsoluteSyntaxInfo) @ 0x000070000b3b2b68, node=SwiftSyntax.ClassDeclSyntax @ 0x00007fea3ed0c610, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1182:14
    frame #&#8203;53: 0x00000001087d393d SwiftFormatter`thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;54: 0x00000001087d39b4 SwiftFormatter`partial apply for thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;55: 0x00007fff639bdc23 libswiftCore.dylib`(extension in Swift):Swift.Sequence.map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> + 1331
    frame #&#8203;56: 0x00000001087d2ee4 SwiftFormatter`SyntaxRewriter.visitChildren(nodeS=SwiftSyntax.ClassDeclSyntax @ 0x00007fea3ed0c5c0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1177:45
    frame #&#8203;57: 0x00000001087b9a5f SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.ClassDeclSyntax @ 0x000070000b3b9c58, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:394:12
    frame #&#8203;58: 0x00000001087ca33c SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.ClassDeclSyntax @ 0x00007fea3ed0c910, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1038:29
    frame #&#8203;59: 0x00000001087d3761 SwiftFormatter`closure #&#8203;1 in SyntaxRewriter.visitChildren(n=(Swift.Optional<SwiftSyntax.RawSyntax>, SwiftSyntax.AbsoluteSyntaxInfo) @ 0x000070000b3bdcb8, node=SwiftSyntax.CodeBlockItemSyntax @ 0x00007fea3ed0c870, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1182:14
    frame #&#8203;60: 0x00000001087d393d SwiftFormatter`thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;61: 0x00000001087d39b4 SwiftFormatter`partial apply for thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;62: 0x00007fff639bdc23 libswiftCore.dylib`(extension in Swift):Swift.Sequence.map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> + 1331
    frame #&#8203;63: 0x00000001087d2ee4 SwiftFormatter`SyntaxRewriter.visitChildren(nodeS=SwiftSyntax.CodeBlockItemSyntax @ 0x00007fea3ed0c7d0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1177:45
    frame #&#8203;64: 0x00000001087b2ac8 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.CodeBlockItemSyntax @ 0x000070000b3c2248, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:46:12
    frame #&#8203;65: 0x00000001087c425d SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.CodeBlockItemSyntax @ 0x00007fea3ed0cb90, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:951:33
    frame #&#8203;66: 0x00000001087d3761 SwiftFormatter`closure #&#8203;1 in SyntaxRewriter.visitChildren(n=(Swift.Optional<SwiftSyntax.RawSyntax>, SwiftSyntax.AbsoluteSyntaxInfo) @ 0x000070000b3c8da8, node=SwiftSyntax.CodeBlockItemListSyntax @ 0x00007fea3ed0c570, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1182:14
    frame #&#8203;67: 0x00000001087d393d SwiftFormatter`thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;68: 0x00000001087d39b4 SwiftFormatter`partial apply for thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;69: 0x00007fff639bdc23 libswiftCore.dylib`(extension in Swift):Swift.Sequence.map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> + 1331
    frame #&#8203;70: 0x00000001087d2ee4 SwiftFormatter`SyntaxRewriter.visitChildren(nodeS=SwiftSyntax.CodeBlockItemListSyntax @ 0x00007fea3ed0c520, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1177:45
    frame #&#8203;71: 0x00000001087b2bc8 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.CodeBlockItemListSyntax @ 0x000070000b3cd3a0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:50:12
    frame #&#8203;72: 0x00000001087c4325 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.CodeBlockItemListSyntax @ 0x00007fea3ed0c4d0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:952:37
    frame #&#8203;73: 0x00000001087d3761 SwiftFormatter`closure #&#8203;1 in SyntaxRewriter.visitChildren(n=(Swift.Optional<SwiftSyntax.RawSyntax>, SwiftSyntax.AbsoluteSyntaxInfo) @ 0x000070000b3d3e98, node=SwiftSyntax.SourceFileSyntax @ 0x00007fea3ed0bb80, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1182:14
    frame #&#8203;74: 0x00000001087d393d SwiftFormatter`thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;75: 0x00000001087d39b4 SwiftFormatter`partial apply for thunk for @callee_guaranteed (@guaranteed RawSyntax?, @unowned AbsoluteSyntaxInfo) -> (@owned RawSyntax?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;76: 0x00007fff639bdc23 libswiftCore.dylib`(extension in Swift):Swift.Sequence.map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> + 1331
    frame #&#8203;77: 0x00000001087d2ee4 SwiftFormatter`SyntaxRewriter.visitChildren(nodeS=SwiftSyntax.SourceFileSyntax @ 0x00007fea3ed0b8b0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1177:45
    frame #&#8203;78: 0x00000001087ba348 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.SourceFileSyntax @ 0x000070000b3db2a0, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:422:12
    frame #&#8203;79: 0x00000001087cab10 SwiftFormatter`SyntaxRewriter.visit(node=SwiftSyntax.SourceFileSyntax @ 0x00007fea3ed03420, self=0x00007fea3ed019c0) at SyntaxRewriter.swift:1045:30
    frame #&#8203;80: 0x00000001082cd468 SwiftFormatter`FormatPipeline.visit(node=SwiftSyntax.SourceFileSyntax @ 0x00007fea3ed060f0, self=SwiftFormat.FormatPipeline @ 0x000070000b3df878) at Pipelines+Generated.swift:280:48
    frame #&#8203;81: 0x00000001082cf54a SwiftFormatter`SwiftFormatter.format<Output>(syntax=SwiftSyntax.SourceFileSyntax @ 0x000070000b3e00d0, url=Foundation.URL @ 0x000070000b3dfb50, outputStream=<no summary available>, self=0x00007fea88a0b590) at SwiftFormatter.swift:79:38
    frame #&#8203;82: 0x00000001081be67c SwiftFormatter`FormatEntireFileCommand.perform(invocation=0x00007fea5ef00d80, completionHandler=0x00000001081bf360 SwiftFormatter`partial apply forwarder for reabstraction thunk helper from @escaping @callee_unowned @convention(block) (@unowned Swift.Optional<__C.NSError>) -> () to @escaping @callee_guaranteed (@guaranteed Swift.Optional<Swift.Error>) -> () at <compiler-generated>, self=0x00007fea5ef04230) at FormatEntireFileCommand.swift:27:27
    frame #&#8203;83: 0x00000001081bf2fb SwiftFormatter`@objc FormatEntireFileCommand.perform(with:completionHandler:) at <compiler-generated>:0
    frame #&#8203;84: 0x0000000109b80ba1 XcodeKit`-[_XCSourceEditorService performCommandInvocation:commandIdentifier:request:reply:] + 469
    frame #&#8203;85: 0x00007fff3a4b3efb Foundation`__NSXPCCONNECTION_IS_CALLING_OUT_TO_EXPORTED_OBJECT_S4__ + 12
    frame #&#8203;86: 0x00007fff3a458e7f Foundation`-[NSXPCConnection _decodeAndInvokeMessageWithEvent:flags:] + 2499
    frame #&#8203;87: 0x00007fff3a458253 Foundation`message_handler + 215
    frame #&#8203;88: 0x00007fff64350f08 libxpc.dylib`_xpc_connection_call_event_handler + 56
    frame #&#8203;89: 0x00007fff6434ee9e libxpc.dylib`_xpc_connection_mach_event + 933
    frame #&#8203;90: 0x00007fff640cf6dd libdispatch.dylib`_dispatch_client_callout4 + 9
    frame #&#8203;91: 0x00007fff640e40d6 libdispatch.dylib`_dispatch_mach_msg_invoke + 436
    frame #&#8203;92: 0x00007fff640d5792 libdispatch.dylib`_dispatch_lane_serial_drain + 268
    frame #&#8203;93: 0x00007fff640e4c19 libdispatch.dylib`_dispatch_mach_invoke + 481
    frame #&#8203;94: 0x00007fff640d5792 libdispatch.dylib`_dispatch_lane_serial_drain + 268
    frame #&#8203;95: 0x00007fff640d63c6 libdispatch.dylib`_dispatch_lane_invoke + 433
    frame #&#8203;96: 0x00007fff640de6ed libdispatch.dylib`_dispatch_workloop_worker_thread + 598
    frame #&#8203;97: 0x00007fff6430f611 libsystem_pthread.dylib`_pthread_wqthread + 421
    frame #&#8203;98: 0x00007fff6430f3fd libsystem_pthread.dylib`start_wqthread + 13

I've uploaded the current state of the project to https://github.com/tonyarnold/XcodeSwiftFormatter as a reproduction of this crash.

Please let me know if you'd like more detail on anything - I'm happy to help!

@akyrtzi
Copy link
Member

@akyrtzi akyrtzi commented Jul 21, 2019

The problem is that the `SyntaxRewriter` visitation exhausts the stack space that dispatch threads get. This can be reproduced from a command-line tool by doing the formatting call on a dispatch queue other than the main one.

To workaround this run the formatting transformation under a pthread thread (which you can customize its stack allocation).

@akyrtzi
Copy link
Member

@akyrtzi akyrtzi commented Jul 21, 2019

Reproducer from a command-line tool:

import Foundation
import SwiftFormat
import SwiftFormatConfiguration
import SwiftSyntax

func doit() throws {
    let configuration = Configuration()
    let formatter = SwiftFormatter(configuration: configuration)
    let syntax = try SyntaxParser.parse(URL(fileURLWithPath: "/path/to/swift/file.swift"))
    let dummyFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString + ".swift")
    var output = String()
    try formatter.format(syntax: syntax, assumingFileURL: dummyFileURL, to: &output)
    print(output)
}
let q = DispatchQueue(label: "queue")
q.async {
    try! doit()
}
sleep(1)

@akyrtzi
Copy link
Member

@akyrtzi akyrtzi commented Jul 21, 2019

Here's how to fix this using `Thread`:

let work = DispatchWorkItem {
    doFormatting()
}
let thread = Thread {
    work.perform()
}
thread.stackSize = 8 << 20 // 8 MB.
thread.start()
work.wait()

@tonyarnold
Copy link
Author

@tonyarnold tonyarnold commented Jul 21, 2019

That did the trick! Thanks so much for the workaround, @akyrtzi - it works really nicely now.

@ahoppen
Copy link
Contributor

@ahoppen ahoppen commented Oct 3, 2019

The root issue of this is being tackled in apple/swift-syntax#147 Once it‘s merged, the workaround using Thread should not be necessary anymore.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 9, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug swift-format SwiftSyntax
Projects
None yet
Development

No branches or pull requests

3 participants