Skip to content

Commit cedbdf9

Browse files
authored
Merge pull request swiftlang#67759 from xedin/rdar-112617922
[CSSimplify] Relax `isBindable` requirements for pack expansion variables
2 parents a0709c4 + 27413f5 commit cedbdf9

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,7 +4210,16 @@ static void enumerateOptionalConversionRestrictions(
42104210
/// Determine whether we can bind the given type variable to the given
42114211
/// fixed type.
42124212
static bool isBindable(TypeVariableType *typeVar, Type type) {
4213-
return !ConstraintSystem::typeVarOccursInType(typeVar, type) &&
4213+
// Disallow recursive bindings.
4214+
if (ConstraintSystem::typeVarOccursInType(typeVar, type))
4215+
return false;
4216+
4217+
// If type variable we are about to bind represents a pack
4218+
// expansion type, allow the binding to happen regardless of
4219+
// what the \c type is, because contextual type is just a hint
4220+
// in this situation and type variable would be bound to its
4221+
// opened type instead.
4222+
return typeVar->getImpl().isPackExpansion() ||
42144223
!type->is<DependentMemberType>();
42154224
}
42164225

@@ -11621,29 +11630,15 @@ bool ConstraintSystem::resolveKeyPath(TypeVariableType *typeVar,
1162111630

1162211631
bool ConstraintSystem::resolvePackExpansion(TypeVariableType *typeVar,
1162311632
Type contextualType) {
11624-
auto *locator = typeVar->getImpl().getLocator();
11633+
assert(typeVar->getImpl().isPackExpansion());
1162511634

11626-
Type openedExpansionType;
11627-
if (auto expansionElt =
11628-
locator->getLastElementAs<LocatorPathElt::PackExpansionType>()) {
11629-
openedExpansionType = expansionElt->getOpenedType();
11630-
}
11635+
auto *locator = typeVar->getImpl().getLocator();
1163111636

11632-
if (!openedExpansionType)
11633-
return false;
11637+
Type openedExpansionType =
11638+
locator->castLastElementTo<LocatorPathElt::PackExpansionType>()
11639+
.getOpenedType();
1163411640

1163511641
assignFixedType(typeVar, openedExpansionType, locator);
11636-
11637-
// We have a fully resolved contextual pack expansion type, let's
11638-
// apply it right away.
11639-
if (!contextualType->isEqual(openedExpansionType)) {
11640-
assert(contextualType->is<PackExpansionType>() &&
11641-
!contextualType->hasTypeVariable());
11642-
auto result = matchTypes(openedExpansionType, contextualType,
11643-
ConstraintKind::Equal, {}, locator);
11644-
return !result.isFailure();
11645-
}
11646-
1164711642
return true;
1164811643
}
1164911644

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: not %target-swift-frontend %s -typecheck
2+
3+
enum MyError: Error {
4+
case unknown
5+
}
6+
7+
extension Result {
8+
init<each S>(success: repeat each S, failure: Failure?) where Success == (repeat each S.Wrapped), Failure == any Error, repeat each S: OptionalProtocol {
9+
}
10+
}
11+
12+
protocol OptionalProtocol {
13+
associatedtype Wrapped
14+
var asOptional: Optional<Wrapped> { get }
15+
}
16+
17+
extension Optional: OptionalProtocol {
18+
var asOptional: Optional<Wrapped> { self }
19+
}
20+
21+
func test<each EncodedResult: OptionalProtocol>(
22+
_ transformed: @escaping (Result<(repeat each EncodedResult.Wrapped), any Error>) -> Void
23+
) -> ((any Error)?, repeat each EncodedResult) -> Void where repeat each EncodedResult.Wrapped: DataProtocol {
24+
return {
25+
transformed(Result(success: $1, failure: $0))
26+
}
27+
}

0 commit comments

Comments
 (0)