Skip to content

Add Feature: NonescapableAccessorOnTrivial #82380

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 1 commit into from
Jun 21, 2025

Conversation

atrick
Copy link
Contributor

@atrick atrick commented Jun 20, 2025

To guard the new UnsafeMutablePointer.mutableSpan APIs.

This allows older compilers to ignore the new APIs. Otherwise, the type checker
will crash on the synthesized _read accessor for a non-Escapable type:

error: cannot infer lifetime dependence on the '_read' accessor because 'self'
is BitwiseCopyable, specify '@lifetime(borrow self)'

I don't know why the _read is synthesized in these cases, but apparently it's
always been that way.

Fixes: rdar://153773093 ([nonescapable] add a compiler feature to guard
~Escapable accessors when self is trivial)

@atrick
Copy link
Contributor Author

atrick commented Jun 20, 2025

@swift-ci test

@atrick atrick requested a review from glessard June 20, 2025 18:15
@atrick atrick marked this pull request as draft June 20, 2025 18:17
Copy link
Contributor

@slavapestov slavapestov left a comment

Choose a reason for hiding this comment

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

LGTM for 6.2, just two minor nitpicks for main

@@ -700,6 +700,13 @@ class alignas(1 << TypeAlignInBits) TypeBase
/// Returns true if this contextual type is (Escapable && !isNoEscape).
bool mayEscape() { return !isNoEscape() && isEscapable(); }

/// Returns true if this contextual type satisfies a conformance to Escapable.
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this doc comment and the next need s/Escapable/BitwiseCopyable/

auto sig = nominal->getGenericSignature();
return !var->getInterfaceType()->isEscapable(sig);
auto sig = dc->getGenericSignatureOfContext();
return !var->getInterfaceType()->isEscapable(sig);
Copy link
Contributor

Choose a reason for hiding this comment

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

var->getTypeInContext()->isEscapable() also works and avoids having to fish out sig entirely

}
if (auto dc = var->getDeclContext()) {
auto sig = dc->getGenericSignatureOfContext();
if (!var->getInterfaceType()->isEscapable(sig)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same remark here about getInterfaceType() vs getTypeInContext()

@atrick atrick force-pushed the nonescapable-accessor-on-trivial branch from ae31edc to 8272a63 Compare June 20, 2025 22:52
Copy link
Contributor Author

@atrick atrick left a comment

Choose a reason for hiding this comment

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

@slavapestov

I folded away the if (auto dc = var->getDeclContext()) checks. Presumably a VarDecl always has a context.

And added a unit test.

@atrick
Copy link
Contributor Author

atrick commented Jun 20, 2025

@swift-ci test

@atrick atrick marked this pull request as ready for review June 20, 2025 22:57
To guard the new UnsafeMutablePointer.mutableSpan APIs.

This allows older compilers to ignore the new APIs. Otherwise, the type checker
will crash on the synthesized _read accessor for a non-Escapable type:

    error: cannot infer lifetime dependence on the '_read' accessor because 'self'
    is BitwiseCopyable, specify '@Lifetime(borrow self)'

I don't know why the _read is synthesized in these cases, but apparently it's
always been that way.

Fixes: rdar://153773093 ([nonescapable] add a compiler feature to guard
~Escapable accessors when self is trivial)
@atrick atrick force-pushed the nonescapable-accessor-on-trivial branch from 8272a63 to cc357f4 Compare June 20, 2025 22:59
@atrick
Copy link
Contributor Author

atrick commented Jun 20, 2025

@swift-ci test

@atrick atrick requested a review from tshortli June 21, 2025 01:41
@atrick atrick merged commit 374026f into swiftlang:main Jun 21, 2025
5 checks passed
@atrick atrick deleted the nonescapable-accessor-on-trivial branch June 21, 2025 16:24
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