160160#include " llvm/ADT/SmallVector.h"
161161#include " llvm/ADT/SetVector.h"
162162#include " RewriteContext.h"
163+ #include " NameLookup.h"
163164
164165using namespace swift ;
165166using namespace rewriting ;
@@ -384,12 +385,40 @@ swift::rewriting::desugarRequirement(Requirement req, SourceLoc loc,
384385// Requirement realization and inference.
385386//
386387
387- static void realizeTypeRequirement (Type subjectType, Type constraintType,
388+ static void realizeTypeRequirement (DeclContext *dc,
389+ Type subjectType, Type constraintType,
388390 SourceLoc loc,
389391 SmallVectorImpl<StructuralRequirement> &result,
390392 SmallVectorImpl<RequirementError> &errors) {
391393 SmallVector<Requirement, 2 > reqs;
392394
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+
393422 if (constraintType->isConstraintType ()) {
394423 // Handle conformance requirements.
395424 desugarConformanceRequirement (subjectType, constraintType, loc, reqs, errors);
@@ -534,18 +563,20 @@ void swift::rewriting::inferRequirements(
534563// / Desugar a requirement and perform requirement inference if requested
535564// / to obtain zero or more structural requirements.
536565void swift::rewriting::realizeRequirement (
566+ DeclContext *dc,
537567 Requirement req, RequirementRepr *reqRepr,
538- ModuleDecl *moduleForInference ,
568+ bool shouldInferRequirements ,
539569 SmallVectorImpl<StructuralRequirement> &result,
540570 SmallVectorImpl<RequirementError> &errors) {
541571 auto firstType = req.getFirstType ();
542572 auto loc = (reqRepr ? reqRepr->getSeparatorLoc () : SourceLoc ());
573+ auto *moduleForInference = dc->getParentModule ();
543574
544575 switch (req.getKind ()) {
545576 case RequirementKind::Superclass:
546577 case RequirementKind::Conformance: {
547578 auto secondType = req.getSecondType ();
548- if (moduleForInference ) {
579+ if (shouldInferRequirements ) {
549580 auto firstLoc = (reqRepr ? reqRepr->getSubjectRepr ()->getStartLoc ()
550581 : SourceLoc ());
551582 inferRequirements (firstType, firstLoc, moduleForInference, result);
@@ -555,12 +586,12 @@ void swift::rewriting::realizeRequirement(
555586 inferRequirements (secondType, secondLoc, moduleForInference, result);
556587 }
557588
558- realizeTypeRequirement (firstType, secondType, loc, result, errors);
589+ realizeTypeRequirement (dc, firstType, secondType, loc, result, errors);
559590 break ;
560591 }
561592
562593 case RequirementKind::Layout: {
563- if (moduleForInference ) {
594+ if (shouldInferRequirements ) {
564595 auto firstLoc = (reqRepr ? reqRepr->getSubjectRepr ()->getStartLoc ()
565596 : SourceLoc ());
566597 inferRequirements (firstType, firstLoc, moduleForInference, result);
@@ -578,7 +609,7 @@ void swift::rewriting::realizeRequirement(
578609
579610 case RequirementKind::SameType: {
580611 auto secondType = req.getSecondType ();
581- if (moduleForInference ) {
612+ if (shouldInferRequirements ) {
582613 auto firstLoc = (reqRepr ? reqRepr->getFirstTypeRepr ()->getStartLoc ()
583614 : SourceLoc ());
584615 inferRequirements (firstType, firstLoc, moduleForInference, result);
@@ -602,11 +633,13 @@ void swift::rewriting::realizeRequirement(
602633// / Collect structural requirements written in the inheritance clause of an
603634// / AssociatedTypeDecl or GenericTypeParamDecl.
604635void swift::rewriting::realizeInheritedRequirements (
605- TypeDecl *decl, Type type, ModuleDecl *moduleForInference ,
636+ TypeDecl *decl, Type type, bool shouldInferRequirements ,
606637 SmallVectorImpl<StructuralRequirement> &result,
607638 SmallVectorImpl<RequirementError> &errors) {
608639 auto &ctx = decl->getASTContext ();
609640 auto inheritedTypes = decl->getInherited ();
641+ auto *dc = decl->getInnermostDeclContext ();
642+ auto *moduleForInference = dc->getParentModule ();
610643
611644 for (unsigned index : indices (inheritedTypes)) {
612645 Type inheritedType
@@ -616,41 +649,13 @@ void swift::rewriting::realizeInheritedRequirements(
616649 Type ());
617650 if (!inheritedType) continue ;
618651
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-
647652 auto *typeRepr = inheritedTypes[index].getTypeRepr ();
648653 SourceLoc loc = (typeRepr ? typeRepr->getStartLoc () : SourceLoc ());
649- if (moduleForInference ) {
654+ if (shouldInferRequirements ) {
650655 inferRequirements (inheritedType, loc, moduleForInference, result);
651656 }
652657
653- realizeTypeRequirement (type, inheritedType, loc, result, errors);
658+ realizeTypeRequirement (dc, type, inheritedType, loc, result, errors);
654659 }
655660}
656661
@@ -823,14 +828,14 @@ StructuralRequirementsRequest::evaluate(Evaluator &evaluator,
823828 auto selfTy = proto->getSelfInterfaceType ();
824829
825830 realizeInheritedRequirements (proto, selfTy,
826- /* moduleForInference =*/ nullptr ,
831+ /* inferRequirements =*/ false ,
827832 result, errors);
828833
829834 // Add requirements from the protocol's own 'where' clause.
830835 WhereClauseOwner (proto).visitRequirements (TypeResolutionStage::Structural,
831836 [&](const Requirement &req, RequirementRepr *reqRepr) {
832- realizeRequirement (req, reqRepr,
833- /* moduleForInference =*/ nullptr ,
837+ realizeRequirement (proto, req, reqRepr,
838+ /* inferRequirements =*/ false ,
834839 result, errors);
835840 return false ;
836841 });
@@ -855,15 +860,15 @@ StructuralRequirementsRequest::evaluate(Evaluator &evaluator,
855860 // Add requirements placed directly on this associated type.
856861 auto assocType = assocTypeDecl->getDeclaredInterfaceType ();
857862 realizeInheritedRequirements (assocTypeDecl, assocType,
858- /* moduleForInference =*/ nullptr ,
863+ /* inferRequirements =*/ false ,
859864 result, errors);
860865
861866 // Add requirements from this associated type's where clause.
862867 WhereClauseOwner (assocTypeDecl).visitRequirements (
863868 TypeResolutionStage::Structural,
864869 [&](const Requirement &req, RequirementRepr *reqRepr) {
865- realizeRequirement (req, reqRepr,
866- /* moduleForInference =*/ nullptr ,
870+ realizeRequirement (proto, req, reqRepr,
871+ /* inferRequirements =*/ false ,
867872 result, errors);
868873 return false ;
869874 });
0 commit comments