Skip to content

Fix ProtocolConformanceAnalysis for extensions #21195

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
Jan 4, 2019
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
16 changes: 16 additions & 0 deletions lib/SILOptimizer/Analysis/ProtocolConformanceAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Module.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILValue.h"
Expand All @@ -34,6 +35,7 @@ class NominalTypeWalker : public ASTWalker {
: ProtocolConformanceCache(ProtocolConformanceCache) {}

bool walkToDeclPre(Decl *D) override {
/// (1) Walk over all NominalTypeDecls to determine conformances.
if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
auto Protocols = NTD->getAllProtocols();
for (auto &Protocol : Protocols) {
Expand All @@ -42,6 +44,20 @@ class NominalTypeWalker : public ASTWalker {
}
}
}
/// (2) Walk over all ExtensionDecls to determine conformances.
if (auto *e = dyn_cast<ExtensionDecl>(D)) {
auto *ntd = e->getExtendedNominal();
if (!isa<ProtocolDecl>(ntd)) {
for (auto *conformance : e->getLocalConformances()) {
if (isa<NormalProtocolConformance>(conformance)) {
auto *proto = conformance->getProtocol();
if (proto->getEffectiveAccess() <= AccessLevel::Internal) {
ProtocolConformanceCache[proto].push_back(ntd);
}
}
}
}
}
return true;
}
};
Expand Down
32 changes: 32 additions & 0 deletions test/Interpreter/existential_transform.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -O -wmo %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s
// REQUIRES: executable_test

protocol Foo {
var myName: String { get }
}

struct MyURL {
}

extension MyURL : Foo {
var myName : String { return "MyURL" }
}

struct MyStruct : Foo {
var myName : String { return "MyStruct" }
}

@inline(never) func getName(_ f: Foo) -> String {
return f.myName
}

@inline(never) func getName_wrapper() {
let u = MyURL()
// CHECK: MyURL
print(getName(u))
}
getName_wrapper()

39 changes: 38 additions & 1 deletion test/SILOptimizer/existential_transform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,42 @@ func wrap_gcp_arch<T:GP>(_ a:T,_ b:GP, _ c:inout Array<T>) -> Int {
return wrap_gcp_arch(a, k, &b)
}

protocol Foo {
var myName: String { get }
}

struct MyURL {
}

extension MyURL : Foo {
var myName : String { return "MyURL" }
}

struct MyStruct : Foo {
var myName : String { return "MyStruct" }
}

// CHECK-LABEL: sil shared [noinline] @$s21existential_transform7getNameySSAA3Foo_pFTf4e_n : $@convention(thin) <τ_0_0 where τ_0_0 : Foo> (@in_guaranteed τ_0_0) -> @owned String {
// CHECK: bb0(%0 : $*τ_0_0):
// CHECK: alloc_stack
// CHECK: init_existential_addr
// CHECK: copy_addr
// CHECK: debug_value_addr
// CHECK: open_existential_addr
// CHECK: witness_method
// CHECK: apply
// CHECK: dealloc_stack
// CHECK: return
// CHECK-LABEL: } // end sil function '$s21existential_transform7getNameySSAA3Foo_pFTf4e_n'
@inline(never) func getName(_ f: Foo) -> String {
return f.myName
}

@inline(never) func getName_wrapper() -> Int32{
let u = MyURL()
return getName(u) == "MyStruct" ? 0 : 1
}

@_optimize(none) public func foo() -> Int {
cp()
ncp()
Expand All @@ -371,5 +407,6 @@ struct_inout_ncp()
let y:Int = gcp(GC())
var a:Array<GC> = [GC()]
let z:Int = gcp_arch(GC(), &a)
return x + y + z
let zz:Int32 = getName_wrapper()
return x + y + z + Int(zz)
}