Skip to content

Subtype relations of existential metatypes are not consistent in the language. #70874

@CrystDragon

Description

@CrystDragon

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

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.castingFeature: explicit casting (is, as, as? and as!)existentialsFeature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased valuesexpressionsFeature: expressionsmetatypesFeature → types: MetatypesruntimeThe Swift Runtimeswift 6.0typesFeature: typesunexpected behaviorBug: Unexpected behavior or incorrect output

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions