Skip to content

Commit 957a5f4

Browse files
committed
[CSOptimizer] Treat all type parameters equally
Dependent members just like generic parameter types could have associated protocol conformances which give us a glimpse into whether arguments could possibly match.
1 parent 2c44e37 commit 957a5f4

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

lib/Sema/CSOptimizer.cpp

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,6 @@ static void determineBestChoicesInContext(
252252
scoreCandidateMatch = [&](GenericSignature genericSig,
253253
Type candidateType, Type paramType,
254254
MatchOptions options) -> double {
255-
// Dependent members cannot be handled here because
256-
// they require substitution of the base type which
257-
// could come from a different argument.
258-
if (paramType->getAs<DependentMemberType>())
259-
return 0;
260-
261255
// Exact match between candidate and parameter types.
262256
if (candidateType->isEqual(paramType))
263257
return options.contains(MatchFlag::Literal) ? 0.3 : 1;
@@ -309,25 +303,22 @@ static void determineBestChoicesInContext(
309303

310304
// Check protocol requirement(s) if this parameter is a
311305
// generic parameter type.
312-
GenericSignature::RequiredProtocols protocolRequirements;
313-
if (genericSig) {
314-
if (auto *GP = paramType->getAs<GenericTypeParamType>()) {
315-
protocolRequirements = genericSig->getRequiredProtocols(GP);
316-
// It's a generic parameter which might be connected via
317-
// same-type constraints to other generic parameters but
318-
// we cannot check that here, so let's add a tiny score
319-
// just to acknowledge that it could possibly match.
320-
if (protocolRequirements.empty()) {
321-
return 0.01;
322-
}
323-
324-
if (llvm::all_of(protocolRequirements, [&](ProtocolDecl *protocol) {
325-
return TypeChecker::conformsToProtocol(candidateType, protocol,
326-
cs.DC->getParentModule(),
327-
/*allowMissing=*/false);
328-
}))
329-
return 0.7;
330-
}
306+
if (genericSig && paramType->isTypeParameter()) {
307+
auto protocolRequirements = genericSig->getRequiredProtocols(paramType);
308+
// It's a generic parameter or dependent member which might
309+
// be connected via ame-type constraints to other generic
310+
// parameters or dependent member but we cannot check that here,
311+
// so let's add a tiny score just to acknowledge that it could
312+
// possibly match.
313+
if (protocolRequirements.empty())
314+
return 0.01;
315+
316+
if (llvm::all_of(protocolRequirements, [&](ProtocolDecl *protocol) {
317+
return TypeChecker::conformsToProtocol(candidateType, protocol,
318+
cs.DC->getParentModule(),
319+
/*allowMissing=*/false);
320+
}))
321+
return 0.7;
331322
}
332323

333324
// Parameter is generic, let's check whether top-level

0 commit comments

Comments
 (0)