Skip to content

Commit b5cd400

Browse files
committed
[ConstraintSystem] De-prioritize fixes which suggest removing extraneous labels
The rule is that if there is a label it's evidence that the intent is to reference a particular overload where that label would match, so let's try to de-prioritize fixes for overloads where labels didn't line up correctly and suggestion is to remove certain labels vs. fixes when labels did line up but types or requirements didn't.
1 parent 654c1ae commit b5cd400

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,14 +863,19 @@ getCalleeDeclAndArgs(ConstraintSystem &cs,
863863

864864
class ArgumentFailureTracker : public MatchCallArgumentListener {
865865
ConstraintSystem &CS;
866+
ArrayRef<AnyFunctionType::Param> Arguments;
867+
ArrayRef<AnyFunctionType::Param> Parameters;
866868
SmallVectorImpl<ParamBinding> &Bindings;
867869
ConstraintLocatorBuilder Locator;
868870

869871
public:
870872
ArgumentFailureTracker(ConstraintSystem &cs,
873+
ArrayRef<AnyFunctionType::Param> args,
874+
ArrayRef<AnyFunctionType::Param> params,
871875
SmallVectorImpl<ParamBinding> &bindings,
872876
ConstraintLocatorBuilder locator)
873-
: CS(cs), Bindings(bindings), Locator(locator) {}
877+
: CS(cs), Arguments(args), Parameters(params), Bindings(bindings),
878+
Locator(locator) {}
874879

875880
bool missingLabel(unsigned paramIndex) override {
876881
return !CS.shouldAttemptFixes();
@@ -902,9 +907,27 @@ class ArgumentFailureTracker : public MatchCallArgumentListener {
902907
if (!anchor)
903908
return true;
904909

910+
unsigned numExtraneous = 0;
911+
for (unsigned paramIdx = 0, n = Bindings.size(); paramIdx != n;
912+
++paramIdx) {
913+
if (Bindings[paramIdx].empty())
914+
continue;
915+
916+
const auto paramLabel = Parameters[paramIdx].getLabel();
917+
for (auto argIdx : Bindings[paramIdx]) {
918+
auto argLabel = Arguments[argIdx].getLabel();
919+
if (paramLabel.empty() && !argLabel.empty())
920+
++numExtraneous;
921+
}
922+
}
923+
905924
auto *locator = CS.getConstraintLocator(anchor);
906925
auto *fix = RelabelArguments::create(CS, newLabels, locator);
907926
CS.recordFix(fix);
927+
// Re-labeling fixes with extraneous labels should take
928+
// lower priority vs. other fixes on same/different
929+
// overload(s) where labels did line up correctly.
930+
CS.increaseScore(ScoreKind::SK_Fix, numExtraneous);
908931
return false;
909932
}
910933
};
@@ -934,7 +957,8 @@ ConstraintSystem::TypeMatchResult constraints::matchCallArguments(
934957

935958
// Match up the call arguments to the parameters.
936959
SmallVector<ParamBinding, 4> parameterBindings;
937-
ArgumentFailureTracker listener(cs, parameterBindings, locator);
960+
ArgumentFailureTracker listener(cs, argsWithLabels, params, parameterBindings,
961+
locator);
938962
if (constraints::matchCallArguments(argsWithLabels, params,
939963
paramInfo,
940964
hasTrailingClosure,

test/Parse/type_expr.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ func typeOfShadowing() {
134134
}
135135

136136
// TODO: Errors need improving here.
137-
_ = type(of: Gen<Foo>.Bar) // expected-error{{argument labels '(of:)' do not match any available overloads}}
138-
// expected-note@-1{{overloads for 'type' exist with these partially matching parameter lists: (T.Type), (fo: T.Type)}}
137+
_ = type(of: Gen<Foo>.Bar) // expected-error{{incorrect argument label in call (have 'of:', expected 'fo:')}}
139138
_ = type(Gen<Foo>.Bar) // expected-error{{expected member name or constructor call after type name}}
140139
// expected-note@-1{{add arguments after the type to construct a value of the type}}
141140
// expected-note@-2{{use '.self' to reference the type object}}

0 commit comments

Comments
 (0)