Skip to content

Commit 43aafcd

Browse files
committed
[CSGen] Use addJoinConstraint for joining array literal element types.
1 parent 0efb86a commit 43aafcd

File tree

5 files changed

+21
-30
lines changed

5 files changed

+21
-30
lines changed

lib/Sema/CSGen.cpp

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,31 +1803,28 @@ namespace {
18031803

18041804
auto locator = CS.getConstraintLocator(expr);
18051805
auto contextualType = CS.getContextualType(expr);
1806-
Type contextualArrayType = nullptr;
1807-
Type contextualArrayElementType = nullptr;
1808-
1806+
1807+
auto joinElementTypes = [&](Optional<Type> elementType) {
1808+
const auto elements = expr->getElements();
1809+
unsigned index = 0;
1810+
1811+
using Iterator = decltype(elements)::iterator;
1812+
CS.addJoinConstraint<Iterator>(locator, elements.begin(), elements.end(),
1813+
elementType, [&](const auto it) {
1814+
auto *locator = CS.getConstraintLocator(expr, LocatorPathElt::TupleElement(index++));
1815+
return std::make_pair(CS.getType(*it), locator);
1816+
});
1817+
};
1818+
18091819
// If a contextual type exists for this expression, apply it directly.
18101820
Optional<Type> arrayElementType;
18111821
if (contextualType &&
18121822
(arrayElementType = ConstraintSystem::isArrayType(contextualType))) {
1813-
// Is the array type a contextual type
1814-
contextualArrayType = contextualType;
1815-
contextualArrayElementType = *arrayElementType;
1816-
18171823
CS.addConstraint(ConstraintKind::LiteralConformsTo, contextualType,
18181824
arrayProto->getDeclaredType(),
18191825
locator);
1820-
1821-
unsigned index = 0;
1822-
for (auto element : expr->getElements()) {
1823-
CS.addConstraint(ConstraintKind::Conversion,
1824-
CS.getType(element),
1825-
contextualArrayElementType,
1826-
CS.getConstraintLocator(
1827-
expr, LocatorPathElt::TupleElement(index++)));
1828-
}
1829-
1830-
return contextualArrayType;
1826+
joinElementTypes(arrayElementType);
1827+
return contextualType;
18311828
}
18321829

18331830
// Produce a specialized diagnostic if this is an attempt to initialize
@@ -1893,14 +1890,7 @@ namespace {
18931890

18941891
// Introduce conversions from each element to the element type of the
18951892
// array.
1896-
unsigned index = 0;
1897-
for (auto element : expr->getElements()) {
1898-
CS.addConstraint(ConstraintKind::Conversion,
1899-
CS.getType(element),
1900-
arrayElementTy,
1901-
CS.getConstraintLocator(
1902-
expr, LocatorPathElt::TupleElement(index++)));
1903-
}
1893+
joinElementTypes(arrayElementTy);
19041894

19051895
// The array element type defaults to 'Any'.
19061896
CS.addConstraint(ConstraintKind::Defaultable, arrayElementTy,

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4216,6 +4216,7 @@ bool ConstraintSystem::repairFailures(
42164216

42174217
conversionsOrFixes.push_back(CollectionElementContextualMismatch::create(
42184218
*this, lhs, rhs, getConstraintLocator(locator)));
4219+
break;
42194220
}
42204221

42214222
// Drop the `tuple element` locator element so that all tuple element

test/Constraints/diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ variadic(arrayWithOtherEltType) // expected-error {{cannot convert value of type
338338
variadic(1, arrayWithOtherEltType) // expected-error {{cannot convert value of type '[String]' to expected argument type 'Int'}}
339339

340340
// FIXME: SR-11104
341-
variadic(["hello", "world"]) // expected-error 2 {{cannot convert value of type 'String' to expected element type 'Int'}}
341+
variadic(["hello", "world"]) // expected-error {{cannot convert value of type 'String' to expected element type 'Int'}}
342342
// expected-error@-1 {{cannot pass array of type '[Int]' as variadic arguments of type 'Int'}}
343343
// expected-note@-2 {{remove brackets to pass array elements directly}}
344344

test/Parse/pointer_conversion.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func constPointerArguments(_ p: UnsafeMutablePointer<Int>,
164164
takesConstPointer([0, 1, 2])
165165
// <rdar://problem/22308330> QoI: CSDiags doesn't handle array -> pointer impl conversions well
166166
takesConstPointer([0.0, 1.0, 2.0])
167-
// expected-error@-1 3 {{cannot convert value of type 'Double' to expected element type 'Int'}}
167+
// expected-error@-1 {{cannot convert value of type 'Double' to expected element type 'Int'}}
168168

169169
// We don't allow these conversions outside of function arguments.
170170
var x: UnsafePointer<Int> = &i // expected-error {{use of extraneous '&'}}

test/expr/cast/as_coerce.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ c3 as C4 // expected-error {{'C3' is not convertible to 'C4'; did you mean to us
8484
Double(1) as Double as String // expected-error{{cannot convert value of type 'Double' to type 'String' in coercion}}
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)
87-
// expected-error@-1 2 {{cannot convert value of type 'Int' to expected element type 'String'}}
87+
// expected-error@-1 {{cannot convert value of type 'Int' to expected element type 'String'}}
8888
// expected-error@-2 {{cannot convert value of type 'Double' to expected element type 'String'}}
8989
[[1]] as [[String]] // expected-error{{cannot convert value of type 'Int' to expected element type 'String'}}
9090
(1, 1.0) as (Int, Int) // expected-error{{cannot convert value of type '(Int, Double)' to type '(Int, Int)' in coercion}}
9191
(1.0, 1, "asd") as (String, Int, Float) // expected-error{{cannot convert value of type '(Double, Int, String)' to type '(String, Int, Float)' in coercion}}
9292
(1, 1.0, "a", [1, 23]) as (Int, Double, String, [String])
93-
// expected-error@-1 2 {{cannot convert value of type 'Int' to expected element type 'String'}}
93+
// expected-error@-1 {{cannot convert value of type 'Int' to expected element type 'String'}}
9494

9595
_ = [1] as! [String] // OK
9696
_ = [(1, (1, 1))] as! [(Int, (String, Int))] // OK

0 commit comments

Comments
 (0)