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