Skip to content

Commit cd057bb

Browse files
committed
[CSFix] Add a fix to detect type specialization arity mismatches
The situations where number of parameters and arguments didn't match.
1 parent 2bbda09 commit cd057bb

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

include/swift/Sema/CSFix.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ enum class FixKind : uint8_t {
457457

458458
/// Ignore an attempt to specialize non-generic type.
459459
AllowConcreteTypeSpecialization,
460+
461+
/// Ignore situations when provided number of generic arguments didn't match
462+
/// expected number of parameters.
463+
IgnoreGenericSpecializationArityMismatch,
460464
};
461465

462466
class ConstraintFix {
@@ -3677,6 +3681,42 @@ class AllowConcreteTypeSpecialization final : public ConstraintFix {
36773681
}
36783682
};
36793683

3684+
class IgnoreGenericSpecializationArityMismatch final : public ConstraintFix {
3685+
ValueDecl *D;
3686+
unsigned NumParams;
3687+
unsigned NumArgs;
3688+
bool HasParameterPack;
3689+
3690+
IgnoreGenericSpecializationArityMismatch(ConstraintSystem &cs,
3691+
ValueDecl *decl, unsigned numParams,
3692+
unsigned numArgs,
3693+
bool hasParameterPack,
3694+
ConstraintLocator *locator)
3695+
: ConstraintFix(cs, FixKind::IgnoreGenericSpecializationArityMismatch,
3696+
locator),
3697+
D(decl), NumParams(numParams), NumArgs(numArgs),
3698+
HasParameterPack(hasParameterPack) {}
3699+
3700+
public:
3701+
std::string getName() const override {
3702+
return "ignore generic specialization mismatch";
3703+
}
3704+
3705+
bool diagnose(const Solution &solution, bool asNote = false) const override;
3706+
3707+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
3708+
return diagnose(*commonFixes.front().first);
3709+
}
3710+
3711+
static IgnoreGenericSpecializationArityMismatch *
3712+
create(ConstraintSystem &cs, ValueDecl *decl, unsigned numParams,
3713+
unsigned numArgs, bool hasParameterPack, ConstraintLocator *locator);
3714+
3715+
static bool classof(const ConstraintFix *fix) {
3716+
return fix->getKind() == FixKind::IgnoreGenericSpecializationArityMismatch;
3717+
}
3718+
};
3719+
36803720
} // end namespace constraints
36813721
} // end namespace swift
36823722

lib/Sema/CSFix.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,3 +2842,19 @@ AllowConcreteTypeSpecialization::create(ConstraintSystem &cs, Type concreteTy,
28422842
return new (cs.getAllocator())
28432843
AllowConcreteTypeSpecialization(cs, concreteTy, locator);
28442844
}
2845+
2846+
bool IgnoreGenericSpecializationArityMismatch::diagnose(
2847+
const Solution &solution, bool asNote) const {
2848+
return false;
2849+
}
2850+
2851+
IgnoreGenericSpecializationArityMismatch *
2852+
IgnoreGenericSpecializationArityMismatch::create(ConstraintSystem &cs,
2853+
ValueDecl *decl,
2854+
unsigned numParams,
2855+
unsigned numArgs,
2856+
bool hasParameterPack,
2857+
ConstraintLocator *locator) {
2858+
return new (cs.getAllocator()) IgnoreGenericSpecializationArityMismatch(
2859+
cs, decl, numParams, numArgs, hasParameterPack, locator);
2860+
}

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14776,7 +14776,8 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1477614776
case FixKind::AllowGlobalActorMismatch:
1477714777
case FixKind::AllowAssociatedValueMismatch:
1477814778
case FixKind::GenericArgumentsMismatch:
14779-
case FixKind::AllowConcreteTypeSpecialization: {
14779+
case FixKind::AllowConcreteTypeSpecialization:
14780+
case FixKind::IgnoreGenericSpecializationArityMismatch: {
1478014781
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
1478114782
}
1478214783
case FixKind::IgnoreInvalidASTNode: {

0 commit comments

Comments
 (0)