@@ -2895,21 +2895,46 @@ bool MissingGenericArgumentsFailure::hasLoc(GenericTypeParamType *GP) const {
2895
2895
}
2896
2896
2897
2897
bool MissingGenericArgumentsFailure::diagnoseAsError () {
2898
+ bool diagnosed = false ;
2898
2899
for (auto *GP : Parameters)
2899
- diagnoseParameter (GP);
2900
+ diagnosed |= diagnoseParameter (GP);
2901
+
2902
+ if (!diagnosed)
2903
+ return false ;
2900
2904
2901
2905
auto *DC = getDeclContext ();
2902
2906
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(DC)) {
2903
- emitDiagnostic (AFD, diag::note_call_to_func, AFD->getFullName ());
2907
+ if (isa<ConstructorDecl>(AFD)) {
2908
+ emitDiagnostic (AFD, diag::note_call_to_initializer);
2909
+ } else {
2910
+ emitDiagnostic (AFD,
2911
+ AFD->isOperator () ? diag::note_call_to_operator
2912
+ : diag::note_call_to_func,
2913
+ AFD->getFullName ());
2914
+ }
2904
2915
return true ;
2905
2916
}
2906
2917
2907
2918
emitGenericSignatureNote ();
2908
2919
return true ;
2909
2920
}
2910
2921
2911
- void MissingGenericArgumentsFailure::diagnoseParameter (
2922
+ bool MissingGenericArgumentsFailure::diagnoseParameter (
2912
2923
GenericTypeParamType *GP) const {
2924
+ auto &cs = getConstraintSystem ();
2925
+
2926
+ auto *locator = getLocator ();
2927
+ // Type variables associated with missing generic parameters are
2928
+ // going to be completely cut off from the rest of constraint system,
2929
+ // that's why we'd get two fixes in this case which is not ideal.
2930
+ if (locator->isForContextualType () &&
2931
+ llvm::count_if (cs.DefaultedConstraints ,
2932
+ [&GP](const ConstraintLocator *locator) {
2933
+ return locator->getGenericParameter () == GP;
2934
+ }) > 1 ) {
2935
+ return false ;
2936
+ }
2937
+
2913
2938
if (auto *CE = dyn_cast<ExplicitCastExpr>(getRawAnchor ())) {
2914
2939
auto castTo = getType (CE->getCastTypeLoc ());
2915
2940
auto *NTD = castTo->getAnyNominal ();
@@ -2919,21 +2944,33 @@ void MissingGenericArgumentsFailure::diagnoseParameter(
2919
2944
emitDiagnostic (getLoc (), diag::unbound_generic_parameter, GP);
2920
2945
}
2921
2946
2947
+ if (!hasLoc (GP))
2948
+ return true ;
2949
+
2950
+ Type baseType;
2922
2951
auto *DC = getDeclContext ();
2923
- if (DC->isTypeContext () && hasLoc (GP)) {
2924
- auto *base = DC->getSelfNominalTypeDecl ();
2925
- emitDiagnostic (GP->getDecl (), diag::archetype_declared_in_type, GP,
2926
- base->getDeclaredType ());
2952
+
2953
+ if (auto *NTD =
2954
+ dyn_cast_or_null<NominalTypeDecl>(DC->getSelfNominalTypeDecl ())) {
2955
+ baseType = NTD->getDeclaredType ();
2956
+ } else if (auto *TAD = dyn_cast<TypeAliasDecl>(DC)) {
2957
+ baseType = TAD->getUnboundGenericType ();
2958
+ } else {
2959
+ baseType = DC->getDeclaredInterfaceType ();
2927
2960
}
2961
+
2962
+ if (!baseType)
2963
+ return true ;
2964
+
2965
+ emitDiagnostic (GP->getDecl (), diag::archetype_declared_in_type, GP, baseType);
2966
+ return true ;
2928
2967
}
2929
2968
2930
2969
void MissingGenericArgumentsFailure::emitGenericSignatureNote () const {
2931
2970
auto &cs = getConstraintSystem ();
2932
2971
auto &TC = getTypeChecker ();
2933
2972
auto *paramDC = getDeclContext ();
2934
2973
2935
- assert (paramDC->isTypeContext ());
2936
-
2937
2974
auto *GTD = dyn_cast<GenericTypeDecl>(paramDC);
2938
2975
if (!GTD || !BaseType)
2939
2976
return ;
@@ -2947,9 +2984,8 @@ void MissingGenericArgumentsFailure::emitGenericSignatureNote() const {
2947
2984
2948
2985
llvm::SmallDenseMap<GenericTypeParamDecl *, Type> params;
2949
2986
for (auto *typeVar : cs.getTypeVariables ()) {
2950
- if (auto *locator = typeVar->getImpl ().getLocator ()) {
2951
- if (auto *GPD = getParamDecl (locator))
2952
- params[GPD] = resolveType (typeVar);
2987
+ if (auto *GP = typeVar->getImpl ().getGenericParameter ()) {
2988
+ params[GP->getDecl ()] = resolveType (typeVar);
2953
2989
}
2954
2990
}
2955
2991
0 commit comments