Skip to content

Commit 88c6cdb

Browse files
committed
Merge pull request #1494 from gregomni/sr-832
[SR-832][Sema] Fix for function type args passed to @autoclosure params
2 parents 24a0c3d + 545a3f9 commit 88c6cdb

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
848848
// An @autoclosure function type can be a subtype of a
849849
// non-@autoclosure function type.
850850
if (func1->isAutoClosure() != func2->isAutoClosure() &&
851-
(func2->isAutoClosure() || kind < TypeMatchKind::Subtype))
851+
kind < TypeMatchKind::Subtype)
852852
return SolutionKind::Error;
853853

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

1483-
case TypeKind::Function:
1484-
return matchFunctionTypes(cast<FunctionType>(desugar1),
1485-
cast<FunctionType>(desugar2),
1486-
kind, flags, locator);
1483+
case TypeKind::Function: {
1484+
auto func1 = cast<FunctionType>(desugar1);
1485+
auto func2 = cast<FunctionType>(desugar2);
1486+
1487+
// If the 2nd type is an autoclosure, then we don't actually want to
1488+
// treat these as parallel. The first type needs wrapping in a closure
1489+
// despite already being a function type.
1490+
if (!func1->isAutoClosure() && func2->isAutoClosure())
1491+
break;
1492+
return matchFunctionTypes(func1, func2, kind, flags, locator);
1493+
}
14871494

14881495
case TypeKind::PolymorphicFunction:
14891496
case TypeKind::GenericFunction:

test/Constraints/closures.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,9 @@ func typeCheckMultiStmtClosureCrash() {
174174
return 1
175175
}
176176
}
177+
178+
// SR-832 - both these should be ok
179+
func someFunc(foo: (String -> String)?, bar: String -> String) {
180+
let _: String -> String = foo != nil ? foo! : bar
181+
let _: String -> String = foo ?? bar
182+
}

0 commit comments

Comments
 (0)