@@ -472,15 +472,16 @@ class FailureDiagnosis :public ASTVisitor<FailureDiagnosis, /*exprresult*/bool>{
472
472
// / true.
473
473
bool diagnoseAmbiguousMultiStatementClosure (ClosureExpr *closure);
474
474
475
- // / Check the associated constraint system to see if it has any archetypes
476
- // / not properly resolved or missing . If so, diagnose the problem with
477
- // / an error and return true.
478
- bool diagnoseArchetypeAmbiguity ();
475
+ // / Check the associated constraint system to see if it has any opened generic
476
+ // / parameters that were not bound to a fixed type . If so, diagnose the
477
+ // / problem with an error and return true.
478
+ bool diagnoseAmbiguousGenericParameters ();
479
479
480
- // / Emit an error message about an unbound generic parameter existing, and
481
- // / emit notes referring to the target of a diagnostic, e.g., the function
482
- // / or parameter being used.
483
- void diagnoseUnboundArchetype (ArchetypeType *archetype, Expr *anchor);
480
+ // / Emit an error message about an unbound generic parameter, and emit notes
481
+ // / referring to the target of a diagnostic, e.g., the function or parameter
482
+ // / being used.
483
+ void diagnoseAmbiguousGenericParameter (GenericTypeParamType *paramTy,
484
+ Expr *anchor);
484
485
485
486
// / Produce a diagnostic for a general member-lookup failure (irrespective of
486
487
// / the exact expression kind).
@@ -4687,9 +4688,12 @@ bool FailureDiagnosis::diagnoseArgumentGenericRequirements(
4687
4688
.getOldType ()
4688
4689
->getWithoutSpecifierType ();
4689
4690
4690
- if (argType->is <ArchetypeType>()) {
4691
- diagnoseUnboundArchetype (archetype, fnExpr);
4692
- return true ;
4691
+ if (auto *archetype = argType->getAs <ArchetypeType>()) {
4692
+ auto interfaceTy = archetype->getInterfaceType ();
4693
+ if (auto *paramTy = interfaceTy->getAs <GenericTypeParamType>()) {
4694
+ diagnoseAmbiguousGenericParameter (paramTy, fnExpr);
4695
+ return true ;
4696
+ }
4693
4697
}
4694
4698
4695
4699
if (isUnresolvedOrTypeVarType (argType) || argType->hasError ())
@@ -7735,22 +7739,18 @@ void ConstraintSystem::diagnoseFailureForExpr(Expr *expr) {
7735
7739
diagnosis.diagnoseAmbiguity (expr);
7736
7740
}
7737
7741
7738
- // FIXME: Instead of doing this, we should store the decl in the type
7739
- // variable, or in the locator.
7740
- static bool hasArchetype (const GenericTypeDecl *generic,
7741
- ArchetypeType *archetype) {
7742
- assert (!archetype->getOpenedExistentialType () &&
7743
- !archetype->getParent ());
7744
-
7745
- auto genericEnv = generic->getGenericEnvironment ();
7746
- if (!genericEnv)
7742
+ static bool hasGenericParameter (const GenericTypeDecl *generic,
7743
+ GenericTypeParamType *paramTy) {
7744
+ auto *decl = paramTy->getDecl ();
7745
+ if (!decl)
7747
7746
return false ;
7748
7747
7749
- return archetype-> getGenericEnvironment () == genericEnv ;
7748
+ return decl-> getDeclContext () == generic ;
7750
7749
}
7751
7750
7752
- static void noteArchetypeSource (const TypeLoc &loc, ArchetypeType *archetype,
7753
- ConstraintSystem &cs) {
7751
+ static void noteGenericParameterSource (const TypeLoc &loc,
7752
+ GenericTypeParamType *paramTy,
7753
+ ConstraintSystem &cs) {
7754
7754
const GenericTypeDecl *FoundDecl = nullptr ;
7755
7755
const ComponentIdentTypeRepr *FoundGenericTypeBase = nullptr ;
7756
7756
@@ -7759,10 +7759,10 @@ static void noteArchetypeSource(const TypeLoc &loc, ArchetypeType *archetype,
7759
7759
struct FindGenericTypeDecl : public ASTWalker {
7760
7760
const GenericTypeDecl *FoundDecl = nullptr ;
7761
7761
const ComponentIdentTypeRepr *FoundGenericTypeBase = nullptr ;
7762
- ArchetypeType *Archetype ;
7762
+ GenericTypeParamType *ParamTy ;
7763
7763
7764
- FindGenericTypeDecl (ArchetypeType *Archetype )
7765
- : Archetype(Archetype ) {}
7764
+ FindGenericTypeDecl (GenericTypeParamType *ParamTy )
7765
+ : ParamTy(ParamTy ) {}
7766
7766
7767
7767
bool walkToTypeReprPre (TypeRepr *T) override {
7768
7768
// If we already emitted the note, we're done.
@@ -7771,7 +7771,7 @@ static void noteArchetypeSource(const TypeLoc &loc, ArchetypeType *archetype,
7771
7771
if (auto ident = dyn_cast<ComponentIdentTypeRepr>(T)) {
7772
7772
auto *generic =
7773
7773
dyn_cast_or_null<GenericTypeDecl>(ident->getBoundDecl ());
7774
- if (generic && hasArchetype (generic, Archetype )) {
7774
+ if (generic && hasGenericParameter (generic, ParamTy )) {
7775
7775
FoundDecl = generic;
7776
7776
FoundGenericTypeBase = ident;
7777
7777
return false ;
@@ -7780,7 +7780,7 @@ static void noteArchetypeSource(const TypeLoc &loc, ArchetypeType *archetype,
7780
7780
// Keep walking.
7781
7781
return true ;
7782
7782
}
7783
- } findGenericTypeDecl (archetype );
7783
+ } findGenericTypeDecl (paramTy );
7784
7784
7785
7785
typerepr->walk (findGenericTypeDecl);
7786
7786
FoundDecl = findGenericTypeDecl.FoundDecl ;
@@ -7791,7 +7791,7 @@ static void noteArchetypeSource(const TypeLoc &loc, ArchetypeType *archetype,
7791
7791
// type checked expression.
7792
7792
if (!FoundDecl) {
7793
7793
if (const GenericTypeDecl *generic = loc.getType ()->getAnyGeneric ())
7794
- if (hasArchetype (generic, archetype ))
7794
+ if (hasGenericParameter (generic, paramTy ))
7795
7795
FoundDecl = generic;
7796
7796
}
7797
7797
@@ -7804,7 +7804,7 @@ static void noteArchetypeSource(const TypeLoc &loc, ArchetypeType *archetype,
7804
7804
type = typeAlias->getUnboundGenericType ();
7805
7805
else
7806
7806
type = FoundDecl->getDeclaredInterfaceType ();
7807
- tc.diagnose (FoundDecl, diag::archetype_declared_in_type, archetype , type);
7807
+ tc.diagnose (FoundDecl, diag::archetype_declared_in_type, paramTy , type);
7808
7808
}
7809
7809
7810
7810
if (FoundGenericTypeBase && !isa<GenericIdentTypeRepr>(FoundGenericTypeBase)){
@@ -8065,25 +8065,23 @@ diagnoseAmbiguousMultiStatementClosure(ClosureExpr *closure) {
8065
8065
// / Check the associated constraint system to see if it has any archetypes
8066
8066
// / not properly resolved or missing. If so, diagnose the problem with
8067
8067
// / an error and return true.
8068
- bool FailureDiagnosis::diagnoseArchetypeAmbiguity () {
8069
- using Archetype = std::tuple<ArchetypeType *, ConstraintLocator *, unsigned >;
8070
-
8071
- llvm::SmallVector<Archetype, 2 > unboundParams;
8072
- // Check out all of the type variables lurking in the system. If any are
8073
- // unbound archetypes, then the problem is that it couldn't be resolved.
8068
+ bool FailureDiagnosis::diagnoseAmbiguousGenericParameters () {
8069
+ using GenericParameter = std::tuple<GenericTypeParamType *,
8070
+ ConstraintLocator *,
8071
+ unsigned >;
8072
+
8073
+ llvm::SmallVector<GenericParameter, 2 > unboundParams;
8074
+ // Check out all of the type variables lurking in the system. If any free
8075
+ // type variables were created when opening generic parameters, diagnose
8076
+ // that the generic parameter could not be inferred.
8074
8077
for (auto tv : CS.getTypeVariables ()) {
8075
8078
auto &impl = tv->getImpl ();
8076
8079
8077
8080
if (impl.hasRepresentativeOrFixed ())
8078
8081
continue ;
8079
8082
8080
- // If this is a conversion to a type variable used to form an archetype,
8081
- // Then diagnose this as a generic parameter that could not be resolved.
8082
- auto archetype = impl.getArchetype ();
8083
-
8084
- // Only diagnose archetypes that don't have a parent, i.e., ones
8085
- // that correspond to generic parameters.
8086
- if (!archetype || archetype->getParent ())
8083
+ auto *paramTy = impl.getGenericParameter ();
8084
+ if (!paramTy)
8087
8085
continue ;
8088
8086
8089
8087
// Number of constraints related to particular unbound parameter
@@ -8117,22 +8115,22 @@ bool FailureDiagnosis::diagnoseArchetypeAmbiguity() {
8117
8115
}
8118
8116
8119
8117
auto locator = impl.getLocator ();
8120
- unboundParams.push_back (
8121
- std::make_tuple (archetype, locator, numConstraints));
8118
+ unboundParams.emplace_back (paramTy, locator, numConstraints);
8122
8119
}
8123
8120
8124
8121
// We've found unbound generic parameters, let's diagnose
8125
8122
// based on the number of constraints each one is related to.
8126
8123
if (!unboundParams.empty ()) {
8127
- // Let's prioritize archetypes that don't have any constraints associated.
8124
+ // Let's prioritize generic parameters that don't have any constraints
8125
+ // associated.
8128
8126
std::stable_sort (unboundParams.begin (), unboundParams.end (),
8129
- [](Archetype a, Archetype b) {
8127
+ [](GenericParameter a, GenericParameter b) {
8130
8128
return std::get<2 >(a) < std::get<2 >(b);
8131
8129
});
8132
8130
8133
8131
auto param = unboundParams.front ();
8134
- diagnoseUnboundArchetype (std::get<0 >(param),
8135
- std::get<1 >(param)->getAnchor ());
8132
+ diagnoseAmbiguousGenericParameter (std::get<0 >(param),
8133
+ std::get<1 >(param)->getAnchor ());
8136
8134
return true ;
8137
8135
}
8138
8136
@@ -8142,18 +8140,19 @@ bool FailureDiagnosis::diagnoseArchetypeAmbiguity() {
8142
8140
// / Emit an error message about an unbound generic parameter existing, and
8143
8141
// / emit notes referring to the target of a diagnostic, e.g., the function
8144
8142
// / or parameter being used.
8145
- void FailureDiagnosis::diagnoseUnboundArchetype (ArchetypeType *archetype,
8146
- Expr *anchor) {
8143
+ void FailureDiagnosis::
8144
+ diagnoseAmbiguousGenericParameter (GenericTypeParamType *paramTy,
8145
+ Expr *anchor) {
8147
8146
auto &tc = CS.getTypeChecker ();
8148
8147
8149
- // The archetype may come from the explicit type in a cast expression.
8148
+ // The generic parameter may come from the explicit type in a cast expression.
8150
8149
if (auto *ECE = dyn_cast_or_null<ExplicitCastExpr>(anchor)) {
8151
8150
tc.diagnose (ECE->getLoc (), diag::unbound_generic_parameter_cast,
8152
- archetype , ECE->getCastTypeLoc ().getType ())
8151
+ paramTy , ECE->getCastTypeLoc ().getType ())
8153
8152
.highlight (ECE->getCastTypeLoc ().getSourceRange ());
8154
8153
8155
8154
// Emit a note specifying where this came from, if we can find it.
8156
- noteArchetypeSource (ECE->getCastTypeLoc (), archetype , CS);
8155
+ noteGenericParameterSource (ECE->getCastTypeLoc (), paramTy , CS);
8157
8156
return ;
8158
8157
}
8159
8158
@@ -8173,17 +8172,17 @@ void FailureDiagnosis::diagnoseUnboundArchetype(ArchetypeType *archetype,
8173
8172
8174
8173
8175
8174
// Otherwise, emit an error message on the expr we have, and emit a note
8176
- // about where the archetype came from.
8177
- tc.diagnose (expr->getLoc (), diag::unbound_generic_parameter, archetype );
8175
+ // about where the generic parameter came from.
8176
+ tc.diagnose (expr->getLoc (), diag::unbound_generic_parameter, paramTy );
8178
8177
8179
8178
// If we have an anchor, drill into it to emit a
8180
- // "note: archetype declared here".
8179
+ // "note: generic parameter declared here".
8181
8180
if (!anchor) return ;
8182
8181
8183
8182
8184
8183
if (auto TE = dyn_cast<TypeExpr>(anchor)) {
8185
8184
// Emit a note specifying where this came from, if we can find it.
8186
- noteArchetypeSource (TE->getTypeLoc (), archetype , CS);
8185
+ noteGenericParameterSource (TE->getTypeLoc (), paramTy , CS);
8187
8186
return ;
8188
8187
}
8189
8188
@@ -8232,8 +8231,8 @@ void FailureDiagnosis::diagnoseUnboundArchetype(ArchetypeType *archetype,
8232
8231
// / Emit an ambiguity diagnostic about the specified expression.
8233
8232
void FailureDiagnosis::diagnoseAmbiguity (Expr *E) {
8234
8233
// First, let's try to diagnose any problems related to ambiguous
8235
- // archetypes ( generic parameters) present in the constraint system.
8236
- if (diagnoseArchetypeAmbiguity ())
8234
+ // generic parameters present in the constraint system.
8235
+ if (diagnoseAmbiguousGenericParameters ())
8237
8236
return ;
8238
8237
8239
8238
// Unresolved/Anonymous ClosureExprs are common enough that we should give
0 commit comments