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-2085] Generic typealias with metatypes and dynamic casts returns unexpected value #44693

Open
DevAndArtist mannequin opened this issue Jul 15, 2016 · 0 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@DevAndArtist
Copy link
Mannequin

DevAndArtist mannequin commented Jul 15, 2016

Previous ID SR-2085
Radar None
Original Reporter @DevAndArtist
Type Bug
Environment

Xcode 8 beta 2 (8S162m), Xcode 8 Beta 1

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

md5: 7a4f140e25b0b4534c2a5644eccee08d

Issue Description:

I've been playing around with the generic typealias which hides the `T.Type` structure and found a strange behavior of `Any.Type`:

public typealias Metatype<T> = T.Type

// true - NO WARNING?
Any.self is Any.Type 

// true - Warning: `is` test is always true
Any.self is Metatype<Any>

// true - Warning: `is` test is always true
Int.self is Any.Type

// true - Warning: `is` test is always true
Int.self is Int.Type

// FALSE - should be TRUE - Warning: Cast from `Int.Type` to unrelated type 'Any.Protocol' (aka 'protocol<>.Protocol') always fails
Int.self is Metatype<Any>

// true - Warning: `is` test is always true
Int.self is Metatype<Int>

// works just fine
let anyMetatype: Any.Type = Int.self

// Error: Cannot convert value of type `Int.Type` to specified type `Any.Protocol` (aka 'protocol<>.Protocol')
let anyMetatype: Metatype<Any> = Int.self

// Workaround: store the metatype inside `Any`
let hiddenMetatype: Any = Int.self

// NOT nil
hiddenMetatype as? Any.Type 

// NOT nil
hiddenMetatype as? Int.Type

// NIL - should be NOT nil    
hiddenMetatype as? Metatype<Any>

// NOT nil
hiddenMetatype as? Metatype<Int>

Update: The issue is caused by this behavior.

Any.self // should be of type `Any.Type`

// BUT

Any.Type // means it can store any metatype, not just the metatype for `Any`

There is no way to express the correct type to store only the metatype for `Any`.

Suggestion:

  • Introduce standalone `AnyMetatype`

  • Fix `Any.Type` to be the storage type for the metatype of `Any`

2. Update:

  • In general this is caused by protocol metatypes becoming `.Protocol` instead of `.Type`.

  • This issue is part of SE-0126.

@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

0 participants