@@ -2765,7 +2765,7 @@ static bool declsAreAssociatedTypes(ArrayRef<TypeDecl *> decls) {
2765
2765
return true ;
2766
2766
}
2767
2767
2768
- // / Whether there are only protocol decl types in the set of declarations.
2768
+ // / Verify there are only protocols in the set of declarations.
2769
2769
static bool declsAreProtocols (ArrayRef<TypeDecl *> decls) {
2770
2770
if (decls.empty ())
2771
2771
return false ;
@@ -2774,10 +2774,15 @@ static bool declsAreProtocols(ArrayRef<TypeDecl *> decls) {
2774
2774
if (!isa<ProtocolDecl>(decl))
2775
2775
return false ;
2776
2776
}
2777
-
2778
2777
return true ;
2779
2778
}
2780
2779
2780
+ bool TypeRepr::isProtocol (DeclContext *dc){
2781
+ auto &ctx = dc->getASTContext ();
2782
+ DirectlyReferencedTypeDecls d = directReferencesForTypeRepr (ctx.evaluator , ctx, this , dc);
2783
+ return declsAreProtocols (d);
2784
+ }
2785
+
2781
2786
static GenericParamList *
2782
2787
createExtensionGenericParams (ASTContext &ctx,
2783
2788
ExtensionDecl *ext,
@@ -2797,54 +2802,48 @@ createExtensionGenericParams(ASTContext &ctx,
2797
2802
return toParams;
2798
2803
}
2799
2804
2800
- CollectedOpaqueReprs swift::collectOpaqueReturnTypeReprs (TypeRepr *r) {
2801
- class Walker : public ASTWalker {
2802
- CollectedOpaqueReprs &Reprs;
2803
-
2804
- public:
2805
- explicit Walker (CollectedOpaqueReprs &reprs) : Reprs(reprs) {}
2806
-
2807
- bool walkToTypeReprPre (TypeRepr *repr) override {
2808
- if (auto opaqueRepr = dyn_cast<OpaqueReturnTypeRepr>(repr))
2809
- Reprs.push_back (opaqueRepr);
2810
- return true ;
2811
- }
2812
- };
2813
-
2814
- CollectedOpaqueReprs reprs;
2815
- r->walk (Walker (reprs));
2816
- return reprs;
2817
- }
2818
-
2819
- CollectedOpaqueReprs swift::collectTypeReprs (TypeRepr *r) {
2805
+ CollectedOpaqueReprs swift::collectOpaqueReturnTypeReprs (TypeRepr *r, ASTContext &ctx, DeclContext *d) {
2820
2806
class Walker : public ASTWalker {
2821
2807
CollectedOpaqueReprs &Reprs;
2808
+ ASTContext &Ctx;
2809
+ DeclContext *dc;
2822
2810
2823
2811
public:
2824
- explicit Walker (CollectedOpaqueReprs &reprs) : Reprs(reprs) {}
2812
+ explicit Walker (CollectedOpaqueReprs &reprs, ASTContext &ctx, DeclContext *d ) : Reprs(reprs), Ctx(ctx), dc(d ) {}
2825
2813
2826
2814
bool walkToTypeReprPre (TypeRepr *repr) override {
2815
+ if (auto existential = dyn_cast<ExistentialTypeRepr>(repr)) {
2816
+ return false ;
2817
+ }
2818
+
2827
2819
if (auto opaqueRepr = dyn_cast<OpaqueReturnTypeRepr>(repr)){
2828
- Reprs.push_back (opaqueRepr);
2820
+ Reprs.push_back (opaqueRepr);
2821
+ if (Ctx.LangOpts .hasFeature (Feature::ImplicitSome))
2829
2822
return false ;
2830
- } else if (auto compositionRepr = dyn_cast<CompositionTypeRepr>(repr)){
2831
- Reprs.push_back (compositionRepr);
2832
- } else if (auto identRepr = dyn_cast<IdentTypeRepr>(repr)){
2833
- Reprs.push_back (identRepr);
2823
+ }
2824
+
2825
+ if (Ctx.LangOpts .hasFeature (Feature::ImplicitSome)){
2826
+ if (auto compositionRepr = dyn_cast<CompositionTypeRepr>(repr)){
2827
+ if (!compositionRepr->isTypeReprAny ())
2828
+ Reprs.push_back (compositionRepr);
2829
+ } else if (auto identRepr = dyn_cast<IdentTypeRepr>(repr)) {
2830
+ if (identRepr->isProtocol (dc))
2831
+ Reprs.push_back (identRepr);
2832
+ }
2834
2833
}
2835
2834
return true ;
2836
2835
}
2837
2836
};
2838
2837
2839
2838
CollectedOpaqueReprs reprs;
2840
- r->walk (Walker (reprs));
2839
+ r->walk (Walker (reprs, ctx, d ));
2841
2840
return reprs;
2842
2841
}
2843
2842
2844
2843
// / If there are opaque parameters in the given declaration, create the
2845
2844
// / generic parameters associated with them.
2846
2845
static SmallVector<GenericTypeParamDecl *, 2 >
2847
- createOpaqueParameterGenericParams (Evaluator &evaluator, GenericContext *genericContext, GenericParamList *parsedGenericParams) {
2846
+ createOpaqueParameterGenericParams (GenericContext *genericContext, GenericParamList *parsedGenericParams) {
2848
2847
ASTContext &ctx = genericContext->getASTContext ();
2849
2848
2850
2849
auto value = dyn_cast_or_null<ValueDecl>(genericContext->getAsDecl ());
@@ -2871,51 +2870,37 @@ createOpaqueParameterGenericParams(Evaluator &evaluator,GenericContext *genericC
2871
2870
2872
2871
// Plain protocols should imply 'some' with experimetal feature
2873
2872
CollectedOpaqueReprs typeReprs;
2874
- if (ctx.LangOpts .hasFeature (Feature::ImplicitSome)) {
2875
- typeReprs = collectTypeReprs (typeRepr);
2876
- } else { typeReprs = collectOpaqueReturnTypeReprs (typeRepr); }
2873
+ typeReprs = collectOpaqueReturnTypeReprs (typeRepr, ctx, dc);
2877
2874
2878
- for (auto repr : typeReprs) {
2875
+ for (auto repr : typeReprs) {
2879
2876
2880
- if (isa<IdentTypeRepr>(repr)){
2881
- if (!isProtocol (evaluator,repr, ctx, dc))
2882
- continue ;
2883
- }
2884
2877
// Allocate a new generic parameter to represent this opaque type.
2885
- auto gp = GenericTypeParamDecl::create (
2878
+ auto gp = GenericTypeParamDecl::create (
2886
2879
dc, Identifier (), SourceLoc (), /* isTypeSequence=*/ false ,
2887
2880
GenericTypeParamDecl::InvalidDepth, index ++, /* isOpaqueType=*/ true ,
2888
2881
repr);
2889
- gp->setImplicit ();
2882
+ gp->setImplicit ();
2890
2883
2891
2884
// Use the underlying constraint as the constraint on the generic parameter.
2892
2885
// The underlying constraint is only present for OpaqueReturnTypeReprs
2893
- if ( auto opaque = dyn_cast<OpaqueReturnTypeRepr>(repr)){
2886
+ if ( auto opaque = dyn_cast<OpaqueReturnTypeRepr>(repr)) {
2894
2887
InheritedEntry inherited[1 ] = {
2895
2888
{ TypeLoc (opaque->getConstraint ()) }
2896
2889
};
2897
2890
gp->setInherited (ctx.AllocateCopy (inherited));
2898
- } else {
2891
+ } else {
2899
2892
InheritedEntry inherited[1 ] = {
2900
2893
{ TypeLoc (repr) }
2901
2894
};
2902
2895
gp->setInherited (ctx.AllocateCopy (inherited));
2903
- }
2896
+ }
2904
2897
implicitGenericParams.push_back (gp);
2905
2898
}
2906
2899
}
2907
2900
2908
2901
return implicitGenericParams;
2909
2902
}
2910
2903
2911
-
2912
- bool swift::isProtocol ( Evaluator &evaluator, TypeRepr *r, ASTContext &ctx, DeclContext *dc) {
2913
- DirectlyReferencedTypeDecls d = directReferencesForTypeRepr (evaluator, ctx, r, dc);
2914
- if (declsAreProtocols (d)){
2915
- return true ;
2916
- } else { return false ; }
2917
- }
2918
-
2919
2904
GenericParamList *
2920
2905
GenericParamListRequest::evaluate (Evaluator &evaluator, GenericContext *value) const {
2921
2906
if (auto *tupleDecl = dyn_cast<BuiltinTupleDecl>(value)) {
@@ -2986,7 +2971,7 @@ GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) c
2986
2971
// Create implicit generic parameters due to opaque parameters, if we need
2987
2972
// them.
2988
2973
auto implicitGenericParams =
2989
- createOpaqueParameterGenericParams (evaluator, value, parsedGenericParams);
2974
+ createOpaqueParameterGenericParams (value, parsedGenericParams);
2990
2975
if (implicitGenericParams.empty ())
2991
2976
return parsedGenericParams;
2992
2977
0 commit comments