Skip to content

Commit b9ef2ca

Browse files
committed
[CSFix] Add a skeleton of a fix for argument destructuring to match pack expansion
1 parent b5aae06 commit b9ef2ca

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

include/swift/Sema/CSFix.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ enum class FixKind : uint8_t {
434434

435435
/// Allow pack expansion expressions in a context that does not support them.
436436
AllowInvalidPackExpansion,
437+
438+
/// Allow a pack expansion parameter of N elements to be matched
439+
/// with a single tuple literal argument of the same arity.
440+
DestructureTupleToMatchPackExpansionParameter,
437441
};
438442

439443
class ConstraintFix {
@@ -3416,6 +3420,39 @@ class AllowGlobalActorMismatch final : public ContextualMismatch {
34163420
}
34173421
};
34183422

3423+
/// Passing an argument of tuple type to a value pack expansion parameter
3424+
/// that expected N distinct elements.
3425+
class DestructureTupleToMatchPackExpansionParameter final
3426+
: public ConstraintFix {
3427+
PackType *ParamShape;
3428+
3429+
DestructureTupleToMatchPackExpansionParameter(ConstraintSystem &cs,
3430+
PackType *paramShapeTy,
3431+
ConstraintLocator *locator)
3432+
: ConstraintFix(cs,
3433+
FixKind::DestructureTupleToMatchPackExpansionParameter,
3434+
locator),
3435+
ParamShape(paramShapeTy) {
3436+
assert(locator->isLastElement<LocatorPathElt::ApplyArgToParam>());
3437+
}
3438+
3439+
public:
3440+
std::string getName() const override {
3441+
return "allow pack expansion to match tuple argument";
3442+
}
3443+
3444+
bool diagnose(const Solution &solution, bool asNote = false) const override;
3445+
3446+
static DestructureTupleToMatchPackExpansionParameter *
3447+
create(ConstraintSystem &cs, PackType *paramShapeTy,
3448+
ConstraintLocator *locator);
3449+
3450+
static bool classof(const ConstraintFix *fix) {
3451+
return fix->getKind() ==
3452+
FixKind::DestructureTupleToMatchPackExpansionParameter;
3453+
}
3454+
};
3455+
34193456
} // end namespace constraints
34203457
} // end namespace swift
34213458

lib/Sema/CSFix.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,3 +2742,15 @@ AllowGlobalActorMismatch::create(ConstraintSystem &cs, Type fromType,
27422742
return new (cs.getAllocator())
27432743
AllowGlobalActorMismatch(cs, fromType, toType, locator);
27442744
}
2745+
2746+
bool DestructureTupleToMatchPackExpansionParameter::diagnose(
2747+
const Solution &solution, bool asNote) const {
2748+
return false;
2749+
}
2750+
2751+
DestructureTupleToMatchPackExpansionParameter *
2752+
DestructureTupleToMatchPackExpansionParameter::create(
2753+
ConstraintSystem &cs, PackType *paramShapeTy, ConstraintLocator *locator) {
2754+
return new (cs.getAllocator())
2755+
DestructureTupleToMatchPackExpansionParameter(cs, paramShapeTy, locator);
2756+
}

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14773,6 +14773,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1477314773
case FixKind::AllowSwiftToCPointerConversion:
1477414774
case FixKind::AllowTupleLabelMismatch:
1477514775
case FixKind::AddExplicitExistentialCoercion:
14776+
case FixKind::DestructureTupleToMatchPackExpansionParameter:
1477614777
llvm_unreachable("handled elsewhere");
1477714778
}
1477814779

0 commit comments

Comments
 (0)