@@ -344,13 +344,17 @@ static void recordTypeWitness(NormalProtocolConformance *conformance,
344
344
}
345
345
346
346
// / Attempt to resolve a type witness via member name lookup.
347
- ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup (
347
+ static ResolveWitnessResult resolveTypeWitnessViaLookup (
348
+ NormalProtocolConformance *conformance,
348
349
AssociatedTypeDecl *assocType) {
350
+ auto *dc = conformance->getDeclContext ();
351
+ auto &ctx = dc->getASTContext ();
352
+
349
353
// Conformances constructed by the ClangImporter should have explicit type
350
354
// witnesses already.
351
- if (isa<ClangModuleUnit>(Conformance-> getDeclContext () ->getModuleScopeContext ())) {
355
+ if (isa<ClangModuleUnit>(dc ->getModuleScopeContext ())) {
352
356
llvm::errs () << " Cannot look up associated type for imported conformance:\n " ;
353
- Conformance ->getType ().dump (llvm::errs ());
357
+ conformance ->getType ().dump (llvm::errs ());
354
358
assocType->dump (llvm::errs ());
355
359
abort ();
356
360
}
@@ -360,10 +364,9 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
360
364
// Look for a member type with the same name as the associated type.
361
365
SmallVector<ValueDecl *, 4 > candidates;
362
366
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);
367
370
368
371
// If there aren't any candidates, we're done.
369
372
if (candidates.empty ()) {
@@ -402,7 +405,7 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
402
405
typeAliasDecl->getParentSourceFile ()->Kind == SourceFileKind::Interface) {
403
406
if (typeAliasDecl->getUnderlyingType ()->isNever ()) {
404
407
if (typeAliasDecl->getDeclContext ()->getSelfNominalTypeDecl () ==
405
- DC ->getSelfNominalTypeDecl ()) {
408
+ dc ->getSelfNominalTypeDecl ()) {
406
409
skipRequirementCheck = true ;
407
410
}
408
411
}
@@ -429,13 +432,13 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
429
432
// clause, check those requirements now.
430
433
if (!skipRequirementCheck &&
431
434
!TypeChecker::checkContextualRequirements (
432
- genericDecl, Adoptee, SourceLoc (), DC-> getParentModule (),
433
- DC ->getGenericSignatureOfContext ())) {
435
+ genericDecl, dc-> getSelfInterfaceType (), SourceLoc (),
436
+ dc-> getParentModule (), dc ->getGenericSignatureOfContext ())) {
434
437
continue ;
435
438
}
436
439
437
- auto memberType = TypeChecker::substMemberTypeWithBase (DC-> getParentModule (),
438
- typeDecl, Adoptee );
440
+ auto memberType = TypeChecker::substMemberTypeWithBase (
441
+ dc-> getParentModule (), typeDecl, dc-> getSelfInterfaceType () );
439
442
440
443
// Type witnesses that resolve to constraint types are always
441
444
// existential types. This can only happen when the type witness
@@ -455,7 +458,7 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
455
458
456
459
// Check this type against the protocol requirements.
457
460
if (auto checkResult =
458
- checkTypeWitness (memberType, assocType, Conformance , llvm::None)) {
461
+ checkTypeWitness (memberType, assocType, conformance , llvm::None)) {
459
462
nonViable.push_back ({typeDecl, checkResult});
460
463
} else {
461
464
viable.push_back ({typeDecl, memberType, nullptr });
@@ -475,18 +478,18 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
475
478
// If there is a single viable candidate, form a substitution for it.
476
479
if (viable.size () == 1 ) {
477
480
auto interfaceType = viable.front ().MemberType ;
478
- recordTypeWitness (Conformance , assocType, interfaceType,
481
+ recordTypeWitness (conformance , assocType, interfaceType,
479
482
viable.front ().Member );
480
483
return ResolveWitnessResult::Success;
481
484
}
482
485
483
486
// Record an error.
484
- recordTypeWitness (Conformance , assocType,
485
- ErrorType::get (getASTContext () ), nullptr );
487
+ recordTypeWitness (conformance , assocType,
488
+ ErrorType::get (ctx ), nullptr );
486
489
487
490
// If we had multiple viable types, diagnose the ambiguity.
488
491
if (!viable.empty ()) {
489
- getASTContext () .addDelayedConformanceDiag (Conformance , true ,
492
+ ctx .addDelayedConformanceDiag (conformance , true ,
490
493
[assocType, viable](NormalProtocolConformance *conformance) {
491
494
auto &diags = assocType->getASTContext ().Diags ;
492
495
diags.diagnose (assocType, diag::ambiguous_witnesses_type,
@@ -499,10 +502,10 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
499
502
return ResolveWitnessResult::ExplicitFailed;
500
503
}
501
504
// Save the missing type witness for later diagnosis.
502
- getASTContext () .addDelayedMissingWitness (Conformance , {assocType, {}});
505
+ ctx .addDelayedMissingWitness (conformance , {assocType, {}});
503
506
504
507
// None of the candidates were viable.
505
- getASTContext () .addDelayedConformanceDiag (Conformance , true ,
508
+ ctx .addDelayedConformanceDiag (conformance , true ,
506
509
[nonViable](NormalProtocolConformance *conformance) {
507
510
auto &diags = conformance->getDeclContext ()->getASTContext ().Diags ;
508
511
for (auto candidate : nonViable) {
@@ -1063,7 +1066,7 @@ class AssociatedTypeInference {
1063
1066
// / Perform associated type inference.
1064
1067
// /
1065
1068
// / \returns \c true if an error occurred, \c false otherwise
1066
- llvm::Optional<InferredTypeWitnesses> solve (ConformanceChecker &checker );
1069
+ llvm::Optional<InferredTypeWitnesses> solve ();
1067
1070
};
1068
1071
1069
1072
}
@@ -3506,7 +3509,7 @@ bool AssociatedTypeInference::canAttemptEagerTypeWitnessDerivation(
3506
3509
return false ;
3507
3510
}
3508
3511
3509
- auto AssociatedTypeInference::solve (ConformanceChecker &checker )
3512
+ auto AssociatedTypeInference::solve ()
3510
3513
-> llvm::Optional<InferredTypeWitnesses> {
3511
3514
LLVM_DEBUG (llvm::dbgs () << " ============ Start " << conformance->getType ()
3512
3515
<< " : " << conformance->getProtocol ()->getName ()
@@ -3542,7 +3545,7 @@ auto AssociatedTypeInference::solve(ConformanceChecker &checker)
3542
3545
3543
3546
// Try to resolve this type witness via name lookup, which is the
3544
3547
// most direct mechanism, overriding all others.
3545
- switch (checker. resolveTypeWitnessViaLookup (assocType)) {
3548
+ switch (resolveTypeWitnessViaLookup (conformance, assocType)) {
3546
3549
case ResolveWitnessResult::Success:
3547
3550
// Success. Move on to the next.
3548
3551
LLVM_DEBUG (llvm::dbgs () << " Associated type " << assocType->getName ()
@@ -3598,15 +3601,15 @@ auto AssociatedTypeInference::solve(ConformanceChecker &checker)
3598
3601
// new type declaration.
3599
3602
// FIXME: This is ridiculous.
3600
3603
for (auto assocType : unresolvedAssocTypes) {
3601
- switch (checker. resolveTypeWitnessViaLookup (assocType)) {
3604
+ switch (resolveTypeWitnessViaLookup (conformance, assocType)) {
3602
3605
case ResolveWitnessResult::Success:
3603
3606
case ResolveWitnessResult::ExplicitFailed:
3604
3607
// A declaration that can become a witness has shown up. Go
3605
3608
// perform the resolution again now that we have more
3606
3609
// information.
3607
3610
LLVM_DEBUG (llvm::dbgs () << " Associated type " << assocType->getName ()
3608
3611
<< " now has a valid witness\n " ;);
3609
- return solve (checker );
3612
+ return solve ();
3610
3613
3611
3614
case ResolveWitnessResult::Missing:
3612
3615
// The type witness is still missing. Keep going.
@@ -3950,7 +3953,7 @@ void ConformanceChecker::resolveTypeWitnesses() {
3950
3953
3951
3954
// Attempt to infer associated type witnesses.
3952
3955
AssociatedTypeInference inference (getASTContext (), Conformance);
3953
- if (auto inferred = inference.solve (* this )) {
3956
+ if (auto inferred = inference.solve ()) {
3954
3957
for (const auto &inferredWitness : *inferred) {
3955
3958
recordTypeWitness (Conformance,
3956
3959
inferredWitness.first ,
@@ -3978,7 +3981,7 @@ void ConformanceChecker::resolveTypeWitnesses() {
3978
3981
3979
3982
void ConformanceChecker::resolveSingleTypeWitness (
3980
3983
AssociatedTypeDecl *assocType) {
3981
- switch (resolveTypeWitnessViaLookup (assocType)) {
3984
+ switch (resolveTypeWitnessViaLookup (Conformance, assocType)) {
3982
3985
case ResolveWitnessResult::Success:
3983
3986
case ResolveWitnessResult::ExplicitFailed:
3984
3987
// We resolved this type witness one way or another.
0 commit comments