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-3840] Closure picks up static type not dynamic #46425

Closed
swift-ci opened this issue Feb 2, 2017 · 1 comment
Closed

[SR-3840] Closure picks up static type not dynamic #46425

swift-ci opened this issue Feb 2, 2017 · 1 comment
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Feb 2, 2017

Previous ID SR-3840
Radar rdar://problem/30336146
Original Reporter hlovatt (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

MacOS 10.12.3

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee @slavapestov
Priority Medium

md5: 22af35df83a7d03ffb5d5b5d0205cdc0

Issue Description:

Hi,

I am seeing a problem were a closure that reads and writes to a computed parameter picks up the static type and not the dynamic type. Whereas a closure that just reads correctly picks up the dynamic type. In:

//: Closure picks up static type not dynamic
class MutableReference<T> {
    init() {
        guard type(of: self) != MutableReference.self else {
            fatalError("MutableReference is an abstract class; create a derivative of MutableReference")
        }
    }
    var value: T {
        get { fatalError("Calculated property value getter should be overridden") }
        set { fatalError("Calculated property value setter should be overridden") }
    }
}

class DefaultMutableReference<T>: MutableReference<T> {
    private var _value: T
    override var value: T {
        get { return _value }
        set { _value = newValue }
    }
    init(_ value: T) { _value = value }
}

let er: (MutableReference<[Int]>, Int) -> Void = { $0.value.append($1) }
let ok: (MutableReference<[Int]>) -> [Int] = { $0.value }
let dm = DefaultMutableReference([2])
ok(dm) // [2]
er(dm, 2) // fatal error: Calculated property value getter should be overridden
ok(dm) // Expect [2, 2]

The above fails when `er` is called because `er` calls `MutableReference.value.get` not `DefaultMutableReference.value.get`, which you would expect since `dm` is a `DefaultMutableReference`.

Note:

  1. if `er` changed to: `let er: (DefaultMutableReference<[Int]>, Int) -> Void = { $0.value.append($1) }` then no problem

  2. `ok` works fine. This is interesting because `er` fails on the read part but `ok` does the same read and it doesn't fail.

@slavapestov
Copy link
Member

Fixed in swift-3.1-branch: ff00520

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants