Skip to content

[Macros] Don't include attr range when checking macro definition #81346

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

rintaro
Copy link
Member

@rintaro rintaro commented May 6, 2025

For macro definition checking, we use the range of the macro declaration and re-parse it with SwiftParser. Previously it uses the range including the attributes, but that can result invalid code because the attribute can be in a #if ... #endif region.

Since we don't use attributes for checking the definition, just use the range without the attributes instead.

rdar://150805795

NOTE: Previously it silently failed here

guard let macroDecl = DeclSyntax.parse(from: &parser).as(MacroDeclSyntax.self) else {
// FIXME: Produce an error
return -1
}

This should only happen if there's a compiler bug (like this). But we should emit an error here. I'll do it in follow-ups.

@@ -234,3 +234,10 @@ func someGlobalNext(
) async throws {
fatalError()
}

// This is testing if the definition is actually checked. The error means the '#eternalMacro' was correctly parsed and checked.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: typo

Suggested change
// This is testing if the definition is actually checked. The error means the '#eternalMacro' was correctly parsed and checked.
// This is testing if the definition is actually checked. The error means the '#externalMacro' was correctly parsed and checked.

Copy link
Contributor

@stmontgomery stmontgomery left a comment

Choose a reason for hiding this comment

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

Thanks!

For macro definition checking, we use the range of the `macro`
declaration and re-parse it with `SwiftParser`. Previously it uses the
range including the attributes, but that can result invalid code because
the attribute can be in a `#if ... #endif` region.

Since we don't use attributes for checking the definition, just use the
range without the attributes instead.

rdar://150805795
@rintaro rintaro force-pushed the macros-definition-typecheck-ifconfig branch from c11f405 to 8519e71 Compare May 6, 2025 23:55
@rintaro
Copy link
Member Author

rintaro commented May 6, 2025

@swift-ci Please smoke test

stmontgomery added a commit to swiftlang/swift-testing that referenced this pull request May 7, 2025
…ability when building w/legacy driver (#1106)

This works around a Swift compiler bug which causes a failure validating
the generated .swiftinterface of the `Testing` module due to it having
macro declarations with `#if`-conditionalized `@available(...)`
attributes _before_ any other `@`-attributes.

The PR which recently landed to enable the Exit Tests feature (#324)
revealed this compiler bug — specifically, that PR removed `@_spi`
attributes which until then _preceded_ `#if SWT_NO_EXIT_TESTS`. The
workaround is to move other attributes on the affected macro
declarations up before the `#if`.

The compiler bug is being fixed in
swiftlang/swift#81346. It only appears to happen
when building with the legacy driver, and Android uses that driver
still. An example CI failure log can be found here:

>
https://github.com/thebrowsercompany/swift-build/actions/runs/14823859186/job/41615678071#step:32:72

### Checklist:

- [x] Code and documentation should follow the style of the [Style
Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md).
- [x] If public symbols are renamed or modified, DocC references should
be updated.
@rintaro
Copy link
Member Author

rintaro commented May 7, 2025

@swift-ci Please smoke test Linux

@rintaro
Copy link
Member Author

rintaro commented May 7, 2025

@swift-ci Please smoke test Windows

@rintaro rintaro merged commit 62b7a6f into swiftlang:main May 7, 2025
3 checks passed
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