Skip to content

Commit 3ed0932

Browse files
authored
Merge pull request #71196 from xedin/rdar-121692664
[CSSimplify] Matching function types with variadic parameters should …
2 parents 5ed9f34 + eb05750 commit 3ed0932

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3411,7 +3411,8 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
34113411
return getTypeMatchFailure(locator);
34123412

34133413
for (auto pair : matcher.pairs) {
3414-
auto result = matchTypes(pair.lhs, pair.rhs, subKind, subflags,
3414+
// Compare the parameter types, taking contravariance into account.
3415+
auto result = matchTypes(pair.rhs, pair.lhs, subKind, subflags,
34153416
(func1Params.size() == 1
34163417
? argumentLocator
34173418
: argumentLocator.withPathElement(

test/Constraints/variadic_generic_types.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,33 @@ do {
7676
self.f = f
7777
}
7878
}
79-
}
79+
}
80+
81+
// rdar://121692664 - compiler doesn't respect contravariance of the variadic function parameters
82+
do {
83+
class Value<T> {
84+
init<each V>(_ v: repeat Value<each V>,
85+
transform: @escaping (repeat each V) -> T) {
86+
}
87+
}
88+
89+
func coerce(_: Int) {}
90+
91+
func test(first: Value<Int?>, second: Value<(a: Int, b: Int)>) {
92+
let _ = Value(first, second) { first, second in
93+
coerce(first)
94+
// expected-error@-1 {{value of optional type 'Int?' must be unwrapped to a value of type 'Int'}}
95+
// expected-note@-2 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
96+
// expected-note@-3 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
97+
}
98+
99+
// multi-statement closure
100+
let _ = Value(first, second) { first, second in
101+
_ = 42
102+
coerce(first)
103+
// expected-error@-1 {{value of optional type 'Int?' must be unwrapped to a value of type 'Int'}}
104+
// expected-note@-2 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
105+
// expected-note@-3 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)