Skip to content

When creation of a syntax node using string interpolation failed, log the diagnostics #1790

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 15, 2023

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. For example

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
Copy link
Member Author

ahoppen commented Jun 15, 2023

@swift-ci Please test

@ahoppen ahoppen force-pushed the ahoppen/log-string-interpolation-parsing-errors branch from 8267d1d to 42f9f5f Compare June 15, 2023 07:05
@ahoppen
Copy link
Member Author

ahoppen commented Jun 15, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Jun 15, 2023

@swift-ci Please test Windows

@ahoppen
Copy link
Member Author

ahoppen commented Jun 15, 2023

@swift-ci Please test

@kimdv
Copy link
Contributor

kimdv commented Jun 16, 2023

@swift-ci Please test Windows

1 similar comment
@ahoppen
Copy link
Member Author

ahoppen commented Jun 16, 2023

@swift-ci Please test Windows

Comment on lines 35 to 36
Parsing a `\(Self.self)` node from string interpolation produced the following parsing errors.
Set a brakpoint in `SyntaxParseable.logStringInterpolationParsingError()` to debug the failure.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Parsing a `\(Self.self)` node from string interpolation produced the following parsing errors.
Set a brakpoint in `SyntaxParseable.logStringInterpolationParsingError()` to debug the failure.
Parsing a `\(Self.self)` node from string interpolation produced the following parsing errors.
Set a breakpoint in `SyntaxParseable.logStringInterpolationParsingError()` to debug the failure.

@@ -69,9 +69,27 @@ final class LinkageTest: XCTestCase {
.library("-lswiftCompatibilityConcurrency"),
.library("-lswiftCompatibilityPacks", condition: .mayBeAbsent("Only in newer compilers")),
.library("-lswiftCore"),
.library("-lswiftCoreFoundation", condition: .when(compilationCondition: .osLogDependency)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could just only run this check when we don't have the oslog dependency. We don't really care what's linked if we do.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean put the entire test behind #if canImport(OSLog) && !SWIFTSYNTAX_NO_OSLOG_DEPENDENCY?

This test is starting to get a little odd because in practice we only ever run this test using swift test on macOS where we don’t set SWIFTSYNTAX_NO_OSLOG_DEPENDENCY, so it does raise a question about its value.

@ahoppen ahoppen force-pushed the ahoppen/log-string-interpolation-parsing-errors branch from cf6f146 to e0b298b Compare June 21, 2023 07:37
@ahoppen
Copy link
Member Author

ahoppen commented Jun 21, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Jun 21, 2023

@swift-ci Please test Windows

@rintaro
Copy link
Member

rintaro commented Jun 22, 2023

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.

It's not clear for me. How this OS log indicates the location? I think the information in the OS is the same as the parser diagnostics as #1758 , no?

(edit)
Oh, I see #1758 doesn't diagnose each expansion. It only diagnose the result SourceFileSyntax. Hmm...

@rintaro
Copy link
Member

rintaro commented Jun 23, 2023

I feel it's MacroSystem's responsibility to somehow propagate which expansion had errors.
Using OSLog for this just because string interpolation doesn't allow throws doesn't sound like a collect motivation. Also I believe what we want to validate is that "macro expansion should not have syntax errors", but not "string interpolation in general should not have errors".

Instead, I think MacroSystem's each expansion should check the hasError flag and propagate it to the test system somehow with the information which expansion had errors.

… 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 force-pushed the ahoppen/log-string-interpolation-parsing-errors branch from e0b298b to 96557f7 Compare June 26, 2023 10:36
@ahoppen
Copy link
Member Author

ahoppen commented Jun 26, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Jun 26, 2023

@swift-ci Please test Windows

@ahoppen
Copy link
Member Author

ahoppen commented Jun 27, 2023

@swift-ci Please test macOS

2 similar comments
@ahoppen
Copy link
Member Author

ahoppen commented Jun 27, 2023

@swift-ci Please test macOS

@ahoppen
Copy link
Member Author

ahoppen commented Jun 28, 2023

@swift-ci Please test macOS

@ahoppen ahoppen merged commit 2a9810b into swiftlang:main Jun 28, 2023
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.

4 participants