Skip to content

Commit 5ba28ed

Browse files
committed
Replace use of getArchetype() in diagnoseGenericParameterErrors()
This is a quick follow-up to <#1160>, to replace the becoming deprecated getArchetype() with doing the same thing via calling through to ArchetypeBuilder with a declContext. Uses findGenericSubstitutions() to do so. So this site could take advantage of destructuring of more complex params containing generics. Right now, though, that never happens. For complex params the (unfortunately, worse) diagnosis happens in diagnoseFailureForExpr() on the argument expression before reaching here. I’d like to improve that in future work.
1 parent 6b30695 commit 5ba28ed

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -806,11 +806,13 @@ namespace {
806806
struct FailedArgumentInfo {
807807
int argumentNumber = -1; ///< Arg # at the call site.
808808
Type parameterType = Type(); ///< Expected type at the decl site.
809+
DeclContext *declContext = nullptr; ///< Context at the candidate declaration.
809810

810811
bool isValid() const { return argumentNumber != -1; }
811812

812813
bool operator!=(const FailedArgumentInfo &other) {
813814
if (argumentNumber != other.argumentNumber) return true;
815+
if (declContext != other.declContext) return true;
814816
// parameterType can be null, and isEqual doesn't handle this.
815817
if (!parameterType || !other.parameterType)
816818
return parameterType.getPointer() != other.parameterType.getPointer();
@@ -1186,6 +1188,8 @@ CalleeCandidateInfo::evaluateCloseness(DeclContext *dc, Type candArgListType,
11861188

11871189
failureInfo.argumentNumber = argNo;
11881190
failureInfo.parameterType = paramType;
1191+
if (paramType->hasTypeParameter())
1192+
failureInfo.declContext = dc;
11891193
}
11901194
}
11911195

@@ -1735,18 +1739,25 @@ bool CalleeCandidateInfo::diagnoseAnyStructuralArgumentError(Expr *fnExpr,
17351739
/// archetype that has argument type errors, diagnose that error and
17361740
/// return true.
17371741
bool CalleeCandidateInfo::diagnoseGenericParameterErrors(Expr *badArgExpr) {
1738-
bool foundFailure = false;
1739-
Type paramType = failedArgument.parameterType;
17401742
Type argType = badArgExpr->getType();
1741-
1742-
if (auto genericParam = paramType->getAs<GenericTypeParamType>())
1743-
paramType = genericParam->getDecl()->getArchetype();
17441743

1745-
if (paramType->is<ArchetypeType>() && !argType->hasTypeVariable() &&
1746-
// FIXME: For protocol argument types, could add specific error
1747-
// similar to could_not_use_member_on_existential.
1748-
!argType->is<ProtocolType>() && !argType->is<ProtocolCompositionType>()) {
1749-
auto archetype = paramType->castTo<ArchetypeType>();
1744+
// FIXME: For protocol argument types, could add specific error
1745+
// similar to could_not_use_member_on_existential.
1746+
if (argType->hasTypeVariable() || argType->is<ProtocolType>() ||
1747+
argType->is<ProtocolCompositionType>())
1748+
return false;
1749+
1750+
bool foundFailure = false;
1751+
SmallVector<ArchetypeType *, 4> archetypes;
1752+
SmallVector<Type, 4> substitutions;
1753+
1754+
if (!findGenericSubstitutions(failedArgument.declContext, failedArgument.parameterType,
1755+
argType, archetypes, substitutions))
1756+
return false;
1757+
1758+
for (unsigned i = 0, c = archetypes.size(); i < c; i++) {
1759+
auto archetype = archetypes[i];
1760+
auto argType = substitutions[i];
17501761

17511762
// FIXME: Add specific error for not subclass, if the archetype has a superclass?
17521763

0 commit comments

Comments
 (0)