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-14482] Inconsistent behavior in multi-layered keypath-member-lookup. #56837

Open
swift-ci opened this issue Apr 14, 2021 · 2 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-14482
Radar rdar://problem/76626958
Original Reporter ensan (JIRA User)
Type Bug
Environment

Apple Swift version 5.4 (swiftlang-1205.0.26.9 clang-1205.0.19.55)

Target: x86_64-apple-darwin20.3.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: bc74d89bb6a0950d1a0166faef1d7829

Issue Description:

I wrote about this behavior in the forum as a question, but it's maybe a bug.
https://forums.swift.org/t/question-limitation-in-depth-of-multiple-keypath-member-lookup/47318

At first, I prepared a property wrapper using keypath-member-lookup.

@propertyWrapper
@dynamicMemberLookup
private struct AddSubscript<T, S> {
    var wrappedValue: T

    init(wrappedValue: T, type: S.Type) {
        self.wrappedValue = wrappedValue
    }

    subscript(to type: S) -> Bool {
        get {
            return true
        }
        set {
            //some process
        }
    }

    subscript<U>(dynamicMember keyPath: WritableKeyPath<T, U>) -> U{
        get {
            return wrappedValue[keyPath: keyPath]
        }
        set {
            wrappedValue[keyPath: keyPath] = newValue
        }
    }
}

I wanted this property wrapper to work as if it add a subscript.

struct Test{
    @AddSubscript(type: [Int].self)
    @AddSubscript(type: String.self)
    @AddSubscript(type: Bool.self)
    @AddSubscript(type: Int.self)
    private var value = true

    func test1(){
        print(_value[to: [0]])   //it should work and it works.
        print(_value[to: ""])   //it should work and it works.


        //Error: Value of type 'Any' has no dynamic member 'subscript' using key path from root type 'Bool'
        print(_value[to: true])     //it should work but it doesn't works!
        //Error: Value of type 'Any' has no dynamic member 'subscript' using key path from root type 'Bool'
        print(_value[to: 1])      //it should work but it doesn't works!


        let keyPath: KeyPath<AddSubscript<Bool, Int>, Bool> = \.[to: 1]
        let keyPath2: KeyPath<AddSubscript<AddSubscript<Bool, Int>, Bool>, Bool> = \.[to: 1]
        let keyPath3: KeyPath<AddSubscript<AddSubscript<Bool, Int>, Bool>, Bool> = \.[to: true]
        let keyPath4: KeyPath<AddSubscript<AddSubscript<AddSubscript<Bool, Int>, Bool>, String>, Bool> = \.[to: ""]
        let keyPath5: KeyPath<AddSubscript<AddSubscript<AddSubscript<Bool, Int>, Bool>, String>, Bool> = \.[to: true]
        //Only this causes error!
        //Error: Value of type 'KeyPath<AddSubscript<AddSubscript<AddSubscript<Bool, Int>, Bool>, String>, Bool>' has no dynamic member 'subscript' using key path from root type 'Bool'
        let keyPath6: KeyPath<AddSubscript<AddSubscript<AddSubscript<Bool, Int>, Bool>, String>, Bool> = \.[to: 1]


    func test2(){
        do{
            //it works.
            let value1 = _value[dynamicMember: \.[to: ""]]
            //therefore, it should work, and it works.
            let value2 = _value[to: ""]
        }
        do{
            //it works.
            let value1 = _value[dynamicMember: \.[to: true]]
            //therefore, it should work, but doesn't work.
            //Error: Value of type '_' has no dynamic member 'subscript' using key path from root type 'Bool'
            let value2 = _value[to: true]
        }
    }
}

Especially the fail of test2 is inconsistent because I think it's correct that if the first code works, then the next code should also work.

let value1 = value[dynamicMember: \.xxx]
let value2 = value.xxx

Best regards,

@typesanitizer
Copy link

@swift-ci create

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 3, 2021

Comment by ensan (JIRA)

I removed property wrapper from code and softened the complexity.

@dynamicMemberLookup
struct Wrapper<Value, Integer: FixedWidthInteger> {
    var value: Value
    init(_ value: Value) {
        self.value = value
    }
    subscript<T>(dynamicMember keyPath: KeyPath<Value, T>) -> T {
        get { return self.value[keyPath: keyPath] }
    }
    subscript(type value: Integer) -> Bool {
        get { return true }
    }
}
struct Test {
    typealias T1 = Wrapper<String, Int8>
    typealias T2 = Wrapper<T1, Int16>
    typealias T3 = Wrapper<T2, Int32>
    typealias T4 = Wrapper<T3, Int64>
    static func test() {
        var value: T4 = .init(.init(.init(.init("Swift"))))
        value[type: Int64.max]    // OK
        value[type: Int32.max]    // OK
        value[type: Int16.max]    // Error
        value[type: Int8.max]     // Error
    }
}

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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
Projects
None yet
Development

No branches or pull requests

2 participants