Skip to content

Commit 7caf193

Browse files
committed
Sema: AssociatedTypeInference::solve() doesn't need a ConformanceChecker anymore
1 parent 9831b0c commit 7caf193

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

lib/Sema/AssociatedTypeInference.cpp

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,17 @@ static void recordTypeWitness(NormalProtocolConformance *conformance,
344344
}
345345

346346
/// Attempt to resolve a type witness via member name lookup.
347-
ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
347+
static ResolveWitnessResult resolveTypeWitnessViaLookup(
348+
NormalProtocolConformance *conformance,
348349
AssociatedTypeDecl *assocType) {
350+
auto *dc = conformance->getDeclContext();
351+
auto &ctx = dc->getASTContext();
352+
349353
// Conformances constructed by the ClangImporter should have explicit type
350354
// witnesses already.
351-
if (isa<ClangModuleUnit>(Conformance->getDeclContext()->getModuleScopeContext())) {
355+
if (isa<ClangModuleUnit>(dc->getModuleScopeContext())) {
352356
llvm::errs() << "Cannot look up associated type for imported conformance:\n";
353-
Conformance->getType().dump(llvm::errs());
357+
conformance->getType().dump(llvm::errs());
354358
assocType->dump(llvm::errs());
355359
abort();
356360
}
@@ -360,10 +364,9 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
360364
// Look for a member type with the same name as the associated type.
361365
SmallVector<ValueDecl *, 4> candidates;
362366

363-
DC->lookupQualified(DC->getSelfNominalTypeDecl(),
364-
assocType->createNameRef(),
365-
DC->getSelfNominalTypeDecl()->getLoc(),
366-
subOptions, candidates);
367+
dc->lookupQualified(dc->getSelfNominalTypeDecl(), assocType->createNameRef(),
368+
dc->getSelfNominalTypeDecl()->getLoc(), subOptions,
369+
candidates);
367370

368371
// If there aren't any candidates, we're done.
369372
if (candidates.empty()) {
@@ -402,7 +405,7 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
402405
typeAliasDecl->getParentSourceFile()->Kind == SourceFileKind::Interface) {
403406
if (typeAliasDecl->getUnderlyingType()->isNever()) {
404407
if (typeAliasDecl->getDeclContext()->getSelfNominalTypeDecl() ==
405-
DC->getSelfNominalTypeDecl()) {
408+
dc->getSelfNominalTypeDecl()) {
406409
skipRequirementCheck = true;
407410
}
408411
}
@@ -429,13 +432,13 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
429432
// clause, check those requirements now.
430433
if (!skipRequirementCheck &&
431434
!TypeChecker::checkContextualRequirements(
432-
genericDecl, Adoptee, SourceLoc(), DC->getParentModule(),
433-
DC->getGenericSignatureOfContext())) {
435+
genericDecl, dc->getSelfInterfaceType(), SourceLoc(),
436+
dc->getParentModule(), dc->getGenericSignatureOfContext())) {
434437
continue;
435438
}
436439

437-
auto memberType = TypeChecker::substMemberTypeWithBase(DC->getParentModule(),
438-
typeDecl, Adoptee);
440+
auto memberType = TypeChecker::substMemberTypeWithBase(
441+
dc->getParentModule(), typeDecl, dc->getSelfInterfaceType());
439442

440443
// Type witnesses that resolve to constraint types are always
441444
// existential types. This can only happen when the type witness
@@ -455,7 +458,7 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
455458

456459
// Check this type against the protocol requirements.
457460
if (auto checkResult =
458-
checkTypeWitness(memberType, assocType, Conformance, llvm::None)) {
461+
checkTypeWitness(memberType, assocType, conformance, llvm::None)) {
459462
nonViable.push_back({typeDecl, checkResult});
460463
} else {
461464
viable.push_back({typeDecl, memberType, nullptr});
@@ -475,18 +478,18 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
475478
// If there is a single viable candidate, form a substitution for it.
476479
if (viable.size() == 1) {
477480
auto interfaceType = viable.front().MemberType;
478-
recordTypeWitness(Conformance, assocType, interfaceType,
481+
recordTypeWitness(conformance, assocType, interfaceType,
479482
viable.front().Member);
480483
return ResolveWitnessResult::Success;
481484
}
482485

483486
// Record an error.
484-
recordTypeWitness(Conformance, assocType,
485-
ErrorType::get(getASTContext()), nullptr);
487+
recordTypeWitness(conformance, assocType,
488+
ErrorType::get(ctx), nullptr);
486489

487490
// If we had multiple viable types, diagnose the ambiguity.
488491
if (!viable.empty()) {
489-
getASTContext().addDelayedConformanceDiag(Conformance, true,
492+
ctx.addDelayedConformanceDiag(conformance, true,
490493
[assocType, viable](NormalProtocolConformance *conformance) {
491494
auto &diags = assocType->getASTContext().Diags;
492495
diags.diagnose(assocType, diag::ambiguous_witnesses_type,
@@ -499,10 +502,10 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
499502
return ResolveWitnessResult::ExplicitFailed;
500503
}
501504
// Save the missing type witness for later diagnosis.
502-
getASTContext().addDelayedMissingWitness(Conformance, {assocType, {}});
505+
ctx.addDelayedMissingWitness(conformance, {assocType, {}});
503506

504507
// None of the candidates were viable.
505-
getASTContext().addDelayedConformanceDiag(Conformance, true,
508+
ctx.addDelayedConformanceDiag(conformance, true,
506509
[nonViable](NormalProtocolConformance *conformance) {
507510
auto &diags = conformance->getDeclContext()->getASTContext().Diags;
508511
for (auto candidate : nonViable) {
@@ -1063,7 +1066,7 @@ class AssociatedTypeInference {
10631066
/// Perform associated type inference.
10641067
///
10651068
/// \returns \c true if an error occurred, \c false otherwise
1066-
llvm::Optional<InferredTypeWitnesses> solve(ConformanceChecker &checker);
1069+
llvm::Optional<InferredTypeWitnesses> solve();
10671070
};
10681071

10691072
}
@@ -3506,7 +3509,7 @@ bool AssociatedTypeInference::canAttemptEagerTypeWitnessDerivation(
35063509
return false;
35073510
}
35083511

3509-
auto AssociatedTypeInference::solve(ConformanceChecker &checker)
3512+
auto AssociatedTypeInference::solve()
35103513
-> llvm::Optional<InferredTypeWitnesses> {
35113514
LLVM_DEBUG(llvm::dbgs() << "============ Start " << conformance->getType()
35123515
<< ": " << conformance->getProtocol()->getName()
@@ -3542,7 +3545,7 @@ auto AssociatedTypeInference::solve(ConformanceChecker &checker)
35423545

35433546
// Try to resolve this type witness via name lookup, which is the
35443547
// most direct mechanism, overriding all others.
3545-
switch (checker.resolveTypeWitnessViaLookup(assocType)) {
3548+
switch (resolveTypeWitnessViaLookup(conformance, assocType)) {
35463549
case ResolveWitnessResult::Success:
35473550
// Success. Move on to the next.
35483551
LLVM_DEBUG(llvm::dbgs() << "Associated type " << assocType->getName()
@@ -3598,15 +3601,15 @@ auto AssociatedTypeInference::solve(ConformanceChecker &checker)
35983601
// new type declaration.
35993602
// FIXME: This is ridiculous.
36003603
for (auto assocType : unresolvedAssocTypes) {
3601-
switch (checker.resolveTypeWitnessViaLookup(assocType)) {
3604+
switch (resolveTypeWitnessViaLookup(conformance, assocType)) {
36023605
case ResolveWitnessResult::Success:
36033606
case ResolveWitnessResult::ExplicitFailed:
36043607
// A declaration that can become a witness has shown up. Go
36053608
// perform the resolution again now that we have more
36063609
// information.
36073610
LLVM_DEBUG(llvm::dbgs() << "Associated type " << assocType->getName()
36083611
<< " now has a valid witness\n";);
3609-
return solve(checker);
3612+
return solve();
36103613

36113614
case ResolveWitnessResult::Missing:
36123615
// The type witness is still missing. Keep going.
@@ -3950,7 +3953,7 @@ void ConformanceChecker::resolveTypeWitnesses() {
39503953

39513954
// Attempt to infer associated type witnesses.
39523955
AssociatedTypeInference inference(getASTContext(), Conformance);
3953-
if (auto inferred = inference.solve(*this)) {
3956+
if (auto inferred = inference.solve()) {
39543957
for (const auto &inferredWitness : *inferred) {
39553958
recordTypeWitness(Conformance,
39563959
inferredWitness.first,
@@ -3978,7 +3981,7 @@ void ConformanceChecker::resolveTypeWitnesses() {
39783981

39793982
void ConformanceChecker::resolveSingleTypeWitness(
39803983
AssociatedTypeDecl *assocType) {
3981-
switch (resolveTypeWitnessViaLookup(assocType)) {
3984+
switch (resolveTypeWitnessViaLookup(Conformance, assocType)) {
39823985
case ResolveWitnessResult::Success:
39833986
case ResolveWitnessResult::ExplicitFailed:
39843987
// We resolved this type witness one way or another.

lib/Sema/TypeCheckProtocol.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,6 @@ class ConformanceChecker : public WitnessChecker {
167167
ResolveWitnessResult
168168
resolveWitnessTryingAllStrategies(ValueDecl *requirement);
169169

170-
/// Attempt to resolve a type witness via member name lookup.
171-
ResolveWitnessResult resolveTypeWitnessViaLookup(
172-
AssociatedTypeDecl *assocType);
173-
174170
/// Check whether all of the protocol's generic requirements are satisfied by
175171
/// the chosen type witnesses.
176172
void ensureRequirementsAreSatisfied();

0 commit comments

Comments
 (0)