Skip to content

[CSBindings] Don't favor application result types before application … #77461

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 2 commits into from
Nov 11, 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
4 changes: 4 additions & 0 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@ class TypeVariableType::Implementation {
/// Determine whether this type variable represents a subscript result type.
bool isSubscriptResultType() const;

/// Determine whether this type variable represents a result type of an
/// application i.e. a call, an operator, or a subscript.
bool isApplicationResultType() const;

/// Determine whether this type variable represents an opened
/// type parameter pack.
bool isParameterPack() const;
Expand Down
13 changes: 10 additions & 3 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,9 +1293,16 @@ bool BindingSet::favoredOverDisjunction(Constraint *disjunction) const {

return !hasConversions(binding.BindingType);
})) {
// Result type of subscript could be l-value so we can't bind it early.
if (!TypeVar->getImpl().isSubscriptResultType() &&
llvm::none_of(Info.DelayedBy, [](const Constraint *constraint) {
bool isApplicationResultType = TypeVar->getImpl().isApplicationResultType();
if (llvm::none_of(Info.DelayedBy, [&isApplicationResultType](
const Constraint *constraint) {
// Let's not attempt to bind result type before application
// happens. For example because it could be discardable or
// l-value (subscript applications).
if (isApplicationResultType &&
constraint->getKind() == ConstraintKind::ApplicableFunction)
return true;

return constraint->getKind() == ConstraintKind::Disjunction ||
constraint->getKind() == ConstraintKind::ValueMember;
}))
Expand Down
10 changes: 10 additions & 0 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ bool TypeVariableType::Implementation::isSubscriptResultType() const {
KeyPathExpr::Component::Kind::UnresolvedSubscript;
}

bool TypeVariableType::Implementation::isApplicationResultType() const {
if (!(locator && locator->getAnchor()))
return false;

if (!locator->isLastElement<LocatorPathElt::FunctionResult>())
return false;

return isExpr<ApplyExpr>(locator->getAnchor()) || isSubscriptResultType();
}

bool TypeVariableType::Implementation::isParameterPack() const {
return locator
&& locator->isForGenericParameter()
Expand Down
17 changes: 17 additions & 0 deletions test/Constraints/async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,20 @@ struct OverloadInImplicitAsyncClosure {

init(int: Int) throws { }
}

@available(SwiftStdlib 5.5, *)
func test(_: Int) async throws {}

@discardableResult
@available(SwiftStdlib 5.5, *)
func test(_: Int) -> String { "" }

@available(SwiftStdlib 5.5, *)
func compute(_: @escaping () -> Void) {}

@available(SwiftStdlib 5.5, *)
func test_sync_in_closure_context() {
compute {
test(42) // Ok (select sync overloads and discards the result)
}
}
16 changes: 16 additions & 0 deletions test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1267,3 +1267,19 @@ do {

// Currently legal.
let _: () -> Int = { return fatalError() }

// Make sure that `Void` assigned to closure result doesn't get eagerly propagated into the body
do {
class C {
func f(_: Any) -> Int! { fatalError() }
static func f(_: Any) -> Int! { fatalError() }
}

class G<T> {
func g<U>(_ u: U, _: (U, T) -> ()) {}

func g<U: C>(_ u: U) {
g(u) { $0.f($1) } // expected-warning {{result of call to 'f' is unused}}
}
}
}