@@ -201,10 +201,6 @@ class FailureDiagnosis :public ASTVisitor<FailureDiagnosis, /*exprresult*/bool>{
201
201
ContextualTypePurpose CTP,
202
202
Type suggestedType = Type());
203
203
204
- bool diagnoseImplicitSelfErrors(Expr *fnExpr, Expr *argExpr,
205
- CalleeCandidateInfo &CCI,
206
- ArrayRef<Identifier> argLabels);
207
-
208
204
private:
209
205
/// Validate potential contextual type for type-checking one of the
210
206
/// sub-expressions, usually correct/valid types are the ones which
@@ -963,223 +959,6 @@ decomposeArgType(Type argType, ArrayRef<Identifier> argLabels) {
963
959
return result;
964
960
}
965
961
966
- bool FailureDiagnosis::diagnoseImplicitSelfErrors(
967
- Expr *fnExpr, Expr *argExpr, CalleeCandidateInfo &CCI,
968
- ArrayRef<Identifier> argLabels) {
969
- // If candidate list is empty it means that problem is somewhere else,
970
- // since we need to have candidates which might be shadowing other funcs.
971
- if (CCI.empty() || !CCI[0].getDecl())
972
- return false;
973
-
974
- auto &ctx = CS.getASTContext();
975
- // Call expression is formed as 'foo.bar' where 'foo' might be an
976
- // implicit "Self" reference, such use wouldn't provide good diagnostics
977
- // for situations where instance members have equal names to functions in
978
- // Swift Standard Library e.g. min/max.
979
- auto UDE = dyn_cast<UnresolvedDotExpr>(fnExpr);
980
- if (!UDE)
981
- return false;
982
-
983
- auto baseExpr = dyn_cast<DeclRefExpr>(UDE->getBase());
984
- if (!baseExpr)
985
- return false;
986
-
987
- auto baseDecl = baseExpr->getDecl();
988
- if (!baseExpr->isImplicit() || baseDecl->getFullName() != ctx.Id_self)
989
- return false;
990
-
991
- // Our base expression is an implicit 'self.' reference e.g.
992
- //
993
- // extension Sequence {
994
- // func test() -> Int {
995
- // return max(1, 2)
996
- // }
997
- // }
998
- //
999
- // In this example the Sequence class already has two methods named 'max'
1000
- // none of which accept two arguments, but there is a function in
1001
- // Swift Standard Library called 'max' which does accept two arguments,
1002
- // so user might have called that by mistake without realizing that
1003
- // compiler would add implicit 'self.' prefix to the call of 'max'.
1004
- auto argType = CS.getType(argExpr);
1005
- // If argument wasn't properly type-checked, let's retry without changing AST.
1006
- if (!argType || argType->hasUnresolvedType() || argType->hasTypeVariable() ||
1007
- argType->hasTypeParameter()) {
1008
- auto *argTuple = dyn_cast<TupleExpr>(argExpr);
1009
- if (!argTuple) {
1010
- // Bail out if we don't have a well-formed argument list.
1011
- return false;
1012
- }
1013
-
1014
- // Let's type check individual argument expressions without any
1015
- // contextual information to try to recover an argument type that
1016
- // matches what the user actually wrote instead of what the typechecker
1017
- // expects.
1018
- SmallVector<TupleTypeElt, 4> elts;
1019
- for (unsigned i = 0, e = argTuple->getNumElements(); i < e; ++i) {
1020
- ConcreteDeclRef ref = nullptr;
1021
- auto *el = argTuple->getElement(i);
1022
- auto typeResult =
1023
- TypeChecker::getTypeOfExpressionWithoutApplying(el, CS.DC, ref);
1024
- if (!typeResult)
1025
- return false;
1026
- auto flags = ParameterTypeFlags().withInOut(typeResult->is<InOutType>());
1027
- elts.push_back(TupleTypeElt(typeResult->getInOutObjectType(),
1028
- argTuple->getElementName(i),
1029
- flags));
1030
- }
1031
-
1032
- argType = TupleType::get(elts, CS.getASTContext());
1033
- }
1034
-
1035
- auto typeKind = argType->getKind();
1036
- if (typeKind != TypeKind::Tuple && typeKind != TypeKind::Paren)
1037
- return false;
1038
-
1039
- // If argument type couldn't be properly resolved or has errors,
1040
- // we can't diagnose anything in here, it points to the different problem.
1041
- if (isUnresolvedOrTypeVarType(argType) || argType->hasError())
1042
- return false;
1043
-
1044
- auto context = CS.DC;
1045
- using CandidateMap =
1046
- llvm::SmallDenseMap<ValueDecl *, llvm::SmallVector<OverloadChoice, 2>>;
1047
-
1048
- auto getBaseKind = [](ValueDecl *base) -> DescriptiveDeclKind {
1049
- DescriptiveDeclKind kind = DescriptiveDeclKind::Module;
1050
- if (!base)
1051
- return kind;
1052
-
1053
- auto context = base->getDeclContext();
1054
- do {
1055
- if (isa<ExtensionDecl>(context))
1056
- return DescriptiveDeclKind::Extension;
1057
-
1058
- if (auto nominal = dyn_cast<NominalTypeDecl>(context)) {
1059
- kind = nominal->getDescriptiveKind();
1060
- break;
1061
- }
1062
-
1063
- context = context->getParent();
1064
- } while (context);
1065
-
1066
- return kind;
1067
- };
1068
-
1069
- auto diagnoseShadowing = [&](ValueDecl *base,
1070
- ArrayRef<OverloadChoice> candidates) -> bool {
1071
- CalleeCandidateInfo calleeInfo(base ? base->getInterfaceType() : nullptr,
1072
- candidates, CCI.hasTrailingClosure, CS,
1073
- base);
1074
-
1075
- calleeInfo.filterListArgs(decomposeArgType(argType, argLabels));
1076
-
1077
- auto diagnostic = diag::member_shadows_global_function_near_match;
1078
- switch (calleeInfo.closeness) {
1079
- case CC_Unavailable:
1080
- case CC_Inaccessible:
1081
- case CC_SelfMismatch:
1082
- case CC_ArgumentLabelMismatch:
1083
- case CC_ArgumentCountMismatch:
1084
- case CC_GeneralMismatch:
1085
- return false;
1086
-
1087
- case CC_NonLValueInOut:
1088
- case CC_OneArgumentNearMismatch:
1089
- case CC_OneArgumentMismatch:
1090
- case CC_OneGenericArgumentNearMismatch:
1091
- case CC_OneGenericArgumentMismatch:
1092
- case CC_ArgumentNearMismatch:
1093
- case CC_ArgumentMismatch:
1094
- case CC_GenericNonsubstitutableMismatch:
1095
- break; // Near match cases
1096
-
1097
- case CC_ExactMatch:
1098
- diagnostic = diag::member_shadows_global_function;
1099
- break;
1100
- }
1101
-
1102
- auto choice = calleeInfo.candidates[0].getDecl();
1103
- auto baseKind = getBaseKind(base);
1104
- auto baseName = getBaseName(choice->getDeclContext());
1105
-
1106
- auto origCandidate = CCI[0].getDecl();
1107
- ctx.Diags.diagnose(UDE->getLoc(), diagnostic, UDE->getName(),
1108
- origCandidate->getDescriptiveKind(),
1109
- origCandidate->getFullName(),
1110
- choice->getDescriptiveKind(),
1111
- choice->getFullName(), baseKind, baseName);
1112
-
1113
- auto topLevelDiag = diag::fix_unqualified_access_top_level;
1114
- if (baseKind == DescriptiveDeclKind::Module)
1115
- topLevelDiag = diag::fix_unqualified_access_top_level_multi;
1116
-
1117
- emitFixItForExplicitlyQualifiedReference(ctx.Diags, UDE, topLevelDiag,
1118
- baseName,
1119
- choice->getDescriptiveKind());
1120
-
1121
- for (auto &candidate : calleeInfo.candidates) {
1122
- if (auto decl = candidate.getDecl())
1123
- ctx.Diags.diagnose(decl, diag::decl_declared_here, decl->getFullName());
1124
- }
1125
-
1126
- return true;
1127
- };
1128
-
1129
- // For each of the parent contexts, let's try to find any candidates
1130
- // which have the same name and the same number of arguments as callee.
1131
- while (context->getParent()) {
1132
- auto result =
1133
- TypeChecker::lookupUnqualified(context, UDE->getName(), UDE->getLoc());
1134
- context = context->getParent();
1135
-
1136
- if (!result || result.empty())
1137
- continue;
1138
-
1139
- CandidateMap candidates;
1140
- for (const auto &candidate : result) {
1141
- auto base = candidate.getBaseDecl();
1142
- auto decl = candidate.getValueDecl();
1143
- if ((base && base->isInvalid()) || decl->isInvalid())
1144
- continue;
1145
-
1146
- // If base is present but it doesn't represent a valid nominal,
1147
- // we can't use current candidate as one of the choices.
1148
- if (base && !base->getInterfaceType()->getNominalOrBoundGenericNominal())
1149
- continue;
1150
-
1151
- auto context = decl->getDeclContext();
1152
- // We are only interested in static or global functions, because
1153
- // there is no way to call anything else properly.
1154
- if (!decl->isStatic() && !context->isModuleScopeContext())
1155
- continue;
1156
-
1157
- OverloadChoice choice(base ? base->getInterfaceType() : nullptr,
1158
- decl, UDE->getFunctionRefKind());
1159
-
1160
- if (base) { // Let's group all of the candidates have a common base.
1161
- candidates[base].push_back(choice);
1162
- continue;
1163
- }
1164
-
1165
- // If there is no base, it means this is one of the global functions,
1166
- // let's try to diagnose its shadowing inline.
1167
- if (diagnoseShadowing(base, choice))
1168
- return true;
1169
- }
1170
-
1171
- if (candidates.empty())
1172
- continue;
1173
-
1174
- for (const auto &candidate : candidates) {
1175
- if (diagnoseShadowing(candidate.getFirst(), candidate.getSecond()))
1176
- return true;
1177
- }
1178
- }
1179
-
1180
- return false;
1181
- }
1182
-
1183
962
// Extract expression for failed argument number
1184
963
static Expr *getFailedArgumentExpr(CalleeCandidateInfo CCI, Expr *argExpr) {
1185
964
if (auto *TE = dyn_cast<TupleExpr>(argExpr))
@@ -1201,10 +980,6 @@ static Expr *getFailedArgumentExpr(CalleeCandidateInfo CCI, Expr *argExpr) {
1201
980
bool FailureDiagnosis::diagnoseParameterErrors(CalleeCandidateInfo &CCI,
1202
981
Expr *fnExpr, Expr *argExpr,
1203
982
ArrayRef<Identifier> argLabels) {
1204
- // Try to diagnose errors related to the use of implicit self reference.
1205
- if (diagnoseImplicitSelfErrors(fnExpr, argExpr, CCI, argLabels))
1206
- return true;
1207
-
1208
983
// If we have a failure where the candidate set differs on exactly one
1209
984
// argument, and where we have a consistent mismatch across the candidate set
1210
985
// (often because there is only one candidate in the set), then diagnose this
0 commit comments