Skip to content

[CS] Don't simplify FunctionResult in simplifyLocator #78377

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 3 commits into from
Jan 2, 2025
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
33 changes: 0 additions & 33 deletions include/swift/Sema/CSFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,6 @@ enum class FixKind : uint8_t {
/// Fix up the generic arguments of two types so they match each other.
GenericArgumentsMismatch,

/// Fix up @autoclosure argument to the @autoclosure parameter,
/// to for a call to be able to forward it properly, since
/// @autoclosure conversions are unsupported starting from
/// Swift version 5.
AutoClosureForwarding,

/// Remove `!` or `?` because base is not an optional type.
RemoveUnwrap,

Expand Down Expand Up @@ -1219,33 +1213,6 @@ class GenericArgumentsMismatch final
}
};

/// Detect situations when argument of the @autoclosure parameter is itself
/// marked as @autoclosure and is not applied. Form a fix which suggests a
/// proper way to forward such arguments, e.g.:
///
/// ```swift
/// func foo(_ fn: @autoclosure () -> Int) {}
/// func bar(_ fn: @autoclosure () -> Int) {
/// foo(fn) // error - fn should be called
/// }
/// ```
class AutoClosureForwarding final : public ConstraintFix {
AutoClosureForwarding(ConstraintSystem &cs, ConstraintLocator *locator)
: ConstraintFix(cs, FixKind::AutoClosureForwarding, locator) {}

public:
std::string getName() const override { return "fix @autoclosure forwarding"; }

bool diagnose(const Solution &solution, bool asNote = false) const override;

static AutoClosureForwarding *create(ConstraintSystem &cs,
ConstraintLocator *locator);

static bool classof(const ConstraintFix *fix) {
return fix->getKind() == FixKind::AutoClosureForwarding;
}
};

class AllowAutoClosurePointerConversion final : public ContextualMismatch {
AllowAutoClosurePointerConversion(ConstraintSystem &cs, Type pointeeType,
Type pointerType,
Expand Down
23 changes: 4 additions & 19 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3832,14 +3832,6 @@ bool FunctionTypeMismatch::diagnoseAsError() {
return true;
}

bool AutoClosureForwardingFailure::diagnoseAsError() {
auto argRange = getSourceRange();
emitDiagnostic(diag::invalid_autoclosure_forwarding)
.highlight(argRange)
.fixItInsertAfter(argRange.End, "()");
return true;
}

bool AutoClosurePointerConversionFailure::diagnoseAsError() {
auto diagnostic = diag::invalid_autoclosure_pointer_conversion;
emitDiagnostic(diagnostic, getFromType(), getToType())
Expand Down Expand Up @@ -3903,18 +3895,11 @@ bool MissingCallFailure::diagnoseAsError() {
return true;
}

case ConstraintLocator::FunctionResult: {
path = path.drop_back();
if (path.back().getKind() != ConstraintLocator::AutoclosureResult)
break;

LLVM_FALLTHROUGH;
}

case ConstraintLocator::AutoclosureResult: {
auto loc = getConstraintLocator(getRawAnchor(), path.drop_back());
AutoClosureForwardingFailure failure(getSolution(), loc);
return failure.diagnoseAsError();
emitDiagnostic(diag::invalid_autoclosure_forwarding)
.highlight(getSourceRange())
.fixItInsertAfter(insertLoc, "()");
return true;
}
default:
break;
Expand Down
11 changes: 0 additions & 11 deletions lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -1078,17 +1078,6 @@ class FunctionTypeMismatch final : public ContextualFailure {
bool diagnoseAsError() override;
};

/// Diagnose situations when @autoclosure argument is passed to @autoclosure
/// parameter directly without calling it first.
class AutoClosureForwardingFailure final : public FailureDiagnostic {
public:
AutoClosureForwardingFailure(const Solution &solution,
ConstraintLocator *locator)
: FailureDiagnostic(solution, locator) {}

bool diagnoseAsError() override;
};

/// Diagnose invalid pointer conversions for an autoclosure result type.
///
/// \code
Expand Down
11 changes: 0 additions & 11 deletions lib/Sema/CSFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,17 +802,6 @@ GenericArgumentsMismatch *GenericArgumentsMismatch::create(
GenericArgumentsMismatch(cs, actual, required, mismatches, locator);
}

bool AutoClosureForwarding::diagnose(const Solution &solution,
bool asNote) const {
AutoClosureForwardingFailure failure(solution, getLocator());
return failure.diagnose(asNote);
}

AutoClosureForwarding *AutoClosureForwarding::create(ConstraintSystem &cs,
ConstraintLocator *locator) {
return new (cs.getAllocator()) AutoClosureForwarding(cs, locator);
}

bool AllowAutoClosurePointerConversion::diagnose(const Solution &solution,
bool asNote) const {
AutoClosurePointerConversionFailure failure(solution, getFromType(),
Expand Down
21 changes: 10 additions & 11 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5186,11 +5186,16 @@ bool ConstraintSystem::repairFailures(
// side isn't, let's check it would be possible to fix
// this by forming an explicit call.
auto convertTo = dstType->lookThroughAllOptionalTypes();
// Right-hand side can't be - a function, a type variable or dependent
// member, or `Any` (if function conversion to `Any` didn't succeed there
// is something else going on e.g. problem with escapiness).
if (convertTo->is<FunctionType>() || convertTo->isTypeVariableOrMember() ||
convertTo->isAny())

// If the RHS is a function type, the source must be a function-returning
// function.
if (convertTo->is<FunctionType>() && !resultType->is<FunctionType>())
return false;

// Right-hand side can't be a type variable or dependent member, or `Any`
// (if function conversion to `Any` didn't succeed there is something else
// going on e.g. problem with escapiness).
if (convertTo->isTypeVariableOrMember() || convertTo->isAny())
return false;

ConstraintKind matchKind;
Expand Down Expand Up @@ -15249,12 +15254,6 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
return result;
}

case FixKind::AutoClosureForwarding: {
if (recordFix(fix))
return SolutionKind::Error;
return matchTypes(type1, type2, matchKind, subflags, locator);
}

case FixKind::AllowTupleTypeMismatch: {
if (fix->getAs<AllowTupleTypeMismatch>()->isElementMismatch()) {
auto *locator = fix->getLocator();
Expand Down
6 changes: 3 additions & 3 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3389,8 +3389,7 @@ void constraints::simplifyLocator(ASTNode &anchor,
continue;
}

case ConstraintLocator::ApplyFunction:
case ConstraintLocator::FunctionResult:
case ConstraintLocator::ApplyFunction: {
// Extract application function.
if (auto applyExpr = getAsExpr<ApplyExpr>(anchor)) {
anchor = applyExpr->getFn();
Expand All @@ -3412,7 +3411,7 @@ void constraints::simplifyLocator(ASTNode &anchor,
}

break;

}
case ConstraintLocator::AutoclosureResult:
case ConstraintLocator::LValueConversion:
case ConstraintLocator::DynamicType:
Expand Down Expand Up @@ -3677,6 +3676,7 @@ void constraints::simplifyLocator(ASTNode &anchor,
case ConstraintLocator::SynthesizedArgument:
break;

case ConstraintLocator::FunctionResult:
case ConstraintLocator::DynamicLookupResult:
case ConstraintLocator::KeyPathComponentResult:
break;
Expand Down
15 changes: 15 additions & 0 deletions test/Constraints/issue-78376.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-typecheck-verify-swift %clang-importer-sdk

// REQUIRES: objc_interop

import Foundation
import CoreGraphics

func foo(_ x: Double) {}
func bar() -> Double { 0 }

// https://github.com/swiftlang/swift/issues/78376
let _: (CGFloat) -> Void = foo
// expected-error@-1 {{cannot convert value of type '(Double) -> ()' to specified type '(CGFloat) -> Void'}}
let _: () -> CGFloat = bar
// expected-error@-1 {{cannot convert value of type '() -> Double' to specified type '() -> CGFloat'}}