Skip to content
Open
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
21 changes: 16 additions & 5 deletions lib/Sema/CSOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1429,11 +1429,22 @@ static void determineBestChoicesInContext(
}
}

// If the parameter is `Any` we assume that all candidates are
// convertible to it, which makes it a perfect match. The solver
// would then decide whether erasing to an existential is preferable.
if (paramType->isAny())
return 1;
if (paramType->isAnyExistentialType()) {
// If the parameter is `Any` we assume that all candidates are
// convertible to it, which makes it a perfect match. The solver
// would then decide whether erasing to an existential is preferable.
if (paramType->isAny())
return 1;

// If the parameter is `Any.Type` we assume that all metatype
// candidates are convertible to it.
if (auto *EMT = paramType->getAs<ExistentialMetatypeType>()) {
if (EMT->getExistentialInstanceType()->isAny() &&
(candidateType->is<ExistentialMetatypeType>() ||
candidateType->is<MetatypeType>()))
return 1;
}
}

// Check if a candidate could be matched to a parameter by
// an existential opening.
Expand Down
33 changes: 33 additions & 0 deletions test/Interpreter/issue-85020.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %target-run-simple-swift | %FileCheck %s

// https://github.com/swiftlang/swift/issues/85020

struct Store {
let theType: Any.Type

init(of theType: Any.Type) {
print("init from TYPE: \(theType)")
self.theType = theType
}

init(of instance: Any) {
print("init from VALUE: \(instance)")
self.init(of: type(of: instance))
}
}

let a: (any Numeric)? = 42
print("a: \(type(of: a))")
// CHECK: a: Optional<Numeric>

let storeA = Store(of: a!)
// CHECK-NEXT: init from VALUE: 42
// CHECK-NEXT: init from TYPE: Int

let b: (any Numeric.Type)? = type(of: 42)
print("b: \(type(of: b))")
// CHECK-NEXT: b: Optional<Numeric.Type>

let storeB = Store(of: b!)
// CHECK-NEXT: init from TYPE: Int