38
38
#include " swift/AST/SourceFile.h"
39
39
#include " swift/AST/TypeCheckRequests.h"
40
40
#include " swift/AST/TypeLoc.h"
41
+ #include " swift/AST/TypeRepr.h"
41
42
#include " swift/AST/TypeResolutionStage.h"
42
43
#include " swift/Basic/SourceManager.h"
43
44
#include " swift/Basic/Statistic.h"
@@ -143,8 +144,8 @@ static unsigned getGenericRequirementKind(TypeResolutionOptions options) {
143
144
case TypeResolverContext::FunctionInput:
144
145
case TypeResolverContext::PackElement:
145
146
case TypeResolverContext::TupleElement:
146
- case TypeResolverContext::GenericArgument :
147
- case TypeResolverContext::ProtocolGenericArgument :
147
+ case TypeResolverContext::ScalarGenericArgument :
148
+ case TypeResolverContext::VariadicGenericArgument :
148
149
case TypeResolverContext::ExtensionBinding:
149
150
case TypeResolverContext::TypeAliasDecl:
150
151
case TypeResolverContext::GenericTypeAliasDecl:
@@ -667,6 +668,51 @@ bool TypeChecker::checkContextualRequirements(GenericTypeDecl *decl,
667
668
llvm_unreachable (" invalid requirement check type" );
668
669
}
669
670
671
+ void swift::diagnoseInvalidGenericArguments (SourceLoc loc,
672
+ ValueDecl *decl,
673
+ unsigned argCount,
674
+ unsigned paramCount,
675
+ bool hasParameterPack,
676
+ GenericIdentTypeRepr *generic) {
677
+ auto &ctx = decl->getASTContext ();
678
+ auto &diags = ctx.Diags ;
679
+
680
+ if (!hasParameterPack) {
681
+ // For generic types without type parameter packs, we require
682
+ // the number of declared generic parameters match the number of
683
+ // arguments.
684
+ if (argCount < paramCount) {
685
+ auto diag = diags
686
+ .diagnose (loc, diag::too_few_generic_arguments, decl->getBaseIdentifier (),
687
+ argCount, paramCount);
688
+ if (generic)
689
+ diag.highlight (generic->getAngleBrackets ());
690
+ } else {
691
+ auto diag = diags
692
+ .diagnose (loc, diag::too_many_generic_arguments, decl->getBaseIdentifier (),
693
+ argCount, paramCount);
694
+ if (generic)
695
+ diag.highlight (generic->getAngleBrackets ());
696
+ }
697
+ } else {
698
+ if (argCount < paramCount - 1 ) {
699
+ auto diag = diags
700
+ .diagnose (loc, diag::too_few_generic_arguments_pack, decl->getBaseIdentifier (),
701
+ argCount, paramCount - 1 );
702
+ if (generic)
703
+ diag.highlight (generic->getAngleBrackets ());
704
+ } else {
705
+ auto diag = diags
706
+ .diagnose (loc, diag::generic_argument_pack_mismatch, decl->getBaseIdentifier ());
707
+ if (generic)
708
+ diag.highlight (generic->getAngleBrackets ());
709
+ }
710
+ }
711
+
712
+ decl->diagnose (diag::kind_declname_declared_here,
713
+ DescriptiveDeclKind::GenericType, decl->getName ());
714
+ }
715
+
670
716
// / Apply generic arguments to the given type.
671
717
// /
672
718
// / If the type is itself not generic, this does nothing.
@@ -747,6 +793,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
747
793
748
794
auto genericArgs = generic->getGenericArgs ();
749
795
796
+ // Parameterized protocol types have their own code path.
750
797
if (auto *protoType = type->getAs <ProtocolType>()) {
751
798
auto *protoDecl = protoType->getDecl ();
752
799
@@ -761,6 +808,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
761
808
return ErrorType::get (ctx);
762
809
}
763
810
811
+ // Make sure we have the right number of generic arguments.
764
812
if (genericArgs.size () != assocTypes.size ()) {
765
813
diags.diagnose (loc,
766
814
diag::parameterized_protocol_type_argument_count_mismatch,
@@ -770,45 +818,15 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
770
818
return ErrorType::get (ctx);
771
819
}
772
820
773
- // Build ParameterizedProtocolType if the protocol has a primary associated
774
- // type and we're in a supported context.
775
- if (resolution.getOptions ().isConstraintImplicitExistential () &&
776
- !ctx.LangOpts .hasFeature (Feature::ImplicitSome)) {
777
-
778
- if (!genericArgs.empty ()) {
779
-
780
- SmallVector<Type, 2 > argTys;
781
- for (auto *genericArg : genericArgs) {
782
- Type argTy = resolution.resolveType (genericArg);
783
- if (!argTy || argTy->hasError ())
784
- return ErrorType::get (ctx);
785
-
786
- argTys.push_back (argTy);
787
- }
788
-
789
- auto parameterized =
790
- ParameterizedProtocolType::get (ctx, protoType, argTys);
791
- diags.diagnose (loc, diag::existential_requires_any, parameterized,
792
- ExistentialType::get (parameterized),
793
- /* isAlias=*/ isa<TypeAliasType>(type.getPointer ()));
794
- } else {
795
- diags.diagnose (loc, diag::existential_requires_any,
796
- protoDecl->getDeclaredInterfaceType (),
797
- protoDecl->getDeclaredExistentialType (),
798
- /* isAlias=*/ isa<TypeAliasType>(type.getPointer ()));
799
- }
800
-
801
- return ErrorType::get (ctx);
802
- }
803
-
804
821
// Disallow opaque types anywhere in the structure of the generic arguments
805
822
// to a parameterized existential type.
806
823
if (options.is (TypeResolverContext::ExistentialConstraint))
807
824
options |= TypeResolutionFlags::DisallowOpaqueTypes;
808
825
auto argOptions = options.withoutContext ().withContext (
809
- TypeResolverContext::ProtocolGenericArgument );
826
+ TypeResolverContext::ScalarGenericArgument );
810
827
auto genericResolution = resolution.withOptions (argOptions);
811
828
829
+ // Resolve the generic arguments.
812
830
SmallVector<Type, 2 > argTys;
813
831
for (auto *genericArg : genericArgs) {
814
832
Type argTy = genericResolution.resolveType (genericArg, silContext);
@@ -818,7 +836,18 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
818
836
argTys.push_back (argTy);
819
837
}
820
838
821
- return ParameterizedProtocolType::get (ctx, protoType, argTys);
839
+ auto parameterized =
840
+ ParameterizedProtocolType::get (ctx, protoType, argTys);
841
+
842
+ if (resolution.getOptions ().isConstraintImplicitExistential () &&
843
+ !ctx.LangOpts .hasFeature (Feature::ImplicitSome)) {
844
+ diags.diagnose (loc, diag::existential_requires_any, parameterized,
845
+ ExistentialType::get (parameterized),
846
+ /* isAlias=*/ isa<TypeAliasType>(type.getPointer ()));
847
+ return ErrorType::get (ctx);
848
+ }
849
+
850
+ return parameterized;
822
851
}
823
852
824
853
// We must either have an unbound generic type, or a generic type alias.
@@ -844,16 +873,20 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
844
873
auto *unboundType = type->castTo <UnboundGenericType>();
845
874
auto *decl = unboundType->getDecl ();
846
875
847
- // Make sure we have the right number of generic arguments.
848
876
auto genericParams = decl->getGenericParams ();
849
877
auto hasParameterPack = llvm::any_of (
850
878
*genericParams, [](auto *paramDecl) {
851
879
return paramDecl->isParameterPack ();
852
880
});
853
881
854
- // Resolve the types of the generic arguments.
882
+ // If the type declares at least one parameter pack, allow pack expansions
883
+ // anywhere in the argument list. We'll use the PackMatcher to ensure that
884
+ // everything lines up. Otherwise, don't allow pack expansions to appear
885
+ // at all.
855
886
auto argOptions = options.withoutContext ().withContext (
856
- TypeResolverContext::GenericArgument);
887
+ hasParameterPack
888
+ ? TypeResolverContext::VariadicGenericArgument
889
+ : TypeResolverContext::ScalarGenericArgument);
857
890
auto genericResolution = resolution.withOptions (argOptions);
858
891
859
892
// In SIL mode, Optional<T> interprets T as a SIL type.
@@ -865,6 +898,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
865
898
}
866
899
}
867
900
901
+ // Resolve the types of the generic arguments.
868
902
SmallVector<Type, 2 > args;
869
903
for (auto tyR : genericArgs) {
870
904
// Propagate failure.
@@ -875,21 +909,16 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
875
909
args.push_back (substTy);
876
910
}
877
911
912
+ // Make sure we have the right number of generic arguments.
878
913
if (!hasParameterPack) {
879
914
// For generic types without type parameter packs, we require
880
915
// the number of declared generic parameters match the number of
881
916
// arguments.
882
917
if (genericArgs.size () != genericParams->size ()) {
883
918
if (!options.contains (TypeResolutionFlags::SilenceErrors)) {
884
- diags
885
- .diagnose (loc, diag::type_parameter_count_mismatch, decl->getName (),
886
- genericParams->size (),
887
- genericArgs.size (),
888
- genericArgs.size () < genericParams->size (),
889
- /* hasParameterPack=*/ 0 )
890
- .highlight (generic->getAngleBrackets ());
891
- decl->diagnose (diag::kind_declname_declared_here,
892
- DescriptiveDeclKind::GenericType, decl->getName ());
919
+ diagnoseInvalidGenericArguments (
920
+ loc, decl, genericArgs.size (), genericParams->size (),
921
+ /* hasParameterPack=*/ false , generic);
893
922
}
894
923
return ErrorType::get (ctx);
895
924
}
@@ -907,17 +936,11 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
907
936
}
908
937
909
938
PackMatcher matcher (params, args, ctx);
910
- if (matcher.match ()) {
939
+ if (matcher.match () || matcher. pairs . size () != params. size () ) {
911
940
if (!options.contains (TypeResolutionFlags::SilenceErrors)) {
912
- diags
913
- .diagnose (loc, diag::type_parameter_count_mismatch, decl->getName (),
914
- genericParams->size () - 1 ,
915
- genericArgs.size (),
916
- genericArgs.size () < genericParams->size (),
917
- /* hasParameterPack=*/ 1 )
918
- .highlight (generic->getAngleBrackets ());
919
- decl->diagnose (diag::kind_declname_declared_here,
920
- DescriptiveDeclKind::GenericType, decl->getName ());
941
+ diagnoseInvalidGenericArguments (
942
+ loc, decl, genericArgs.size (), genericParams->size (),
943
+ /* hasParameterPack=*/ true , generic);
921
944
}
922
945
return ErrorType::get (ctx);
923
946
}
@@ -953,6 +976,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
953
976
}
954
977
}
955
978
979
+ // Construct the substituted type.
956
980
const auto result = resolution.applyUnboundGenericArguments (
957
981
decl, unboundType->getParent (), loc, args);
958
982
@@ -4405,7 +4429,7 @@ NeverNullType
4405
4429
TypeResolver::resolveDictionaryType (DictionaryTypeRepr *repr,
4406
4430
TypeResolutionOptions options) {
4407
4431
auto argOptions = options.withoutContext ().withContext (
4408
- TypeResolverContext::GenericArgument );
4432
+ TypeResolverContext::ScalarGenericArgument );
4409
4433
4410
4434
auto keyTy = resolveType (repr->getKey (), argOptions);
4411
4435
if (keyTy->hasError ()) {
@@ -4479,8 +4503,8 @@ NeverNullType TypeResolver::resolveImplicitlyUnwrappedOptionalType(
4479
4503
break ;
4480
4504
case TypeResolverContext::PackElement:
4481
4505
case TypeResolverContext::TupleElement:
4482
- case TypeResolverContext::GenericArgument :
4483
- case TypeResolverContext::ProtocolGenericArgument :
4506
+ case TypeResolverContext::ScalarGenericArgument :
4507
+ case TypeResolverContext::VariadicGenericArgument :
4484
4508
case TypeResolverContext::VariadicFunctionInput:
4485
4509
case TypeResolverContext::ForEachStmt:
4486
4510
case TypeResolverContext::ExtensionBinding:
0 commit comments