Skip to content

[5.9] When creation of a syntax node using string interpolation failed, log the diagnostics #1846

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

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Jun 26, 2023

  • Explanation: A very common mistake that clients of SwiftSyntax make, is to construct incorrect syntax nodes using string interpolation. E.g. creating an ExprSyntax node from source code that represents a declaration. Since construction of a type using string interpolation cannot throw an error and we don’t want to cause an assertion failure for invalid source code (since we don’t want to crash macros compiled using debug mode in case there is a syntax error), log a fault using OSLog on macOS informing the user about the API misuse. This log will show in Xcode’s console.
  • Risk: Low, only adds logging and the logging is not present when building swift-syntax for the compiler.
  • Testing: Manually tested that the log messages show up in Xcode’s console when running macro tests.
  • Issue: n/a
  • Reviewer: @bnbarham and @rintaro on When creation of a syntax node using string interpolation failed, log the diagnostics #1790

… the diagnostics

From the first feedback of developers starting to build macros, the number one mistake was to build incorrect syntax nodes, e.g. build an `ExprSyntax` for a declaration. Up until recently, these errors weren’t diagnosed at all. By now we at least issue an XCTest assertion if the expanded macro code contains syntax errors. But I think the issues are a lot easier to debug if we can emit an error at the location where the incorrect syntax node is being created.

Since we can’t throw an error from string interpolation, use OSLog to log the parsing error to Xcode or OS console. When OSLog is not available or we are building using CMake (and thus don’t want to introduce any dependency on the SDK), silently accept the error as we do right now. Logging to stderr might be too fragile in case some application is expecting to populate stderr itself.
@ahoppen ahoppen requested a review from bnbarham June 26, 2023 12:51
@ahoppen
Copy link
Member Author

ahoppen commented Jun 26, 2023

@swift-ci Please test

@ahoppen ahoppen merged commit 7901f52 into swiftlang:release/5.9 Jun 27, 2023
@ahoppen ahoppen deleted the ahoppen/5.9/log-string-interpolation-parsing-errors branch June 27, 2023 18:55
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