Skip to content

Commit 1273fa4

Browse files
committed
Support opening of arguments of existential metatype type.
1 parent 51abd3d commit 1273fa4

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1364,12 +1364,19 @@ shouldOpenExistentialCallArgument(
13641364
if (param->isVariadic() && !param->getVarargBaseTy()->hasTypeSequence())
13651365
return None;
13661366

1367+
// If the argument is of an existential metatype, look through the
1368+
// metatype on the parameter.
1369+
auto formalParamTy = param->getInterfaceType();
1370+
if (argTy->is<AnyMetatypeType>()) {
1371+
formalParamTy = formalParamTy->getMetatypeInstanceType();
1372+
paramTy = paramTy->getMetatypeInstanceType();
1373+
}
1374+
13671375
// The parameter type must be a type variable.
13681376
auto paramTypeVar = paramTy->getAs<TypeVariableType>();
13691377
if (!paramTypeVar)
13701378
return None;
13711379

1372-
auto formalParamTy = param->getInterfaceType();
13731380
auto genericParam = formalParamTy->getAs<GenericTypeParamType>();
13741381
if (!genericParam)
13751382
return None;

test/Constraints/opened_existentials.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ func testMultipleOpened(a: any P, b: any P & Q) {
6161
let _: Int = r2 // expected-error{{cannot convert value of type '(Q, Q)?' to specified type 'Int'}}
6262
}
6363

64+
// --- Opening existential metatypes
65+
func conjureValue<T: P>(of type: T.Type) -> T? {
66+
nil
67+
}
68+
69+
func testMagic(pt: any P.Type) {
70+
let pOpt = conjureValue(of: pt)
71+
let _: Int = pOpt // expected-error{{cannot convert value of type 'P?' to specified type 'Int'}}
72+
}
73+
6474
// --- With primary associated types and opaque parameter types
6575
protocol CollectionOf: Collection {
6676
@_primaryAssociatedType associatedtype Element

0 commit comments

Comments
 (0)