Skip to content

Commit 6c37640

Browse files
authored
Merge pull request #7854 from slavapestov/small-sil-verifier-fix
SIL: Fix verifier crash with metatypes of dynamic Self
2 parents b20882b + 5b44e35 commit 6c37640

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

lib/SIL/SILType.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,6 @@ SILType SILType::getMetatypeInstanceType(SILModule &M) const {
376376
assert(MetatypeType->is<AnyMetatypeType>() &&
377377
"This method should only be called on SILTypes with an underlying "
378378
"metatype type.");
379-
assert(isObject() && "Should only be called on object types.");
380379
Type instanceType =
381380
MetatypeType->castTo<AnyMetatypeType>()->getInstanceType();
382381

lib/SIL/SILVerifier.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,14 +1649,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
16491649
}
16501650

16511651
// Is a SIL type a potential lowering of a formal type?
1652-
static bool isLoweringOf(SILType loweredType,
1653-
CanType formalType) {
1654-
1655-
1656-
// Dynamic self has the same lowering as its contained type.
1657-
if (auto dynamicSelf = dyn_cast<DynamicSelfType>(formalType))
1658-
formalType = CanType(dynamicSelf->getSelfType());
1659-
1652+
bool isLoweringOf(SILType loweredType,
1653+
CanType formalType) {
16601654
// Optional lowers its contained type. The difference between Optional
16611655
// and IUO is lowered away.
16621656
SILType loweredObjectType = loweredType
@@ -1672,7 +1666,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
16721666
// Metatypes preserve their instance type through lowering.
16731667
if (auto loweredMT = loweredType.getAs<MetatypeType>()) {
16741668
if (auto formalMT = dyn_cast<MetatypeType>(formalType)) {
1675-
return loweredMT.getInstanceType() == formalMT.getInstanceType();
1669+
return isLoweringOf(loweredType.getMetatypeInstanceType(F.getModule()),
1670+
formalMT.getInstanceType());
16761671
}
16771672
}
16781673
if (auto loweredEMT = loweredType.getAs<ExistentialMetatypeType>()) {
@@ -1701,7 +1696,11 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
17011696
}
17021697
return true;
17031698
}
1704-
1699+
1700+
// Dynamic self has the same lowering as its contained type.
1701+
if (auto dynamicSelf = dyn_cast<DynamicSelfType>(formalType))
1702+
formalType = dynamicSelf.getSelfType();
1703+
17051704
// Other types are preserved through lowering.
17061705
return loweredType.getSwiftRValueType() == formalType;
17071706
}

test/SILGen/dynamic_self.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,20 @@ func partialApplySelfReturn(c: Factory, t: Factory.Type) {
249249
_ = Factory.staticNewInstance
250250
}
251251

252+
class FactoryFactory {
253+
254+
// CHECK-LABEL: sil hidden @_T012dynamic_self07FactoryC0C11newInstanceACXDyFZ : $@convention(method) (@thick FactoryFactory.Type) -> @owned FactoryFactory
255+
static func newInstance() -> Self {
256+
// CHECK: bb0(%0 : $@thick FactoryFactory.Type):
257+
258+
// CHECK: [[METATYPE:%.*]] = value_metatype $@thick @dynamic_self FactoryFactory.Type.Type, %0 : $@thick FactoryFactory.Type
259+
// CHECK: [[ANY:%.*]] = init_existential_metatype [[METATYPE]] : $@thick @dynamic_self FactoryFactory.Type.Type, $@thick Any.Type
260+
let _: Any.Type = type(of: self)
261+
262+
// CHECK: unreachable
263+
}
264+
}
265+
252266
// CHECK-LABEL: sil_witness_table hidden X: P module dynamic_self {
253267
// CHECK: method #P.f!1: {{.*}} : @_T012dynamic_self1XCAA1PAaaDP1f{{[_0-9a-zA-Z]*}}FTW
254268

0 commit comments

Comments
 (0)