-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Open
Labels
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.castingFeature: explicit casting (is, as, as? and as!)Feature: explicit casting (is, as, as? and as!)existentialsFeature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased valuesFeature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased valuesexpressionsFeature: expressionsFeature: expressionsmetatypesFeature → types: MetatypesFeature → types: MetatypesruntimeThe Swift RuntimeThe Swift Runtimeswift 6.0typesFeature: typesFeature: typesunexpected behaviorBug: Unexpected behavior or incorrect outputBug: Unexpected behavior or incorrect output
Description
Description
For classes , if we can write code like let a: TypeA = xxxx
, where the static type of xxxx
is TypeB
, it reflects the fact that TypeB
is a subtype of TypeA
(at least at compile time).
However, when dealing with existentials, the language seems to have inconsistent behaviors.
Reproduction
protocol Base {}
protocol Derived: Base {}
class C: Derived {}
// case 1
let derived: Derived.Type = C.self
print(C.self is Derived.Type) // true, as expected
print(C.Type.self is Derived.Type.Type) // compiler warning: 'is' test is always true (1)
// runtime: prints false (2)
let derividMeta: Derived.Type.Type = C.Type.self // compiles fine (3)
The behaviors of (1), (2), (3) are not consistent.
// case 2:
let base: Base.Type = derived
print(Derived.self is Base.Type) // prints false, not as expected (4)
Expected behavior
// case 1
Behaviors of (1), (2), (3) should be consistent, either they all fail or they or success.
// case 2
I am not sure about this, but if (4) could prints true, the language surface will be more consistent in my opinion.
Environment
- 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
- Target: arm64-apple-macosx14.0
- Xcode: Version 15.1 (15C65)
Additional information
No response
Metadata
Metadata
Assignees
Labels
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.castingFeature: explicit casting (is, as, as? and as!)Feature: explicit casting (is, as, as? and as!)existentialsFeature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased valuesFeature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased valuesexpressionsFeature: expressionsFeature: expressionsmetatypesFeature → types: MetatypesFeature → types: MetatypesruntimeThe Swift RuntimeThe Swift Runtimeswift 6.0typesFeature: typesFeature: typesunexpected behaviorBug: Unexpected behavior or incorrect outputBug: Unexpected behavior or incorrect output