Skip to content

Commit 20bbd8d

Browse files
authored
Merge pull request #82770 from hamishknight/simple-fix
[CS] Ensure type variables are eliminated by `Solution::simplifyType`
2 parents 05204bd + d603704 commit 20bbd8d

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,7 @@ Type Solution::simplifyType(Type type, bool wantInterfaceType) const {
17701770
if (wantInterfaceType)
17711771
type = type->mapTypeOutOfContext();
17721772

1773-
if (!(type->hasTypeVariable() || type->hasPlaceholder()))
1773+
if (!type->hasTypeVariableOrPlaceholder())
17741774
return type;
17751775

17761776
// Map type variables to fixed types from bindings.
@@ -1788,16 +1788,28 @@ Type Solution::simplifyType(Type type, bool wantInterfaceType) const {
17881788
});
17891789
ASSERT(!(wantInterfaceType && resolvedType->hasPrimaryArchetype()));
17901790

1791-
// Placeholders shouldn't be reachable through a solution, they are only
1792-
// useful to determine what went wrong exactly.
1793-
if (resolvedType->hasPlaceholder()) {
1794-
return resolvedType.transformRec([&](Type type) -> std::optional<Type> {
1795-
if (type->isPlaceholder())
1796-
return Type(cs.getASTContext().TheUnresolvedType);
1797-
return std::nullopt;
1798-
});
1791+
// We may have type variables and placeholders left over. These are solver
1792+
// allocated so cannot escape this function. Turn them into UnresolvedType.
1793+
// - Type variables may still be present from unresolved pack expansions where
1794+
// e.g the count type is a hole, so the pattern may never become a
1795+
// concrete type.
1796+
// - Placeholders may be present for any holes.
1797+
if (resolvedType->hasTypeVariableOrPlaceholder()) {
1798+
auto &ctx = cs.getASTContext();
1799+
resolvedType =
1800+
resolvedType.transformRec([&](Type type) -> std::optional<Type> {
1801+
if (!type->hasTypeVariableOrPlaceholder())
1802+
return type;
1803+
1804+
auto *typePtr = type.getPointer();
1805+
if (isa<TypeVariableType>(typePtr) || isa<PlaceholderType>(typePtr))
1806+
return Type(ctx.TheUnresolvedType);
1807+
1808+
return std::nullopt;
1809+
});
17991810
}
18001811

1812+
ASSERT(!resolvedType->getRecursiveProperties().isSolverAllocated());
18011813
return resolvedType;
18021814
}
18031815

test/Constraints/pack-expansion-expressions.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,3 +798,8 @@ do {
798798
}
799799
}
800800
}
801+
802+
func testInvalidDecomposition() {
803+
func id<T>(_ x: T) -> T {}
804+
let (a, b) = id((repeat each undefined)) // expected-error {{cannot find 'undefined' in scope}}
805+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// {"kind":"complete","signature":"swift::constraints::Solution::simplifyType(swift::Type, bool) const"}
2+
// RUN: %target-swift-ide-test -code-completion --code-completion-token=COMPLETE -code-completion-diagnostics -source-filename %s
3+
for (a(b, d)) [
4+
#^COMPLETE^#, 0, (repeat.c)].enumerated(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// {"kind":"typecheck","signature":"swift::constraints::Solution::simplifyType(swift::Type, bool) const"}
2+
// RUN: not %target-swift-frontend -typecheck %s
3+
for (a(b, d)) in [(repeat .c)].enumerated(<#expression#>) {
4+
}

validation-test/compiler_crashers_2/dc3a3e26162b43be.swift renamed to validation-test/compiler_crashers_2_fixed/dc3a3e26162b43be.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// {"signature":"(anonymous namespace)::SetExprTypes::walkToExprPost(swift::Expr*)"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
typealias a<b, c> = c
44
struct d < each b {
55
typealias e< c > =

0 commit comments

Comments
 (0)