Skip to content

IRGen: Handle protocol inheritance in witness_method lowering. #2920

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
Jun 7, 2016
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
5 changes: 5 additions & 0 deletions include/swift/AST/ProtocolConformanceRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class ProtocolConformanceRef {

/// Return the protocol requirement.
ProtocolDecl *getRequirement() const;

/// Get the inherited conformance corresponding to the given protocol.
/// Returns `this` if `parent` is already the same as the protocol this
/// conformance represents.
ProtocolConformanceRef getInherited(ProtocolDecl *parent) const;

void dump() const;
void dump(llvm::raw_ostream &out, unsigned indent = 0) const;
Expand Down
24 changes: 24 additions & 0 deletions lib/AST/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ ProtocolDecl *ProtocolConformanceRef::getRequirement() const {
}
}

ProtocolConformanceRef
ProtocolConformanceRef::getInherited(ProtocolDecl *parent) const {
assert((getRequirement() == parent ||
getRequirement()->inheritsFrom(parent)) &&
"not a parent of this protocol");

if (parent == getRequirement())
return *this;

// For an abstract requirement, simply produce a new abstract requirement
// for the parent.
if (isAbstract()) {
return ProtocolConformanceRef(parent);
}

// Navigate concrete conformances.
if (isConcrete()) {
return ProtocolConformanceRef(
getConcrete()->getInheritedConformance(parent));
}

llvm_unreachable("unhandled ProtocolConformanceRef");
}

void *ProtocolConformance::operator new(size_t bytes, ASTContext &context,
AllocationArena arena,
unsigned alignment) {
Expand Down
6 changes: 3 additions & 3 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2621,9 +2621,9 @@ irgen::emitWitnessMethodValue(IRGenFunction &IGF,
ProtocolConformanceRef conformance,
Explosion &out) {
auto fn = cast<AbstractFunctionDecl>(member.getDecl());

assert(cast<ProtocolDecl>(fn->getDeclContext())
== conformance.getRequirement());
auto fnProto = cast<ProtocolDecl>(fn->getDeclContext());

conformance = conformance.getInherited(fnProto);

// Find the witness table.
// FIXME conformance for concrete type
Expand Down
18 changes: 18 additions & 0 deletions test/IRGen/witness_method_after_devirt.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %target-swift-frontend -emit-ir -verify %s

protocol BaseProtocol {
static func run()
}

protocol DerivedProtocol: BaseProtocol {
}

struct Foo: DerivedProtocol {
static func run() { }
}

@inline(never)
func test() {
let t: DerivedProtocol.Type = Foo.self
t.run()
}