Skip to content

Commit 4cbe481

Browse files
authored
Merge pull request #78377 from hamishknight/no-simplify-result
[CS] Don't simplify `FunctionResult` in `simplifyLocator`
2 parents efd1954 + 2fd6863 commit 4cbe481

File tree

7 files changed

+32
-88
lines changed

7 files changed

+32
-88
lines changed

include/swift/Sema/CSFix.h

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,6 @@ enum class FixKind : uint8_t {
109109
/// Fix up the generic arguments of two types so they match each other.
110110
GenericArgumentsMismatch,
111111

112-
/// Fix up @autoclosure argument to the @autoclosure parameter,
113-
/// to for a call to be able to forward it properly, since
114-
/// @autoclosure conversions are unsupported starting from
115-
/// Swift version 5.
116-
AutoClosureForwarding,
117-
118112
/// Remove `!` or `?` because base is not an optional type.
119113
RemoveUnwrap,
120114

@@ -1219,33 +1213,6 @@ class GenericArgumentsMismatch final
12191213
}
12201214
};
12211215

1222-
/// Detect situations when argument of the @autoclosure parameter is itself
1223-
/// marked as @autoclosure and is not applied. Form a fix which suggests a
1224-
/// proper way to forward such arguments, e.g.:
1225-
///
1226-
/// ```swift
1227-
/// func foo(_ fn: @autoclosure () -> Int) {}
1228-
/// func bar(_ fn: @autoclosure () -> Int) {
1229-
/// foo(fn) // error - fn should be called
1230-
/// }
1231-
/// ```
1232-
class AutoClosureForwarding final : public ConstraintFix {
1233-
AutoClosureForwarding(ConstraintSystem &cs, ConstraintLocator *locator)
1234-
: ConstraintFix(cs, FixKind::AutoClosureForwarding, locator) {}
1235-
1236-
public:
1237-
std::string getName() const override { return "fix @autoclosure forwarding"; }
1238-
1239-
bool diagnose(const Solution &solution, bool asNote = false) const override;
1240-
1241-
static AutoClosureForwarding *create(ConstraintSystem &cs,
1242-
ConstraintLocator *locator);
1243-
1244-
static bool classof(const ConstraintFix *fix) {
1245-
return fix->getKind() == FixKind::AutoClosureForwarding;
1246-
}
1247-
};
1248-
12491216
class AllowAutoClosurePointerConversion final : public ContextualMismatch {
12501217
AllowAutoClosurePointerConversion(ConstraintSystem &cs, Type pointeeType,
12511218
Type pointerType,

lib/Sema/CSDiagnostics.cpp

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3831,14 +3831,6 @@ bool FunctionTypeMismatch::diagnoseAsError() {
38313831
return true;
38323832
}
38333833

3834-
bool AutoClosureForwardingFailure::diagnoseAsError() {
3835-
auto argRange = getSourceRange();
3836-
emitDiagnostic(diag::invalid_autoclosure_forwarding)
3837-
.highlight(argRange)
3838-
.fixItInsertAfter(argRange.End, "()");
3839-
return true;
3840-
}
3841-
38423834
bool AutoClosurePointerConversionFailure::diagnoseAsError() {
38433835
auto diagnostic = diag::invalid_autoclosure_pointer_conversion;
38443836
emitDiagnostic(diagnostic, getFromType(), getToType())
@@ -3902,18 +3894,11 @@ bool MissingCallFailure::diagnoseAsError() {
39023894
return true;
39033895
}
39043896

3905-
case ConstraintLocator::FunctionResult: {
3906-
path = path.drop_back();
3907-
if (path.back().getKind() != ConstraintLocator::AutoclosureResult)
3908-
break;
3909-
3910-
LLVM_FALLTHROUGH;
3911-
}
3912-
39133897
case ConstraintLocator::AutoclosureResult: {
3914-
auto loc = getConstraintLocator(getRawAnchor(), path.drop_back());
3915-
AutoClosureForwardingFailure failure(getSolution(), loc);
3916-
return failure.diagnoseAsError();
3898+
emitDiagnostic(diag::invalid_autoclosure_forwarding)
3899+
.highlight(getSourceRange())
3900+
.fixItInsertAfter(insertLoc, "()");
3901+
return true;
39173902
}
39183903
default:
39193904
break;

lib/Sema/CSDiagnostics.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,17 +1078,6 @@ class FunctionTypeMismatch final : public ContextualFailure {
10781078
bool diagnoseAsError() override;
10791079
};
10801080

1081-
/// Diagnose situations when @autoclosure argument is passed to @autoclosure
1082-
/// parameter directly without calling it first.
1083-
class AutoClosureForwardingFailure final : public FailureDiagnostic {
1084-
public:
1085-
AutoClosureForwardingFailure(const Solution &solution,
1086-
ConstraintLocator *locator)
1087-
: FailureDiagnostic(solution, locator) {}
1088-
1089-
bool diagnoseAsError() override;
1090-
};
1091-
10921081
/// Diagnose invalid pointer conversions for an autoclosure result type.
10931082
///
10941083
/// \code

lib/Sema/CSFix.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -802,17 +802,6 @@ GenericArgumentsMismatch *GenericArgumentsMismatch::create(
802802
GenericArgumentsMismatch(cs, actual, required, mismatches, locator);
803803
}
804804

805-
bool AutoClosureForwarding::diagnose(const Solution &solution,
806-
bool asNote) const {
807-
AutoClosureForwardingFailure failure(solution, getLocator());
808-
return failure.diagnose(asNote);
809-
}
810-
811-
AutoClosureForwarding *AutoClosureForwarding::create(ConstraintSystem &cs,
812-
ConstraintLocator *locator) {
813-
return new (cs.getAllocator()) AutoClosureForwarding(cs, locator);
814-
}
815-
816805
bool AllowAutoClosurePointerConversion::diagnose(const Solution &solution,
817806
bool asNote) const {
818807
AutoClosurePointerConversionFailure failure(solution, getFromType(),

lib/Sema/CSSimplify.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5180,11 +5180,16 @@ bool ConstraintSystem::repairFailures(
51805180
// side isn't, let's check it would be possible to fix
51815181
// this by forming an explicit call.
51825182
auto convertTo = dstType->lookThroughAllOptionalTypes();
5183-
// Right-hand side can't be - a function, a type variable or dependent
5184-
// member, or `Any` (if function conversion to `Any` didn't succeed there
5185-
// is something else going on e.g. problem with escapiness).
5186-
if (convertTo->is<FunctionType>() || convertTo->isTypeVariableOrMember() ||
5187-
convertTo->isAny())
5183+
5184+
// If the RHS is a function type, the source must be a function-returning
5185+
// function.
5186+
if (convertTo->is<FunctionType>() && !resultType->is<FunctionType>())
5187+
return false;
5188+
5189+
// Right-hand side can't be a type variable or dependent member, or `Any`
5190+
// (if function conversion to `Any` didn't succeed there is something else
5191+
// going on e.g. problem with escapiness).
5192+
if (convertTo->isTypeVariableOrMember() || convertTo->isAny())
51885193
return false;
51895194

51905195
ConstraintKind matchKind;
@@ -15243,12 +15248,6 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1524315248
return result;
1524415249
}
1524515250

15246-
case FixKind::AutoClosureForwarding: {
15247-
if (recordFix(fix))
15248-
return SolutionKind::Error;
15249-
return matchTypes(type1, type2, matchKind, subflags, locator);
15250-
}
15251-
1525215251
case FixKind::AllowTupleTypeMismatch: {
1525315252
if (fix->getAs<AllowTupleTypeMismatch>()->isElementMismatch()) {
1525415253
auto *locator = fix->getLocator();

lib/Sema/ConstraintSystem.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3405,8 +3405,7 @@ void constraints::simplifyLocator(ASTNode &anchor,
34053405
continue;
34063406
}
34073407

3408-
case ConstraintLocator::ApplyFunction:
3409-
case ConstraintLocator::FunctionResult:
3408+
case ConstraintLocator::ApplyFunction: {
34103409
// Extract application function.
34113410
if (auto applyExpr = getAsExpr<ApplyExpr>(anchor)) {
34123411
anchor = applyExpr->getFn();
@@ -3428,7 +3427,7 @@ void constraints::simplifyLocator(ASTNode &anchor,
34283427
}
34293428

34303429
break;
3431-
3430+
}
34323431
case ConstraintLocator::AutoclosureResult:
34333432
case ConstraintLocator::LValueConversion:
34343433
case ConstraintLocator::DynamicType:
@@ -3693,6 +3692,7 @@ void constraints::simplifyLocator(ASTNode &anchor,
36933692
case ConstraintLocator::SynthesizedArgument:
36943693
break;
36953694

3695+
case ConstraintLocator::FunctionResult:
36963696
case ConstraintLocator::DynamicLookupResult:
36973697
case ConstraintLocator::KeyPathComponentResult:
36983698
break;

test/Constraints/issue-78376.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-typecheck-verify-swift %clang-importer-sdk
2+
3+
// REQUIRES: objc_interop
4+
5+
import Foundation
6+
import CoreGraphics
7+
8+
func foo(_ x: Double) {}
9+
func bar() -> Double { 0 }
10+
11+
// https://github.com/swiftlang/swift/issues/78376
12+
let _: (CGFloat) -> Void = foo
13+
// expected-error@-1 {{cannot convert value of type '(Double) -> ()' to specified type '(CGFloat) -> Void'}}
14+
let _: () -> CGFloat = bar
15+
// expected-error@-1 {{cannot convert value of type '() -> Double' to specified type '() -> CGFloat'}}

0 commit comments

Comments
 (0)