Skip to content

Span: Remove ownership modifiers and explicit copies from pointers. #77729

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 2 commits into from
Nov 21, 2024

Conversation

atrick
Copy link
Contributor

@atrick atrick commented Nov 20, 2024

Without this fix, the standard library source will break with shortly upcoming compiler toolchain.

Never explicitly copy a pointer before passing it to an argument that is the source of a lifetime dependency on the function's return value. That will always raise a diagnostic error: depending on a temporary value is not the same as depending on a variable. A temporary value's scope is only the current expression.

Also avoid using ownership modifiers for UnsafePointer. We don't want to treat them like noncopyable types. They are simply values. Treating them like noncopyable types creates a lot of overhead in the representation, which is likely to interfere with diagnostics and optimization.

@atrick atrick requested review from glessard and meg-gupta November 20, 2024 07:52
@atrick atrick requested a review from a team as a code owner November 20, 2024 07:52
@atrick
Copy link
Contributor Author

atrick commented Nov 20, 2024

@swift-ci test

@atrick atrick marked this pull request as draft November 20, 2024 08:05
@atrick
Copy link
Contributor Author

atrick commented Nov 20, 2024

BitwiseCopyable appears to be broken for UnsafeBufferPointer. I'm seeing this diagnostic, which never triggers for BitwiseCopyable types:

error: invalid use of borrow dependence with consuming ownership
extension Swift.Span where Element : ~Copyable {
   #if compiler(>=5.3) && $LifetimeDependence
   @lifetime(borrow buffer)
                    `- error: invalid use of borrow dependence with consuming ownership
   @_alwaysEmitIntoClient public init(_unsafeElements buffer: Swift.UnsafeBufferPointer<Element>) {

Without this fix, the standard library source will break with shortly upcoming
compiler toolchain.

Never explicitly copy a pointer before passing it to an argument that is the
source of a lifetime dependency on the function's return value. That will always
raise a diagnostic error: depending on a temporary value is not the same as
depending on a variable. A temporary value's scope is only the current expression.

Also avoid using ownership modifiers for UnsafePointer. We don't want to treat
them like noncopyable types. They are simply values. Treating them like
noncopyable types creates a lot of overhead in the representation, which is
likely to interfere with diagnostics and optimization.
The Span interface requires this. It needs to be able to create dependencies on
UBP values without creating extra temporary copies.

This is only a temporary workaround for:

rdar://140291657 (ASTPrinter: print synthesized conformances on the type
definition, not as an extension)

Until that ASTPrinter bug is fixed, .swiftinterface files drop BitwiseCopyable
conformance on types that have conditionally ~Copyable generic parameters.
@atrick atrick marked this pull request as ready for review November 20, 2024 20:33
@atrick
Copy link
Contributor Author

atrick commented Nov 20, 2024

@swift-ci test

@meg-gupta
Copy link
Contributor

LGTM

@atrick atrick enabled auto-merge November 20, 2024 23:05
@atrick
Copy link
Contributor Author

atrick commented Nov 21, 2024

@swift-ci smoke test windows

@atrick atrick merged commit c9c2b17 into swiftlang:main Nov 21, 2024
5 checks passed
@atrick atrick deleted the fix-span-pointers branch November 21, 2024 06:36
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