diff --git a/include/swift/Sema/CSFix.h b/include/swift/Sema/CSFix.h index 78cd715719920..9821fcea3312c 100644 --- a/include/swift/Sema/CSFix.h +++ b/include/swift/Sema/CSFix.h @@ -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, @@ -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, diff --git a/lib/Sema/CSDiagnostics.cpp b/lib/Sema/CSDiagnostics.cpp index bfe6fccb871ee..c9ae73089fe1f 100644 --- a/lib/Sema/CSDiagnostics.cpp +++ b/lib/Sema/CSDiagnostics.cpp @@ -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()) @@ -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; diff --git a/lib/Sema/CSDiagnostics.h b/lib/Sema/CSDiagnostics.h index c2331ddc0d1ec..7544300e20079 100644 --- a/lib/Sema/CSDiagnostics.h +++ b/lib/Sema/CSDiagnostics.h @@ -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 diff --git a/lib/Sema/CSFix.cpp b/lib/Sema/CSFix.cpp index adcb3c7674c99..139358d858ab1 100644 --- a/lib/Sema/CSFix.cpp +++ b/lib/Sema/CSFix.cpp @@ -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(), diff --git a/lib/Sema/CSSimplify.cpp b/lib/Sema/CSSimplify.cpp index a253f1e58057c..a7dca3d24339a 100644 --- a/lib/Sema/CSSimplify.cpp +++ b/lib/Sema/CSSimplify.cpp @@ -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() || convertTo->isTypeVariableOrMember() || - convertTo->isAny()) + + // If the RHS is a function type, the source must be a function-returning + // function. + if (convertTo->is() && !resultType->is()) + 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; @@ -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()->isElementMismatch()) { auto *locator = fix->getLocator(); diff --git a/lib/Sema/ConstraintSystem.cpp b/lib/Sema/ConstraintSystem.cpp index 14354a0bfd498..4133db8e63365 100644 --- a/lib/Sema/ConstraintSystem.cpp +++ b/lib/Sema/ConstraintSystem.cpp @@ -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(anchor)) { anchor = applyExpr->getFn(); @@ -3412,7 +3411,7 @@ void constraints::simplifyLocator(ASTNode &anchor, } break; - + } case ConstraintLocator::AutoclosureResult: case ConstraintLocator::LValueConversion: case ConstraintLocator::DynamicType: @@ -3677,6 +3676,7 @@ void constraints::simplifyLocator(ASTNode &anchor, case ConstraintLocator::SynthesizedArgument: break; + case ConstraintLocator::FunctionResult: case ConstraintLocator::DynamicLookupResult: case ConstraintLocator::KeyPathComponentResult: break; diff --git a/test/Constraints/issue-78376.swift b/test/Constraints/issue-78376.swift new file mode 100644 index 0000000000000..1227b879cfb34 --- /dev/null +++ b/test/Constraints/issue-78376.swift @@ -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'}}