Skip to content

Commit 922f372

Browse files
Adding condition on repair to handle explicit coercion
1 parent b02fbcf commit 922f372

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,15 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const {
956956
}
957957

958958
bool MissingForcedDowncastFailure::diagnoseAsError() {
959-
if (hasComplexLocator())
960-
return false;
959+
bool isExplicitCoercion =
960+
getLocator()
961+
->isLastElement<
962+
ConstraintLocator::PathElement::ExplicitTypeCoercion>();
963+
964+
if (!isExplicitCoercion) {
965+
if (hasComplexLocator())
966+
return false;
967+
}
961968

962969
auto *expr = getAnchor();
963970
if (auto *assignExpr = dyn_cast<AssignExpr>(expr))

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,7 +2503,8 @@ bool ConstraintSystem::repairFailures(
25032503
});
25042504
};
25052505

2506-
if (path.empty()) {
2506+
if (path.empty() ||
2507+
path.back().getKind() == ConstraintLocator::ExplicitTypeCoercion) {
25072508
if (!anchor)
25082509
return false;
25092510

@@ -3125,7 +3126,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
31253126
if (!(desugar1->is<DependentMemberType>() &&
31263127
desugar2->is<DependentMemberType>())) {
31273128
if (desugar1->isEqual(desugar2)) {
3128-
if (kind >= ConstraintKind::Conversion) {
3129+
if (kind >= ConstraintKind::Conversion &&
3130+
!flags.contains(TMF_ApplyingFix)) {
31293131
if (RemoveUnnecessaryCoercion::attempt(*this, type1, type2,
31303132
getConstraintLocator(locator))) {
31313133
return getTypeMatchFailure(locator);

test/Sema/suppress-argument-labels-in-types.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,18 @@ let _ = min(Int(3), Float(2.5)) // expected-error{{cannot convert value of type
207207

208208
// SR-11429
209209
func testIntermediateCoercions() {
210-
_ = (f1 as (Int, Int) -> Int)(a: 0, b: 1) // expected-error {{extraneous argument labels 'a:b:' in call}}
211-
_ = (f1 as (Int, Int) -> Int)(0, 1)
210+
_ = (f1 as (Int, Int) -> Int)(a: 0, b: 1) // expected-error {{extraneous argument labels 'a:b:' in call}} expected-warning {{redundant cast to '(Int, Int) -> Int' has no effect}}
211+
_ = (f1 as (Int, Int) -> Int)(0, 1) // expected-warning {{redundant cast to '(Int, Int) -> Int' has no effect}}
212212

213213
typealias Magic<T> = T
214-
_ = (f1 as Magic)(a: 0, b: 1) // expected-error {{extraneous argument labels 'a:b:' in call}}
215-
_ = (f1 as Magic)(0, 1)
214+
_ = (f1 as Magic)(a: 0, b: 1) // expected-error {{extraneous argument labels 'a:b:' in call}} expected-warning {{redundant cast to '(Int, Int) -> Int' has no effect}}
215+
_ = (f1 as Magic)(0, 1) // expected-warning {{redundant cast to '(Int, Int) -> Int' has no effect}}
216216

217217
_ = (f4 as (Int, Int) -> Int)(0, 0)
218218
_ = (f4 as (Double, Double) -> Double)(0, 0)
219219

220220
func iuoReturning() -> Int! {}
221-
_ = (iuoReturning as () -> Int?)()
222-
_ = (iuoReturning as Magic)()
221+
_ = (iuoReturning as () -> Int?)() // expected-warning {{redundant cast to '() -> Int?' has no effect}}
222+
_ = (iuoReturning as Magic)() // expected-warning {{redundant cast to '() -> Int?' has no effect}}
223223
_ = (iuoReturning as () -> Int)() // expected-error {{cannot convert value of type '() -> Int?' to type '() -> Int' in coercion}}
224224
}

0 commit comments

Comments
 (0)