@@ -50,29 +50,25 @@ bool FailureDiagnostic::diagnoseAsNote() {
50
50
return false ;
51
51
}
52
52
53
- std::pair< Expr *, bool > FailureDiagnostic::computeAnchor () const {
53
+ Expr *FailureDiagnostic::computeAnchor () const {
54
54
auto &cs = getConstraintSystem ();
55
55
56
56
auto *locator = getLocator ();
57
57
// Resolve the locator to a specific expression.
58
58
SourceRange range;
59
- bool isSubscriptMember =
60
- (!locator->getPath ().empty () && locator->getPath ().back ().getKind () ==
61
- ConstraintLocator::SubscriptMember);
62
-
63
59
ConstraintLocator *resolved = simplifyLocator (cs, locator, range);
64
60
if (!resolved || !resolved->getAnchor ())
65
- return { locator->getAnchor (), true } ;
61
+ return locator->getAnchor ();
66
62
67
63
Expr *anchor = resolved->getAnchor ();
68
64
// FIXME: Work around an odd locator representation that doesn't separate the
69
65
// base of a subscript member from the member access.
70
- if (isSubscriptMember ) {
66
+ if (locator-> isLastElement <LocatorPathElt::SubscriptMember>() ) {
71
67
if (auto subscript = dyn_cast<SubscriptExpr>(anchor))
72
68
anchor = subscript->getBase ();
73
69
}
74
70
75
- return { anchor, !resolved-> getPath (). empty ()} ;
71
+ return anchor;
76
72
}
77
73
78
74
Type FailureDiagnostic::getType (Expr *expr, bool wantRValue) const {
@@ -130,11 +126,6 @@ Expr *FailureDiagnostic::getBaseExprFor(Expr *anchor) const {
130
126
return nullptr ;
131
127
}
132
128
133
- Optional<SelectedOverload>
134
- FailureDiagnostic::getChoiceFor (ConstraintLocator *locator) const {
135
- return getOverloadChoiceIfAvailable (S.getCalleeLocator (locator));
136
- }
137
-
138
129
Type FailureDiagnostic::restoreGenericParameters (
139
130
Type type,
140
131
llvm::function_ref<void (GenericTypeParamType *, Type)> substitution) {
@@ -231,7 +222,7 @@ ValueDecl *RequirementFailure::getDeclRef() const {
231
222
return getAffectedDeclFromType (
232
223
getContextualType (getLocator ()->getAnchor ()));
233
224
234
- if (auto overload = getChoiceFor (getLocator ())) {
225
+ if (auto overload = getCalleeOverloadChoiceIfAvailable (getLocator ())) {
235
226
// If there is a declaration associated with this
236
227
// failure e.g. an overload choice of the call
237
228
// expression, let's see whether failure is
@@ -761,7 +752,7 @@ bool LabelingFailure::diagnoseAsNote() {
761
752
return " (" + str + " )" ;
762
753
};
763
754
764
- auto selectedOverload = getChoiceFor (getLocator ());
755
+ auto selectedOverload = getCalleeOverloadChoiceIfAvailable (getLocator ());
765
756
if (!selectedOverload)
766
757
return false ;
767
758
@@ -874,9 +865,6 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const {
874
865
}
875
866
876
867
bool MissingForcedDowncastFailure::diagnoseAsError () {
877
- if (hasComplexLocator ())
878
- return false ;
879
-
880
868
auto *expr = getAnchor ();
881
869
if (auto *assignExpr = dyn_cast<AssignExpr>(expr))
882
870
expr = assignExpr->getSrc ();
@@ -894,9 +882,6 @@ bool MissingForcedDowncastFailure::diagnoseAsError() {
894
882
}
895
883
896
884
bool MissingAddressOfFailure::diagnoseAsError () {
897
- if (hasComplexLocator ())
898
- return false ;
899
-
900
885
auto *anchor = getAnchor ();
901
886
auto argTy = getFromType ();
902
887
auto paramTy = getToType ();
@@ -913,9 +898,6 @@ bool MissingAddressOfFailure::diagnoseAsError() {
913
898
}
914
899
915
900
bool MissingExplicitConversionFailure::diagnoseAsError () {
916
- if (hasComplexLocator ())
917
- return false ;
918
-
919
901
auto *DC = getDC ();
920
902
auto *anchor = getAnchor ();
921
903
if (auto *assign = dyn_cast<AssignExpr>(anchor))
@@ -976,9 +958,6 @@ bool MissingExplicitConversionFailure::diagnoseAsError() {
976
958
}
977
959
978
960
bool MemberAccessOnOptionalBaseFailure::diagnoseAsError () {
979
- if (hasComplexLocator ())
980
- return false ;
981
-
982
961
auto *anchor = getAnchor ();
983
962
auto baseType = getType (anchor);
984
963
bool resultIsOptional = ResultTypeIsOptional;
@@ -1101,9 +1080,6 @@ class VarDeclMultipleReferencesChecker : public ASTWalker {
1101
1080
};
1102
1081
1103
1082
bool MissingOptionalUnwrapFailure::diagnoseAsError () {
1104
- if (hasComplexLocator ())
1105
- return false ;
1106
-
1107
1083
if (!getUnwrappedType ()->isBool ()) {
1108
1084
if (diagnoseConversionToBool ())
1109
1085
return true ;
@@ -1261,19 +1237,41 @@ bool RValueTreatedAsLValueFailure::diagnoseAsError() {
1261
1237
subElementDiagID = diag::assignment_lhs_is_apply_expression;
1262
1238
}
1263
1239
} else if (auto inoutExpr = dyn_cast<InOutExpr>(diagExpr)) {
1264
- if (auto restriction = getRestrictionForType (getType (inoutExpr))) {
1265
- PointerTypeKind pointerKind;
1266
- if (restriction->second == ConversionRestrictionKind::ArrayToPointer &&
1267
- restriction->first ->getAnyPointerElementType (pointerKind) &&
1268
- (pointerKind == PTK_UnsafePointer ||
1269
- pointerKind == PTK_UnsafeRawPointer)) {
1270
- // If we're converting to an UnsafePointer, then the programmer
1271
- // specified an & unnecessarily. Produce a fixit hint to remove it.
1272
- emitDiagnostic (inoutExpr->getLoc (),
1273
- diag::extra_address_of_unsafepointer, restriction->first )
1274
- .highlight (inoutExpr->getSourceRange ())
1275
- .fixItRemove (inoutExpr->getStartLoc ());
1276
- return true ;
1240
+ if (auto *parentExpr = findParentExpr (inoutExpr)) {
1241
+ if (auto *call =
1242
+ dyn_cast_or_null<ApplyExpr>(findParentExpr (parentExpr))) {
1243
+ // Since this `inout` expression is an argument to a call/operator
1244
+ // let's figure out whether this is an impliict conversion from
1245
+ // array to an unsafe pointer type and diagnose it.
1246
+ unsigned argIdx = 0 ;
1247
+ if (auto *TE = dyn_cast<TupleExpr>(parentExpr)) {
1248
+ for (unsigned n = TE->getNumElements (); argIdx != n; ++argIdx) {
1249
+ if (TE->getElement (argIdx) == inoutExpr)
1250
+ break ;
1251
+ }
1252
+ }
1253
+
1254
+ auto *argLoc = getConstraintLocator (
1255
+ call, {ConstraintLocator::ApplyArgument,
1256
+ LocatorPathElt::ApplyArgToParam (argIdx, argIdx,
1257
+ ParameterTypeFlags ())});
1258
+
1259
+ if (auto info = getFunctionArgApplyInfo (argLoc)) {
1260
+ auto &cs = getConstraintSystem ();
1261
+ auto paramType = info->getParamType ();
1262
+ auto argType = getType (inoutExpr)->getWithoutSpecifierType ();
1263
+
1264
+ PointerTypeKind ptr;
1265
+ if (cs.isArrayType (argType) &&
1266
+ paramType->getAnyPointerElementType (ptr) &&
1267
+ (ptr == PTK_UnsafePointer || ptr == PTK_UnsafeRawPointer)) {
1268
+ emitDiagnostic (inoutExpr->getLoc (),
1269
+ diag::extra_address_of_unsafepointer, paramType)
1270
+ .highlight (inoutExpr->getSourceRange ())
1271
+ .fixItRemove (inoutExpr->getStartLoc ());
1272
+ return true ;
1273
+ }
1274
+ }
1277
1275
}
1278
1276
}
1279
1277
@@ -1296,8 +1294,10 @@ bool RValueTreatedAsLValueFailure::diagnoseAsError() {
1296
1294
ConstructorDecl::BodyInitKind::Delegating) {
1297
1295
emitDiagnostic (loc, diag::assignment_let_property_delegating_init,
1298
1296
member->getName ());
1299
- if (auto *ref = getResolvedMemberRef (member)) {
1300
- emitDiagnostic (ref, diag::decl_declared_here, ref->getFullName ());
1297
+ if (auto overload = getOverloadChoiceIfAvailable (
1298
+ getConstraintLocator (member, ConstraintLocator::Member))) {
1299
+ if (auto *ref = overload->choice .getDeclOrNull ())
1300
+ emitDiagnostic (ref, diag::decl_declared_here, ref->getFullName ());
1301
1301
}
1302
1302
return true ;
1303
1303
}
@@ -1328,7 +1328,7 @@ bool RValueTreatedAsLValueFailure::diagnoseAsError() {
1328
1328
}
1329
1329
1330
1330
bool RValueTreatedAsLValueFailure::diagnoseAsNote () {
1331
- auto overload = getChoiceFor (getLocator ());
1331
+ auto overload = getCalleeOverloadChoiceIfAvailable (getLocator ());
1332
1332
if (!(overload && overload->choice .isDecl ()))
1333
1333
return false ;
1334
1334
@@ -2020,8 +2020,8 @@ bool ContextualFailure::diagnoseAsError() {
2020
2020
case ConstraintLocator::RValueAdjustment: {
2021
2021
auto &cs = getConstraintSystem ();
2022
2022
2023
- auto overload = getChoiceFor (
2024
- cs. getConstraintLocator (anchor, ConstraintLocator::UnresolvedMember));
2023
+ auto overload = getOverloadChoiceIfAvailable (
2024
+ getConstraintLocator (anchor, ConstraintLocator::UnresolvedMember));
2025
2025
if (!(overload && overload->choice .isDecl ()))
2026
2026
return false ;
2027
2027
@@ -2084,7 +2084,7 @@ bool ContextualFailure::diagnoseAsError() {
2084
2084
}
2085
2085
2086
2086
bool ContextualFailure::diagnoseAsNote () {
2087
- auto overload = getChoiceFor (getLocator ());
2087
+ auto overload = getCalleeOverloadChoiceIfAvailable (getLocator ());
2088
2088
if (!(overload && overload->choice .isDecl ()))
2089
2089
return false ;
2090
2090
@@ -3512,7 +3512,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
3512
3512
};
3513
3513
3514
3514
auto *baseLoc = cs.getConstraintLocator (ctorRef->getBase ());
3515
- if (auto selection = getChoiceFor (baseLoc)) {
3515
+ if (auto selection = getCalleeOverloadChoiceIfAvailable (baseLoc)) {
3516
3516
OverloadChoice choice = selection->choice ;
3517
3517
if (choice.isDecl () && isMutable (choice.getDecl ()) &&
3518
3518
!isCallArgument (initCall) &&
@@ -3923,7 +3923,7 @@ bool MissingArgumentsFailure::diagnoseAsError() {
3923
3923
3924
3924
diag.flush ();
3925
3925
3926
- if (auto selectedOverload = getChoiceFor (locator)) {
3926
+ if (auto selectedOverload = getCalleeOverloadChoiceIfAvailable (locator)) {
3927
3927
if (auto *decl = selectedOverload->choice .getDeclOrNull ()) {
3928
3928
emitDiagnostic (decl, diag::decl_declared_here, decl->getFullName ());
3929
3929
}
@@ -3934,7 +3934,7 @@ bool MissingArgumentsFailure::diagnoseAsError() {
3934
3934
3935
3935
bool MissingArgumentsFailure::diagnoseAsNote () {
3936
3936
auto *locator = getLocator ();
3937
- if (auto overload = getChoiceFor (locator)) {
3937
+ if (auto overload = getCalleeOverloadChoiceIfAvailable (locator)) {
3938
3938
auto *fn = resolveType (overload->openedType )->getAs <AnyFunctionType>();
3939
3939
auto loc = overload->choice .getDecl ()->getLoc ();
3940
3940
if (loc.isInvalid ())
@@ -4055,7 +4055,8 @@ bool MissingArgumentsFailure::diagnoseSingleMissingArgument() const {
4055
4055
.fixItInsert (insertLoc, insertText.str ());
4056
4056
}
4057
4057
4058
- if (auto selectedOverload = getChoiceFor (getLocator ())) {
4058
+ if (auto selectedOverload =
4059
+ getCalleeOverloadChoiceIfAvailable (getLocator ())) {
4059
4060
if (auto *decl = selectedOverload->choice .getDeclOrNull ()) {
4060
4061
emitDiagnostic (decl, diag::decl_declared_here, decl->getFullName ());
4061
4062
}
@@ -4175,7 +4176,7 @@ bool MissingArgumentsFailure::diagnoseInvalidTupleDestructuring() const {
4175
4176
if (!(argExpr && getType (argExpr)->getRValueType ()->is <TupleType>()))
4176
4177
return false ;
4177
4178
4178
- auto selectedOverload = getChoiceFor (locator);
4179
+ auto selectedOverload = getCalleeOverloadChoiceIfAvailable (locator);
4179
4180
if (!selectedOverload)
4180
4181
return false ;
4181
4182
@@ -4603,7 +4604,7 @@ bool ExtraneousArgumentsFailure::diagnoseAsError() {
4603
4604
4604
4605
emitDiagnostic (anchor->getLoc (), diag::extra_arguments_in_call, OS.str ());
4605
4606
4606
- if (auto overload = getChoiceFor (getLocator ())) {
4607
+ if (auto overload = getCalleeOverloadChoiceIfAvailable (getLocator ())) {
4607
4608
if (auto *decl = overload->choice .getDeclOrNull ()) {
4608
4609
emitDiagnostic (decl, diag::decl_declared_here, decl->getFullName ());
4609
4610
}
@@ -4613,7 +4614,7 @@ bool ExtraneousArgumentsFailure::diagnoseAsError() {
4613
4614
}
4614
4615
4615
4616
bool ExtraneousArgumentsFailure::diagnoseAsNote () {
4616
- auto overload = getChoiceFor (getLocator ());
4617
+ auto overload = getCalleeOverloadChoiceIfAvailable (getLocator ());
4617
4618
if (!(overload && overload->choice .isDecl ()))
4618
4619
return false ;
4619
4620
@@ -5251,7 +5252,7 @@ bool MutatingMemberRefOnImmutableBase::diagnoseAsError() {
5251
5252
}
5252
5253
5253
5254
bool InvalidTupleSplatWithSingleParameterFailure::diagnoseAsError () {
5254
- auto selectedOverload = getChoiceFor (getLocator ());
5255
+ auto selectedOverload = getCalleeOverloadChoiceIfAvailable (getLocator ());
5255
5256
if (!selectedOverload || !selectedOverload->choice .isDecl ())
5256
5257
return false ;
5257
5258
@@ -5716,7 +5717,7 @@ bool ExpandArrayIntoVarargsFailure::diagnoseAsError() {
5716
5717
}
5717
5718
5718
5719
bool ExpandArrayIntoVarargsFailure::diagnoseAsNote () {
5719
- auto overload = getChoiceFor (getLocator ());
5720
+ auto overload = getCalleeOverloadChoiceIfAvailable (getLocator ());
5720
5721
auto anchor = getAnchor ();
5721
5722
if (!overload || !anchor)
5722
5723
return false ;
@@ -5750,7 +5751,7 @@ bool ExtraneousCallFailure::diagnoseAsError() {
5750
5751
}
5751
5752
};
5752
5753
5753
- if (auto overload = getChoiceFor (cs. getCalleeLocator ( locator) )) {
5754
+ if (auto overload = getCalleeOverloadChoiceIfAvailable ( locator)) {
5754
5755
if (auto *decl = overload->choice .getDeclOrNull ()) {
5755
5756
if (auto *enumCase = dyn_cast<EnumElementDecl>(decl)) {
5756
5757
auto diagnostic = emitDiagnostic (
@@ -5783,13 +5784,12 @@ bool ExtraneousCallFailure::diagnoseAsError() {
5783
5784
5784
5785
bool InvalidUseOfTrailingClosure::diagnoseAsError () {
5785
5786
auto *anchor = getAnchor ();
5786
- auto &cs = getConstraintSystem ();
5787
5787
5788
5788
emitDiagnostic (anchor->getLoc (), diag::trailing_closure_bad_param,
5789
5789
getToType ())
5790
5790
.highlight (anchor->getSourceRange ());
5791
5791
5792
- if (auto overload = getChoiceFor (cs. getCalleeLocator ( getLocator () ))) {
5792
+ if (auto overload = getCalleeOverloadChoiceIfAvailable ( getLocator ())) {
5793
5793
if (auto *decl = overload->choice .getDeclOrNull ()) {
5794
5794
emitDiagnostic (decl, diag::decl_declared_here, decl->getFullName ());
5795
5795
}
@@ -6042,9 +6042,9 @@ bool AssignmentTypeMismatchFailure::diagnoseAsError() {
6042
6042
6043
6043
bool AssignmentTypeMismatchFailure::diagnoseAsNote () {
6044
6044
auto *anchor = getAnchor ();
6045
- auto &cs = getConstraintSystem ();
6046
6045
6047
- if (auto overload = getChoiceFor (cs.getConstraintLocator (anchor))) {
6046
+ if (auto overload =
6047
+ getCalleeOverloadChoiceIfAvailable (getConstraintLocator (anchor))) {
6048
6048
if (auto *decl = overload->choice .getDeclOrNull ()) {
6049
6049
emitDiagnostic (decl,
6050
6050
diag::cannot_convert_candidate_result_to_contextual_type,
0 commit comments