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,
@@ -775,7 +823,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
775
823
if (options.is (TypeResolverContext::ExistentialConstraint))
776
824
options |= TypeResolutionFlags::DisallowOpaqueTypes;
777
825
auto argOptions = options.withoutContext ().withContext (
778
- TypeResolverContext::ProtocolGenericArgument );
826
+ TypeResolverContext::ScalarGenericArgument );
779
827
auto genericResolution = resolution.withOptions (argOptions);
780
828
781
829
// Resolve the generic arguments.
@@ -791,8 +839,6 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
791
839
auto parameterized =
792
840
ParameterizedProtocolType::get (ctx, protoType, argTys);
793
841
794
- // Build ParameterizedProtocolType if the protocol has primary associated
795
- // types and we're in a supported context.
796
842
if (resolution.getOptions ().isConstraintImplicitExistential () &&
797
843
!ctx.LangOpts .hasFeature (Feature::ImplicitSome)) {
798
844
diags.diagnose (loc, diag::existential_requires_any, parameterized,
@@ -827,16 +873,20 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
827
873
auto *unboundType = type->castTo <UnboundGenericType>();
828
874
auto *decl = unboundType->getDecl ();
829
875
830
- // Make sure we have the right number of generic arguments.
831
876
auto genericParams = decl->getGenericParams ();
832
877
auto hasParameterPack = llvm::any_of (
833
878
*genericParams, [](auto *paramDecl) {
834
879
return paramDecl->isParameterPack ();
835
880
});
836
881
837
- // 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.
838
886
auto argOptions = options.withoutContext ().withContext (
839
- TypeResolverContext::GenericArgument);
887
+ hasParameterPack
888
+ ? TypeResolverContext::VariadicGenericArgument
889
+ : TypeResolverContext::ScalarGenericArgument);
840
890
auto genericResolution = resolution.withOptions (argOptions);
841
891
842
892
// In SIL mode, Optional<T> interprets T as a SIL type.
@@ -848,6 +898,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
848
898
}
849
899
}
850
900
901
+ // Resolve the types of the generic arguments.
851
902
SmallVector<Type, 2 > args;
852
903
for (auto tyR : genericArgs) {
853
904
// Propagate failure.
@@ -858,21 +909,16 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
858
909
args.push_back (substTy);
859
910
}
860
911
912
+ // Make sure we have the right number of generic arguments.
861
913
if (!hasParameterPack) {
862
914
// For generic types without type parameter packs, we require
863
915
// the number of declared generic parameters match the number of
864
916
// arguments.
865
917
if (genericArgs.size () != genericParams->size ()) {
866
918
if (!options.contains (TypeResolutionFlags::SilenceErrors)) {
867
- diags
868
- .diagnose (loc, diag::type_parameter_count_mismatch, decl->getName (),
869
- genericParams->size (),
870
- genericArgs.size (),
871
- genericArgs.size () < genericParams->size (),
872
- /* hasParameterPack=*/ 0 )
873
- .highlight (generic->getAngleBrackets ());
874
- decl->diagnose (diag::kind_declname_declared_here,
875
- DescriptiveDeclKind::GenericType, decl->getName ());
919
+ diagnoseInvalidGenericArguments (
920
+ loc, decl, genericArgs.size (), genericParams->size (),
921
+ /* hasParameterPack=*/ false , generic);
876
922
}
877
923
return ErrorType::get (ctx);
878
924
}
@@ -890,17 +936,11 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
890
936
}
891
937
892
938
PackMatcher matcher (params, args, ctx);
893
- if (matcher.match ()) {
939
+ if (matcher.match () || matcher. pairs . size () != params. size () ) {
894
940
if (!options.contains (TypeResolutionFlags::SilenceErrors)) {
895
- diags
896
- .diagnose (loc, diag::type_parameter_count_mismatch, decl->getName (),
897
- genericParams->size () - 1 ,
898
- genericArgs.size (),
899
- genericArgs.size () < genericParams->size (),
900
- /* hasParameterPack=*/ 1 )
901
- .highlight (generic->getAngleBrackets ());
902
- decl->diagnose (diag::kind_declname_declared_here,
903
- DescriptiveDeclKind::GenericType, decl->getName ());
941
+ diagnoseInvalidGenericArguments (
942
+ loc, decl, genericArgs.size (), genericParams->size (),
943
+ /* hasParameterPack=*/ true , generic);
904
944
}
905
945
return ErrorType::get (ctx);
906
946
}
@@ -936,6 +976,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
936
976
}
937
977
}
938
978
979
+ // Construct the substituted type.
939
980
const auto result = resolution.applyUnboundGenericArguments (
940
981
decl, unboundType->getParent (), loc, args);
941
982
@@ -4433,7 +4474,7 @@ NeverNullType
4433
4474
TypeResolver::resolveDictionaryType (DictionaryTypeRepr *repr,
4434
4475
TypeResolutionOptions options) {
4435
4476
auto argOptions = options.withoutContext ().withContext (
4436
- TypeResolverContext::GenericArgument );
4477
+ TypeResolverContext::ScalarGenericArgument );
4437
4478
4438
4479
auto keyTy = resolveType (repr->getKey (), argOptions);
4439
4480
if (keyTy->hasError ()) {
@@ -4507,8 +4548,8 @@ NeverNullType TypeResolver::resolveImplicitlyUnwrappedOptionalType(
4507
4548
break ;
4508
4549
case TypeResolverContext::PackElement:
4509
4550
case TypeResolverContext::TupleElement:
4510
- case TypeResolverContext::GenericArgument :
4511
- case TypeResolverContext::ProtocolGenericArgument :
4551
+ case TypeResolverContext::ScalarGenericArgument :
4552
+ case TypeResolverContext::VariadicGenericArgument :
4512
4553
case TypeResolverContext::VariadicFunctionInput:
4513
4554
case TypeResolverContext::ForEachStmt:
4514
4555
case TypeResolverContext::ExtensionBinding:
0 commit comments