Skip to content

Commit adbbc00

Browse files
committed
[CSFix] Add a warning fix for situations when trailing closure is matched via backward scan
1 parent f78a87a commit adbbc00

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/Sema/CSFix.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,3 +1526,15 @@ UnwrapOptionalBaseKeyPathApplication::attempt(ConstraintSystem &cs, Type baseTy,
15261526
return new (cs.getAllocator())
15271527
UnwrapOptionalBaseKeyPathApplication(cs, baseTy, rootTy, locator);
15281528
}
1529+
1530+
bool SpecifyLabelToAssociateTrailingClosure::diagnose(const Solution &solution,
1531+
bool asNote) const {
1532+
return false;
1533+
}
1534+
1535+
SpecifyLabelToAssociateTrailingClosure *
1536+
SpecifyLabelToAssociateTrailingClosure::create(ConstraintSystem &cs,
1537+
ConstraintLocator *locator) {
1538+
return new (cs.getAllocator())
1539+
SpecifyLabelToAssociateTrailingClosure(cs, locator);
1540+
}

lib/Sema/CSFix.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ enum class FixKind : uint8_t {
269269

270270
/// Unwrap optional base on key path application.
271271
UnwrapOptionalBaseKeyPathApplication,
272+
273+
/// Explicitly specify a label to match trailing closure to a certain
274+
/// parameter in the call.
275+
SpecifyLabelToAssociateTrailingClosure,
272276
};
273277

274278
class ConstraintFix {
@@ -1926,6 +1930,34 @@ class UnwrapOptionalBaseKeyPathApplication final : public ContextualMismatch {
19261930
ConstraintLocator *locator);
19271931
};
19281932

1933+
/// Diagnose situations when solver used old (backward scan) rule
1934+
/// to match trailing closure to a parameter.
1935+
///
1936+
/// \code
1937+
/// func multiple_trailing_with_defaults(
1938+
/// duration: Int,
1939+
/// animations: (() -> Void)? = nil,
1940+
/// completion: (() -> Void)? = nil) {}
1941+
///
1942+
/// multiple_trailing_with_defaults(duration: 42) {} // picks `completion:`
1943+
/// \endcode
1944+
class SpecifyLabelToAssociateTrailingClosure final : public ConstraintFix {
1945+
SpecifyLabelToAssociateTrailingClosure(ConstraintSystem &cs,
1946+
ConstraintLocator *locator)
1947+
: ConstraintFix(cs, FixKind::SpecifyLabelToAssociateTrailingClosure,
1948+
locator, /*isWarning=*/true) {}
1949+
1950+
public:
1951+
std::string getName() const override {
1952+
return "specify a label to associate trailing closure with parameter";
1953+
}
1954+
1955+
bool diagnose(const Solution &solution, bool asNote = false) const override;
1956+
1957+
static SpecifyLabelToAssociateTrailingClosure *
1958+
create(ConstraintSystem &cs, ConstraintLocator *locator);
1959+
};
1960+
19291961
} // end namespace constraints
19301962
} // end namespace swift
19311963

0 commit comments

Comments
 (0)