Skip to content

Conversation

meg-gupta
Copy link
Contributor

@meg-gupta meg-gupta commented Sep 30, 2025

This PR adds IRGen support and enables writing borrow accessors for ~Copyable types and library evolution.

It also introduces interim @_unsafeSelfDependentResult attribute to enable writing borrow accessors for unsafe pointer based containers.

Borrow accessor result can sometimes be generated from a local borrow and returning the result produced within the
local borrow scope will cause ownership errors. This PR works around this issue for ~Copyable types and library evolution. But for nested borrow accessors that return @guaranteed values with an @in_guaranteed self, I introduce a pair of unchecked_ownership_conversion instructions to silence the ownership errors. This is needed until we define new SIL semantics that allows returning values produced within local borrow scopes in borrow accessors.

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta meg-gupta force-pushed the moreborrowaccessors branch 2 times, most recently from cab01cf to 4f1bfb2 Compare September 30, 2025 18:45
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci test

Introduce copy_value + mark_unresolved_non_copyable_value + begin_borrow at the return value of
borrow accessor apply to drive move-only diagnostics.

Also strip the copy_value + mark_unresolved_non_copyable_value + begin_borrow trio in a few places, since
they create an artificial scope out of which we cannot return values in a borrow accessor
without resorting to unsafe SIL operations currently.

Borrow accessor diagnostics allow stripping these instructions safely in the following places:

- return value of a borrow accessor
- self argument reference in the borrow accessor return expression and borrow accessor apply
It can be used in borrow/mutate accessors to unsafely specify that the
result is dependent on the self access.
Under library evolution, loadable self arguments are passed as @in_guaranteed.
SILGen generates load_borrow for such self arguments proactively.
load_borrow creates an artifical scope and returning values produced within this scope
will be illegal without resorting to unsafe operations today.

This change avoids creating a load_borrow proactively for borrow accessors.
…n a local borrow

Borrow accessor result can sometimes be generated from a local borrow and
returning the result produced within the local borrow scope will cause ownership errors.

We have to introduce new SIL semantics to make this possible.
Until then use a pair of unchecked_ownership_conversion instructions to silence the ownership errors.
We encounter this when we have:

Address-only self and @guaranteed result
Loadable self and @guaranteed result derived from an unsafe pointer stored property

This change also updates the result convention of borrow accessors with loadable result types
and indirect self argument type.
@meg-gupta meg-gupta force-pushed the moreborrowaccessors branch from 8efa0ab to 3d74cf4 Compare October 2, 2025 14:42
@meg-gupta
Copy link
Contributor Author

@swift-ci test

Copy link
Contributor

@aschwaighofer aschwaighofer left a comment

Choose a reason for hiding this comment

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

IRGen looks good to me.

@meg-gupta meg-gupta merged commit ea0e017 into swiftlang:main Oct 3, 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