Skip to content
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

[SwiftSyntax] Add #_objectFileFormat compilation conditional #3027

Merged
merged 5 commits into from
Mar 27, 2025

Conversation

kubamracek
Copy link
Contributor

SwiftSyntax part for swiftlang/swift#80212.

This implements the relevant part of the "Section Placement" proposal (draft: https://github.com/kubamracek/swift-evolution/blob/section-placement-control/proposals/0nnn-section-control.md, forum thread: https://forums.swift.org/t/pitch-3-section-placement-control/77435). Namely the ability to conditionalize on the object file format that the compiler is targeting (MachO, ELF, COFF of Wasm).

Adding with an underscored name (#_objectFileFormat) until the proposal is codified.

rdar://147505228

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Should this also be guarded behind and experimental feature?

Edit: This should be guarded behind an experimental feature.

@kubamracek kubamracek requested a review from rintaro March 24, 2025 23:29
Copy link
Member

@rintaro rintaro left a comment

Choose a reason for hiding this comment

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

Regarding the experimental feature flag, we currently don't have infrastructure to handle feature flags in SwiftIfConfig, and I don't think we should include such change in this specific PR. So IMO we should take this as-is, then establish the feature flag facility asap, if we want to hide this in a feature flag.

/// - Parameters:
/// - name: The name of the object file format.
/// - Returns: Whether the target object file format matches the given name.
func isActiveTargetObjectFileFormat(name: String) throws -> Bool
Copy link
Member

@rintaro rintaro Mar 24, 2025

Choose a reason for hiding this comment

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

Could you add a default implementation returning false so that we can land this swift-syntax PR independently before swift repo counterpart?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added!

Copy link
Member

@rintaro rintaro Mar 25, 2025

Choose a reason for hiding this comment

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

The test failure is because the default implementation is not public 🙏

Copy link
Member

Choose a reason for hiding this comment

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

We should also make this @_spi(ExperimentalLanguageFeatures) so that clients can’t call it as public API and start relying on it.

@rintaro
Copy link
Member

rintaro commented Mar 24, 2025

As for the "format check" failure, please just do swift format -ipr . in the repository.

@rintaro
Copy link
Member

rintaro commented Mar 25, 2025

@swift-ci Please test

/// - Parameters:
/// - name: The name of the object file format.
/// - Returns: Whether the target object file format matches the given name.
func isActiveTargetObjectFileFormat(name: String) throws -> Bool
Copy link
Member

Choose a reason for hiding this comment

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

We should also make this @_spi(ExperimentalLanguageFeatures) so that clients can’t call it as public API and start relying on it.

/// swift repo.
extension BuildConfiguration {
func isActiveTargetObjectFileFormat(name: String) throws -> Bool {
return false
Copy link
Member

Choose a reason for hiding this comment

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

I think this method should throw an error that has a description like '_objectFileFormat' is an experimental feature. That way _objectFileFormat will not be allowed unless BuildConfiguration actively implements it. This way we also don’t silently assume that there is no active target object file format, as we do right now.

The BuildConfiguration implementation in ASTGen can then check whether the _objectFileFormat experimental feature is enabled. If not, it can throw a similar error. And if the feature is enabled, it can return true or false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 295 to 296
/// Default implementation of BuildConfiguration, to avoid a revlock with the
/// swift repo.
Copy link
Member

Choose a reason for hiding this comment

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

This is not only to avoid revlock with the compiler repo. It is also to not introduce a new requirement to BuildConfiguration for an experimental feature that would be API breaking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed comment

@@ -36,6 +36,7 @@ enum IfConfigDiagnostic: Error, CustomStringConvertible {
case likelySimulatorPlatform(syntax: ExprSyntax)
case likelyTargetOS(syntax: ExprSyntax, replacement: ExprSyntax?)
case endiannessDoesNotMatch(syntax: ExprSyntax, argument: String)
case objectFileFormatDoesNotMatch(syntax: ExprSyntax, argument: String)
Copy link
Member

Choose a reason for hiding this comment

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

Is this used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

@swift-ci please test windows

@kubamracek kubamracek merged commit 9e33a8f into main Mar 27, 2025
24 checks passed
@kubamracek kubamracek deleted the mracek/object-file-format branch March 27, 2025 23:08
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.

3 participants