Skip to content

Commit d6f9f1c

Browse files
committed
[CSFix] Add a fix to detect incorrect argument to subscript key path index
1 parent 8a0d61f commit d6f9f1c

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

include/swift/Sema/CSFix.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ enum class FixKind : uint8_t {
463463
/// Ignore situations when provided number of generic arguments didn't match
464464
/// expected number of parameters.
465465
IgnoreGenericSpecializationArityMismatch,
466+
467+
/// Ignore situations when key path subscript index gets passed an invalid
468+
/// type as an argument (something that is not a key path).
469+
IgnoreKeyPathSubscriptIndexMismatch,
466470
};
467471

468472
class ConstraintFix {
@@ -3742,6 +3746,30 @@ class IgnoreGenericSpecializationArityMismatch final : public ConstraintFix {
37423746
}
37433747
};
37443748

3749+
class IgnoreKeyPathSubscriptIndexMismatch final : public ConstraintFix {
3750+
Type ArgType;
3751+
3752+
IgnoreKeyPathSubscriptIndexMismatch(ConstraintSystem &cs, Type argTy,
3753+
ConstraintLocator *locator)
3754+
: ConstraintFix(cs, FixKind::IgnoreKeyPathSubscriptIndexMismatch,
3755+
locator),
3756+
ArgType(argTy) {}
3757+
3758+
public:
3759+
std::string getName() const override {
3760+
return "ignore invalid key path subscript index";
3761+
}
3762+
3763+
bool diagnose(const Solution &solution, bool asNote = false) const override;
3764+
3765+
static IgnoreKeyPathSubscriptIndexMismatch *
3766+
create(ConstraintSystem &cs, Type argType, ConstraintLocator *locator);
3767+
3768+
static bool classof(const ConstraintFix *fix) {
3769+
return fix->getKind() == FixKind::IgnoreKeyPathSubscriptIndexMismatch;
3770+
}
3771+
};
3772+
37453773
} // end namespace constraints
37463774
} // end namespace swift
37473775

lib/Sema/CSFix.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,3 +2880,16 @@ IgnoreGenericSpecializationArityMismatch::create(ConstraintSystem &cs,
28802880
return new (cs.getAllocator()) IgnoreGenericSpecializationArityMismatch(
28812881
cs, decl, numParams, numArgs, hasParameterPack, locator);
28822882
}
2883+
2884+
bool IgnoreKeyPathSubscriptIndexMismatch::diagnose(const Solution &solution,
2885+
bool asNote) const {
2886+
InvalidTypeAsKeyPathSubscriptIndex failure(solution, ArgType, getLocator());
2887+
return failure.diagnose(asNote);
2888+
}
2889+
2890+
IgnoreKeyPathSubscriptIndexMismatch *
2891+
IgnoreKeyPathSubscriptIndexMismatch::create(ConstraintSystem &cs, Type argType,
2892+
ConstraintLocator *locator) {
2893+
return new (cs.getAllocator())
2894+
IgnoreKeyPathSubscriptIndexMismatch(cs, argType, locator);
2895+
}

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12417,7 +12417,7 @@ ConstraintSystem::simplifyKeyPathApplicationConstraint(
1241712417
}
1241812418
if (!keyPathTy->isTypeVariableOrMember())
1241912419
return SolutionKind::Error;
12420-
12420+
1242112421
return unsolved();
1242212422
}
1242312423

@@ -14900,7 +14900,8 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1490014900
case FixKind::AllowAssociatedValueMismatch:
1490114901
case FixKind::GenericArgumentsMismatch:
1490214902
case FixKind::AllowConcreteTypeSpecialization:
14903-
case FixKind::IgnoreGenericSpecializationArityMismatch: {
14903+
case FixKind::IgnoreGenericSpecializationArityMismatch:
14904+
case FixKind::IgnoreKeyPathSubscriptIndexMismatch: {
1490414905
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
1490514906
}
1490614907
case FixKind::IgnoreThrownErrorMismatch: {

0 commit comments

Comments
 (0)