Skip to content

SIL: Fix verifier crash with metatypes of dynamic Self #7854

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 1 commit into from
Mar 2, 2017
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
1 change: 0 additions & 1 deletion lib/SIL/SILType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ SILType SILType::getMetatypeInstanceType(SILModule &M) const {
assert(MetatypeType->is<AnyMetatypeType>() &&
"This method should only be called on SILTypes with an underlying "
"metatype type.");
assert(isObject() && "Should only be called on object types.");
Type instanceType =
MetatypeType->castTo<AnyMetatypeType>()->getInstanceType();

Expand Down
19 changes: 9 additions & 10 deletions lib/SIL/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1649,14 +1649,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
}

// Is a SIL type a potential lowering of a formal type?
static bool isLoweringOf(SILType loweredType,
CanType formalType) {


// Dynamic self has the same lowering as its contained type.
if (auto dynamicSelf = dyn_cast<DynamicSelfType>(formalType))
formalType = CanType(dynamicSelf->getSelfType());

bool isLoweringOf(SILType loweredType,
CanType formalType) {
// Optional lowers its contained type. The difference between Optional
// and IUO is lowered away.
SILType loweredObjectType = loweredType
Expand All @@ -1672,7 +1666,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
// Metatypes preserve their instance type through lowering.
if (auto loweredMT = loweredType.getAs<MetatypeType>()) {
if (auto formalMT = dyn_cast<MetatypeType>(formalType)) {
return loweredMT.getInstanceType() == formalMT.getInstanceType();
return isLoweringOf(loweredType.getMetatypeInstanceType(F.getModule()),
formalMT.getInstanceType());
}
}
if (auto loweredEMT = loweredType.getAs<ExistentialMetatypeType>()) {
Expand Down Expand Up @@ -1701,7 +1696,11 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
}
return true;
}


// Dynamic self has the same lowering as its contained type.
if (auto dynamicSelf = dyn_cast<DynamicSelfType>(formalType))
formalType = dynamicSelf.getSelfType();

// Other types are preserved through lowering.
return loweredType.getSwiftRValueType() == formalType;
}
Expand Down
14 changes: 14 additions & 0 deletions test/SILGen/dynamic_self.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ func partialApplySelfReturn(c: Factory, t: Factory.Type) {
_ = Factory.staticNewInstance
}

class FactoryFactory {

// CHECK-LABEL: sil hidden @_T012dynamic_self07FactoryC0C11newInstanceACXDyFZ : $@convention(method) (@thick FactoryFactory.Type) -> @owned FactoryFactory
static func newInstance() -> Self {
// CHECK: bb0(%0 : $@thick FactoryFactory.Type):

// CHECK: [[METATYPE:%.*]] = value_metatype $@thick @dynamic_self FactoryFactory.Type.Type, %0 : $@thick FactoryFactory.Type
// CHECK: [[ANY:%.*]] = init_existential_metatype [[METATYPE]] : $@thick @dynamic_self FactoryFactory.Type.Type, $@thick Any.Type
let _: Any.Type = type(of: self)

// CHECK: unreachable
}
}

// CHECK-LABEL: sil_witness_table hidden X: P module dynamic_self {
// CHECK: method #P.f!1: {{.*}} : @_T012dynamic_self1XCAA1PAaaDP1f{{[_0-9a-zA-Z]*}}FTW

Expand Down