Skip to content

Swiftify fixes for 6.2 #81827

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 4 commits into from
Jun 4, 2025
Merged

Conversation

hnrklssn
Copy link
Contributor

This is stacked on top of #81640 to avoid merge conflicts. The changes in that PR are not included in the rest of the description.
This contains the results of multiple PRs, because they would all result in merge conflicts with each other if they weren't stacked.

  • Explanation:
    CxxSpan initializers taking [Mutable]Span still
    had their availability set to Swift 6.2+, even after back-deploying
    caused [Mutable]Span to have availability back to Swift 5.0. Since
    _SwiftifyImport expansions copy the availbility of Span, this resulted
    in the macro expansions calling unavailable initializers.

For parameters with a type
that is Escapable, their lifetime is normally borrowed using @lifetime(borrow foo). However C++ reference parameters are imported as inout, which
requires the @lifetime(&foo) syntax. Update _SwiftifyImport to use this syntax when applicable.


Parameters can be named with keywords without escaping, because it's
unambiguous in the grammar that they are parameters. They still need to
escaped when referred to inside the function body however. This test parses parameter names, and escapes references to parameters using backticks when they are keywords.


Previously we would only add @_disfavoredOverload if the only type
changed was the return type, because in any other case it is unambiguous
which overload to call. However it is still ambiguous when storing the
function as a value rather than calling the function, unless explicit
type annotations are used.

To avoid breaking any existing code we now add
@_disfavoredOverload to every overload generated by @_SwiftifyImport.


Previously we did not remove count parameters if any count parameters
were shared between count expressions, or if any count expression
contained operations. Buffer sizes were also just checked to be larger
than or equal than the given count.

We now extract the count from Spans/BufferPointers whenever possible,
and store that value in a variable at the start of the function. If
multiple parameters share the same count, a bounds check is emitted to
make sure that they have the same size. Subspans can be used if one span
is larger than necessary.

@hnrklssn hnrklssn requested a review from a team as a code owner May 29, 2025 01:30
@hnrklssn
Copy link
Contributor Author

@swift-ci please test

@hnrklssn hnrklssn requested review from DougGregor and j-hui May 29, 2025 01:39
@hnrklssn hnrklssn force-pushed the swiftify-fixes-6.2 branch from 2aa3ee0 to 1d06a01 Compare May 29, 2025 07:30
@hnrklssn
Copy link
Contributor Author

@swift-ci please test

hnrklssn added 4 commits May 30, 2025 18:19
…on parameters with reference type (swiftlang#81634)

Update availability for CxxSpan<->Span, fix lifetimebound on parameters
with reference type

Because swift-ide-test doesn't care about typechecking,
std-span-interface.swift passed despite containing 2 separate errors.
This updates the test file to properly exercise the entire compilation
pipeline for the macro expansions, by running swift-frontend
-emit-module and calling each macro expansion.

The first issue was that CxxSpan initializers taking [Mutable]Span still
had their availability set to Swift 6.2+, even after back-deploying
caused [Mutable]Span to have availability back to Swift 5.0. Since
_SwiftifyImport expansions copy the availbility of Span, this resulted
in the macro expansions calling unavailable initializers. Interestingly
enough, this manifested itself in the form of a tripped assert in SIL
verification, because although we do now typecheck the expansions from
_SwiftifyImport, the compilation can still keep going after
`shouldEmitFunctionBody` returns false: the macro expansion declaration
is still there, but is now missing its definition, despite not being
external.

The second issue was when parameters with C++ reference types were
annotated with `[[clang::lifetimebound]]`. For parameters with a type
that is `Escapable`, this is normally done using `@lifetime(borrow
foo)`. However C++ reference parameters are imported as `inout`, which
requires the `@lifetime(&foo)` syntax.

rdar://151493400
rdar://151678415
(cherry picked from commit 262a53f)
Parameters can be named with keywords without escaping, because it's
unambiguous in the grammar that they are parameters. They still need to
escaped when referred to inside the function body however. This escapes
all references to parameters using backticks.

Parameter names are also checked for clashes with the function name - in
such cases the parameter is renamed in the same way as unnamed
parameters.

rdar://151024645
(cherry picked from commit ebe2c60)
…lang#81579)

Previously we would only add @_disfavoredOverload if the only type
changed was the return type, because in any other case it is unambiguous
which overload to call. However it is still ambiguous when storing the
function as a value rather than calling the function, unless explicit
type annotations are used.

To avoid breaking any existing code, this patch adds
@_disfavoredOverload to every overload generated by @_SwiftifyImport.

rdar://151206394
(cherry picked from commit 0f312ad)
)

Previously we did not remove count parameters if any count parameters
were shared between count expressions, or if any count expression
contained operations. Buffer sizes were also just checked to be larger
than or equal than the given count.

We now extract the count from Spans/BufferPointers whenever possible,
and store that value in a variable at the start of the function. If
multiple parameters share the same count, a bounds check is emitted to
make sure that they have the same size. Subspans can be used if one span
is larger than necessary.

The message in the bounds check is changed so that it includes the
expected and actual value, to aid in debugging.

This patch also fixes some incorrect indentation, and adds the
Whitespace.swift test case to act as a regression test in case the
indentation changes, since the other test cases don't use significant
whitespace.

rdar://151488820
rdar://151511090
rdar://146333006
rdar://147715799
(cherry picked from commit f5fa481)
@hnrklssn hnrklssn force-pushed the swiftify-fixes-6.2 branch from 1d06a01 to 58366a8 Compare May 31, 2025 01:20
@hnrklssn
Copy link
Contributor Author

@swift-ci please test

@hnrklssn hnrklssn merged commit 18a3f08 into swiftlang:release/6.2 Jun 4, 2025
5 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