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

[SR-15238] Key paths are not "Sendable" #57560

Open
stephencelis opened this issue Sep 23, 2021 · 8 comments
Open

[SR-15238] Key paths are not "Sendable" #57560

stephencelis opened this issue Sep 23, 2021 · 8 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself concurrency Feature: umbrella label for concurrency language features key paths Feature: key paths (both native and Objective-C) standard library Area: Standard library umbrella swift 5.9 type checker Area → compiler: Semantic analysis

Comments

@stephencelis
Copy link
Contributor

stephencelis commented Sep 23, 2021

Previous ID SR-15238
Radar rdar://problem/83465949
Original Reporter @stephencelis
Type Bug
Environment

Swift 5.5+ (with -Xfrontend -warn-concurrency)

Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Bug, Concurrency, TypeChecker
Assignee None
Priority Medium

md5: e9831dccbc5222ce92d77b619d62c314

Issue Description:

Key paths cannot be captured inside code that should be concurrently executable. To repro, try compiling the following code with the -Xfrontend -warn-concurrency build flag.

func f(_ f: @Sendable @escaping () -> Void) {}

func g() {
  let kp = \String.count
  f { kp } // Cannot use let 'kp' with a non-sendable type 'KeyPath<String, Int>' from concurrently-executed code
}

While the proposal suggests that key paths should conform to sendable (except for subscripts in which the referenced types are not sendable), I do not see any conformance in the repo.

@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@nh7a
Copy link

nh7a commented Oct 7, 2022

With Xcode 14.1 beta 3 with SWIFT_STRICT_CONCURRENCY = "targeted", I'm seeing a lot of warnings as below.

Cannot form key path that captures non-sendable type 'KeyPath<AttributeScopes.SwiftUIAttributes, AttributeScopes.SwiftUIAttributes.FontAttribute>
Generic class 'KeyPath' does not conform to the 'Sendable' protocol

and I can't even tell where it's happening.

@BastianKusserow
Copy link

@nh7a did you by any chance find out how to solve or at least suppress those warnings?
Normally I'd think I can just suppress the warning with an @preconcurrency import, but as KeyPath lives in the swift standard library, I think I can't do that?

@AnthonyLatsis AnthonyLatsis added the key paths Feature: key paths (both native and Objective-C) label Jan 13, 2023
@kokluch
Copy link

kokluch commented Feb 7, 2023

I'm facing the same issue. Lot's of warnings for KeyPath non-sendable conformance in various files where there are no KeyPath what so ever...

"Generic class 'KeyPath' does not conform to the 'Sendable' protocol"

@jpsim
Copy link
Contributor

jpsim commented Sep 6, 2023

This is a significant pain point when using SWIFT_STRICT_CONCURRENCY=complete with Swift 5.8/5.9 as well.

In particular the AttributedString APIs that use dynamic member lookup completely break:

var string = AttributedString("Hello")
string.accessibilitySpeechSpellsOutCharacters = true

Produces this error:

<unknown>:0: error: cannot form key path that captures non-sendable type 'KeyPath<AttributeScopes.AccessibilityAttributes, AttributeScopes.AccessibilityAttributes.SpellOutAttribute>'
Swift.KeyPath:1:14: note: generic class 'KeyPath' does not conform to the 'Sendable' protocol
public class KeyPath<Root, Value> : PartialKeyPath<Root> {
             ^
error: fatalError

As mentioned by others in this issue, the diagnostics have no line/column information so it can be hard to pinpoint what line is causing the issue.

Changing -strict-concurrency=targeted works around the problem. I've filed FB13085158 to track the bug on the Foundation side of things.

For AttributedString in particular, I've been able to work around it by using more verbose APIs:

var attributes = AttributeContainer()
attributes[AttributeScopes.AccessibilityAttributes.SpellOutAttribute.self] = true
let string = AttributedString("Hello", attributes: attributes)

But that's very non-obvious that you can even do this.

@AnthonyLatsis AnthonyLatsis added standard library Area: Standard library umbrella Concurrencу Area → standard library: The `Concurrency` module under the standard library umbrella swift 5.9 and removed Concurrencу Area → standard library: The `Concurrency` module under the standard library umbrella labels Sep 6, 2023
@hborla
Copy link
Member

hborla commented Jan 23, 2024

This will be resolved with SE-0418. You can test it out using a main development snapshot and enabling -enable-experimental-feature InferSendableFromCaptures.

@jpsim
Copy link
Contributor

jpsim commented Jan 24, 2024

Thanks for the good news @hborla will it be included in 5.10? If not, is it possible for it to be cherry-picked?

bryanboateng added a commit to bryanboateng/budget that referenced this issue Feb 17, 2024
This reverts commit 96ebad3.
Revert until this bug fixed
apple/swift#57560
@StefaniOSApps
Copy link

Hello everyone,

I wanted to share a workaround specifically for suppressing the non-Sendable warning for KeyPaths in concurrent Swift code:

extension KeyPath: @unchecked Sendable {}

This approach is targeted solely at the Sendable conformance warning for KeyPaths and should be used cautiously as a temporary solution. It allows continued development without the immediate need to refactor large codebases, awaiting a more permanent resolution.

Looking forward to any input or better alternatives from the community.

Best,
Stefan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself concurrency Feature: umbrella label for concurrency language features key paths Feature: key paths (both native and Objective-C) standard library Area: Standard library umbrella swift 5.9 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

9 participants