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"
@@ -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.
@@ -834,7 +880,6 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
834
880
return paramDecl->isParameterPack ();
835
881
});
836
882
837
- // Resolve the types of the generic arguments.
838
883
auto argOptions = options.withoutContext ().withContext (
839
884
TypeResolverContext::GenericArgument);
840
885
auto genericResolution = resolution.withOptions (argOptions);
@@ -848,6 +893,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
848
893
}
849
894
}
850
895
896
+ // Resolve the types of the generic arguments.
851
897
SmallVector<Type, 2 > args;
852
898
for (auto tyR : genericArgs) {
853
899
// Propagate failure.
@@ -864,15 +910,9 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
864
910
// arguments.
865
911
if (genericArgs.size () != genericParams->size ()) {
866
912
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 ());
913
+ diagnoseInvalidGenericArguments (
914
+ loc, decl, genericArgs.size (), genericParams->size (),
915
+ /* hasParameterPack=*/ false , generic);
876
916
}
877
917
return ErrorType::get (ctx);
878
918
}
@@ -890,17 +930,11 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
890
930
}
891
931
892
932
PackMatcher matcher (params, args, ctx);
893
- if (matcher.match ()) {
933
+ if (matcher.match () || matcher. pairs . size () != params. size () ) {
894
934
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 ());
935
+ diagnoseInvalidGenericArguments (
936
+ loc, decl, genericArgs.size (), genericParams->size (),
937
+ /* hasParameterPack=*/ true , generic);
904
938
}
905
939
return ErrorType::get (ctx);
906
940
}
0 commit comments