Skip to content

Commit 1d2c363

Browse files
committed
[ConstraintSystem] Fix out-of-order arguments
Detect and fix out-of-order arguments by moving them into correct positions.
1 parent 5dd5454 commit 1d2c363

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

lib/Sema/CSFix.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,14 @@ AddMissingArguments::create(ConstraintSystem &cs, FunctionType *funcType,
371371
void *mem = cs.getAllocator().Allocate(size, alignof(AddMissingArguments));
372372
return new (mem) AddMissingArguments(cs, funcType, synthesizedArgs, locator);
373373
}
374+
375+
bool MoveOutOfOrderArgument::diagnose(Expr *root, bool asNote) const {
376+
return false;
377+
}
378+
379+
MoveOutOfOrderArgument *MoveOutOfOrderArgument::create(
380+
ConstraintSystem &cs, unsigned argIdx, unsigned prevArgIdx,
381+
ArrayRef<ParamBinding> bindings, ConstraintLocator *locator) {
382+
return new (cs.getAllocator())
383+
MoveOutOfOrderArgument(cs, argIdx, prevArgIdx, bindings, locator);
384+
}

lib/Sema/CSFix.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ enum class FixKind : uint8_t {
129129

130130
/// Allow single tuple closure parameter destructuring into N arguments.
131131
AllowClosureParameterDestructuring,
132+
133+
/// If there is out-of-order argument, let's fix that by re-ordering.
134+
MoveOutOfOrderArgument,
132135
};
133136

134137
class ConstraintFix {
@@ -681,6 +684,35 @@ class AddMissingArguments final
681684
}
682685
};
683686

687+
class MoveOutOfOrderArgument final : public ConstraintFix {
688+
using ParamBinding = SmallVector<unsigned, 1>;
689+
690+
unsigned ArgIdx;
691+
unsigned PrevArgIdx;
692+
693+
SmallVector<ParamBinding, 4> Bindings;
694+
695+
MoveOutOfOrderArgument(ConstraintSystem &cs, unsigned argIdx,
696+
unsigned prevArgIdx, ArrayRef<ParamBinding> bindings,
697+
ConstraintLocator *locator)
698+
: ConstraintFix(cs, FixKind::MoveOutOfOrderArgument, locator),
699+
ArgIdx(argIdx), PrevArgIdx(prevArgIdx),
700+
Bindings(bindings.begin(), bindings.end()) {}
701+
702+
public:
703+
std::string getName() const override {
704+
return "move out-of-order argument to correct position";
705+
}
706+
707+
bool diagnose(Expr *root, bool asNote = false) const override;
708+
709+
static MoveOutOfOrderArgument *create(ConstraintSystem &cs,
710+
unsigned argIdx,
711+
unsigned prevArgIdx,
712+
ArrayRef<ParamBinding> bindings,
713+
ConstraintLocator *locator);
714+
};
715+
684716
} // end namespace constraints
685717
} // end namespace swift
686718

lib/Sema/CSSimplify.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -855,11 +855,14 @@ getCalleeDeclAndArgs(ConstraintSystem &cs,
855855

856856
class ArgumentFailureTracker : public MatchCallArgumentListener {
857857
ConstraintSystem &CS;
858+
SmallVectorImpl<ParamBinding> &Bindings;
858859
ConstraintLocatorBuilder Locator;
859860

860861
public:
861-
ArgumentFailureTracker(ConstraintSystem &cs, ConstraintLocatorBuilder locator)
862-
: CS(cs), Locator(locator) {}
862+
ArgumentFailureTracker(ConstraintSystem &cs,
863+
SmallVectorImpl<ParamBinding> &bindings,
864+
ConstraintLocatorBuilder locator)
865+
: CS(cs), Bindings(bindings), Locator(locator) {}
863866

864867
bool missingLabel(unsigned paramIndex) override {
865868
return !CS.shouldAttemptFixes();
@@ -873,6 +876,16 @@ class ArgumentFailureTracker : public MatchCallArgumentListener {
873876
return !CS.shouldAttemptFixes();
874877
}
875878

879+
bool outOfOrderArgument(unsigned argIdx, unsigned prevArgIdx) override {
880+
if (CS.shouldAttemptFixes()) {
881+
auto *fix = MoveOutOfOrderArgument::create(
882+
CS, argIdx, prevArgIdx, Bindings, CS.getConstraintLocator(Locator));
883+
return CS.recordFix(fix);
884+
}
885+
886+
return true;
887+
}
888+
876889
bool relabelArguments(ArrayRef<Identifier> newLabels) override {
877890
if (!CS.shouldAttemptFixes())
878891
return true;
@@ -940,8 +953,8 @@ ConstraintSystem::TypeMatchResult constraints::matchCallArguments(
940953
}
941954

942955
// Match up the call arguments to the parameters.
943-
ArgumentFailureTracker listener(cs, locator);
944956
SmallVector<ParamBinding, 4> parameterBindings;
957+
ArgumentFailureTracker listener(cs, parameterBindings, locator);
945958
if (constraints::matchCallArguments(argsWithLabels, params,
946959
defaultMap,
947960
hasTrailingClosure,
@@ -6022,6 +6035,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
60226035
case FixKind::AllowInvalidPartialApplication:
60236036
case FixKind::AllowInvalidInitRef:
60246037
case FixKind::AllowClosureParameterDestructuring:
6038+
case FixKind::MoveOutOfOrderArgument:
60256039
llvm_unreachable("handled elsewhere");
60266040
}
60276041

0 commit comments

Comments
 (0)