Skip to content

Commit 506b356

Browse files
authored
Merge pull request #67935 from xedin/rdar-112617922-5.9
[5.9][CSSimplify] Relax isBindable requirements for pack expansion variables
2 parents f26a173 + 349c005 commit 506b356

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
@@ -4194,7 +4194,16 @@ static void enumerateOptionalConversionRestrictions(
41944194
/// Determine whether we can bind the given type variable to the given
41954195
/// fixed type.
41964196
static bool isBindable(TypeVariableType *typeVar, Type type) {
4197-
return !ConstraintSystem::typeVarOccursInType(typeVar, type) &&
4197+
// Disallow recursive bindings.
4198+
if (ConstraintSystem::typeVarOccursInType(typeVar, type))
4199+
return false;
4200+
4201+
// If type variable we are about to bind represents a pack
4202+
// expansion type, allow the binding to happen regardless of
4203+
// what the \c type is, because contextual type is just a hint
4204+
// in this situation and type variable would be bound to its
4205+
// opened type instead.
4206+
return typeVar->getImpl().isPackExpansion() ||
41984207
!type->is<DependentMemberType>();
41994208
}
42004209

@@ -11492,29 +11501,15 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1149211501

1149311502
bool ConstraintSystem::resolvePackExpansion(TypeVariableType *typeVar,
1149411503
Type contextualType) {
11495-
auto *locator = typeVar->getImpl().getLocator();
11504+
assert(typeVar->getImpl().isPackExpansion());
1149611505

11497-
Type openedExpansionType;
11498-
if (auto expansionElt =
11499-
locator->getLastElementAs<LocatorPathElt::PackExpansionType>()) {
11500-
openedExpansionType = expansionElt->getOpenedType();
11501-
}
11506+
auto *locator = typeVar->getImpl().getLocator();
1150211507

11503-
if (!openedExpansionType)
11504-
return false;
11508+
Type openedExpansionType =
11509+
locator->castLastElementTo<LocatorPathElt::PackExpansionType>()
11510+
.getOpenedType();
1150511511

1150611512
assignFixedType(typeVar, openedExpansionType, locator);
11507-
11508-
// We have a fully resolved contextual pack expansion type, let's
11509-
// apply it right away.
11510-
if (!contextualType->isEqual(openedExpansionType)) {
11511-
assert(contextualType->is<PackExpansionType>() &&
11512-
!contextualType->hasTypeVariable());
11513-
auto result = matchTypes(openedExpansionType, contextualType,
11514-
ConstraintKind::Equal, {}, locator);
11515-
return !result.isFailure();
11516-
}
11517-
1151811513
return true;
1151911514
}
1152011515

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)