Skip to content

[SR-15038][Sema] Use locator to propagate information of array, set, dictonary element on check cast constraint #38884

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
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
31 changes: 20 additions & 11 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6600,7 +6600,8 @@ static ConstraintFix *maybeWarnAboutExtraneousCast(
SmallVector<Type, 4> toOptionals, ConstraintSystem::TypeMatchOptions flags,
ConstraintLocatorBuilder locator) {

if (flags.contains(ConstraintSystem::TypeMatchFlags::TMF_ApplyingFix))
auto last = locator.last();
if (last && last->is<LocatorPathElt::GenericArgument>())
return nullptr;

// Both types have to be fixed.
Expand Down Expand Up @@ -6777,9 +6778,11 @@ ConstraintSystem::simplifyCheckedCastConstraint(
case CheckedCastKind::ArrayDowncast: {
auto fromBaseType = *isArrayType(fromType);
auto toBaseType = *isArrayType(toType);

auto result = simplifyCheckedCastConstraint(
fromBaseType, toBaseType, subflags | TMF_ApplyingFix, locator);

auto elementLocator =
locator.withPathElement(LocatorPathElt::GenericArgument(0));
auto result = simplifyCheckedCastConstraint(fromBaseType, toBaseType,
subflags, elementLocator);
attemptRecordCastFixIfSolved(result);
return result;
}
Expand All @@ -6791,22 +6794,28 @@ ConstraintSystem::simplifyCheckedCastConstraint(
Type toKeyType, toValueType;
std::tie(toKeyType, toValueType) = *isDictionaryType(toType);

if (simplifyCheckedCastConstraint(fromKeyType, toKeyType,
subflags | TMF_ApplyingFix,
locator) == SolutionKind::Error)
auto keyLocator =
locator.withPathElement(LocatorPathElt::GenericArgument(0));
if (simplifyCheckedCastConstraint(fromKeyType, toKeyType, subflags,
keyLocator) == SolutionKind::Error)
return SolutionKind::Error;

auto result = simplifyCheckedCastConstraint(
fromValueType, toValueType, subflags | TMF_ApplyingFix, locator);
auto valueLocator =
locator.withPathElement(LocatorPathElt::GenericArgument(1));
auto result = simplifyCheckedCastConstraint(fromValueType, toValueType,
subflags, valueLocator);
attemptRecordCastFixIfSolved(result);
return result;
}

case CheckedCastKind::SetDowncast: {
auto fromBaseType = *isSetType(fromType);
auto toBaseType = *isSetType(toType);
auto result = simplifyCheckedCastConstraint(
fromBaseType, toBaseType, subflags | TMF_ApplyingFix, locator);

auto elementLocator =
locator.withPathElement(LocatorPathElt::GenericArgument(0));
auto result = simplifyCheckedCastConstraint(fromBaseType, toBaseType,
subflags, elementLocator);
attemptRecordCastFixIfSolved(result);
return result;
}
Expand Down
64 changes: 64 additions & 0 deletions test/Constraints/casts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,67 @@ extension ChangeType where T == String? {
var foo: String? { return self.delta?.previous as? String } // OK
var bar: String? { self.delta?.next }
}

// SR-15038
protocol ExperimentDeserializable {
static func deserializeExperiment(_ value: Any) -> Self?
}

extension String: ExperimentDeserializable {
static func deserializeExperiment(_ value: Any) -> String? { value as? String }
}

extension Int: ExperimentDeserializable {
static func deserializeExperiment(_ value: Any) -> Int? { value as? Int }
}

class Constant<T> {
private init(getUnderlyingValue: @escaping () -> T) {
print(getUnderlyingValue())
}
}

struct Thing {
let storage: [String: Any]
}

extension Constant where T: Sequence, T.Element: ExperimentDeserializable {
static func foo<U>(thing: Thing, defaultValue: T) -> T where T == [U] {
guard let array = thing.storage["foo"] as? [Any] else {
fatalError()
}

let value = array.map(T.Element.deserializeExperiment) as? [T.Element] ?? defaultValue // OK
return value
}
}

// Array
func decodeStringOrInt<T: FixedWidthInteger>() -> [T] {
let stringWrapped = [String]()
if let values = stringWrapped.map({ $0.isEmpty ? 0 : T($0) }) as? [T] { // OK
return values
} else {
fatalError()
}
}

// Set
func decodeStringOrIntSet<T: FixedWidthInteger>() -> Set<T> {
let stringWrapped = [String]()
if let values = Set(stringWrapped.map({ $0.isEmpty ? 0 : T($0) })) as? Set<T> { // OK
return values
} else {
fatalError()
}
}

// Dictionary
func decodeStringOrIntDictionary<T: FixedWidthInteger>() -> [Int: T] {
let stringWrapped = [String]()
if let values = Dictionary(uniqueKeysWithValues: stringWrapped.map({ $0.isEmpty ? (0, 0) : (0, T($0)) })) as? [Int: T] { // OK
return values
} else {
fatalError()
}
}