Skip to content

[SR-832][Sema] Fix for function type args passed to @autoclosure params #1494

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 1 commit into from
Mar 1, 2016
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
17 changes: 12 additions & 5 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
// An @autoclosure function type can be a subtype of a
// non-@autoclosure function type.
if (func1->isAutoClosure() != func2->isAutoClosure() &&
(func2->isAutoClosure() || kind < TypeMatchKind::Subtype))
kind < TypeMatchKind::Subtype)
return SolutionKind::Error;

// A non-throwing function can be a subtype of a throwing function.
Expand Down Expand Up @@ -1480,10 +1480,17 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
ConstraintLocator::InstanceType));
}

case TypeKind::Function:
return matchFunctionTypes(cast<FunctionType>(desugar1),
cast<FunctionType>(desugar2),
kind, flags, locator);
case TypeKind::Function: {
auto func1 = cast<FunctionType>(desugar1);
auto func2 = cast<FunctionType>(desugar2);

// If the 2nd type is an autoclosure, then we don't actually want to
// treat these as parallel. The first type needs wrapping in a closure
// despite already being a function type.
if (!func1->isAutoClosure() && func2->isAutoClosure())
break;
return matchFunctionTypes(func1, func2, kind, flags, locator);
}

case TypeKind::PolymorphicFunction:
case TypeKind::GenericFunction:
Expand Down
6 changes: 6 additions & 0 deletions test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,9 @@ func typeCheckMultiStmtClosureCrash() {
return 1
}
}

// SR-832 - both these should be ok
func someFunc(foo: (String -> String)?, bar: String -> String) {
let _: String -> String = foo != nil ? foo! : bar
let _: String -> String = foo ?? bar
}