@@ -1087,7 +1087,8 @@ static DirectlyReferencedTypeDecls
1087
1087
directReferencesForTypeRepr (Evaluator &evaluator, ASTContext &ctx,
1088
1088
TypeRepr *typeRepr, DeclContext *dc,
1089
1089
bool allowUsableFromInline,
1090
- bool rhsOfSelfRequirement);
1090
+ bool rhsOfSelfRequirement,
1091
+ bool allowProtocolMembers);
1091
1092
1092
1093
// / Retrieve the set of type declarations that are directly referenced from
1093
1094
// / the given type.
@@ -1148,7 +1149,8 @@ SelfBounds SelfBoundsFromWhereClauseRequest::evaluate(
1148
1149
rhsDecls = directReferencesForTypeRepr (evaluator, ctx, typeRepr,
1149
1150
const_cast <DeclContext *>(dc),
1150
1151
/* allowUsableFromInline=*/ false ,
1151
- /* rhsOfSelfRequirement=*/ true );
1152
+ /* rhsOfSelfRequirement=*/ true ,
1153
+ /* allowProtocolMembers=*/ true );
1152
1154
}
1153
1155
1154
1156
SmallVector<ModuleDecl *, 2 > modulesFound;
@@ -1212,7 +1214,8 @@ TypeDeclsFromWhereClauseRequest::evaluate(Evaluator &evaluator,
1212
1214
auto resolve = [&](TypeRepr *typeRepr) {
1213
1215
auto decls = directReferencesForTypeRepr (evaluator, ctx, typeRepr, ext,
1214
1216
/* allowUsableFromInline=*/ false ,
1215
- /* rhsOfSelfRequirement=*/ false );
1217
+ /* rhsOfSelfRequirement=*/ false ,
1218
+ /* allowProtocolMembers=*/ true );
1216
1219
result.first .insert (result.first .end (),
1217
1220
decls.first .begin (),
1218
1221
decls.first .end ());
@@ -2834,10 +2837,11 @@ directReferencesForUnqualifiedTypeLookup(DeclNameRef name,
2834
2837
SourceLoc loc, DeclContext *dc,
2835
2838
LookupOuterResults lookupOuter,
2836
2839
bool allowUsableFromInline,
2837
- bool rhsOfSelfRequirement) {
2838
- UnqualifiedLookupOptions options =
2839
- UnqualifiedLookupFlags::TypeLookup |
2840
- UnqualifiedLookupFlags::AllowProtocolMembers;
2840
+ bool rhsOfSelfRequirement,
2841
+ bool allowProtocolMembers) {
2842
+ UnqualifiedLookupOptions options = UnqualifiedLookupFlags::TypeLookup;
2843
+ if (allowProtocolMembers)
2844
+ options |= UnqualifiedLookupFlags::AllowProtocolMembers;
2841
2845
if (lookupOuter == LookupOuterResults::Included)
2842
2846
options |= UnqualifiedLookupFlags::IncludeOuterResults;
2843
2847
@@ -2951,11 +2955,12 @@ static DirectlyReferencedTypeDecls
2951
2955
directReferencesForDeclRefTypeRepr (Evaluator &evaluator, ASTContext &ctx,
2952
2956
DeclRefTypeRepr *repr, DeclContext *dc,
2953
2957
bool allowUsableFromInline,
2954
- bool rhsOfSelfRequirement) {
2958
+ bool rhsOfSelfRequirement,
2959
+ bool allowProtocolMembers) {
2955
2960
if (auto *qualIdentTR = dyn_cast<QualifiedIdentTypeRepr>(repr)) {
2956
2961
auto result = directReferencesForTypeRepr (
2957
2962
evaluator, ctx, qualIdentTR->getBase (), dc,
2958
- allowUsableFromInline, rhsOfSelfRequirement);
2963
+ allowUsableFromInline, rhsOfSelfRequirement, allowProtocolMembers );
2959
2964
2960
2965
// For a qualified identifier, perform qualified name lookup.
2961
2966
result.first = directReferencesForQualifiedTypeLookup (
@@ -2968,14 +2973,15 @@ directReferencesForDeclRefTypeRepr(Evaluator &evaluator, ASTContext &ctx,
2968
2973
// For an unqualified identifier, perform unqualified name lookup.
2969
2974
return directReferencesForUnqualifiedTypeLookup (
2970
2975
repr->getNameRef (), repr->getLoc (), dc, LookupOuterResults::Excluded,
2971
- allowUsableFromInline, rhsOfSelfRequirement);
2976
+ allowUsableFromInline, rhsOfSelfRequirement, allowProtocolMembers );
2972
2977
}
2973
2978
2974
2979
static DirectlyReferencedTypeDecls
2975
2980
directReferencesForTypeRepr (Evaluator &evaluator,
2976
2981
ASTContext &ctx, TypeRepr *typeRepr,
2977
2982
DeclContext *dc, bool allowUsableFromInline,
2978
- bool rhsOfSelfRequirement) {
2983
+ bool rhsOfSelfRequirement,
2984
+ bool allowProtocolMembers) {
2979
2985
DirectlyReferencedTypeDecls result;
2980
2986
2981
2987
switch (typeRepr->getKind ()) {
@@ -2988,7 +2994,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
2988
2994
return directReferencesForTypeRepr (evaluator, ctx,
2989
2995
attributed->getTypeRepr (), dc,
2990
2996
allowUsableFromInline,
2991
- rhsOfSelfRequirement);
2997
+ rhsOfSelfRequirement,
2998
+ allowProtocolMembers);
2992
2999
}
2993
3000
2994
3001
case TypeReprKind::Composition: {
@@ -2997,7 +3004,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
2997
3004
auto componentResult =
2998
3005
directReferencesForTypeRepr (evaluator, ctx, component, dc,
2999
3006
allowUsableFromInline,
3000
- rhsOfSelfRequirement);
3007
+ rhsOfSelfRequirement,
3008
+ allowProtocolMembers);
3001
3009
result.first .insert (result.first .end (),
3002
3010
componentResult.first .begin (),
3003
3011
componentResult.first .end ());
@@ -3013,7 +3021,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
3013
3021
return directReferencesForDeclRefTypeRepr (evaluator, ctx,
3014
3022
cast<DeclRefTypeRepr>(typeRepr),
3015
3023
dc, allowUsableFromInline,
3016
- rhsOfSelfRequirement);
3024
+ rhsOfSelfRequirement,
3025
+ allowProtocolMembers);
3017
3026
3018
3027
case TypeReprKind::Dictionary:
3019
3028
result.first .push_back (ctx.getDictionaryDecl ());
@@ -3025,7 +3034,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
3025
3034
result = directReferencesForTypeRepr (evaluator, ctx,
3026
3035
tupleRepr->getElementType (0 ), dc,
3027
3036
allowUsableFromInline,
3028
- rhsOfSelfRequirement);
3037
+ rhsOfSelfRequirement,
3038
+ allowProtocolMembers);
3029
3039
} else {
3030
3040
result.first .push_back (ctx.getBuiltinTupleDecl ());
3031
3041
}
@@ -3037,23 +3047,26 @@ directReferencesForTypeRepr(Evaluator &evaluator,
3037
3047
return directReferencesForTypeRepr (evaluator, ctx,
3038
3048
packExpansionRepr->getElementType (), dc,
3039
3049
allowUsableFromInline,
3040
- rhsOfSelfRequirement);
3050
+ rhsOfSelfRequirement,
3051
+ allowProtocolMembers);
3041
3052
}
3042
3053
3043
3054
case TypeReprKind::PackExpansion: {
3044
3055
auto packExpansionRepr = cast<PackExpansionTypeRepr>(typeRepr);
3045
3056
return directReferencesForTypeRepr (evaluator, ctx,
3046
3057
packExpansionRepr->getPatternType (), dc,
3047
3058
allowUsableFromInline,
3048
- rhsOfSelfRequirement);
3059
+ rhsOfSelfRequirement,
3060
+ allowProtocolMembers);
3049
3061
}
3050
3062
3051
3063
case TypeReprKind::PackElement: {
3052
3064
auto packReferenceRepr = cast<PackElementTypeRepr>(typeRepr);
3053
3065
return directReferencesForTypeRepr (evaluator, ctx,
3054
3066
packReferenceRepr->getPackType (), dc,
3055
3067
allowUsableFromInline,
3056
- rhsOfSelfRequirement);
3068
+ rhsOfSelfRequirement,
3069
+ allowProtocolMembers);
3057
3070
}
3058
3071
3059
3072
case TypeReprKind::Inverse: {
@@ -3063,7 +3076,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
3063
3076
auto innerResult = directReferencesForTypeRepr (evaluator, ctx,
3064
3077
inverseRepr->getConstraint (), dc,
3065
3078
allowUsableFromInline,
3066
- rhsOfSelfRequirement);
3079
+ rhsOfSelfRequirement,
3080
+ allowProtocolMembers);
3067
3081
if (innerResult.first .size () == 1 ) {
3068
3082
if (auto *proto = dyn_cast<ProtocolDecl>(innerResult.first [0 ])) {
3069
3083
if (auto ip = proto->getInvertibleProtocolKind ()) {
@@ -3173,10 +3187,16 @@ DirectlyReferencedTypeDecls InheritedDeclsReferencedRequest::evaluate(
3173
3187
else
3174
3188
dc = (DeclContext *)decl.get <const ExtensionDecl *>();
3175
3189
3190
+ // If looking at a protocol's inheritance list,
3191
+ // do not look at protocol members to avoid circularity.
3192
+ // Protocols cannot inherit from any protocol members anyway.
3193
+ bool allowProtocolMembers = (dc->getSelfProtocolDecl () == nullptr );
3194
+
3176
3195
return directReferencesForTypeRepr (evaluator, dc->getASTContext (), typeRepr,
3177
3196
const_cast <DeclContext *>(dc),
3178
3197
/* allowUsableFromInline=*/ false ,
3179
- /* rhsOfSelfRequirement=*/ false );
3198
+ /* rhsOfSelfRequirement=*/ false ,
3199
+ allowProtocolMembers);
3180
3200
}
3181
3201
3182
3202
// Fall back to semantic types.
@@ -3197,7 +3217,8 @@ DirectlyReferencedTypeDecls UnderlyingTypeDeclsReferencedRequest::evaluate(
3197
3217
return directReferencesForTypeRepr (evaluator, typealias->getASTContext (),
3198
3218
typeRepr, typealias,
3199
3219
/* allowUsableFromInline=*/ false ,
3200
- /* rhsOfSelfRequirement=*/ false );
3220
+ /* rhsOfSelfRequirement=*/ false ,
3221
+ /* allowProtocolMembers=*/ true );
3201
3222
}
3202
3223
3203
3224
// Fall back to semantic types.
@@ -3356,7 +3377,8 @@ ExtendedNominalRequest::evaluate(Evaluator &evaluator,
3356
3377
DirectlyReferencedTypeDecls referenced =
3357
3378
directReferencesForTypeRepr (evaluator, ctx, typeRepr, ext->getParent (),
3358
3379
ext->isInSpecializeExtensionContext (),
3359
- /* rhsOfSelfRequirement=*/ false );
3380
+ /* rhsOfSelfRequirement=*/ false ,
3381
+ /* allowProtocolMembers=*/ true );
3360
3382
3361
3383
// Resolve those type declarations to nominal type declarations.
3362
3384
SmallVector<ModuleDecl *, 2 > modulesFound;
@@ -3406,7 +3428,8 @@ bool TypeRepr::isProtocolOrProtocolComposition(DeclContext *dc) {
3406
3428
auto &ctx = dc->getASTContext ();
3407
3429
auto references = directReferencesForTypeRepr (ctx.evaluator , ctx, this , dc,
3408
3430
/* allowUsableFromInline=*/ false ,
3409
- /* rhsOfSelfRequirement=*/ false );
3431
+ /* rhsOfSelfRequirement=*/ false ,
3432
+ /* allowProtocolMembers=*/ true );
3410
3433
return declsAreProtocols (references.first );
3411
3434
}
3412
3435
@@ -3441,7 +3464,8 @@ createTupleExtensionGenericParams(ASTContext &ctx,
3441
3464
extendedTypeRepr,
3442
3465
ext->getParent (),
3443
3466
/* allowUsableFromInline=*/ false ,
3444
- /* rhsOfSelfRequirement=*/ false );
3467
+ /* rhsOfSelfRequirement=*/ false ,
3468
+ /* allowProtocolMembers=*/ true );
3445
3469
assert (referenced.second .empty () && " Implement me" );
3446
3470
if (referenced.first .size () != 1 || !isa<TypeAliasDecl>(referenced.first [0 ]))
3447
3471
return nullptr ;
@@ -3716,7 +3740,8 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
3716
3740
decls = directReferencesForTypeRepr (
3717
3741
evaluator, ctx, typeRepr, dc,
3718
3742
/* allowUsableFromInline=*/ false ,
3719
- /* rhsOfSelfRequirement=*/ false );
3743
+ /* rhsOfSelfRequirement=*/ false ,
3744
+ /* allowProtocolMembers=*/ true );
3720
3745
} else if (Type type = attr->getType ()) {
3721
3746
decls = directReferencesForType (type);
3722
3747
}
@@ -3747,7 +3772,8 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
3747
3772
decls = directReferencesForUnqualifiedTypeLookup (
3748
3773
name, loc, dc, LookupOuterResults::Included,
3749
3774
/* allowUsableFromInline=*/ false ,
3750
- /* rhsOfSelfRequirement=*/ false );
3775
+ /* rhsOfSelfRequirement=*/ false ,
3776
+ /* allowProtocolMembers*/ true );
3751
3777
nominals = resolveTypeDeclsToNominal (evaluator, ctx, decls.first ,
3752
3778
ResolveToNominalOptions (),
3753
3779
modulesFound, anyObject);
@@ -3982,7 +4008,8 @@ ProtocolDecl *ImplementsAttrProtocolRequest::evaluate(
3982
4008
DirectlyReferencedTypeDecls referenced =
3983
4009
directReferencesForTypeRepr (evaluator, ctx, typeRepr, dc,
3984
4010
/* allowUsableFromInline=*/ false ,
3985
- /* rhsOfSelfRequirement=*/ false );
4011
+ /* rhsOfSelfRequirement=*/ false ,
4012
+ /* allowProtocolMembers=*/ true );
3986
4013
3987
4014
// Resolve those type declarations to nominal type declarations.
3988
4015
SmallVector<ModuleDecl *, 2 > modulesFound;
0 commit comments