160
160
#include " llvm/ADT/SmallVector.h"
161
161
#include " llvm/ADT/SetVector.h"
162
162
#include " RewriteContext.h"
163
+ #include " NameLookup.h"
163
164
164
165
using namespace swift ;
165
166
using namespace rewriting ;
@@ -384,12 +385,40 @@ swift::rewriting::desugarRequirement(Requirement req, SourceLoc loc,
384
385
// Requirement realization and inference.
385
386
//
386
387
387
- static void realizeTypeRequirement (Type subjectType, Type constraintType,
388
+ static void realizeTypeRequirement (DeclContext *dc,
389
+ Type subjectType, Type constraintType,
388
390
SourceLoc loc,
389
391
SmallVectorImpl<StructuralRequirement> &result,
390
392
SmallVectorImpl<RequirementError> &errors) {
391
393
SmallVector<Requirement, 2 > reqs;
392
394
395
+ // The GenericSignatureBuilder allowed the right hand side of a
396
+ // conformance or superclass requirement to reference a protocol
397
+ // typealias whose underlying type was a protocol or class.
398
+ //
399
+ // Since protocol typealiases resolve to DependentMemberTypes in
400
+ // ::Structural mode, this relied on the GSB's "delayed requirements"
401
+ // mechanism.
402
+ //
403
+ // The RequirementMachine does not have an equivalent, and cannot really
404
+ // support that because we need to collect the protocols mentioned on
405
+ // the right hand sides of conformance requirements ahead of time.
406
+ //
407
+ // However, we can support it in simple cases where the typealias is
408
+ // defined in the protocol itself and is accessed as a member of 'Self'.
409
+ if (auto *proto = dc->getSelfProtocolDecl ()) {
410
+ if (auto memberType = constraintType->getAs <DependentMemberType>()) {
411
+ if (memberType->getBase ()->isEqual (proto->getSelfInterfaceType ())) {
412
+ SmallVector<TypeDecl *, 1 > result;
413
+ lookupConcreteNestedType (proto, memberType->getName (), result);
414
+ auto *typeDecl = findBestConcreteNestedType (result);
415
+ if (auto *aliasDecl = dyn_cast_or_null<TypeAliasDecl>(typeDecl)) {
416
+ constraintType = aliasDecl->getUnderlyingType ();
417
+ }
418
+ }
419
+ }
420
+ }
421
+
393
422
if (constraintType->isConstraintType ()) {
394
423
// Handle conformance requirements.
395
424
desugarConformanceRequirement (subjectType, constraintType, loc, reqs, errors);
@@ -534,18 +563,20 @@ void swift::rewriting::inferRequirements(
534
563
// / Desugar a requirement and perform requirement inference if requested
535
564
// / to obtain zero or more structural requirements.
536
565
void swift::rewriting::realizeRequirement (
566
+ DeclContext *dc,
537
567
Requirement req, RequirementRepr *reqRepr,
538
- ModuleDecl *moduleForInference ,
568
+ bool shouldInferRequirements ,
539
569
SmallVectorImpl<StructuralRequirement> &result,
540
570
SmallVectorImpl<RequirementError> &errors) {
541
571
auto firstType = req.getFirstType ();
542
572
auto loc = (reqRepr ? reqRepr->getSeparatorLoc () : SourceLoc ());
573
+ auto *moduleForInference = dc->getParentModule ();
543
574
544
575
switch (req.getKind ()) {
545
576
case RequirementKind::Superclass:
546
577
case RequirementKind::Conformance: {
547
578
auto secondType = req.getSecondType ();
548
- if (moduleForInference ) {
579
+ if (shouldInferRequirements ) {
549
580
auto firstLoc = (reqRepr ? reqRepr->getSubjectRepr ()->getStartLoc ()
550
581
: SourceLoc ());
551
582
inferRequirements (firstType, firstLoc, moduleForInference, result);
@@ -555,12 +586,12 @@ void swift::rewriting::realizeRequirement(
555
586
inferRequirements (secondType, secondLoc, moduleForInference, result);
556
587
}
557
588
558
- realizeTypeRequirement (firstType, secondType, loc, result, errors);
589
+ realizeTypeRequirement (dc, firstType, secondType, loc, result, errors);
559
590
break ;
560
591
}
561
592
562
593
case RequirementKind::Layout: {
563
- if (moduleForInference ) {
594
+ if (shouldInferRequirements ) {
564
595
auto firstLoc = (reqRepr ? reqRepr->getSubjectRepr ()->getStartLoc ()
565
596
: SourceLoc ());
566
597
inferRequirements (firstType, firstLoc, moduleForInference, result);
@@ -578,7 +609,7 @@ void swift::rewriting::realizeRequirement(
578
609
579
610
case RequirementKind::SameType: {
580
611
auto secondType = req.getSecondType ();
581
- if (moduleForInference ) {
612
+ if (shouldInferRequirements ) {
582
613
auto firstLoc = (reqRepr ? reqRepr->getFirstTypeRepr ()->getStartLoc ()
583
614
: SourceLoc ());
584
615
inferRequirements (firstType, firstLoc, moduleForInference, result);
@@ -602,11 +633,13 @@ void swift::rewriting::realizeRequirement(
602
633
// / Collect structural requirements written in the inheritance clause of an
603
634
// / AssociatedTypeDecl or GenericTypeParamDecl.
604
635
void swift::rewriting::realizeInheritedRequirements (
605
- TypeDecl *decl, Type type, ModuleDecl *moduleForInference ,
636
+ TypeDecl *decl, Type type, bool shouldInferRequirements ,
606
637
SmallVectorImpl<StructuralRequirement> &result,
607
638
SmallVectorImpl<RequirementError> &errors) {
608
639
auto &ctx = decl->getASTContext ();
609
640
auto inheritedTypes = decl->getInherited ();
641
+ auto *dc = decl->getInnermostDeclContext ();
642
+ auto *moduleForInference = dc->getParentModule ();
610
643
611
644
for (unsigned index : indices (inheritedTypes)) {
612
645
Type inheritedType
@@ -616,41 +649,13 @@ void swift::rewriting::realizeInheritedRequirements(
616
649
Type ());
617
650
if (!inheritedType) continue ;
618
651
619
- // The GenericSignatureBuilder allowed an associated type's inheritance
620
- // clause to reference a protocol typealias whose underlying type was a
621
- // protocol or class.
622
- //
623
- // Since protocol typealiases resolve to DependentMemberTypes in
624
- // ::Structural mode, this relied on the GSB's "delayed requirements"
625
- // mechanism.
626
- //
627
- // The RequirementMachine does not have an equivalent, and cannot really
628
- // support that because we need to collect the protocols mentioned on
629
- // the right hand sides of conformance requirements ahead of time.
630
- //
631
- // However, we can support it in simple cases where the typealias is
632
- // defined in the protocol itself and is accessed as a member of 'Self'.
633
- if (auto *assocTypeDecl = dyn_cast<AssociatedTypeDecl>(decl)) {
634
- if (auto memberType = inheritedType->getAs <DependentMemberType>()) {
635
- if (memberType->getBase ()->isEqual (
636
- assocTypeDecl->getProtocol ()->getSelfInterfaceType ())) {
637
- inheritedType
638
- = evaluateOrDefault (ctx.evaluator ,
639
- InheritedTypeRequest{decl, index,
640
- TypeResolutionStage::Interface},
641
- Type ());
642
- if (!inheritedType) continue ;
643
- }
644
- }
645
- }
646
-
647
652
auto *typeRepr = inheritedTypes[index].getTypeRepr ();
648
653
SourceLoc loc = (typeRepr ? typeRepr->getStartLoc () : SourceLoc ());
649
- if (moduleForInference ) {
654
+ if (shouldInferRequirements ) {
650
655
inferRequirements (inheritedType, loc, moduleForInference, result);
651
656
}
652
657
653
- realizeTypeRequirement (type, inheritedType, loc, result, errors);
658
+ realizeTypeRequirement (dc, type, inheritedType, loc, result, errors);
654
659
}
655
660
}
656
661
@@ -823,14 +828,14 @@ StructuralRequirementsRequest::evaluate(Evaluator &evaluator,
823
828
auto selfTy = proto->getSelfInterfaceType ();
824
829
825
830
realizeInheritedRequirements (proto, selfTy,
826
- /* moduleForInference =*/ nullptr ,
831
+ /* inferRequirements =*/ false ,
827
832
result, errors);
828
833
829
834
// Add requirements from the protocol's own 'where' clause.
830
835
WhereClauseOwner (proto).visitRequirements (TypeResolutionStage::Structural,
831
836
[&](const Requirement &req, RequirementRepr *reqRepr) {
832
- realizeRequirement (req, reqRepr,
833
- /* moduleForInference =*/ nullptr ,
837
+ realizeRequirement (proto, req, reqRepr,
838
+ /* inferRequirements =*/ false ,
834
839
result, errors);
835
840
return false ;
836
841
});
@@ -855,15 +860,15 @@ StructuralRequirementsRequest::evaluate(Evaluator &evaluator,
855
860
// Add requirements placed directly on this associated type.
856
861
auto assocType = assocTypeDecl->getDeclaredInterfaceType ();
857
862
realizeInheritedRequirements (assocTypeDecl, assocType,
858
- /* moduleForInference =*/ nullptr ,
863
+ /* inferRequirements =*/ false ,
859
864
result, errors);
860
865
861
866
// Add requirements from this associated type's where clause.
862
867
WhereClauseOwner (assocTypeDecl).visitRequirements (
863
868
TypeResolutionStage::Structural,
864
869
[&](const Requirement &req, RequirementRepr *reqRepr) {
865
- realizeRequirement (req, reqRepr,
866
- /* moduleForInference =*/ nullptr ,
870
+ realizeRequirement (proto, req, reqRepr,
871
+ /* inferRequirements =*/ false ,
867
872
result, errors);
868
873
return false ;
869
874
});
0 commit comments