<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hello all,<div class=""><br class=""></div><div class="">For context, I spent the weekend seeing what I could do about <a href="https://bugs.swift.org/browse/SR-1560" class="">SE-0075</a>. From the last conversation I had with Jordan (which is summed up in the linked JIRA ticket pretty well), this kind of operation is currently very expensive and hence not as simple a matter as just making Parse ask the ASTContext to load modules. I wound up with <a href="https://github.com/apple/swift/compare/master...CodaFi:fine-imported-goods" class="">a patch</a> that mostly divests Parse of the responsibility of resolving compilation condition blocks. This defers the bulk of the resolution work to a walker in NameBinding (increasingly, a file becoming less and less about binding names!). Problems arose when I thought about how best to resolve these condition clauses in the walker and I tried replicating the gist of what Parse was doing anyway. </div><div class=""><br class=""></div><div class="">For reference, when the parser encounters a #if in the middle of parsing a statement it parses the conditions and their bodies then evaluates them on the spot and reparents the freshly-parsed statements under the “active clause” to whatever enclosing aggregate statement they belong to (usually the top-level or a braced statement block). For decls, the logic is a bit more convoluted due to the tangled web of callbacks in parseDecl, but it does much the same thing wherein active decls are reparented as members of the enclosing aggregate declaration.</div><div class=""><br class=""></div><div class="">From the Walker’s perspective, this means traversing the AST, evaluating any latent #if-conditions, and reparenting statements and declarations thereby “collapsing” the config (or marking it evaluated, etc.). The approach in the NameBinding part of the patch is messy, but it works for simple cases. It gets into trouble when in parse-as-library mode tries to reparent decls to the SourceFile itself because there is no longer a TopLevelCodeDecl they can be a part of. The approach in the patch causes serious issues with decl scoping as the reparenting process doesn’t actually switch the scopes of top-level decls and they wind up being deallocated prematurely. This Crashes The Compiler™.</div><div class=""><br class=""></div><div class="">I know there are more principled ways of approaching this, and I’m wondering what ideas anybody might have to help me get this resolved. Implementing `canImport` on top of this patch is incredibly simple, so I’m hoping we can get the hard part out of the way to start staging things in sooner rather than later.</div><div class=""><br class=""></div><div class="">Thanks,</div><div class=""><br class=""></div><div class="">~Robert Widmann </div></body></html>