Skip to content

[CSSimplify] Matching function types with variadic parameters should … #71196

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 1 commit into from
Jan 29, 2024
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
3 changes: 2 additions & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3411,7 +3411,8 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
return getTypeMatchFailure(locator);

for (auto pair : matcher.pairs) {
auto result = matchTypes(pair.lhs, pair.rhs, subKind, subflags,
// Compare the parameter types, taking contravariance into account.
auto result = matchTypes(pair.rhs, pair.lhs, subKind, subflags,
(func1Params.size() == 1
? argumentLocator
: argumentLocator.withPathElement(
Expand Down
31 changes: 30 additions & 1 deletion test/Constraints/variadic_generic_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,33 @@ do {
self.f = f
}
}
}
}

// rdar://121692664 - compiler doesn't respect contravariance of the variadic function parameters
do {
class Value<T> {
init<each V>(_ v: repeat Value<each V>,
transform: @escaping (repeat each V) -> T) {
}
}

func coerce(_: Int) {}

func test(first: Value<Int?>, second: Value<(a: Int, b: Int)>) {
let _ = Value(first, second) { first, second in
coerce(first)
// expected-error@-1 {{value of optional type 'Int?' must be unwrapped to a value of type 'Int'}}
// expected-note@-2 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
// expected-note@-3 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
}

// multi-statement closure
let _ = Value(first, second) { first, second in
_ = 42
coerce(first)
// expected-error@-1 {{value of optional type 'Int?' must be unwrapped to a value of type 'Int'}}
// expected-note@-2 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
// expected-note@-3 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
}
}
}