Skip to content

Use the SwiftIfConfig library for evaluating #if conditions #75688

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
merged 3 commits into from
Aug 25, 2024

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Aug 5, 2024

Replace the C++ implementation of #if condition operator folding, validation, and evaluation with the corresponding facilities in the SwiftIfConfig library. Leave the C++ implementation in place for now to permit the compiler to continue building without swift-syntax.

There are a few differences in the diagnostics produced relative to the C++ implementation; the second commit shows the actual changes, all of which are benign or are improvements.

@DougGregor
Copy link
Member Author

Note that this builds on #75014; only the last commit is new.

@DougGregor DougGregor force-pushed the swiftifconfig-replace-cpp branch from 4551387 to 4d8a9ff Compare August 7, 2024 16:07
@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2783

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2783

@swift-ci please test source compatibility

@DougGregor DougGregor marked this pull request as ready for review August 7, 2024 16:07
var description: String {
switch self {
case .unexpectedRuntimeCondition:
return "unexpected argument for the '_runtime' condition; expected '_Native' or '_ObjC'"
Copy link
Member

Choose a reason for hiding this comment

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

Isn’t _multithreaded also allowed, based on the check above?

Copy link
Member Author

Choose a reason for hiding this comment

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

Heh, yes. I was originally following the actually-incorrect diagnostic text in the compiler itself to try to minimize the diffs in this PR. We can fix this now.

) -> Int {
// Retrieve the #if condition that we're evaluating here.
// FIXME: Use 'ExportedSourceFile' when C++ parser is replaced.
let textBuffer = UnsafeBufferPointer<UInt8>(start: ifConditionText.data, count: ifConditionText.count)
Copy link
Member

Choose a reason for hiding this comment

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

Don’t we have an ExportedSourceFile already (the one that we also use for macro evaluation)? Or is this happening earlier where we haven’t created the ExportedSourceFile yet?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm following the path @rintaro used for checking the definitions of macros, which avoids running the swift-syntax parser on the whole file (for performance reasons). This also avoids the somewhat brittle "go find this exact swift-syntax node at this source location" lookup we need to do with that approach, which can fail when the C++ parser and SwiftParser don't recover in exactly the same way.

Copy link
Member

Choose a reason for hiding this comment

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

I see. I was still thinking we had the syntax tree for the entire file anyway and forgot that we no longer parse it completely.

@DougGregor
Copy link
Member Author

/home/build-user/swift/test/Parse/ifconfig_expr.swift:172:22: error: expected error not produced
#if canImport(A,) // expected-error {{unexpected ',' separator}}
                 ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Whoops, bad rebase on my part now that SE-0439 has landed and made this code well-formed.

@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor DougGregor enabled auto-merge August 8, 2024 05:54
@DougGregor
Copy link
Member Author

@swift-ci please test Windows

@DougGregor
Copy link
Member Author

Oh, I bet I have to fix my blatant layering violation for Windows .

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

2 similar comments
@DougGregor
Copy link
Member Author

@swift-ci please test Windows

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

@DougGregor DougGregor force-pushed the swiftifconfig-replace-cpp branch 2 times, most recently from d2390ed to 0735f51 Compare August 22, 2024 05:08
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2814

@swift-ci please test

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2814

@swift-ci please test source compatibility

Replace the C++ implementation of #if condition operator folding,
validation, and evaluation with the corresponding facilities in the
SwiftIfConfig library.

Leave the C++ implementation in place for now to permit the compiler
to continue building without swift-syntax.
For the most part, the differences between the diagnostics introduced
by the C++ implementation and the new SwiftIfConfig implementation are
cosmetic, so these are only wording changes.

The one major difference is that we've dropped the warnings about
potential typos in os/arch checks. For example, if one writes:

    #if os(bisionos)
    // ...
    #endif

The C++ implementation will produce a warning "unknown operating system
for build configuration 'os'" with a note asking "did you mean
'visionOS'"? These warnings rely on a static list of known operating
systems and architectures, which is somewhat unfortunate: the whole
point of these checks is that the Swift you're dealing with might not
have support for those operating systems/architectures, so while these
warnings can be helpful in a few cases, they also cause false
positives when porting. Therefore, I chose not to bring them forward.
We're not caching this now, but it lets us dodge annoying layering
issues because ASTGen (where this is available) sits on top of the C++
parser, which needs to call it.
@DougGregor DougGregor force-pushed the swiftifconfig-replace-cpp branch from bcf1fe2 to 225e562 Compare August 25, 2024 04:32
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor DougGregor merged commit e6f9642 into swiftlang:main Aug 25, 2024
7 checks passed
@DougGregor DougGregor deleted the swiftifconfig-replace-cpp branch August 25, 2024 14:45
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