@@ -99,67 +99,6 @@ SubstitutionMap Solution::computeSubstitutions(
99
99
lookupConformanceFn);
100
100
}
101
101
102
- // / Find a particular named function witness for a type that conforms to
103
- // / the given protocol.
104
- // /
105
- // / \param tc The type check we're using.
106
- // /
107
- // / \param dc The context in which we need a witness.
108
- // /
109
- // / \param type The type whose witness to find.
110
- // /
111
- // / \param proto The protocol to which the type conforms.
112
- // /
113
- // / \param name The name of the requirement.
114
- // /
115
- // / \param diag The diagnostic to emit if the protocol definition doesn't
116
- // / have a requirement with the given name.
117
- // /
118
- // / \returns The named witness, or an empty ConcreteDeclRef if no witness
119
- // / could be found.
120
- ConcreteDeclRef findNamedWitnessImpl (
121
- TypeChecker &tc, DeclContext *dc, Type type,
122
- ProtocolDecl *proto, DeclName name,
123
- Diag<> diag,
124
- Optional<ProtocolConformanceRef> conformance = None) {
125
- // Find the named requirement.
126
- ValueDecl *requirement = nullptr ;
127
- for (auto member : proto->getMembers ()) {
128
- auto d = dyn_cast<ValueDecl>(member);
129
- if (!d || !d->hasName ())
130
- continue ;
131
-
132
- if (d->getFullName ().matchesRef (name)) {
133
- requirement = d;
134
- break ;
135
- }
136
- }
137
-
138
- if (!requirement || requirement->isInvalid ()) {
139
- tc.diagnose (proto->getLoc (), diag);
140
- return nullptr ;
141
- }
142
-
143
- // Find the member used to satisfy the named requirement.
144
- if (!conformance) {
145
- conformance = tc.conformsToProtocol (type, proto, dc,
146
- ConformanceCheckFlags::InExpression);
147
- if (!conformance)
148
- return nullptr ;
149
- }
150
-
151
- // For a type with dependent conformance, just return the requirement from
152
- // the protocol. There are no protocol conformance tables.
153
- if (!conformance->isConcrete ()) {
154
- auto subMap = SubstitutionMap::getProtocolSubstitutions (proto, type,
155
- *conformance);
156
- return ConcreteDeclRef (requirement, subMap);
157
- }
158
-
159
- auto concrete = conformance->getConcrete ();
160
- return concrete->getWitnessDeclRef (requirement, &tc);
161
- }
162
-
163
102
static bool shouldAccessStorageDirectly (Expr *base, VarDecl *member,
164
103
DeclContext *DC) {
165
104
// This only matters for stored properties.
@@ -2974,10 +2913,8 @@ namespace {
2974
2913
2975
2914
DeclName name (tc.Context , DeclBaseName::createConstructor (),
2976
2915
{ tc.Context .Id_arrayLiteral });
2977
-
2978
2916
ConcreteDeclRef witness =
2979
- findNamedWitnessImpl (tc, dc, arrayTy->getRValueType (), arrayProto,
2980
- name, diag::array_protocol_broken, conformance);
2917
+ conformance->getWitnessByName (arrayTy->getRValueType (), name);
2981
2918
if (!witness || !isa<AbstractFunctionDecl>(witness.getDecl ()))
2982
2919
return nullptr ;
2983
2920
expr->setInitializer (witness);
@@ -3330,8 +3267,20 @@ namespace {
3330
3267
return nullptr ;
3331
3268
3332
3269
// Extract a Bool from the resulting expression.
3333
- return solution.convertOptionalToBool (result,
3334
- cs.getConstraintLocator (expr));
3270
+ tc.requireOptionalIntrinsics (expr->getLoc ());
3271
+
3272
+ // Match the optional value against its `Some` case.
3273
+ auto &ctx = tc.Context ;
3274
+ auto *someDecl = tc.Context .getOptionalSomeDecl ();
3275
+ auto isSomeExpr = new (tc.Context ) EnumIsCaseExpr (result, someDecl);
3276
+ auto boolDecl = ctx.getBoolDecl ();
3277
+
3278
+ if (!boolDecl) {
3279
+ tc.diagnose (SourceLoc (), diag::broken_bool);
3280
+ }
3281
+
3282
+ cs.setType (isSomeExpr, boolDecl ? boolDecl->getDeclaredType () : Type ());
3283
+ return isSomeExpr;
3335
3284
}
3336
3285
3337
3286
return expr;
@@ -3973,10 +3922,7 @@ namespace {
3973
3922
}
3974
3923
3975
3924
Expr *visitLazyInitializerExpr (LazyInitializerExpr *expr) {
3976
- // Since `LazyInitializerExpr` should always have a type set,
3977
- // there is no need to do anything here.
3978
- assert (cs.getType (expr)->isEqual (expr->getSubExpr ()->getType ()));
3979
- return expr;
3925
+ llvm_unreachable (" Already type-checked" );
3980
3926
}
3981
3927
3982
3928
Expr *visitEditorPlaceholderExpr (EditorPlaceholderExpr *E) {
@@ -6845,10 +6791,8 @@ Expr *ExprRewriter::convertLiteralInPlace(Expr *literal,
6845
6791
6846
6792
// Find the witness that we'll use to initialize the type via a builtin
6847
6793
// literal.
6848
- auto witness = findNamedWitnessImpl (
6849
- tc, dc, type->getRValueType (), builtinProtocol,
6850
- builtinLiteralFuncName, brokenBuiltinProtocolDiag,
6851
- *builtinConformance);
6794
+ auto witness = builtinConformance->getWitnessByName (type->getRValueType (),
6795
+ builtinLiteralFuncName);
6852
6796
if (!witness || !isa<AbstractFunctionDecl>(witness.getDecl ()))
6853
6797
return nullptr ;
6854
6798
@@ -6896,10 +6840,8 @@ Expr *ExprRewriter::convertLiteralInPlace(Expr *literal,
6896
6840
}
6897
6841
6898
6842
// Find the witness that we'll use to initialize the literal value.
6899
- auto witness = findNamedWitnessImpl (
6900
- tc, dc, type->getRValueType (), protocol,
6901
- literalFuncName, brokenProtocolDiag,
6902
- conformance);
6843
+ auto witness = conformance->getWitnessByName (type->getRValueType (),
6844
+ literalFuncName);
6903
6845
if (!witness || !isa<AbstractFunctionDecl>(witness.getDecl ()))
6904
6846
return nullptr ;
6905
6847
@@ -7743,10 +7685,9 @@ Expr *TypeChecker::callWitness(Expr *base, DeclContext *dc,
7743
7685
7744
7686
if (auto metaType = type->getAs <AnyMetatypeType>())
7745
7687
type = metaType->getInstanceType ();
7746
-
7747
- auto witness = findNamedWitnessImpl (
7748
- *this , dc, type->getRValueType (), protocol,
7749
- name, brokenProtocolDiag);
7688
+
7689
+ // Find the member used to satisfy the named requirement.
7690
+ auto witness = conformance.getWitnessByName (type, name);
7750
7691
if (!witness || !isa<AbstractFunctionDecl>(witness.getDecl ()))
7751
7692
return nullptr ;
7752
7693
@@ -7853,22 +7794,3 @@ Expr *TypeChecker::callWitness(Expr *base, DeclContext *dc,
7853
7794
rewriter.finalize (result);
7854
7795
return result;
7855
7796
}
7856
-
7857
- Expr *Solution::convertOptionalToBool (Expr *expr,
7858
- ConstraintLocator *locator) const {
7859
- auto &cs = getConstraintSystem ();
7860
- auto &tc = cs.getTypeChecker ();
7861
- tc.requireOptionalIntrinsics (expr->getLoc ());
7862
-
7863
- // Match the optional value against its `Some` case.
7864
- auto &ctx = tc.Context ;
7865
- auto isSomeExpr = new (ctx) EnumIsCaseExpr (expr, ctx.getOptionalSomeDecl ());
7866
- auto boolDecl = ctx.getBoolDecl ();
7867
-
7868
- if (!boolDecl) {
7869
- tc.diagnose (SourceLoc (), diag::broken_bool);
7870
- }
7871
-
7872
- cs.setType (isSomeExpr, boolDecl ? boolDecl->getDeclaredType () : Type ());
7873
- return isSomeExpr;
7874
- }
0 commit comments