Skip to content

[6.1] Revert some changes to CSBinding that landed after branch was created #78305

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 4 commits into from
Dec 21, 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: 0 additions & 4 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,6 @@ 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
30 changes: 5 additions & 25 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,19 +1208,7 @@ bool BindingSet::isViable(PotentialBinding &binding, bool isTransitive) {
if (!existingNTD || NTD != existingNTD)
continue;

// What is going on here needs to be thoroughly re-evaluated,
// but at least for now, let's not filter bindings of different
// kinds so if we have a situation like: `Array<$T0> conv $T1`
// and `$T1 conv Array<(String, Int)>` we can't lose `Array<$T0>`
// as a binding because `$T0` could be inferred to
// `(key: String, value: Int)` and binding `$T1` to `Array<(String, Int)>`
// eagerly would be incorrect.
if (existing->Kind != binding.Kind) {
// Array, Set and Dictionary allow conversions, everything else
// requires their generic arguments to match exactly.
if (existingType->isKnownStdlibCollectionType())
continue;
}
// FIXME: What is going on here needs to be thoroughly re-evaluated.

// If new type has a type variable it shouldn't
// be considered viable.
Expand Down Expand Up @@ -1276,8 +1264,7 @@ static bool hasConversions(Type type) {
}

return !(type->is<StructType>() || type->is<EnumType>() ||
type->is<BuiltinType>() || type->is<ArchetypeType>() ||
type->isVoid());
type->is<BuiltinType>() || type->is<ArchetypeType>());
}

bool BindingSet::favoredOverDisjunction(Constraint *disjunction) const {
Expand All @@ -1293,16 +1280,9 @@ bool BindingSet::favoredOverDisjunction(Constraint *disjunction) const {

return !hasConversions(binding.BindingType);
})) {
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;

// 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) {
return constraint->getKind() == ConstraintKind::Disjunction ||
constraint->getKind() == ConstraintKind::ValueMember;
}))
Expand Down
10 changes: 0 additions & 10 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,6 @@ 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
37 changes: 37 additions & 0 deletions test/Constraints/rdar139812024.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: %target-typecheck-verify-swift %clang-importer-sdk

// REQUIRES: objc_interop

// rdar://139812024 - error: cannot convert return expression of type 'NSObject' to return type 'NSImage'

import Foundation

@objc
final class Image: NSObject {
}

@available(*, unavailable)
extension Image: Sendable {} // expected-note {{explicitly marked unavailable here}}

class Lock<State> {
func withLock<R: Sendable>(_: @Sendable (inout State) -> R) -> R {
fatalError()
}
}

extension Lock where State == Void {
func withLock<R: Sendable>(_: @Sendable () -> R) -> R {
fatalError()
}
}

class Test {
var images: [Int: Image] = [:]

func fetchImage(lock: Lock<Void>, id: Int) -> Image? {
if let existingImage = lock.withLock({ return images[id] }) { // expected-warning {{unavailable}}
return existingImage
}
return nil
}
}