Skip to content

Commit 3652190

Browse files
authored
Merge pull request #7346 from slavapestov/misc-gardening
Add a couple of regression tests and make a small cleanup
2 parents 5002096 + 6874b89 commit 3652190

File tree

14 files changed

+228
-69
lines changed

14 files changed

+228
-69
lines changed

include/swift/AST/ProtocolConformance.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include "swift/AST/ConcreteDeclRef.h"
2020
#include "swift/AST/Decl.h"
21-
#include "swift/AST/ProtocolConformanceRef.h"
2221
#include "swift/AST/Substitution.h"
2322
#include "swift/AST/Type.h"
2423
#include "swift/AST/Types.h"
@@ -160,12 +159,6 @@ class alignas(1 << DeclAlignInBits) ProtocolConformance {
160159
getTypeWitnessSubstAndDecl(AssociatedTypeDecl *assocType,
161160
LazyResolver *resolver) const;
162161

163-
static Type
164-
getTypeWitnessByName(Type type,
165-
ProtocolConformanceRef conformance,
166-
Identifier name,
167-
LazyResolver *resolver);
168-
169162
/// Apply the given function object to each type witness within this
170163
/// protocol conformance.
171164
///

include/swift/AST/ProtocolConformanceRef.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/Hashing.h"
2020
#include "llvm/ADT/PointerUnion.h"
2121
#include "swift/AST/TypeAlignments.h"
22+
#include "swift/AST/Type.h"
2223

2324
namespace llvm {
2425
class raw_ostream;
@@ -101,6 +102,12 @@ class ProtocolConformanceRef {
101102
friend llvm::hash_code hash_value(ProtocolConformanceRef conformance) {
102103
return llvm::hash_value(conformance.Union.getOpaqueValue());
103104
}
105+
106+
static Type
107+
getTypeWitnessByName(Type type,
108+
ProtocolConformanceRef conformance,
109+
Identifier name,
110+
LazyResolver *resolver);
104111
};
105112

106113
} // end namespace swift

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3848,7 +3848,7 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
38483848

38493849
// Find the Objective-C class type we bridge to.
38503850
if (conformance->isConcrete()) {
3851-
return ProtocolConformance::getTypeWitnessByName(
3851+
return ProtocolConformanceRef::getTypeWitnessByName(
38523852
type, *conformance, Id_ObjectiveCType,
38533853
getLazyResolver());
38543854
} else {

lib/AST/ProtocolConformance.cpp

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,40 @@ ProtocolConformanceRef::getInherited(ProtocolDecl *parent) const {
9494
llvm_unreachable("unhandled ProtocolConformanceRef");
9595
}
9696

97+
Type
98+
ProtocolConformanceRef::getTypeWitnessByName(Type type,
99+
ProtocolConformanceRef conformance,
100+
Identifier name,
101+
LazyResolver *resolver) {
102+
// For an archetype, retrieve the nested type with the appropriate
103+
// name. There are no conformance tables.
104+
if (auto archetype = type->getAs<ArchetypeType>()) {
105+
return archetype->getNestedType(name);
106+
}
107+
108+
// Find the named requirement.
109+
AssociatedTypeDecl *assocType = nullptr;
110+
auto members = conformance.getRequirement()->lookupDirect(name);
111+
for (auto member : members) {
112+
assocType = dyn_cast<AssociatedTypeDecl>(member);
113+
if (assocType)
114+
break;
115+
}
116+
117+
// FIXME: Shouldn't this be a hard error?
118+
if (!assocType)
119+
return nullptr;
120+
121+
if (conformance.isAbstract())
122+
return DependentMemberType::get(type, assocType);
123+
124+
auto concrete = conformance.getConcrete();
125+
if (!concrete->hasTypeWitness(assocType, resolver)) {
126+
return nullptr;
127+
}
128+
return concrete->getTypeWitness(assocType, resolver).getReplacement();
129+
}
130+
97131
void *ProtocolConformance::operator new(size_t bytes, ASTContext &context,
98132
AllocationArena arena,
99133
unsigned alignment) {
@@ -154,40 +188,6 @@ ProtocolConformance::getTypeWitness(AssociatedTypeDecl *assocType,
154188
return getTypeWitnessSubstAndDecl(assocType, resolver).first;
155189
}
156190

157-
Type
158-
ProtocolConformance::getTypeWitnessByName(Type type,
159-
ProtocolConformanceRef conformance,
160-
Identifier name,
161-
LazyResolver *resolver) {
162-
// For an archetype, retrieve the nested type with the appropriate
163-
// name. There are no conformance tables.
164-
if (auto archetype = type->getAs<ArchetypeType>()) {
165-
return archetype->getNestedType(name);
166-
}
167-
168-
// Find the named requirement.
169-
AssociatedTypeDecl *assocType = nullptr;
170-
auto members = conformance.getRequirement()->lookupDirect(name);
171-
for (auto member : members) {
172-
assocType = dyn_cast<AssociatedTypeDecl>(member);
173-
if (assocType)
174-
break;
175-
}
176-
177-
// FIXME: Shouldn't this be a hard error?
178-
if (!assocType)
179-
return nullptr;
180-
181-
if (conformance.isAbstract())
182-
return DependentMemberType::get(type, assocType);
183-
184-
auto concrete = conformance.getConcrete();
185-
if (!concrete->hasTypeWitness(assocType, resolver)) {
186-
return nullptr;
187-
}
188-
return concrete->getTypeWitness(assocType, resolver).getReplacement();
189-
}
190-
191191
Witness ProtocolConformance::getWitness(ValueDecl *requirement,
192192
LazyResolver *resolver) const {
193193
CONFORMANCE_SUBCLASS_DISPATCH(getWitness, (requirement, resolver))

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
11011101

11021102
// Dig out the Objective-C type.
11031103
auto conformance = conformances.front();
1104-
Type objcType = ProtocolConformance::getTypeWitnessByName(
1104+
Type objcType = ProtocolConformanceRef::getTypeWitnessByName(
11051105
nominal->getDeclaredType(),
11061106
ProtocolConformanceRef(conformance),
11071107
ctx.Id_ObjectiveCType,

lib/SIL/Bridging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ Type TypeConverter::getLoweredCBridgedType(AbstractionPattern pattern,
225225
auto conformance = foreignRepresentation.second;
226226
assert(conformance && "Missing conformance?");
227227
Type bridgedTy =
228-
ProtocolConformance::getTypeWitnessByName(
228+
ProtocolConformanceRef::getTypeWitnessByName(
229229
t, ProtocolConformanceRef(conformance),
230230
M.getASTContext().Id_ObjectiveCType,
231231
nullptr);

lib/Sema/CSDiag.cpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3686,10 +3686,11 @@ static Type isRawRepresentable(Type fromType,
36863686
if (!conformance)
36873687
return Type();
36883688

3689-
Type rawTy = ProtocolConformance::getTypeWitnessByName(fromType,
3690-
*conformance,
3691-
CS->getASTContext().Id_RawValue,
3692-
&CS->TC);
3689+
Type rawTy = ProtocolConformanceRef::getTypeWitnessByName(
3690+
fromType,
3691+
*conformance,
3692+
CS->getASTContext().Id_RawValue,
3693+
&CS->TC);
36933694
return rawTy;
36943695
}
36953696

@@ -4056,9 +4057,9 @@ bool FailureDiagnosis::diagnoseContextualConversionError() {
40564057
ConformanceCheckFlags::InExpression)) {
40574058
Type errorCodeType = expr->getType();
40584059
Type errorType =
4059-
ProtocolConformance::getTypeWitnessByName(errorCodeType, *conformance,
4060-
TC.Context.Id_ErrorType,
4061-
&TC)->getCanonicalType();
4060+
ProtocolConformanceRef::getTypeWitnessByName(errorCodeType, *conformance,
4061+
TC.Context.Id_ErrorType,
4062+
&TC)->getCanonicalType();
40624063
if (errorType) {
40634064
auto diag = diagnose(expr->getLoc(), diag::cannot_throw_error_code,
40644065
errorCodeType, errorType);
@@ -6698,12 +6699,12 @@ bool FailureDiagnosis::visitArrayExpr(ArrayExpr *E) {
66986699
return true;
66996700
}
67006701

6701-
contextualElementType = ProtocolConformance::getTypeWitnessByName(
6702-
contextualType,
6703-
*Conformance,
6704-
CS->getASTContext().Id_Element,
6705-
&CS->TC)
6706-
->getDesugaredType();
6702+
contextualElementType = ProtocolConformanceRef::getTypeWitnessByName(
6703+
contextualType,
6704+
*Conformance,
6705+
CS->getASTContext().Id_Element,
6706+
&CS->TC)
6707+
->getDesugaredType();
67076708
elementTypePurpose = CTP_ArrayElement;
67086709
}
67096710

@@ -6749,17 +6750,17 @@ bool FailureDiagnosis::visitDictionaryExpr(DictionaryExpr *E) {
67496750
}
67506751

67516752
contextualKeyType =
6752-
ProtocolConformance::getTypeWitnessByName(contextualType,
6753-
*Conformance,
6754-
CS->getASTContext().Id_Key,
6755-
&CS->TC)
6753+
ProtocolConformanceRef::getTypeWitnessByName(contextualType,
6754+
*Conformance,
6755+
CS->getASTContext().Id_Key,
6756+
&CS->TC)
67566757
->getDesugaredType();
67576758

67586759
contextualValueType =
6759-
ProtocolConformance::getTypeWitnessByName(contextualType,
6760-
*Conformance,
6761-
CS->getASTContext().Id_Value,
6762-
&CS->TC)
6760+
ProtocolConformanceRef::getTypeWitnessByName(contextualType,
6761+
*Conformance,
6762+
CS->getASTContext().Id_Value,
6763+
&CS->TC)
67636764
->getDesugaredType();
67646765

67656766
assert(contextualKeyType && contextualValueType &&

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,8 @@ Type TypeChecker::getWitnessType(Type type, ProtocolDecl *protocol,
980980
ProtocolConformanceRef conformance,
981981
Identifier name,
982982
Diag<> brokenProtocolDiag) {
983-
Type ty = ProtocolConformance::getTypeWitnessByName(type, conformance,
984-
name, this);
983+
Type ty = ProtocolConformanceRef::getTypeWitnessByName(type, conformance,
984+
name, this);
985985
if (!ty &&
986986
!(conformance.isConcrete() && conformance.getConcrete()->isInvalid()))
987987
diagnose(protocol->getLoc(), brokenProtocolDiag);

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5095,7 +5095,7 @@ void TypeChecker::useBridgedNSErrorConformances(DeclContext *dc, Type type) {
50955095
// Hack: If we've used a conformance to the _BridgedStoredNSError
50965096
// protocol, also use the RawRepresentable and _ErrorCodeProtocol
50975097
// conformances on the Code associated type witness.
5098-
if (auto codeType = ProtocolConformance::getTypeWitnessByName(
5098+
if (auto codeType = ProtocolConformanceRef::getTypeWitnessByName(
50995099
type, *conformance, Context.Id_Code, this)) {
51005100
(void)conformsToProtocol(codeType, errorCodeProto, dc,
51015101
ConformanceCheckFlags::Used);
@@ -5110,7 +5110,7 @@ void TypeChecker::useBridgedNSErrorConformances(DeclContext *dc, Type type) {
51105110
(ConformanceCheckFlags::SuppressDependencyTracking|
51115111
ConformanceCheckFlags::Used));
51125112
if (conformance && conformance->isConcrete()) {
5113-
if (Type errorType = ProtocolConformance::getTypeWitnessByName(
5113+
if (Type errorType = ProtocolConformanceRef::getTypeWitnessByName(
51145114
type, *conformance, Context.Id_ErrorType, this)) {
51155115
(void)conformsToProtocol(errorType, bridgedStoredNSError, dc,
51165116
ConformanceCheckFlags::Used);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// RUN: %target-swift-frontend %s -emit-ir
2+
3+
public class TiPresenter<V> {
4+
5+
private var view: V? = nil
6+
7+
public func create() {
8+
9+
}
10+
11+
public func attchView(view: V) {
12+
self.view = view
13+
}
14+
15+
public func detachView() {
16+
self.view = nil
17+
}
18+
19+
public func destroy() {
20+
21+
}
22+
}
23+
24+
25+
public class TiViewController<P: TiPresenter<V>, V>
26+
/*: UiViewController*/ // should extend UiViewController but this is not problem here
27+
{
28+
29+
lazy var presenter: P = {
30+
return self.providePresenter()
31+
}()
32+
33+
public init() {
34+
presenter.create()
35+
}
36+
37+
func providePresenter() -> P {
38+
fatalError("must override")
39+
}
40+
41+
func provideView() -> V {
42+
if (self is V) {
43+
return self as! V
44+
} else {
45+
fatalError("UIViewController doesn't implement TiView interface")
46+
}
47+
}
48+
}
49+
50+
51+
protocol MyView {
52+
func setDataItems(_: [String])
53+
}
54+
55+
class MyPresenter: TiPresenter<MyView> {
56+
}
57+
58+
59+
class MyController: TiViewController<MyPresenter, MyView>, MyView {
60+
61+
62+
override func providePresenter() -> MyPresenter {
63+
return MyPresenter()
64+
}
65+
66+
internal func setDataItems(_: [String]) {
67+
//TODO
68+
}
69+
}
70+
71+
let vc = MyController()
72+
let p = vc.presenter

0 commit comments

Comments
 (0)