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.
@@ -851,7 +897,6 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
851
897
return paramDecl->isParameterPack ();
852
898
});
853
899
854
- // Resolve the types of the generic arguments.
855
900
auto argOptions = options.withoutContext ().withContext (
856
901
TypeResolverContext::GenericArgument);
857
902
auto genericResolution = resolution.withOptions (argOptions);
@@ -865,6 +910,7 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
865
910
}
866
911
}
867
912
913
+ // Resolve the types of the generic arguments.
868
914
SmallVector<Type, 2 > args;
869
915
for (auto tyR : genericArgs) {
870
916
// Propagate failure.
@@ -881,15 +927,9 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
881
927
// arguments.
882
928
if (genericArgs.size () != genericParams->size ()) {
883
929
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 ());
930
+ diagnoseInvalidGenericArguments (
931
+ loc, decl, genericArgs.size (), genericParams->size (),
932
+ /* hasParameterPack=*/ false , generic);
893
933
}
894
934
return ErrorType::get (ctx);
895
935
}
@@ -907,17 +947,11 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
907
947
}
908
948
909
949
PackMatcher matcher (params, args, ctx);
910
- if (matcher.match ()) {
950
+ if (matcher.match () || matcher. pairs . size () != params. size () ) {
911
951
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 ());
952
+ diagnoseInvalidGenericArguments (
953
+ loc, decl, genericArgs.size (), genericParams->size (),
954
+ /* hasParameterPack=*/ true , generic);
921
955
}
922
956
return ErrorType::get (ctx);
923
957
}
0 commit comments