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-14649] Incorrect warning - 'unsafeBitCast' from 'PartialKeyPath<Object>' to 'PartialKeyPath<T>' can be replaced with 'unsafeDowncast' #57001

Open
swift-ci opened this issue May 19, 2021 · 6 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. casting Feature: explicit casting (is, as, as? and as!) compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation generics Feature: generic declarations and types swift 5.9 type checker Area → compiler: Semantic analysis unexpected warning Bug: Unexpected warning

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented May 19, 2021

Previous ID SR-14649
Radar rdar://problem/78276246
Original Reporter Olbo (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 1cb28da019f7958df1fc3fa446f6bad2

Issue Description:

The warning produced when trying to use unsafeBitCast and PartialKeyPath objects is incorrect, it is warning that it should be replaced with unsafeDowncast which is incorrect.

class Bar { var bar: Int = 42 }
class Baz: Bar { }

struct Foo<Object> {
    let baz: PartialKeyPath<Object>
    init(_ baz: PartialKeyPath<Object>) {
        self.baz = baz
    }
    func unsafeCast<T>(to _: T.Type) -> Foo<T> {
        .init(unsafeBitCast(baz, to: PartialKeyPath<T>.self)) // 'unsafeBitCast' from 'PartialKeyPath<Object>' to 'PartialKeyPath<T>' can be replaced with 'unsafeDowncast'
    }
    func peek(_ object: Object) -> Any {
        object[keyPath: baz]
    }
}


let foo = Foo(\Bar.bar)
let bar = Bar()
foo.peek(bar)
let baz = Baz()
foo.unsafeCast(to: Baz.self).peek(baz)

The reason I am trying this at all is that I wanted to mimic and use the super-type KeyPath on a subtype

baz[keyPath: \Bar.bar] // 42
baz[keyPath: \Baz.bar] // 42
@typesanitizer
Copy link

@swift-ci create

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

xedin commented May 10, 2023

@jreference this one might not be too tricky as well.

@xedin
Copy link
Member

xedin commented May 10, 2023

Also could be dupe of #57285

@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler in itself unexpected warning Bug: Unexpected warning type checker Area → compiler: Semantic analysis generics Feature: generic declarations and types casting Feature: explicit casting (is, as, as? and as!) labels May 11, 2023
@AnthonyLatsis
Copy link
Collaborator

Reduced examples:

class C1<T> {}

func test1<A, B>(_: A, x: C1<B>) {
  _ = unsafeBitCast(x, to: C1<A>.self)
}

class C2 {}

func test2<A: C2, B: C2>(_: A, x: B) {
  _ = unsafeBitCast(x, to: A.self)
}

@jreference
Copy link

jreference commented May 12, 2023

https://github.com/apple/swift/blob/d3efca36dbda829b992931d04b814f98d7e3dae2/test/Constraints/suspicious_bit_casts.swift#L29-L32C2

Was looking at the tests related to this. I'm not sure why an arbitrary C<T> can be downcast to a subclass of C<Int>, it does not seem to be generally safe.

@xedin
Copy link
Member

xedin commented May 12, 2023

That’s why it’s unsafe :)

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. casting Feature: explicit casting (is, as, as? and as!) compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation generics Feature: generic declarations and types swift 5.9 type checker Area → compiler: Semantic analysis unexpected warning Bug: Unexpected warning
Projects
None yet
Development

No branches or pull requests

5 participants