Skip to content

ProtocolConformanceRef: invalid conformances should carry a conforming type #88902

@airspeedswift

Description

@airspeedswift

Description

ProtocolConformanceRef::forInvalid() currently returns a singleton — a default-constructed ref with a null Union pointer and no associated type or protocol. As a result, getType(), getProtocol(), and getRequirement() are unsafe to call on an invalid ref: getType() returns null and the others assert.

This shape forces every caller that consumes the result of substitution or associated-conformance lookup to defensively guard against an invalid conformance and synthesize a fallback type (typically ErrorType). The same workaround has been re-introduced in several places:

// lib/AST/ProtocolConformanceRef.cpp
Type ProtocolConformanceRef::getType() const {                                                                                                                         
  if (isInvalid())                                                                                                                                                     
    return Type();   // null Type — caller must handle                                                                                                                 
  ...                                                                                                                                                                  
}                                                         
// include/swift/AST/ProtocolConformanceRef.h
static ProtocolConformanceRef forInvalid() {                                                                                                                           
  return ProtocolConformanceRef();   // singleton, no type
}                                                                                                                                                                      

Examples:

We should make ProtocolConformanceRef::forInvalid (and the substitution paths that produce invalid refs) carry the conforming type, even when the ref itself is invalid:

static ProtocolConformanceRef forInvalid(Type conformingType,
                                         ProtocolDecl *protocol);                                                                                                      

with getType() returning the stored type unconditionally (likely ErrorType for cases where no real conforming type was available, but the type slot is always populated. The two-arg shape mirrorsforAbstract(conformingType, protocol).

Additional information

No response

Metadata

Metadata

Assignees

Labels

compilerThe Swift compiler itselfgenericsFeature: generic declarations and types

Type

No fields configured for Task.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions