Skip to content

Fix bug conversions of function returning function failed #82398

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

Closed
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
11 changes: 11 additions & 0 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3884,6 +3884,17 @@ class AnyFunctionType : public TypeBase {
return containsPackExpansionType(getParams());
}

/// Returns the number of curry levels of the function type.
unsigned getNumCurryLevels() const {
unsigned num = 1;
auto fn = this;
while (auto resultFn = fn->getResult()->getAs<AnyFunctionType>()) {
num++;
fn = resultFn;
}
return num;
}

static bool containsPackExpansionType(ArrayRef<Param> params);

static void printParams(ArrayRef<Param> Params, raw_ostream &OS,
Expand Down
10 changes: 6 additions & 4 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5347,10 +5347,12 @@ bool ConstraintSystem::repairFailures(
// this by forming an explicit call.
auto convertTo = dstType->lookThroughAllOptionalTypes();

// If the RHS is a function type, the source must be a function-returning
// function.
if (convertTo->is<FunctionType>() && !resultType->is<FunctionType>())
return false;
// If the RHS is a function type,
// the source must have more curry levels.
if (auto convertToFnType = convertTo->getAs<FunctionType>()) {
if (fnType->getNumCurryLevels() <= convertToFnType->getNumCurryLevels())
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but I don't think this is the right fix. Before attempting this repair we need to make sure that there are no restrictions recorded (other locations in repairFailures do it with hasAnyRestriction) otherwise we end up in a situation like this where the fix is greedily applied before optional injection could be attempted (if that was allowed to proceed the types would line up exactly).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for quick review!

we need to make sure that there are no restrictions recorded

I see, it sounds like related to the overall flow of reparingByInserting, it may be difficult for me to fix it 😅 let me close this PR and leave to owners 🙇 (Since I'm still a beginner of compiler)

}

// 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
Expand Down
11 changes: 11 additions & 0 deletions test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1366,3 +1366,14 @@ do {
try $0.missing // expected-error {{value of type 'Int' has no member 'missing'}}
}
}

do {
let a: () -> () -> Void = {{}}
let _: (() -> () -> Void)? = a
let _: (() -> () -> Void)?? = a

class Super {}
class Sub: Super {}
let b: () -> () -> Sub = {{ return Sub() }}
let _: (() -> () -> Super)? = b
}