Skip to content

Ensure predicate diagnostics contain source information when possible #1025

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

Merged

Conversation

jmschonfeld
Copy link
Contributor

#Predicate currently performs its transformation in two passes over the AST:

  1. Rewrite all optional chaining as explicit calls to flatMap (ex. foo?.bar --> foo.flatMap { $0.bar })
  2. Perform transformation to PredicateExpression builder calls (ex. foo.bar() --> PredicateExpressions.build_bar(PredicateExpressions.build_Arg(foo)))

Unfortunately, performing the rewrite in Step 1 creates a new AST that lacks all source information from the original code in the source file. This means that when diagnostics are emitted during pass 2, the diagnostics are detached from any source locations and only appear as standalone messages in the build log which makes for a challenging debugging experience.

After talking with the swift-syntax folks, in the fullness of time we will need to rewrite the #Predicate implementation to perform its rewrite in a single pass to retain all source location information. However, until we can perform such a rewrite (which is not trivial) this PR helps by "skipping" step 1 when optional chaining is not present. For predicates that don't contain optional chaining, pass 1 will now just pass on the original AST with source information so that pass 2 emits diagnostics with attached locations. This change also updates our unit testing infrastructure to fail if any diagnostics are emitted without attached source locations so that we'll pick up issues like this in CI in the future.

Note: I also had to switch the package dependency from [email protected] to swift-syntax@main. This is fine because when building Foundation in the toolchain, we build against main anyways and this mirrors how we build against swift-foundation-icu.

Resolves rdar://125125964

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld jmschonfeld merged commit 919053f into swiftlang:main Nov 5, 2024
3 checks passed
@jmschonfeld jmschonfeld deleted the predicate-diagnostics-source-loc branch November 5, 2024 20:20
cthielen pushed a commit to cthielen/swift-foundation that referenced this pull request Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants