Skip to content

Commit f032b9c

Browse files
committed
[CSFix] Attempt to replace .subscript(...) with subscript operator
If there are no members named "subscript" available, let's try to replace it with subscript operator with might be what was intended.
1 parent 5636cd3 commit f032b9c

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

lib/Sema/CSFix.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,12 @@ InsertExplicitCall *InsertExplicitCall::create(ConstraintSystem &cs,
235235
ConstraintLocator *locator) {
236236
return new (cs.getAllocator()) InsertExplicitCall(cs, locator);
237237
}
238+
239+
bool UseSubscriptOperator::diagnose(Expr *root, bool asNote) const {
240+
return false;
241+
}
242+
243+
UseSubscriptOperator *UseSubscriptOperator::create(ConstraintSystem &cs,
244+
ConstraintLocator *locator) {
245+
return new (cs.getAllocator()) UseSubscriptOperator(cs, locator);
246+
}

lib/Sema/CSFix.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ enum class FixKind : uint8_t {
9696

9797
/// Add explicit `()` at the end of function or member to call it.
9898
InsertCall,
99+
100+
/// Instead of spelling out `subscript` directly, use subscript operator.
101+
UseSubscriptOperator,
99102
};
100103

101104
class ConstraintFix {
@@ -447,6 +450,21 @@ class InsertExplicitCall final : public ConstraintFix {
447450
ConstraintLocator *locator);
448451
};
449452

453+
class UseSubscriptOperator final : public ConstraintFix {
454+
public:
455+
UseSubscriptOperator(ConstraintSystem &cs, ConstraintLocator *locator)
456+
: ConstraintFix(cs, FixKind::UseSubscriptOperator, locator) {}
457+
458+
std::string getName() const override {
459+
return "replace '.subscript(...)' with subscript operator";
460+
}
461+
462+
bool diagnose(Expr *root, bool asNote = false) const override;
463+
464+
static UseSubscriptOperator *create(ConstraintSystem &cs,
465+
ConstraintLocator *locator);
466+
};
467+
450468
} // end namespace constraints
451469
} // end namespace swift
452470

lib/Sema/CSSimplify.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3765,10 +3765,6 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
37653765
// function takes no arguments, otherwise it
37663766
// would make sense to report this a missing member.
37673767
if (funcType->getNumParams() == 0) {
3768-
auto *fix = InsertExplicitCall::create(*this, locator);
3769-
if (recordFix(fix))
3770-
return SolutionKind::Error;
3771-
37723768
auto result = simplifyMemberConstraint(
37733769
kind, funcType->getResult(), member, memberTy, useDC,
37743770
functionRefKind, outerAlternatives, flags, locatorB);
@@ -3777,9 +3773,24 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
37773773
// let's return, otherwise let's fall-through and report
37783774
// this problem as a missing member.
37793775
if (result == SolutionKind::Solved)
3780-
return result;
3776+
return recordFix(InsertExplicitCall::create(*this, locator))
3777+
? SolutionKind::Error
3778+
: SolutionKind::Solved;
37813779
}
37823780
}
3781+
3782+
// Instead of using subscript operator spelled out `subscript` directly.
3783+
if (member.getBaseName() == getTokenText(tok::kw_subscript)) {
3784+
auto result = simplifyMemberConstraint(
3785+
kind, baseTy, DeclBaseName::createSubscript(), memberTy, useDC,
3786+
functionRefKind, {}, flags, locatorB);
3787+
3788+
// Looks like it was indeed meant to be a subscript operator.
3789+
if (result == SolutionKind::Solved)
3790+
return recordFix(UseSubscriptOperator::create(*this, locator))
3791+
? SolutionKind::Error
3792+
: SolutionKind::Solved;
3793+
}
37833794
}
37843795
return SolutionKind::Error;
37853796
}
@@ -5442,6 +5453,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
54425453
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
54435454
}
54445455

5456+
case FixKind::UseSubscriptOperator:
54455457
case FixKind::InsertCall:
54465458
case FixKind::ExplicitlyEscaping:
54475459
case FixKind::CoerceToCheckedCast:

0 commit comments

Comments
 (0)