Skip to content

Commit 0ec84e7

Browse files
authored
Merge pull request #70214 from xedin/rdar-119040159
[CSBindings] Extend early array literal favoring to cover dictionaries
2 parents 4df6213 + 911ba10 commit 0ec84e7

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,9 @@ class TypeVariableType::Implementation {
519519
/// Determine whether this type variable represents an opened opaque type.
520520
bool isOpaqueType() const;
521521

522-
/// Determine whether this type variable represents a type of an array literal
523-
/// (represented by `ArrayExpr` in AST).
524-
bool isArrayLiteralType() const;
522+
/// Determine whether this type variable represents a type of a collection
523+
/// literal (represented by `ArrayExpr` and `DictionaryExpr` in AST).
524+
bool isCollectionLiteralType() const;
525525

526526
/// Retrieve the representative of the equivalence class to which this
527527
/// type variable belongs.

lib/Sema/CSBindings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,10 +1253,10 @@ bool BindingSet::favoredOverDisjunction(Constraint *disjunction) const {
12531253
return boundType->lookThroughAllOptionalTypes()->is<TypeVariableType>();
12541254
}
12551255

1256-
// If this is an array literal type, it's preferrable to bind it
1256+
// If this is a collection literal type, it's preferrable to bind it
12571257
// early (unless it's delayed) to connect all of its elements even
12581258
// if it doesn't have any bindings.
1259-
if (TypeVar->getImpl().isArrayLiteralType())
1259+
if (TypeVar->getImpl().isCollectionLiteralType())
12601260
return !involvesTypeVariables();
12611261

12621262
// Don't prioritize type variables that don't have any direct bindings.

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ bool TypeVariableType::Implementation::isOpaqueType() const {
191191
return false;
192192
}
193193

194-
bool TypeVariableType::Implementation::isArrayLiteralType() const {
195-
return locator && locator->directlyAt<ArrayExpr>();
194+
bool TypeVariableType::Implementation::isCollectionLiteralType() const {
195+
return locator && (locator->directlyAt<ArrayExpr>() ||
196+
locator->directlyAt<DictionaryExpr>());
196197
}
197198

198199
void *operator new(size_t bytes, ConstraintSystem& cs,

test/expr/cast/cf.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,29 @@ func testBridgedCFDowncast(array: [Any], dictionary: [AnyHashable : Any], set: S
105105
_ = cfDictionary as? [AnyHashable : Any]
106106
_ = cfSet as? Set<AnyHashable>
107107
}
108+
109+
func testCastWithImplicitErasure() {
110+
enum Info {
111+
var id: String { "" }
112+
var options: [CFString : Any]? { nil }
113+
}
114+
115+
class Null {}
116+
117+
struct Test {
118+
var flag: Bool = false
119+
var info: Info
120+
121+
func test(key1: CFString!, key2: CFString!, key3: CFString) -> CFDictionary {
122+
[
123+
key1: flag,
124+
key2: info.id,
125+
key3: info.options ?? Null()
126+
// expected-warning@-1 {{expression implicitly coerced from 'Any?' to 'Any'}}
127+
// expected-note@-2 {{provide a default value to avoid this warning}}
128+
// expected-note@-3 {{force-unwrap the value to avoid this warning}}
129+
// expected-note@-4 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}
130+
] as CFDictionary
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)