Skip to content

Commit cdefbb3

Browse files
Adding tests for coercion fix and fixing test/expr/cast/ tests
1 parent 9fc4de6 commit cdefbb3

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,12 +2807,17 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
28072807

28082808
// If the types are obviously equivalent, we're done.
28092809
if (desugar1->isEqual(desugar2)) {
2810+
auto locatorPtr = locator.getBaseLocator();
28102811
if (kind >= ConstraintKind::Conversion &&
2811-
locator.getBaseLocator()->
2812+
locatorPtr->
28122813
isLastElement(ConstraintLocator::PathElementKind::ExplicityTypeCoercion)) {
2813-
if (!hasFixFor(locator.getBaseLocator())) {
2814+
if (!llvm::any_of(getFixes(), [&locatorPtr](const ConstraintFix *fix) -> bool {
2815+
if (fix->getLocator() == locatorPtr)
2816+
return true;
2817+
return fix->getLocator()->getAnchor() == locatorPtr->getAnchor();
2818+
})) {
28142819
auto *fix = RemoveUnecessaryCoercion::create(*this, type2,
2815-
locator.getBaseLocator());
2820+
locatorPtr);
28162821
recordFix(fix);
28172822
}
28182823
}

test/expr/cast/array_downcast.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var ta = [t]
2424

2525
va = ta
2626

27-
var va2: ([V])? = va as [V]
27+
var va2: ([V])? = va as [V] // expected-warning {{casting expression to '[V]' doesn't change the type}} {{22-29=}}
2828
var v2: V = va2![0]
2929

3030
var ua2: ([U])? = va as? [U]

test/expr/cast/as_coerce.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ c3 as C4 // expected-error {{'C3' is not convertible to 'C4'; did you mean to us
8181
// <rdar://problem/19495142> Various incorrect diagnostics for explicit type conversions
8282
1 as Double as Float // expected-error{{cannot convert value of type 'Double' to type 'Float' in coercion}}
8383
1 as Int as String // expected-error{{cannot convert value of type 'Int' to type 'String' in coercion}}
84-
Double(1) as Double as String // expected-error{{cannot convert value of type 'Double' to type 'String' in coercion}}
84+
Double(1) as Double as String // expected-error{{cannot convert value of type 'Double' to type 'String' in coercion}} // expected-warning {{casting expression to 'Double' doesn't change the type}} {{11-21=}}
8585
["awd"] as [Int] // expected-error{{cannot convert value of type 'String' to expected element type 'Int'}}
8686
([1, 2, 1.0], 1) as ([String], Int)
8787
// expected-error@-1 2 {{cannot convert value of type 'Int' to expected element type 'String'}}
@@ -155,13 +155,13 @@ _ = "Hello" as String // Ok
155155
_ = 1 as Int64 // Ok
156156
_ = [] as Set<Int> // Ok
157157

158-
class A {}
159-
class B: A {}
158+
class SR11295A {}
159+
class SR11295B: SR11295A {}
160160

161-
var a = A()
162-
var b = B()
161+
var sr11295ap = SR11295A()
162+
var sr11295bc = SR11295B()
163163

164-
var ba = b as A // Ok
164+
_ = sr11295bc as SR11295A // Ok
165165

166166
_ = 1 as Double as Double // expected-warning {{casting expression to 'Double' doesn't change the type}} {{17-27=}}
167167
_ = Double(1) as Double // expected-warning {{casting expression to 'Double' doesn't change the type}} {{15-25=}}

0 commit comments

Comments
 (0)