Skip to content

Commit 663fa09

Browse files
committed
Sema: Replace existing noescape checks in favor of using TVO_CanBindToNoEscape
1 parent eed84ab commit 663fa09

File tree

3 files changed

+20
-38
lines changed

3 files changed

+20
-38
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,22 +1851,6 @@ ConstraintSystem::matchTypesBindTypeVar(
18511851
}
18521852
}
18531853

1854-
// Disallow bindings of types containing noescape functions to type
1855-
// variables that represent an opened generic parameter. If we allowed
1856-
// this it would allow noescape functions to potentially escape.
1857-
if (type->isNoEscape() && typeVar->getImpl().getGenericParameter()) {
1858-
if (shouldAttemptFixes()) {
1859-
auto *fix = MarkExplicitlyEscaping::create(
1860-
*this, getConstraintLocator(locator));
1861-
if (recordFix(fix))
1862-
return getTypeMatchFailure(locator);
1863-
1864-
// Allow no-escape function to be bound with recorded fix.
1865-
} else {
1866-
return getTypeMatchFailure(locator);
1867-
}
1868-
}
1869-
18701854
// We do not allow keypaths to go through AnyObject. Let's create a fix
18711855
// so this can be diagnosed later.
18721856
if (auto loc = typeVar->getImpl().getLocator()) {
@@ -2449,27 +2433,9 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
24492433
if (!type1->is<LValueType>() &&
24502434
type2->isExistentialType()) {
24512435

2452-
// Penalize conversions to Any, and disallow conversions of
2453-
// types containing noescape functions to Any.
2454-
if (kind >= ConstraintKind::Conversion && type2->isAny()) {
2455-
if (type1->isNoEscape()) {
2456-
if (shouldAttemptFixes()) {
2457-
auto &ctx = getASTContext();
2458-
auto *fix = MarkExplicitlyEscaping::create(
2459-
*this, getConstraintLocator(locator), ctx.TheAnyType);
2460-
if (recordFix(fix))
2461-
return getTypeMatchFailure(locator);
2462-
2463-
// Allow 'no-escape' functions to be converted to 'Any'
2464-
// with a recorded fix that helps us to properly diagnose
2465-
// such situations.
2466-
} else {
2467-
return getTypeMatchFailure(locator);
2468-
}
2469-
}
2470-
2436+
// Penalize conversions to Any.
2437+
if (kind >= ConstraintKind::Conversion && type2->isAny())
24712438
increaseScore(ScoreKind::SK_EmptyExistentialConversion);
2472-
}
24732439

24742440
conversionsOrFixes.push_back(ConversionRestrictionKind::Existential);
24752441
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,8 +1084,7 @@ void ConstraintSystem::openGeneric(
10841084
locator.withPathElement(LocatorPathElt(gp)));
10851085

10861086
auto typeVar = createTypeVariable(locatorPtr,
1087-
TVO_PrefersSubtypeBinding |
1088-
TVO_CanBindToNoEscape);
1087+
TVO_PrefersSubtypeBinding);
10891088
auto result = replacements.insert(
10901089
std::make_pair(cast<GenericTypeParamType>(gp->getCanonicalType()),
10911090
typeVar));

test/Constraints/tuple_arguments.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,4 +1737,21 @@ func autoclosureSplat() {
17371737

17381738
takeFn { (fn: @autoclosure @escaping () -> Int, x: Int) in }
17391739
// expected-error@-1 {{contextual closure type '(_) -> ()' expects 1 argument, but 2 were used in closure body}}
1740+
}
1741+
1742+
func noescapeSplat() {
1743+
func takesFn<T>(_ fn: (T) -> ()) -> T {}
1744+
func takesEscaping(_: @escaping () -> Int) {}
1745+
1746+
do {
1747+
let t = takesFn { (fn: () -> Int) in }
1748+
takesEscaping(t)
1749+
// This type checks because we find a solution T:= (@escaping () -> Int).
1750+
}
1751+
1752+
do {
1753+
let t = takesFn { (fn: () -> Int, x: Int) in }
1754+
// expected-error@-1 {{converting non-escaping value to 'T' may allow it to escape}}
1755+
takesEscaping(t.0)
1756+
}
17401757
}

0 commit comments

Comments
 (0)