Skip to content

Fix an incorrect assertion involving existential formation. #39454

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
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
13 changes: 12 additions & 1 deletion lib/IRGen/GenExistential.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,16 @@ void irgen::emitClassExistentialContainer(IRGenFunction &IGF,
});
}

#ifndef NDEBUG
static size_t numProtocolsWithWitnessTables(
ArrayRef<ProtocolConformanceRef> conformances) {
return llvm::count_if(conformances, [](ProtocolConformanceRef conformance) {
auto proto = conformance.getRequirement();
return Lowering::TypeConverter::protocolRequiresWitnessTable(proto);
});
}
#endif

/// Emit an existential container initialization operation for a concrete type.
/// Returns the address of the uninitialized fixed-size buffer for the concrete
/// value.
Expand All @@ -1791,7 +1801,8 @@ Address irgen::emitOpaqueExistentialContainerInit(IRGenFunction &IGF,
"initializing a class existential container as opaque");
auto &destTI = IGF.getTypeInfo(destType).as<OpaqueExistentialTypeInfo>();
OpaqueExistentialLayout destLayout = destTI.getLayout();
assert(destTI.getStoredProtocols().size() == conformances.size());
assert(destTI.getStoredProtocols().size()
== numProtocolsWithWitnessTables(conformances));

// First, write out the metadata.
llvm::Value *metadata = IGF.emitTypeMetadataRef(formalSrcType);
Expand Down
12 changes: 12 additions & 0 deletions test/IRGen/marker_protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ public func testGeneric(i: Int, array: [Int]) {
generic(i)
generic(array)
}

// Forming an existential involving a marker protocol would crash the compiler
protocol SelfConstrainedProtocol {
static var shared: Self { get }
}

struct Foo: SelfConstrainedProtocol {
let x: P
static var shared: Self {
Foo(x: 123)
}
}