Skip to content

objcProtocol.Type.self fail to cast to Protocol  #62134

@miku1958

Description

@miku1958

Describe the bug
I have a method for manipulating objcProtocol that looks like this

func classConforming<P>(to objcProtocol: P.Type) -> P? {
	let p: Any = objcProtocol
	guard let pp = p as? Protocol else {
		return nil
	}
	return self.objonly_classConformingToProtocol(pp) as? P
}

self.objonly_classConformingToProtocol accepts a Protocol object as parameter and return AnyObject

When I call it like:
classConforming(to: aProtocol.Type.self)
Xcode shows me that aProtocol.Type.self is aProtocol.Type.Protocol and lldb tells me that objcProtocol is "@thick aProtocol.Type.Protocol" and p is "" but print will tell me it is aProtocol.Type and I can't cast it to Protocol, pp will always nil.

I also tried adding where P.Type == Protocol, but the compiler would give me an error:

Generic signature requires types 'P.Type' and 'Protocol' to be the same

I'm not sure how the compiler converts, but I think since Swift lacks a mechanism to determine whether it's a protocol/class/struct/enum/actor, it should at least ensure type consistency when passing parameters

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfmetatypesFeature → types: Metatypestype checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions