Skip to content

[CSBindings] Extend early array literal favoring to cover dictionaries #70214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,9 @@ class TypeVariableType::Implementation {
/// Determine whether this type variable represents an opened opaque type.
bool isOpaqueType() const;

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

/// Retrieve the representative of the equivalence class to which this
/// type variable belongs.
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1253,10 +1253,10 @@ bool BindingSet::favoredOverDisjunction(Constraint *disjunction) const {
return boundType->lookThroughAllOptionalTypes()->is<TypeVariableType>();
}

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

// Don't prioritize type variables that don't have any direct bindings.
Expand Down
5 changes: 3 additions & 2 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ bool TypeVariableType::Implementation::isOpaqueType() const {
return false;
}

bool TypeVariableType::Implementation::isArrayLiteralType() const {
return locator && locator->directlyAt<ArrayExpr>();
bool TypeVariableType::Implementation::isCollectionLiteralType() const {
return locator && (locator->directlyAt<ArrayExpr>() ||
locator->directlyAt<DictionaryExpr>());
}

void *operator new(size_t bytes, ConstraintSystem& cs,
Expand Down
26 changes: 26 additions & 0 deletions test/expr/cast/cf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,29 @@ func testBridgedCFDowncast(array: [Any], dictionary: [AnyHashable : Any], set: S
_ = cfDictionary as? [AnyHashable : Any]
_ = cfSet as? Set<AnyHashable>
}

func testCastWithImplicitErasure() {
enum Info {
var id: String { "" }
var options: [CFString : Any]? { nil }
}

class Null {}

struct Test {
var flag: Bool = false
var info: Info

func test(key1: CFString!, key2: CFString!, key3: CFString) -> CFDictionary {
[
key1: flag,
key2: info.id,
key3: info.options ?? Null()
// expected-warning@-1 {{expression implicitly coerced from 'Any?' to 'Any'}}
// expected-note@-2 {{provide a default value to avoid this warning}}
// expected-note@-3 {{force-unwrap the value to avoid this warning}}
// expected-note@-4 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}
] as CFDictionary
}
}
}