Skip to content

Key path sendability warning should be suppressed #68943

@stephencelis

Description

@stephencelis

Description

The sendability story for key paths is not complete right now, and so users can encounter sendability warnings for them when turning up concurrency warnings in their project. Ideally the warnings a user encounters should highlight problems that can be addressed in their code bases, and not unsolved problems in the language. And yet, key paths emit these warnings nonetheless, and these warnings will persist and cannot be suppressed short of turning down concurrency warnings in the entire project.

Steps to reproduce

We can see the problem by turning up concurrency warnings and compiling the following code:

import SwiftUI

@dynamicMemberLookup struct Box<Value> {
    var value: Value
    subscript<T>(dynamicMember keyPath: WritableKeyPath<Value, T>) -> T {
        get { value[keyPath: keyPath] }
        set { value[keyPath: keyPath] = newValue }
    }
}

struct Dog {
    var id: Int?
    var name: String = ""
}

struct ContentView: View {
    @State private var state = Box(value: Dog())

    var body: some View {
        TextField("", text: $state.name)  // ⚠️
    }
}

Warning
Cannot form key path that captures non-sendable type 'WritableKeyPath<Dog, String>'

(Thanks @sidepelican for this example.)

Expected behavior

I expect no warning, or for there to be a way to avoid the warning while still having concurrency warnings turned up.

Environment

  • Swift compiler version info
    swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
    Target: arm64-apple-macosx14.0
    
  • Xcode version info
    Xcode 15.0
    Build version 15A240d
    
  • Deployment target: N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions