Skip to content

Sema: Fix existential-metatype-to-Any conversion #72762

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

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions include/swift/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,6 @@ class MakeAbstractConformanceForGenericType {
ProtocolDecl *conformedProtocol) const;
};

/// Functor class suitable for use as a \c LookupConformanceFn that provides
/// only abstract conformances, or builtin conformances for invertible protocols
/// for generic types. Asserts that the replacement
/// type is an opaque generic type.
class MakeAbstractOrBuiltinConformanceForGenericType {
public:
ProtocolConformanceRef operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const;
};

/// Functor class suitable for use as a \c LookupConformanceFn that fetches
/// conformances from a generic signature.
class LookUpConformanceInSignature {
Expand Down
8 changes: 4 additions & 4 deletions include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
// If we found a type containing a local archetype, substitute
// open existentials throughout the substitution map.
Subs = Subs.subst(QueryTypeSubstitutionMapOrIdentity{LocalArchetypeSubs},
MakeAbstractOrBuiltinConformanceForGenericType());
MakeAbstractConformanceForGenericType());
}
}

Expand All @@ -219,7 +219,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
return Ty.subst(
Builder.getModule(),
QueryTypeSubstitutionMapOrIdentity{LocalArchetypeSubs},
MakeAbstractOrBuiltinConformanceForGenericType(),
MakeAbstractConformanceForGenericType(),
CanGenericSignature());
}
SILType getOpType(SILType Ty) {
Expand All @@ -239,7 +239,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {

return ty.subst(
QueryTypeSubstitutionMapOrIdentity{LocalArchetypeSubs},
MakeAbstractOrBuiltinConformanceForGenericType()
MakeAbstractConformanceForGenericType()
)->getCanonicalType();
}

Expand Down Expand Up @@ -352,7 +352,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
conformance.subst(ty,
QueryTypeSubstitutionMapOrIdentity{
LocalArchetypeSubs},
MakeAbstractOrBuiltinConformanceForGenericType());
MakeAbstractConformanceForGenericType());
}

return asImpl().remapConformance(getASTTypeInClonedContext(ty),
Expand Down
16 changes: 0 additions & 16 deletions lib/AST/TypeSubstitution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,22 +230,6 @@ operator()(CanType dependentType, Type conformingReplacementType,
return Subs.lookupConformance(dependentType, conformedProtocol);
}

ProtocolConformanceRef MakeAbstractOrBuiltinConformanceForGenericType::
operator()(CanType dependentType, Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const {
// Workaround for rdar://125460667
if (conformedProtocol->getInvertibleProtocolKind()) {
auto &ctx = conformedProtocol->getASTContext();
return ProtocolConformanceRef(
ctx.getBuiltinConformance(conformingReplacementType, conformedProtocol,
BuiltinConformanceKind::Synthesized));
}

return MakeAbstractConformanceForGenericType()(dependentType,
conformingReplacementType,
conformedProtocol);
}

ProtocolConformanceRef MakeAbstractConformanceForGenericType::
operator()(CanType dependentType, Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const {
Expand Down
28 changes: 16 additions & 12 deletions lib/SIL/IR/SILInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2185,14 +2185,24 @@ ObjCMethodInst::create(SILDebugLocation DebugLoc, SILValue Operand,
Member, Ty);
}

static void checkExistentialPreconditions(SILType ExistentialType,
CanType ConcreteType,
ArrayRef<ProtocolConformanceRef> Conformances) {
#ifndef NDEBUG
auto layout = ExistentialType.getASTType().getExistentialLayout();
assert(layout.getProtocols().size() == Conformances.size());

for (auto conformance : Conformances) {
assert(!conformance.isAbstract() || isa<ArchetypeType>(ConcreteType));
}
#endif
}

InitExistentialAddrInst *InitExistentialAddrInst::create(
SILDebugLocation Loc, SILValue Existential, CanType ConcreteType,
SILType ConcreteLoweredType, ArrayRef<ProtocolConformanceRef> Conformances,
SILFunction *F) {
#ifndef NDEBUG
auto layout = Existential->getType().getASTType().getExistentialLayout();
assert(layout.getProtocols().size() == Conformances.size());
#endif
checkExistentialPreconditions(Existential->getType(), ConcreteType, Conformances);

SILModule &Mod = F->getModule();
SmallVector<SILValue, 8> TypeDependentOperands;
Expand All @@ -2212,10 +2222,7 @@ InitExistentialValueInst *InitExistentialValueInst::create(
SILDebugLocation Loc, SILType ExistentialType, CanType ConcreteType,
SILValue Instance, ArrayRef<ProtocolConformanceRef> Conformances,
SILFunction *F) {
#ifndef NDEBUG
auto layout = ExistentialType.getASTType().getExistentialLayout();
assert(layout.getProtocols().size() == Conformances.size());
#endif
checkExistentialPreconditions(ExistentialType, ConcreteType, Conformances);

SILModule &Mod = F->getModule();
SmallVector<SILValue, 8> TypeDependentOperands;
Expand All @@ -2233,10 +2240,7 @@ InitExistentialRefInst *InitExistentialRefInst::create(
SILDebugLocation Loc, SILType ExistentialType, CanType ConcreteType,
SILValue Instance, ArrayRef<ProtocolConformanceRef> Conformances,
SILFunction *F, ValueOwnershipKind forwardingOwnershipKind) {
#ifndef NDEBUG
auto layout = ExistentialType.getASTType().getExistentialLayout();
assert(layout.getProtocols().size() == Conformances.size());
#endif
checkExistentialPreconditions(ExistentialType, ConcreteType, Conformances);

SILModule &Mod = F->getModule();
SmallVector<SILValue, 8> TypeDependentOperands;
Expand Down
9 changes: 6 additions & 3 deletions lib/SILGen/SILGenConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,9 +853,6 @@ ManagedValue SILGenFunction::emitExistentialErasure(
[&, concreteFormalType, F](SGFContext C) -> ManagedValue {
auto concreteValue = F(SGFContext());
assert(concreteFormalType->isBridgeableObjectType());
auto *M = SGM.M.getSwiftModule();
auto conformances = M->collectExistentialConformances(
concreteFormalType, anyObjectTy);
return B.createInitExistentialRef(
loc, SILType::getPrimitiveObjectType(anyObjectTy), concreteFormalType,
concreteValue, conformances);
Expand All @@ -864,6 +861,12 @@ ManagedValue SILGenFunction::emitExistentialErasure(
if (this->F.getLoweredFunctionType()->isPseudogeneric()) {
if (anyObjectTy && concreteFormalType->is<ArchetypeType>()) {
concreteFormalType = anyObjectTy;

// The original conformances are no good because they have the wrong
// (pseudogeneric) subject type.
auto *M = SGM.M.getSwiftModule();
conformances = M->collectExistentialConformances(
concreteFormalType, anyObjectTy);
F = eraseToAnyObject;
}
}
Expand Down
30 changes: 17 additions & 13 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6688,13 +6688,24 @@ Expr *ExprRewriter::coerceExistential(Expr *expr, Type toType,
Type fromInstanceType = fromType;
Type toInstanceType = toType;

// Look through metatypes
// For existential-to-existential coercions, open the source existential.
Type openedFromType;
if (fromType->isAnyExistentialType()) {
openedFromType = OpenedArchetypeType::getAny(fromType->getCanonicalType(),
dc->getGenericSignatureOfContext());
}

Type openedFromInstanceType = openedFromType;

// Look through metatypes.
while ((fromInstanceType->is<UnresolvedType>() ||
fromInstanceType->is<AnyMetatypeType>()) &&
toInstanceType->is<ExistentialMetatypeType>()) {
if (!fromInstanceType->is<UnresolvedType>())
fromInstanceType = fromInstanceType->castTo<AnyMetatypeType>()->getInstanceType();
toInstanceType = toInstanceType->castTo<ExistentialMetatypeType>()->getExistentialInstanceType();
fromInstanceType = fromInstanceType->getMetatypeInstanceType();
if (openedFromInstanceType && !openedFromInstanceType->is<UnresolvedType>())
openedFromInstanceType = openedFromInstanceType->getMetatypeInstanceType();
toInstanceType = toInstanceType->getMetatypeInstanceType();
}

ASTContext &ctx = cs.getASTContext();
Expand Down Expand Up @@ -6758,20 +6769,13 @@ Expr *ExprRewriter::coerceExistential(Expr *expr, Type toType,
}

// For existential-to-existential coercions, open the source existential.
if (fromType->isAnyExistentialType()) {
fromType = OpenedArchetypeType::getAny(fromType->getCanonicalType(),
dc->getGenericSignatureOfContext());

if (openedFromType) {
auto *archetypeVal = cs.cacheType(
new (ctx) OpaqueValueExpr(expr->getSourceRange(), fromType));

fromInstanceType = fromType;
while (auto *metatypeType = fromInstanceType->getAs<MetatypeType>())
fromInstanceType = metatypeType->getInstanceType();
new (ctx) OpaqueValueExpr(expr->getSourceRange(), openedFromType));

auto conformances =
dc->getParentModule()
->collectExistentialConformances(fromInstanceType->getCanonicalType(),
->collectExistentialConformances(openedFromInstanceType->getCanonicalType(),
toInstanceType->getCanonicalType(),
/*allowMissing=*/true);

Expand Down
9 changes: 9 additions & 0 deletions test/SILOptimizer/rdar125460667.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %target-swift-frontend -emit-sil %s

@_transparent func f(a: Any.Type) -> Any {
return a
}

func g(a: Any.Type) {
f(a: a)
}